Advertisement
geminilabs

[site-reviews] add external rating counts to summary

Apr 10th, 2023 (edited)
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. // Use these meta_keys in the Custom Fields metabox to add external rating values to your pages.
  2. // If found, the values of these meta_keys will be added to the total rating counts of the Rating Summary.
  3. // These meta keys will only work if you are assigning reviews to the page.  
  4.  
  5. // external_rating_0
  6. // external_rating_1
  7. // external_rating_2
  8. // external_rating_3
  9. // external_rating_4
  10. // external_rating_5
  11.  
  12. /**
  13.  * Add external rating counts to the Rating Summary
  14.  */
  15. add_filter('site-reviews/ratings', function (array $ratings, array $args) {
  16.     if (empty($ratings)) {
  17.         $ratings = [0,0,0,0,0,0];
  18.     }
  19.     foreach ((array) $args['assigned_posts'] as $postId) {
  20.         foreach ($ratings as $rating => $value) {
  21.             $metaKey = 'external_rating_'.$rating;
  22.             $ratings[$rating] += (int) get_post_meta($postId, $metaKey, true);
  23.         }
  24.     }
  25.     return $ratings;
  26. }, 10, 2);
  27.  
  28. /**
  29.  * Add external rating counts when calculating the average rating,
  30.  * ranking, and counts for assigned posts, users, and terms.
  31.  */
  32. add_filter('site-reviews/ratings/grouped', function (array $ratings, $metaKey) {
  33.     if ('post' !== $metaKey) {
  34.         return $ratings;
  35.     }
  36.     foreach ($ratings as $postId => $ratingValues) {
  37.         foreach ($ratingValues as $rating => $value) {
  38.             $metaKey = 'external_rating_'.$rating;
  39.             $ratings[$postId][$rating] += (int) get_post_meta($postId, $metaKey, true);
  40.         }
  41.     }
  42.     return $ratings;
  43. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement