Advertisement
geminilabs

[site-reviews] [glsr_summary] shortcode

Apr 15th, 2021 (edited)
1,877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. /**
  2.  * [glsr_summary] shortcode
  3.  *
  4.  * Most of the [site_reviews] options can be used in this shortcode
  5.  * Additional options specific to this shortcode are:
  6.  * - display: Allowed values are "rating", "recommend", "count". The recommend value is the percentage of your 4-5 star reviews from the total
  7.  * - class: Here you can add CSS classes to the parent div
  8.  * - style: Here you can add CSS styles to the parent div
  9.  * - text: Here you can add some text to display under the heading
  10.  *
  11.  * Examples:
  12.  * [glsr_summary display="count" text="Reviews from people like you"]
  13.  * [glsr_summary display="rating" text="Out of 5"]
  14.  * [glsr_summary display="recommend" text="Would recommend to a friend"]
  15.  */
  16. add_shortcode('glsr_summary', function ($atts) {
  17.     $atts = wp_parse_args($atts, [
  18.         'display' => 'rating',
  19.         'class' => '',
  20.         'style' => '',
  21.         'text' => '',
  22.     ]);
  23.     if (!function_exists('glsr_get_ratings') || !function_exists('glsr_star_rating')) {
  24.         return 'This shortcode requires Site Reviews.';
  25.     }
  26.     $ratingInfo = glsr_get_ratings($atts);
  27.     $text = wpautop($atts['text']);
  28.     if ('count' === $atts['display']) {
  29.         $heading = $ratingInfo->reviews;
  30.     } elseif ('recommend' === $atts['display']) {
  31.         $positive = $ratingInfo->ratings[4] + $ratingInfo->ratings[5];
  32.         $percent = round(($positive / $ratingInfo->reviews) * 100);
  33.         $heading = sprintf('%d%%', $percent);
  34.     } else {
  35.         $heading = number_format($ratingInfo->average, 1);
  36.         $text .= glsr_star_rating($ratingInfo->average, $ratingInfo->reviews);
  37.     }
  38.     return sprintf('<div class="glsr %s" style="%s"><h3>%s</h3>%s</div>',
  39.         $atts['class'],
  40.         $atts['style'],
  41.         $heading,
  42.         $text
  43.     );
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement