Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } $comment_ID = ! empty( $comment->comment_ID ) ? absint( $comment->comment_ID ) : 0; $ratings = get_comment_meta( $comment_ID, 'adqs_review_rating', true ); if ( empty( $ratings ) ) { return; } add_meta_box( 'adqs_listing_ratings_meta_box', esc_html__( 'Directory Listing Ratings', 'adirectory' ), [ $this, 'render_comment_meta_box' ], 'comment', 'normal', 'default' ); } /** * Render comment meta box field * * @since 1.0.0 * @return void */ public function render_comment_meta_box( $comment ) { wp_nonce_field( basename( __FILE__ ), 'adqs_comment_metabox' ); $comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : ''; $ratings = get_comment_meta( $comment_id, 'adqs_review_rating', true ); ?>

Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } if ( ! isset( $_POST['adqs_review_rating'] ) || ! wp_verify_nonce( $_POST['adqs_comment_metabox'], basename( __FILE__ ) ) || ! current_user_can( 'edit_comment', $comment_id ) ) { return; } $review_rating = absint( $_POST['adqs_review_rating'] ); $ratings = get_comment_meta( $comment_id, 'adqs_review_rating', true ); update_comment_meta( $comment_id, 'adqs_review_rating', $review_rating, $ratings ); } /** * Save Review Rating * * @since 1.0.0 * @return void */ public function save_comment_rating( $comment_id ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } if ( isset( $_POST['adqs_review_rating'] ) && ( '' !== $_POST['adqs_review_rating'] ) && wp_verify_nonce( $_POST['review_nonce'], 'review_action' ) ) { $rating = intval( $_POST['adqs_review_rating'] ); add_comment_meta( $comment_id, 'adqs_review_rating', $rating ); } } /** * Save Review Rating * * @since 1.0.0 * @return string */ public function comment_require_rating( $commentdata ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { // Return commentdata without processing if review system is disabled // The restrict_users filter will handle closing comments for directory post types return $commentdata; } // Check if current post is one of our dynamic post types $dynamic_post_types = adqs_get_directory_post_types(); $is_dynamic_post_type = false; if ( is_array( $dynamic_post_types ) ) { foreach ( $dynamic_post_types as $post_type ) { if ( is_singular( $post_type ) ) { $is_dynamic_post_type = true; break; } } } if ( ! $is_dynamic_post_type ) { return $commentdata; } if ( ! is_admin() && ( ! isset( $_POST['adqs_review_rating'] ) || ( 0 === absint( $_POST['adqs_review_rating'] ) ) || ! wp_verify_nonce( $_POST['review_nonce'], 'review_action' ) ) ) { wp_die( esc_html__( 'Error: You did not add a rating. Hit the Back button on your Web browser and resubmit your comment with a rating.', 'adirectory' ) ); } return $commentdata; } /** * Only One Review restrict users * * @since 1.0.0 * @return boolean */ public function restrict_users( $open, $post_id ) { // Check if review system is enabled first $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { // Check if this is a directory post type before closing comments $post = get_post( $post_id ); if ( $post ) { $dynamic_post_types = adqs_get_directory_post_types(); if ( in_array( $post->post_type, $dynamic_post_types, true ) ) { return false; // Close comments when review system is disabled for directory post types } } return $open; // Don't affect non-directory post types } // Check if current post is one of our dynamic post types $dynamic_post_types = adqs_get_directory_post_types(); $is_dynamic_post_type = false; $post = get_post( $post_id ); if ( $post && in_array( $post->post_type, $dynamic_post_types, true ) ) { $is_dynamic_post_type = true; } if ( ! $is_dynamic_post_type ) { return $open; } if ( intval( $post_id ) && get_post( $post_id ) ) { $args = [ 'post_id' => $post_id, 'count' => true, ]; $user = wp_get_current_user(); if ( $user && intval( $user->ID ) ) { // for registered users $skip = false; $ignoreTheseRoles = [ 'administrator', 'editor' ]; // which user roles should be ignored if ( $user->roles && is_array( $user->roles ) ) { foreach ( $user->roles as $role ) { if ( in_array( $role, $ignoreTheseRoles ) ) { $skip = true; break; } } } if ( ! $skip ) { $args['user_id'] = $user->ID; $open = get_comments( $args ) ? false : true; } } } return $open; } /** * Add comments columns * * @param array $cols * @return array */ public function add_comments_columns( $cols ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return $cols; } $add_columns = [ 'adqs_ratings' => esc_html__( 'Ratings', 'adirectory' ), ]; $cols = array_slice( $cols, 0, 3, true ) + $add_columns + array_slice( $cols, 3, null, true ); return $cols; } /** * Add comment columns content * * @param [type] $column * @param [type] $comment_ID * @return void */ public function add_comment_columns_content( $column, $comment_ID ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } if ( $column === 'adqs_ratings' ) { $comment = get_comment( $comment_ID ); $ratings = get_comment_meta( $comment_ID, 'adqs_review_rating', true ); if ( ! empty( $ratings ) ) { // This is a review (has rating) AD()->Helper->get_review_rating_html( $ratings ); } elseif ( $comment && $comment->comment_parent > 0 ) { // This is a reply to a review $parent_comment = get_comment( $comment->comment_parent ); if ( $parent_comment ) { $parent_ratings = get_comment_meta( $parent_comment->comment_ID, 'adqs_review_rating', true ); if ( ! empty( $parent_ratings ) ) { echo '' . esc_html__( 'Reply to Review', 'adirectory' ) . ''; } } } } } /** * Filter comments by ratings in admin list table * * @param array $args Comment query arguments * @return array Modified query arguments */ public function filter_comments_by_ratings( $args ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return $args; // Don't filter comments if review system is disabled } // Only apply this filter on the comments admin page $screen = get_current_screen(); if ( ! is_admin() || ! $screen || $screen->id !== 'edit-comments' ) { return $args; } // Check if we're on the comments page with adqs_review parameter $is_review_page = isset( $_GET['adqs_review'] ) && $_GET['adqs_review'] === 'yes'; if ( $is_review_page ) { // Show both reviews (comments with ratings) and their replies // We need to get all comments that are either: // 1. Comments with ratings (parent reviews) // 2. Replies to comments with ratings (child comments of reviews) global $wpdb; // Get all comment IDs that have ratings $review_comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = %s", 'adqs_review_rating' ) ); if ( ! empty( $review_comment_ids ) ) { // Sanitize all comment IDs to integers $review_comment_ids = array_map( 'absint', $review_comment_ids ); $review_comment_ids = array_filter( $review_comment_ids ); // Remove any invalid IDs if ( ! empty( $review_comment_ids ) ) { // Build safe placeholders for IN clause $placeholders = implode( ',', array_fill( 0, count( $review_comment_ids ), '%d' ) ); // Get all replies to these review comments using prepared statement $reply_comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent IN ($placeholders)", $review_comment_ids ) ); } else { $reply_comment_ids = []; } // Combine review IDs and reply IDs $all_comment_ids = array_merge( $review_comment_ids, $reply_comment_ids ); if ( ! empty( $all_comment_ids ) ) { $args['comment__in'] = array_map( 'absint', $all_comment_ids ); } else { // If no reviews found, show nothing $args['comment__in'] = [ 0 ]; } } else { // If no reviews found, show nothing $args['comment__in'] = [ 0 ]; } } else { // Hide all reviews and their replies when not on review page global $wpdb; // Get all comment IDs that have ratings $review_comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = %s", 'adqs_review_rating' ) ); if ( ! empty( $review_comment_ids ) ) { // Sanitize all comment IDs to integers $review_comment_ids = array_map( 'absint', $review_comment_ids ); $review_comment_ids = array_filter( $review_comment_ids ); // Remove any invalid IDs if ( ! empty( $review_comment_ids ) ) { // Build safe placeholders for IN clause $placeholders = implode( ',', array_fill( 0, count( $review_comment_ids ), '%d' ) ); // Get all replies to these review comments using prepared statement $reply_comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_parent IN ($placeholders)", $review_comment_ids ) ); } else { $reply_comment_ids = []; } // Combine review IDs and reply IDs $all_comment_ids = array_merge( $review_comment_ids, $reply_comment_ids ); if ( ! empty( $all_comment_ids ) ) { $args['comment__not_in'] = array_map( 'absint', $all_comment_ids ); } } } return $args; } /** * Modify comment row actions to show review/reply status * * @param array $actions Comment row actions * @param WP_Comment $comment Comment object * @return array Modified actions */ public function modify_comment_row_actions( $actions, $comment ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return $actions; } $ratings = get_comment_meta( $comment->comment_ID, 'adqs_review_rating', true ); if ( ! empty( $ratings ) ) { // This is a review $actions['review_status'] = '' . esc_html__( 'Review', 'adirectory' ) . ''; } elseif ( $comment->comment_parent > 0 ) { // This is a reply $parent_comment = get_comment( $comment->comment_parent ); if ( $parent_comment ) { $parent_ratings = get_comment_meta( $parent_comment->comment_ID, 'adqs_review_rating', true ); if ( ! empty( $parent_ratings ) ) { $actions['reply_status'] = '' . esc_html__( 'Reply to Review', 'adirectory' ) . ''; } } } return $actions; } /** * Modify comment text display to show review/reply status * * @param string $comment_text Comment text * @param WP_Comment $comment Comment object * @param array $args Additional arguments * @return string Modified comment text */ public function modify_comment_text_display( $comment_text, $comment, $args ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return $comment_text; } // Only modify in admin area if ( ! is_admin() ) { return $comment_text; } $ratings = get_comment_meta( $comment->comment_ID, 'adqs_review_rating', true ); if ( ! empty( $ratings ) ) { // This is a review $prefix = '[' . esc_html__( 'REVIEW', 'adirectory' ) . '] '; return $prefix . $comment_text; } elseif ( $comment->comment_parent > 0 ) { // This is a reply $parent_comment = get_comment( $comment->comment_parent ); if ( $parent_comment ) { $parent_ratings = get_comment_meta( $parent_comment->comment_ID, 'adqs_review_rating', true ); if ( ! empty( $parent_ratings ) ) { $prefix = '[' . esc_html__( 'REPLY', 'adirectory' ) . '] '; return $prefix . $comment_text; } } } return $comment_text; } /** * Auto-delete child comments when parent comment is deleted * * @param int $comment_id The ID of the comment being deleted * @return void */ public function auto_delete_child_comments( $comment_id ) { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } // Get the comment to find the post and author $comment = get_comment( $comment_id ); if ( ! $comment ) { return; } $post_id = $comment->comment_post_ID; $author_id = get_post_field( 'post_author', $post_id ); // Get all child comments (replies) of the deleted comment $child_comments = get_comments( [ 'parent' => $comment_id, 'status' => 'any', // Include all statuses (approved, pending, spam, trash) 'number' => 0, // Get all child comments ] ); if ( ! empty( $child_comments ) ) { foreach ( $child_comments as $child_comment ) { // Recursively delete child comments (in case there are nested replies) $this->auto_delete_child_comments( $child_comment->comment_ID ); // Delete the child comment wp_delete_comment( $child_comment->comment_ID, true ); // true = force delete (skip trash) } } // Clear author review count cache after deletion if ( $author_id ) { AD()->Helper->clear_author_review_cache( $author_id ); } } /** * Handle bulk comment deletion to ensure child comments are also deleted * * @return void */ public function handle_bulk_comment_deletion() { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } // Check if this is a bulk action if ( ! isset( $_POST['action'] ) || $_POST['action'] !== 'delete-comment' ) { return; } // Check if comments are being deleted if ( ! isset( $_POST['delete_comments'] ) || ! is_array( $_POST['delete_comments'] ) ) { return; } // Process each comment being deleted foreach ( $_POST['delete_comments'] as $comment_id ) { $comment_id = absint( $comment_id ); if ( $comment_id > 0 ) { // This will trigger the delete_comment hook which calls auto_delete_child_comments wp_delete_comment( $comment_id, true ); } } } /** * Enqueue admin comments CSS * * @since 1.0.0 * @return void */ public function enqueue_admin_comments_css() { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } $screen = get_current_screen(); if ( $screen && ( $screen->id === 'edit-comments' ) && ( ( $_GET['adqs_review'] ?? '' ) === 'yes' ) ) { wp_enqueue_style( 'adqs_admin_comments' ); } } /** * Ensure post types are available for comments table * This prevents null post type object errors in the comments list table */ public function ensure_post_types_for_comments_table() { // Check if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); if ( empty( $enable_review ) ) { return; } // Only run on comments page $current_screen = get_current_screen(); if ( ! $current_screen || $current_screen->id !== 'edit-comments' ) { return; } // Check if we're on the aDirectory reviews page if ( ! isset( $_GET['adqs_review'] ) || $_GET['adqs_review'] !== 'yes' ) { return; } // Ensure all dynamic post types are registered $dynamic_post_types = adqs_get_directory_post_types(); foreach ( $dynamic_post_types as $post_type ) { if ( ! post_type_exists( $post_type ) ) { // Force re-registration by calling the init action do_action( 'init' ); break; } } } }