Advertisement
geminilabs

[reviewer_experience] shortcode

Dec 3rd, 2023 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. /**
  2.  * Use the Code Snippets (https://wordpress.org/plugins/code-snippets/)
  3.  * plugin to add this shortcode to your website.
  4.  *
  5.  * Example usage:
  6.  * [reviewer_experience assigned_posts="post_id" text="{positive}% are positive reviews, {negative}% are negative reviews."]
  7.  */
  8. add_shortcode('reviewer_experience', function ($content = '') {
  9.     $args = wp_parse_args($content, [
  10.         'text' => '{positive}% of reviewers reported a positive experience, while {negative}% reported a negative experience.',
  11.     ]);
  12.     $results = glsr_get_ratings($args);
  13.     $negative = $results->ratings[0] + $results->ratings[1] + $results->ratings[2];
  14.     $positive = $results->ratings[3] + $results->ratings[4] + $results->ratings[5];
  15.     if ($negative > 0) {
  16.         $negative = round($negative / $results->reviews * 100);
  17.     }
  18.     if ($positive > 0) {
  19.         $positive = round($positive / $results->reviews * 100);
  20.     }
  21.     return str_replace(['{positive}', '{negative}'], [$positive, $negative], $args['text']);
  22. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement