Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <?php
  2. function topratings_function() {
  3.  
  4. /**
  5. * Adds the Tasty Recipes ratings to the top of the post.
  6. *
  7. * @param string $content Existing post content.
  8. * @return string
  9. */
  10. add_filter( 'the_content', function( $content ) {
  11. if ( ! class_exists( 'Tasty_Recipes' ) ) {
  12. return $content;
  13. }
  14. // Get all of the recipes that might be in the post.
  15. $recipe_ids = Tasty_Recipes::get_recipe_ids_from_content( $content );
  16. if ( ! empty( $recipe_ids ) ) {
  17. // Get the first recipe ID in the post and fetch its object.
  18. $recipe_id = array_shift( $recipe_ids );
  19. $recipe = Tasty_Recipes\Objects\Recipe::get_by_id( $recipe_id );
  20. if ( $recipe ) {
  21. // Fetch all total reviews, but only process if there's more than zero reviews.
  22. $total_reviews = $recipe->get_total_reviews();
  23. if ( $total_reviews ) {
  24. // Build the label and icon markup.
  25. $average_rating = round( (float) $recipe->get_average_rating(), 1 );
  26. $recipe_rating_label = '<span class="rating-label">';
  27. $recipe_rating_label .= sprintf(
  28. // translators: Ratings from number of reviews.
  29. __( '%1$s from %2$s reviews', 'tasty-recipes' ),
  30. '<span class="average">' . $average_rating . '</span>',
  31. '<span class="count">' . (int) $total_reviews . '</span>'
  32. );
  33. $recipe_rating_label .= '</span>';
  34. $recipe_rating_icons = Tasty_Recipes\Ratings::get_rendered_rating( $average_rating );
  35. // Prepend the ratings markup to the post content.
  36. $content = '<div class="ifoodreal-recipes-rating"> <p class="recipe-rating-icons"' . $recipe_rating_icons . '</p>'. $recipe_rating_label .'</div>'. PHP_EOL . $content;
  37. }
  38. }
  39. }
  40.  
  41. return $content;
  42. });
  43. }
  44. add_shortcode('topratings', 'topratings_function');
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement