Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement