Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add review count notice #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions includes/components/class-review.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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,
];
}
Expand Down Expand Up @@ -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 ) {
kuznev marked this conversation as resolved.
Show resolved Hide resolved
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' );
}
}
Loading