diff --git a/includes/components/class-review.php b/includes/components/class-review.php index b20efc5..34b71fb 100644 --- a/includes/components/class-review.php +++ b/includes/components/class-review.php @@ -59,6 +59,9 @@ public function __construct( $args = [] ) { add_filter( 'hivepress/v1/templates/review_view_block', [ $this, 'alter_review_view_block' ] ); + // Clear review cache. + add_action( 'transition_comment_status', [ $this, 'clear_review_cache' ], 10, 3 ); + parent::__construct( $args ); } @@ -251,9 +254,29 @@ public function alter_listing_manage_menu( $items, $menu ) { $listing = $menu->get_context( 'listing' ); if ( $listing && $listing->get_rating_count() ) { + + // Get cached review count. + $review_count = hivepress()->cache->get_post_cache( $listing->get_id(), 'review_count', 'models/review' ); + + if ( is_null( $review_count ) ) { + + // Get review count. + $review_count = Models\Review::query()->filter( + [ + 'listing' => $listing->get_id(), + 'parent' => null, + 'approved' => true, + ] + )->get_count(); + + // Set cached review count. + hivepress()->cache->set_post_cache( $listing->get_id(), 'review_count', 'models/review', $review_count ); + } + $items['listing_reviews'] = [ 'label' => hivepress()->translator->get_string( 'reviews' ), 'url' => $items['listing_view']['url'] . '#reviews', + 'meta' => $review_count, '_order' => 20, ]; } @@ -415,4 +438,20 @@ public function alter_review_view_block( $template ) { return $template; } + + /** + * Clear review cache. + * + * @param string $new_status New comment status. + * @param string $old_status Old comment status. + * @param WP_Comment $comment Comment object. + */ + public function clear_review_cache( $new_status, $old_status, $comment ) { + if ( 'hp_review' !== $comment->comment_type || $comment->comment_parent ) { + return; + } + + // Delete post cache. + hivepress()->cache->delete_post_cache( $comment->comment_post_ID, 'review_count', 'models/review' ); + } }