Origin commit
This commit is contained in:
87
inc/rating/class-opalestate-rating-features-posttype.php
Executable file
87
inc/rating/class-opalestate-rating-features-posttype.php
Executable file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* $Desc$
|
||||
*
|
||||
* @version $Id$
|
||||
* @package opalestate
|
||||
* @author Opal Team <info@wpopal.com >
|
||||
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
|
||||
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* @website http://www.wpopal.com
|
||||
* @support http://www.wpopal.com/support/forum.html
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Opalestate_PostType_Rating_Features
|
||||
*/
|
||||
class Opalestate_PostType_Rating_Features {
|
||||
/**
|
||||
* Init action and filter data to define property post type
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'init', [ $this, 'definition' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition
|
||||
*/
|
||||
public function definition() {
|
||||
if ( ! is_blog_installed() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rating_supports = Opalestate_Rating::get_rating_supports();
|
||||
|
||||
foreach ( $rating_supports as $key => $support ) {
|
||||
$this->register_post_type( $support['features_cpt'], $support['post_type'] );
|
||||
}
|
||||
}
|
||||
|
||||
public function register_post_type( $cpt_feature, $cpt_support ) {
|
||||
if ( post_type_exists( $cpt_feature ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
register_post_type( $cpt_feature, apply_filters( $cpt_feature . '_cpt_args', [
|
||||
'labels' => [
|
||||
'name' => esc_html_x( 'Rating Features', 'Feature plural name', 'opalestate-pro' ),
|
||||
'singular_name' => esc_html_x( 'Rating Feature', 'Feature singular name', 'opalestate-pro' ),
|
||||
'menu_name' => esc_html_x( 'Rating Features', 'Admin menu name', 'opalestate-pro' ),
|
||||
'add_new' => esc_html__( 'Add rating feature', 'opalestate-pro' ),
|
||||
'add_new_item' => esc_html__( 'Add new rating feature', 'opalestate-pro' ),
|
||||
'edit' => esc_html__( 'Edit', 'opalestate-pro' ),
|
||||
'edit_item' => esc_html__( 'Edit rating feature', 'opalestate-pro' ),
|
||||
'new_item' => esc_html__( 'New rating feature', 'opalestate-pro' ),
|
||||
'view_item' => esc_html__( 'View rating feature', 'opalestate-pro' ),
|
||||
'search_items' => esc_html__( 'Query rating features', 'opalestate-pro' ),
|
||||
'not_found' => esc_html__( 'No rating features found', 'opalestate-pro' ),
|
||||
'not_found_in_trash' => esc_html__( 'No rating features found in trash', 'opalestate-pro' ),
|
||||
'parent' => esc_html__( 'Parent rating features', 'opalestate-pro' ),
|
||||
'filter_items_list' => esc_html__( 'Filter rating features', 'opalestate-pro' ),
|
||||
'items_list_navigation' => esc_html__( 'Rating Features navigation', 'opalestate-pro' ),
|
||||
'items_list' => esc_html__( 'Rating Features List', 'opalestate-pro' ),
|
||||
],
|
||||
'description' => esc_html__( 'This is where store rating features are stored.', 'opalestate-pro' ),
|
||||
'public' => false,
|
||||
'hierarchical' => false,
|
||||
'exclude_from_search' => true,
|
||||
'publicly_queryable' => false,
|
||||
'show_ui' => true,
|
||||
'show_in_menu' => 'edit.php?post_type=' . $cpt_support,
|
||||
'show_in_nav_menus' => false,
|
||||
'show_in_admin_bar' => false,
|
||||
'show_in_rest' => true,
|
||||
'map_meta_cap' => true,
|
||||
'supports' => [ 'title' ],
|
||||
'rewrite' => false,
|
||||
'has_archive' => false,
|
||||
] ) );
|
||||
}
|
||||
}
|
||||
|
||||
new Opalestate_PostType_Rating_Features();
|
||||
169
inc/rating/class-opalestate-rating-helper.php
Executable file
169
inc/rating/class-opalestate-rating-helper.php
Executable file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Opalestate_Rating_Helper
|
||||
*/
|
||||
class Opalestate_Rating_Helper {
|
||||
public static function get_average_rating( $comment, $cpt_feature ) {
|
||||
$features = static::get_features( $cpt_feature );
|
||||
if ( $features ) {
|
||||
$sum = 0;
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$sum += absint( get_comment_meta( $comment->comment_ID, $cpt_feature . '_' . $feature_slug, true ) );
|
||||
}
|
||||
|
||||
$average = number_format( $sum / count( $features ), 1 );
|
||||
} else {
|
||||
$average = absint( get_comment_meta( $comment->comment_ID, 'opalestate_rating', true ) );
|
||||
}
|
||||
|
||||
return $average;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get property rating for a property. Please note this is not cached.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public static function get_average_rating_for_post( $post_id, $cpt_feature ) {
|
||||
$comments = get_comments( [ 'post_id' => $post_id, 'status' => 'approve' ] );
|
||||
if ( ! $comments ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$sum = 0;
|
||||
foreach ( $comments as $comment ) {
|
||||
$sum += static::get_average_rating( $comment, $cpt_feature );
|
||||
}
|
||||
|
||||
return number_format( $sum / static::get_review_count_for_post( $post_id ), 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get property review count for a property (not replies). Please note this is not cached.
|
||||
*/
|
||||
public static function get_review_count_for_post( $post_id ) {
|
||||
global $wpdb;
|
||||
|
||||
$count = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT COUNT(*) FROM $wpdb->comments
|
||||
WHERE comment_parent = 0
|
||||
AND comment_post_ID = %d
|
||||
AND comment_approved = '1'
|
||||
",
|
||||
$post_id
|
||||
)
|
||||
);
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get property rating count for a property. Please note this is not cached.
|
||||
*/
|
||||
public static function get_rating_counts_for_post( $post_id, $cpt_feature ) {
|
||||
$features = static::get_features( $cpt_feature );
|
||||
if ( $features ) {
|
||||
return static::get_review_count_for_post( $post_id ) * count( $features );
|
||||
}
|
||||
|
||||
return static::get_review_count_for_post( $post_id );
|
||||
}
|
||||
|
||||
public static function get_rating_count_stats_for_post( $post_id, $cpt_feature ) {
|
||||
$output = [
|
||||
5 => 0,
|
||||
4 => 0,
|
||||
3 => 0,
|
||||
2 => 0,
|
||||
1 => 0,
|
||||
];
|
||||
|
||||
$features = static::get_features( $cpt_feature );
|
||||
|
||||
for ( $i = 5; $i >= 1; $i-- ) {
|
||||
$args = [
|
||||
'post_id' => $post_id,
|
||||
'count' => true,
|
||||
'status' => 'approve',
|
||||
];
|
||||
|
||||
if ( $features ) {
|
||||
$features_query = [];
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$features_query[] = [
|
||||
'key' => $cpt_feature . '_' . $feature_slug,
|
||||
'value' => $i,
|
||||
];
|
||||
}
|
||||
$args['meta_query'] = $features_query;
|
||||
$args['meta_query']['relation'] = 'OR';
|
||||
} else {
|
||||
$args['meta_query'] = [
|
||||
[
|
||||
'key' => 'opalestate_rating',
|
||||
'value' => $i,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$output[ $i ] = get_comments( $args );
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public static function get_rating_average_stats_by_features_for_post( $post_id, $cpt_feature, $meta_prefix ) {
|
||||
global $wpdb;
|
||||
|
||||
$output = [];
|
||||
$count = get_post_meta( $post_id, $meta_prefix . 'review_count', true );
|
||||
$features = static::get_features( $cpt_feature );
|
||||
|
||||
if ( ! $features || ! $count ) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$meta_key = $cpt_feature . '_' . $feature_slug;
|
||||
|
||||
$ratings = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT SUM(meta_value) FROM $wpdb->commentmeta
|
||||
LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
|
||||
WHERE meta_key = %s
|
||||
AND comment_post_ID = %d
|
||||
AND comment_approved = '1'
|
||||
AND meta_value > 0
|
||||
",
|
||||
$meta_key,
|
||||
$post_id
|
||||
)
|
||||
);
|
||||
$average = number_format( $ratings / $count, 2, '.', '' );
|
||||
$output[ $feature_slug ] = $average;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public static function get_features( $cpt_feature, $posts_per_page = -1 ) {
|
||||
$args = [
|
||||
'post_type' => $cpt_feature,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $posts_per_page,
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'meta_value_num',
|
||||
'meta_key' => 'opalestate_feature_order',
|
||||
];
|
||||
|
||||
$features = get_posts( $args );
|
||||
|
||||
return wp_list_pluck( $features, 'post_title', 'post_name' );
|
||||
}
|
||||
}
|
||||
|
||||
new Opalestate_Rating_Helper();
|
||||
245
inc/rating/class-opalestate-rating-init.php
Executable file
245
inc/rating/class-opalestate-rating-init.php
Executable file
@@ -0,0 +1,245 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Opalestate_Rating_Init
|
||||
*/
|
||||
class Opalestate_Rating_Init {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cpt_support;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cpt_feature;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $meta_prefix;
|
||||
|
||||
/**
|
||||
* Opalestate_Rating_Init constructor.
|
||||
*
|
||||
* @param string $cpt_support Custom post type supported name
|
||||
* @param string $cpt_feature Custom post type feature rating name
|
||||
* @param string $meta_prefix Meta data prefix.
|
||||
*/
|
||||
public function __construct( $cpt_support, $cpt_feature, $meta_prefix ) {
|
||||
$this->cpt_support = $cpt_support;
|
||||
$this->cpt_feature = $cpt_feature;
|
||||
$this->meta_prefix = $meta_prefix;
|
||||
|
||||
// Check comment fields.
|
||||
add_filter( 'comments_open', [ $this, 'comments_open' ], 10, 2 );
|
||||
add_filter( 'preprocess_comment', [ $this, 'check_comment_rating' ], 0 );
|
||||
|
||||
// Add comment rating.
|
||||
add_action( 'comment_post', [ $this, 'add_comment_rating' ], 1 );
|
||||
|
||||
// Clear transients.
|
||||
add_action( 'wp_update_comment_count', [ $this, 'clear_transients' ] );
|
||||
|
||||
// Support avatars for `review` comment type.
|
||||
add_filter( 'get_avatar_comment_types', [ $this, 'add_avatar_for_review_comment_type' ] );
|
||||
|
||||
// Set comment type.
|
||||
add_filter( 'preprocess_comment', [ $this, 'update_comment_type' ], 1 );
|
||||
|
||||
// Remove comment meta boxes.
|
||||
add_action( 'admin_menu', [ $this, 'remove_meta_boxes' ] );
|
||||
|
||||
// Clean
|
||||
add_action( 'trashed_post', [ $this, 'trash_feature' ] );
|
||||
add_action( 'untrashed_post', [ $this, 'trash_feature' ] );
|
||||
add_action( 'delete_post', [ $this, 'clear_meta' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets comment type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_comment_type() {
|
||||
return str_replace( 'opalestate_', '', $this->cpt_support ) . '_review';
|
||||
}
|
||||
|
||||
/**
|
||||
* See if comments are open.
|
||||
*
|
||||
* @param bool $open Whether the current post is open for comments.
|
||||
* @param int $post_id Post ID.
|
||||
* @return bool
|
||||
*/
|
||||
public function comments_open( $open, $post_id ) {
|
||||
if ( $this->cpt_support === get_post_type( $post_id ) && ! post_type_supports( $this->cpt_support, 'comments' ) ) {
|
||||
$open = false;
|
||||
}
|
||||
|
||||
return $open;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the comment ratings.
|
||||
*
|
||||
* @param array $comment_data Comment data.
|
||||
* @return array
|
||||
*/
|
||||
public function check_comment_rating( $comment_data ) {
|
||||
// If posting a comment (not trackback etc) and not logged in.
|
||||
$features = Opalestate_Rating_Helper::get_features( $this->cpt_feature );
|
||||
if ( $features ) {
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$post = $this->cpt_feature . '_' . $feature_slug;
|
||||
if ( ! is_admin() && isset( $_POST['comment_post_ID'], $_POST[ $post ], $comment_data['comment_type'] ) && $this->cpt_support === get_post_type( absint( $_POST['comment_post_ID'] ) ) && empty( $_POST[ $post ] ) && '' === $comment_data['comment_type'] ) { // WPCS: input var ok, CSRF ok.
|
||||
wp_die( esc_html__( 'Please rate all features.', 'opalestate-pro' ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( ! is_admin() && isset( $_POST['comment_post_ID'], $_POST['opalestate_rating'], $comment_data['comment_type'] ) && $this->cpt_support === get_post_type( absint( $_POST['comment_post_ID'] ) ) && empty( $_POST['opalestate_rating'] ) && '' === $comment_data['comment_type'] ) { // WPCS: input var ok, CSRF ok.
|
||||
wp_die( esc_html__( 'Please rate.', 'opalestate-pro' ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
return $comment_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rating field for comments.
|
||||
*
|
||||
* @param int $comment_id Comment ID.
|
||||
*/
|
||||
public function add_comment_rating( $comment_id ) {
|
||||
if ( ! isset( $_POST['comment_post_ID'] ) || ( $this->cpt_support !== get_post_type( absint( $_POST['comment_post_ID'] ) ) ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$features = Opalestate_Rating_Helper::get_features( $this->cpt_feature );
|
||||
if ( $features ) {
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$post = $this->cpt_feature . '_' . $feature_slug;
|
||||
if ( isset( $_POST[ $post ] ) ) {
|
||||
if ( ! $_POST[ $post ] || $_POST[ $post ] > 5 || $_POST[ $post ] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
|
||||
continue;
|
||||
}
|
||||
add_comment_meta( $comment_id, $post, intval( $_POST[ $post ] ), true ); // WPCS: input var ok, CSRF ok.
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( isset( $_POST['opalestate_rating'] ) ) { // WPCS: input var ok, CSRF ok.
|
||||
if ( ! $_POST['opalestate_rating'] || $_POST['opalestate_rating'] > 5 || $_POST['opalestate_rating'] < 0 ) { // WPCS: input var ok, CSRF ok, sanitization ok.
|
||||
return;
|
||||
}
|
||||
add_comment_meta( $comment_id, 'opalestate_rating', intval( $_POST['opalestate_rating'] ), true ); // WPCS: input var ok, CSRF ok.
|
||||
}
|
||||
}
|
||||
|
||||
$post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // WPCS: input var ok, CSRF ok.
|
||||
if ( $post_id ) {
|
||||
$this->clear_transients( $post_id );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure WP displays avatars for comments with the `$this->cpt_support` type.
|
||||
*
|
||||
* @param array $comment_types Comment types.
|
||||
* @return array
|
||||
*/
|
||||
public function add_avatar_for_review_comment_type( $comment_types ) {
|
||||
return array_merge( $comment_types, [ $this->get_comment_type() ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure property average rating and review count is kept up to date.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
*/
|
||||
public function clear_transients( $post_id ) {
|
||||
if ( $this->cpt_support === get_post_type( $post_id ) ) {
|
||||
do_action( 'opalestate_rating_before_clear_transients', $post_id, $this->cpt_support, $this->cpt_feature );
|
||||
|
||||
update_post_meta( $post_id, $this->meta_prefix . 'rating_count', Opalestate_Rating_Helper::get_rating_counts_for_post( $post_id, $this->cpt_feature ) );
|
||||
update_post_meta( $post_id, $this->meta_prefix . 'average_rating', Opalestate_Rating_Helper::get_average_rating_for_post( $post_id, $this->cpt_feature ) );
|
||||
update_post_meta( $post_id, $this->meta_prefix . 'review_count', Opalestate_Rating_Helper::get_review_count_for_post( $post_id ) );
|
||||
update_post_meta( $post_id, $this->meta_prefix . 'rating_count_stats', Opalestate_Rating_Helper::get_rating_count_stats_for_post( $post_id, $this->cpt_feature ) );
|
||||
update_post_meta( $post_id, $this->meta_prefix . 'rating_average_stats', Opalestate_Rating_Helper::get_rating_average_stats_by_features_for_post( $post_id, $this->cpt_feature,
|
||||
$this->meta_prefix ) );
|
||||
|
||||
do_action( 'opalestate_rating_after_clear_transients', $post_id, $this->cpt_support, $this->cpt_feature );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update comment type of property reviews.
|
||||
*
|
||||
* @param array $comment_data Comment data.
|
||||
* @return array
|
||||
*/
|
||||
public function update_comment_type( $comment_data ) {
|
||||
if ( ! is_admin() && isset( $_POST['comment_post_ID'], $comment_data['comment_type'] ) && '' === $comment_data['comment_type'] && $this->cpt_support === get_post_type( absint( $_POST['comment_post_ID'] ) ) ) { // WPCS: input var ok, CSRF ok.
|
||||
$comment_data['comment_type'] = $this->get_comment_type();
|
||||
}
|
||||
|
||||
return $comment_data;
|
||||
}
|
||||
|
||||
public function remove_meta_boxes() {
|
||||
// remove_meta_box( 'commentstatusdiv', $this->cpt_support, 'normal' );
|
||||
remove_meta_box( 'commentsdiv', $this->cpt_support, 'normal' );
|
||||
}
|
||||
|
||||
public function trash_feature( $id ) {
|
||||
if ( ! $id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post_type = get_post_type( $id );
|
||||
|
||||
if ( $post_type !== $this->cpt_feature ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->clean_and_recal();
|
||||
}
|
||||
|
||||
public function clear_meta( $id ) {
|
||||
if ( ! current_user_can( 'delete_posts' ) || ! $id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$post_type = get_post_type( $id );
|
||||
|
||||
if ( $post_type !== $this->cpt_feature ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$feature = get_post( $id );
|
||||
if ( ! $feature ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta_key = $this->cpt_feature . '_' . str_replace( '__trashed', '', $feature->post_name );
|
||||
if ( $meta_key && ( 0 !== $meta_key ) ) {
|
||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE meta_key = %s;", $meta_key ) );
|
||||
}
|
||||
|
||||
$this->clean_and_recal();
|
||||
}
|
||||
|
||||
protected function clean_and_recal() {
|
||||
$posts = get_posts( [
|
||||
'post_type' => $this->cpt_support,
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'any',
|
||||
] );
|
||||
|
||||
foreach ( $posts as $post ) {
|
||||
$this->clear_transients( $post->ID );
|
||||
}
|
||||
}
|
||||
}
|
||||
175
inc/rating/class-opalestate-rating-metabox.php
Executable file
175
inc/rating/class-opalestate-rating-metabox.php
Executable file
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Opalestate_Rating_MetaBox
|
||||
*
|
||||
* @package opalestate
|
||||
* @author Opal Team <info@wpopal.com >
|
||||
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
|
||||
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* @website http://www.wpopal.com
|
||||
* @support http://www.wpopal.com/support/forum.html
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Opalestate_Rating_MetaBox
|
||||
*/
|
||||
class Opalestate_Rating_MetaBox {
|
||||
|
||||
public function register_admin_comment_fields() {
|
||||
$rating_supports = Opalestate_Rating::get_rating_supports();
|
||||
|
||||
foreach ( $rating_supports as $key => $support ) {
|
||||
$this->register_comment_metabox( $support['post_type'], $support['features_cpt'] );
|
||||
}
|
||||
}
|
||||
|
||||
public function register_admin_feature_fields() {
|
||||
$rating_supports = Opalestate_Rating::get_rating_supports();
|
||||
|
||||
foreach ( $rating_supports as $key => $support ) {
|
||||
$this->register_feature_metabox( $support['features_cpt'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook in and register a metabox for the admin comment edit page.
|
||||
*/
|
||||
public function register_comment_metabox( $cpt_support, $cpt_feature ) {
|
||||
if ( ! isset( $_GET['c'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$comment_type = get_comment_type( sanitize_text_field( $_GET['c'] ) );
|
||||
|
||||
if ( $comment_type !== $this->get_comment_type( $cpt_support ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$features = Opalestate_Rating_Helper::get_features( $cpt_feature );
|
||||
|
||||
$cmb = new_cmb2_box( [
|
||||
'id' => $cpt_support . '_comment_meta',
|
||||
'title' => $features ? esc_html__( 'Rating features', 'opalestate-pro' ) : esc_html__( 'Rating', 'opalestate-pro' ),
|
||||
'object_types' => [ 'comment' ],
|
||||
] );
|
||||
|
||||
if ( $features ) {
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$id = $cpt_feature . '_' . $feature_slug;
|
||||
|
||||
$cmb->add_field( [
|
||||
'id' => $id,
|
||||
'type' => 'select',
|
||||
'name' => $feature_title,
|
||||
'options' => [
|
||||
'1' => '1 ★',
|
||||
'2' => '2 ★★',
|
||||
'3' => '3 ★★★',
|
||||
'4' => '4 ★★★★',
|
||||
'5' => '5 ★★★★★',
|
||||
],
|
||||
// 'show_on_cb' => function ( $cmb ) {
|
||||
// return isset( $_GET['c'] );
|
||||
// },
|
||||
] );
|
||||
}
|
||||
} else {
|
||||
$cmb->add_field( [
|
||||
'id' => 'opalestate_rating',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'1' => '1 ★',
|
||||
'2' => '2 ★★',
|
||||
'3' => '3 ★★★',
|
||||
'4' => '4 ★★★★',
|
||||
'5' => '5 ★★★★★',
|
||||
],
|
||||
// 'show_on_cb' => function ( $cmb ) {
|
||||
// return isset( $_GET['c'] );
|
||||
// },
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook in and register a metabox for the admin comment edit page.
|
||||
*/
|
||||
public function register_feature_metabox( $cpt_feature ) {
|
||||
$cmb = new_cmb2_box( [
|
||||
'id' => $cpt_feature . '_meta',
|
||||
'title' => esc_html__( 'Data', 'opalestate-pro' ),
|
||||
'object_types' => [ $cpt_feature ],
|
||||
] );
|
||||
|
||||
$cmb->add_field( [
|
||||
'name' => esc_html__( 'Description', 'opalestate-pro' ),
|
||||
'id' => 'opalestate_feature_desc',
|
||||
'type' => 'textarea_small',
|
||||
] );
|
||||
|
||||
$cmb->add_field( [
|
||||
'name' => esc_html__( 'Order', 'opalestate-pro' ),
|
||||
'desc' => esc_html__( 'Set a priority to display', 'opalestate-pro' ),
|
||||
'id' => 'opalestate_feature_order',
|
||||
'type' => 'text_small',
|
||||
'attributes' => [
|
||||
'type' => 'number',
|
||||
],
|
||||
'default' => 0,
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save meta box data
|
||||
*
|
||||
* @param mixed $data Data to save.
|
||||
* @return mixed
|
||||
*/
|
||||
public static function save( $data ) {
|
||||
if ( ! isset( $data['comment_post_ID'] ) || ! $data['comment_post_ID'] ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$comment_post_ID = $data['comment_post_ID'];
|
||||
$cpt_support = get_post_type( $comment_post_ID );
|
||||
$rating_supports = Opalestate_Rating::get_rating_supports();
|
||||
|
||||
if ( ! isset( $rating_supports[ $cpt_support ] ) || ! isset( $rating_supports[ $cpt_support ]['features_cpt'] ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$cpt_feature = $rating_supports[ $cpt_support ]['features_cpt'];
|
||||
$comment_id = $data['comment_ID'];
|
||||
$features = Opalestate_Rating_Helper::get_features( $cpt_feature );
|
||||
|
||||
if ( $features ) {
|
||||
foreach ( $features as $feature_slug => $feature_title ) {
|
||||
$id = $cpt_feature . '_' . $feature_slug;
|
||||
if ( isset( $_POST[ $id ] ) && ( $_POST[ $id ] > 0 ) && ( $_POST[ $id ] <= 5 ) ) {
|
||||
update_comment_meta( $comment_id, $id, intval( wp_unslash( $_POST[ $id ] ) ) ); // WPCS: input var ok.
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( isset( $_POST['opalestate_rating'] ) && ( $_POST['opalestate_rating'] > 0 ) && ( $_POST['opalestate_rating'] <= 5 ) ) {
|
||||
update_comment_meta( $comment_id, 'opalestate_rating', intval( wp_unslash( $_POST['opalestate_rating'] ) ) ); // WPCS: input var ok.
|
||||
}
|
||||
}
|
||||
|
||||
// Return regular value after updating.
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets comment type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_comment_type( $cpt_support ) {
|
||||
return str_replace( 'opalestate_', '', $cpt_support ) . '_review';
|
||||
}
|
||||
}
|
||||
102
inc/rating/class-opalestate-rating.php
Executable file
102
inc/rating/class-opalestate-rating.php
Executable file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
class Opalestate_Rating {
|
||||
public function __construct() {
|
||||
$this->includes();
|
||||
$this->process();
|
||||
|
||||
// Template loader.
|
||||
add_filter( 'comments_template', [ $this, 'comments_template_loader' ] );
|
||||
|
||||
// Add shortcode User property reviews.
|
||||
add_shortcode( 'opalestate_user_property_reviews', [ $this, 'property_reviews_template' ] );
|
||||
add_filter( 'opalestate_user_content_reviews_page', [ $this, 'property_reviews_template' ] );
|
||||
}
|
||||
|
||||
public function includes() {
|
||||
require_once 'class-opalestate-rating-features-posttype.php';
|
||||
require_once 'class-opalestate-rating-metabox.php';
|
||||
require_once 'class-opalestate-rating-helper.php';
|
||||
require_once 'class-opalestate-rating-init.php';
|
||||
require_once 'rating-functions.php';
|
||||
require_once 'rating-hook-functions.php';
|
||||
}
|
||||
|
||||
public static function get_rating_supports() {
|
||||
return [
|
||||
// Support property rating.
|
||||
'opalestate_property' => [
|
||||
'post_type' => 'opalestate_property',
|
||||
'features_cpt' => 'opalestate_rating_ft',
|
||||
'prefix' => OPALESTATE_PROPERTY_PREFIX,
|
||||
],
|
||||
// Support agency rating.
|
||||
'opalestate_agency' => [
|
||||
'post_type' => 'opalestate_agency',
|
||||
'features_cpt' => 'opalestate_agency_ft',
|
||||
'prefix' => OPALESTATE_AGENCY_PREFIX,
|
||||
],
|
||||
// Support agent rating.
|
||||
'opalestate_agent' => [
|
||||
'post_type' => 'opalestate_agent',
|
||||
'features_cpt' => 'opalestate_agent_ft',
|
||||
'prefix' => OPALESTATE_AGENT_PREFIX,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function process() {
|
||||
$rating_supports = static::get_rating_supports();
|
||||
foreach ( $rating_supports as $key => $support ) {
|
||||
new Opalestate_Rating_Init( $support['post_type'], $support['features_cpt'], $support['prefix'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load comments template.
|
||||
*
|
||||
* @param string $template template to load.
|
||||
* @return string
|
||||
*/
|
||||
public function comments_template_loader( $template ) {
|
||||
$supports = static::get_rating_supports();
|
||||
$post_type_supports = array_keys( $supports );
|
||||
|
||||
if ( ! in_array( get_post_type(), $post_type_supports ) ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
$check_dirs = [
|
||||
trailingslashit( get_stylesheet_directory() ) . 'opalestate/rating/',
|
||||
trailingslashit( get_template_directory() ) . 'opalestate/rating/',
|
||||
trailingslashit( get_stylesheet_directory() ),
|
||||
trailingslashit( get_template_directory() ),
|
||||
trailingslashit( OPALESTATE_PLUGIN_DIR ) . 'templates/rating/',
|
||||
];
|
||||
|
||||
foreach ( $check_dirs as $dir ) {
|
||||
$file = 'opalestate-ratings.php';
|
||||
if ( file_exists( trailingslashit( $dir ) . $file ) ) {
|
||||
return trailingslashit( $dir ) . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function property_reviews_template() {
|
||||
if ( ! is_user_logged_in() || ! $current_user_id = get_current_user_id() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$args = [
|
||||
'post_author__in' => [ $current_user_id ],
|
||||
'status' => 'approve',
|
||||
'type' => 'property_review',
|
||||
];
|
||||
|
||||
$comments = get_comments( $args );
|
||||
|
||||
return opalestate_load_template_path( 'user/property-ratings', [ 'comments' => $comments ] );
|
||||
}
|
||||
}
|
||||
|
||||
new Opalestate_Rating();
|
||||
107
inc/rating/rating-functions.php
Executable file
107
inc/rating/rating-functions.php
Executable file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* Check if reviews are enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function opalestate_property_reviews_enabled() {
|
||||
return 'on' === opalestate_get_option( 'enable_property_reviews', 'on' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if reviews are enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function opalestate_agency_reviews_enabled() {
|
||||
return 'on' === opalestate_get_option( 'enable_agency_reviews', 'on' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if reviews are enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function opalestate_agent_reviews_enabled() {
|
||||
return 'on' === opalestate_get_option( 'enable_agent_reviews', 'on' );
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_comments' ) ) {
|
||||
|
||||
/**
|
||||
* Output the Review comments template.
|
||||
*
|
||||
* @param WP_Comment $comment Comment object.
|
||||
* @param array $args Arguments.
|
||||
* @param int $depth Depth.
|
||||
*/
|
||||
function opalestate_comments( $comment, $args, $depth ) {
|
||||
$GLOBALS['comment'] = $comment; // WPCS: override ok.
|
||||
echo opalestate_load_template_path(
|
||||
'rating/review',
|
||||
[
|
||||
'comment' => $comment,
|
||||
'args' => $args,
|
||||
'depth' => $depth,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_review_display_gravatar' ) ) {
|
||||
/**
|
||||
* Display the review authors gravatar
|
||||
*
|
||||
* @param array $comment WP_Comment.
|
||||
* @return void
|
||||
*/
|
||||
function opalestate_review_display_gravatar( $comment ) {
|
||||
echo get_avatar( $comment, apply_filters( 'opalestate_review_gravatar_size', '60' ), '' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_review_display_rating' ) ) {
|
||||
/**
|
||||
* Display the reviewers star rating
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function opalestate_review_display_rating() {
|
||||
echo opalestate_load_template_path( 'rating/review-rating' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_review_display_meta' ) ) {
|
||||
/**
|
||||
* Display the review authors meta (name, verified owner, review date)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function opalestate_review_display_meta() {
|
||||
echo opalestate_load_template_path( 'rating/review-meta' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_review_display_comment_text' ) ) {
|
||||
|
||||
/**
|
||||
* Display the review content.
|
||||
*/
|
||||
function opalestate_review_display_comment_text() {
|
||||
echo '<div class="description">';
|
||||
comment_text();
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
function opalestate_get_property_rating_features() {
|
||||
return Opalestate_Rating_Helper::get_features( 'opalestate_rating_ft' );
|
||||
}
|
||||
|
||||
function opalestate_get_agency_rating_features() {
|
||||
return Opalestate_Rating_Helper::get_features( 'opalestate_agency_ft' );
|
||||
}
|
||||
|
||||
function opalestate_get_agent_rating_features() {
|
||||
return Opalestate_Rating_Helper::get_features( 'opalestate_agent_ft' );
|
||||
}
|
||||
13
inc/rating/rating-hook-functions.php
Executable file
13
inc/rating/rating-hook-functions.php
Executable file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Reviews for opalestate.
|
||||
*
|
||||
* @see opalestate_review_display_gravatar()
|
||||
* @see opalestate_review_display_rating()
|
||||
* @see opalestate_review_display_meta()
|
||||
* @see opalestate_review_display_comment_text()
|
||||
*/
|
||||
add_action( 'opalestate_review_before', 'opalestate_review_display_gravatar', 10 );
|
||||
add_action( 'opalestate_review_before_comment_meta', 'opalestate_review_display_meta', 10 );
|
||||
add_action( 'opalestate_review_meta', 'opalestate_review_display_rating', 10 );
|
||||
add_action( 'opalestate_review_comment_text', 'opalestate_review_display_comment_text', 10 );
|
||||
Reference in New Issue
Block a user