From 97a2728cdbeb977d1ca1691c59f479865fde9ae0 Mon Sep 17 00:00:00 2001 From: Nikita Sinelnikov Date: Fri, 23 Jul 2021 15:53:07 +0300 Subject: [PATCH] - added comments query changes; --- includes/class-init.php | 5 +- includes/core/class-access.php | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/includes/class-init.php b/includes/class-init.php index d5021351..b46961aa 100644 --- a/includes/class-init.php +++ b/includes/class-init.php @@ -529,6 +529,7 @@ if ( ! class_exists( 'UM' ) ) { public function includes() { $this->common(); + $this->access(); if ( $this->is_request( 'ajax' ) ) { $this->admin(); @@ -540,7 +541,6 @@ if ( ! class_exists( 'UM' ) ) { $this->columns(); $this->admin()->notices(); $this->admin_navmenu(); - $this->access(); $this->plugin_updater(); $this->theme_updater(); } elseif ( $this->is_request( 'admin' ) ) { @@ -565,7 +565,6 @@ if ( ! class_exists( 'UM' ) ) { $this->login(); $this->register(); $this->user_posts(); - $this->access(); $this->logout(); } @@ -1517,4 +1516,4 @@ function UM() { // Global for backwards compatibility. -$GLOBALS['ultimatemember'] = UM(); \ No newline at end of file +$GLOBALS['ultimatemember'] = UM(); diff --git a/includes/core/class-access.php b/includes/core/class-access.php index f68d4e9a..621d66d2 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -58,6 +58,9 @@ if ( ! class_exists( 'um\core\Access' ) ) { add_filter( 'widget_posts_args', array( &$this, 'exclude_restricted_posts_widget' ), 99, 1 ); add_filter( 'comment_feed_where', array( &$this, 'exclude_posts_comments_feed' ), 99, 2 ); + + add_filter( 'wp_count_comments', array( &$this, 'custom_comments_count_handler' ), 99, 2 ); + //there is posts (Posts/Page/CPT) filtration if site is accessible //there also will be redirects if they need //protect posts types @@ -93,6 +96,101 @@ if ( ! class_exists( 'um\core\Access' ) ) { } + function custom_comments_count_handler( $stats, $post_id ) { + if ( ! empty( $stats ) ) { + return $stats; + } + + $exclude_posts = $this->exclude_posts_array( false ); + + if ( empty( $exclude_posts ) ) { + return $stats; + } + + $count = wp_cache_get( "comments-{$post_id}", 'counts' ); + if ( false !== $count ) { + return $count; + } + + $stats = $this->get_comment_count( $post_id, $exclude_posts ); + $stats['moderated'] = $stats['awaiting_moderation']; + unset( $stats['awaiting_moderation'] ); + + $stats_object = (object) $stats; + wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' ); + + return $stats_object; + } + + + function get_comment_count( $post_id = 0, $exclude_posts = array() ) { + global $wpdb; + + $post_id = (int) $post_id; + + $where = 'WHERE 1=1 '; + if ( $post_id > 0 ) { + $where .= $wpdb->prepare( 'AND comment_post_ID = %d', $post_id ); + } + + if ( ! empty( $exclude_posts ) ) { + $exclude_string = implode( ',', $exclude_posts ); + $where .= ' AND comment_post_ID NOT IN ( ' . $exclude_string . ' )'; + } + + $where .= $wpdb->prepare( 'AND comment_post_ID = %d', $post_id ); + + $totals = (array) $wpdb->get_results( + " + SELECT comment_approved, COUNT( * ) AS total + FROM {$wpdb->comments} + {$where} + GROUP BY comment_approved + ", + ARRAY_A + ); + + $comment_count = array( + 'approved' => 0, + 'awaiting_moderation' => 0, + 'spam' => 0, + 'trash' => 0, + 'post-trashed' => 0, + 'total_comments' => 0, + 'all' => 0, + ); + + foreach ( $totals as $row ) { + switch ( $row['comment_approved'] ) { + case 'trash': + $comment_count['trash'] = $row['total']; + break; + case 'post-trashed': + $comment_count['post-trashed'] = $row['total']; + break; + case 'spam': + $comment_count['spam'] = $row['total']; + $comment_count['total_comments'] += $row['total']; + break; + case '1': + $comment_count['approved'] = $row['total']; + $comment_count['total_comments'] += $row['total']; + $comment_count['all'] += $row['total']; + break; + case '0': + $comment_count['awaiting_moderation'] = $row['total']; + $comment_count['total_comments'] += $row['total']; + $comment_count['all'] += $row['total']; + break; + default: + break; + } + } + + return array_map( 'intval', $comment_count ); + } + + /** * @param array $restriction *