Created
June 12, 2019 21:20
-
-
Save aosipov/2baa6473174b4bec388b9650aaaf70de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function topratings_function() { | |
/** | |
* Adds the Tasty Recipes ratings to the top of the post. | |
* | |
* @param string $content Existing post content. | |
* @return string | |
*/ | |
add_filter( 'the_content', function( $content ) { | |
if ( ! class_exists( 'Tasty_Recipes' ) ) { | |
return $content; | |
} | |
// Get all of the recipes that might be in the post. | |
$recipe_ids = Tasty_Recipes::get_recipe_ids_from_content( $content ); | |
if ( ! empty( $recipe_ids ) ) { | |
// Get the first recipe ID in the post and fetch its object. | |
$recipe_id = array_shift( $recipe_ids ); | |
$recipe = Tasty_Recipes\Objects\Recipe::get_by_id( $recipe_id ); | |
if ( $recipe ) { | |
// Fetch all total reviews, but only process if there's more than zero reviews. | |
$total_reviews = $recipe->get_total_reviews(); | |
if ( $total_reviews ) { | |
// Build the label and icon markup. | |
$average_rating = round( (float) $recipe->get_average_rating(), 1 ); | |
$recipe_rating_label = '<span class="rating-label">'; | |
$recipe_rating_label .= sprintf( | |
// translators: Ratings from number of reviews. | |
__( '%1$s from %2$s reviews', 'tasty-recipes' ), | |
'<span class="average">' . $average_rating . '</span>', | |
'<span class="count">' . (int) $total_reviews . '</span>' | |
); | |
$recipe_rating_label .= '</span>'; | |
$recipe_rating_icons = Tasty_Recipes\Ratings::get_rendered_rating( $average_rating ); | |
// Prepend the ratings markup to the post content. | |
$content = '<div class="ifoodreal-recipes-rating"> <p class="recipe-rating-icons"' . $recipe_rating_icons . '</p>'. $recipe_rating_label .'</div>'. PHP_EOL . $content; | |
} | |
} | |
} | |
return $content; | |
}); | |
} | |
add_shortcode('topratings', 'topratings_function'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment