integrations
7 years ago
class-strong-form.php
7 years ago
class-strong-log.php
7 years ago
class-strong-mail.php
7 years ago
class-strong-templates.php
7 years ago
class-strong-testimonials-order.php
7 years ago
class-strong-testimonials-privacy.php
7 years ago
class-strong-testimonials-render.php
7 years ago
class-strong-testimonials-shortcode-average.php
7 years ago
class-strong-testimonials-shortcode-count.php
7 years ago
class-strong-testimonials-shortcode.php
7 years ago
class-strong-view-display.php
7 years ago
class-strong-view-form.php
7 years ago
class-strong-view-slideshow.php
7 years ago
class-strong-view.php
7 years ago
class-walker-strong-category-checklist-front.php
7 years ago
deprecated.php
7 years ago
filters.php
7 years ago
functions-activation.php
7 years ago
functions-content.php
7 years ago
functions-image.php
7 years ago
functions-rating.php
7 years ago
functions-template-form.php
7 years ago
functions-template.php
7 years ago
functions-views.php
7 years ago
functions.php
7 years ago
l10n-polylang.php
7 years ago
l10n-wpml.php
7 years ago
post-types.php
7 years ago
retro.php
7 years ago
scripts.php
7 years ago
widget2.php
7 years ago
class-strong-testimonials-shortcode-average.php
322 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Strong_Testimonials_Average_Shortcode |
| 4 | * |
| 5 | * @since 2.31.0 |
| 6 | */ |
| 7 | |
| 8 | class Strong_Testimonials_Average_Shortcode { |
| 9 | |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | public $shortcode = 'testimonial_average_rating'; |
| 14 | |
| 15 | public function __construct() { |
| 16 | add_shortcode( $this->shortcode, array( $this, 'testimonial_average_rating_shortcode' ) ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Return average rating. |
| 21 | * |
| 22 | * @param $atts |
| 23 | * @param null $content |
| 24 | * @since 2.31.0 |
| 25 | * @return string |
| 26 | */ |
| 27 | public function testimonial_average_rating_shortcode( $atts, $content = null ) { |
| 28 | $pairs = array( |
| 29 | // parts |
| 30 | 'average' => '', |
| 31 | 'count' => '', |
| 32 | 'stars' => '', |
| 33 | // style |
| 34 | 'block' => '', |
| 35 | 'centered' => '', |
| 36 | // HTML |
| 37 | 'element' => 'div', // span |
| 38 | 'class' => '', // on wrapper |
| 39 | // filters |
| 40 | 'category' => '', |
| 41 | ); |
| 42 | $pairs = apply_filters( "wpmtst_shortcode_defaults__{$this->shortcode}", $pairs ); |
| 43 | |
| 44 | $atts = shortcode_atts( $pairs, normalize_empty_atts( $atts ), $this->shortcode ); |
| 45 | |
| 46 | // default parts |
| 47 | if ( ! $content ) { |
| 48 | $content = '{title} {stars} {summary}'; |
| 49 | } |
| 50 | |
| 51 | // set parts |
| 52 | preg_match_all( "#{(.*?)}#", $content, $parts ); |
| 53 | /* |
| 54 | * Example: |
| 55 | * |
| 56 | * Array |
| 57 | * ( |
| 58 | * [0] => Array |
| 59 | * ( |
| 60 | * [0] => {title} |
| 61 | * [1] => {stars} |
| 62 | * [2] => {summary} |
| 63 | * ) |
| 64 | * |
| 65 | * [1] => Array |
| 66 | * ( |
| 67 | * [0] => title |
| 68 | * [1] => stars |
| 69 | * [2] => summary |
| 70 | * ) |
| 71 | * ) |
| 72 | */ |
| 73 | $tag_list = $parts[0]; |
| 74 | $tag_keys = $parts[1]; |
| 75 | $parts = array_fill_keys( $tag_keys, '' ); |
| 76 | |
| 77 | // get posts |
| 78 | $args = array( |
| 79 | 'posts_per_page' => -1, |
| 80 | 'post_type' => 'wpm-testimonial', |
| 81 | 'post_status' => 'publish', |
| 82 | 'suppress_filters' => true, |
| 83 | ); |
| 84 | |
| 85 | // category |
| 86 | if ( $atts['category'] ) { |
| 87 | $args['tax_query'] = array( |
| 88 | array( |
| 89 | 'taxonomy' => 'wpm-testimonial-category', |
| 90 | 'field' => is_numeric( $atts['category'] ) ? 'id' : 'slug', |
| 91 | 'terms' => $atts['category'], |
| 92 | ), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | $args = apply_filters( 'wpmtst_query_args', $args, $atts ); |
| 97 | $posts_array = get_posts( $args ); |
| 98 | |
| 99 | // get summary |
| 100 | $summary = $this->get_summary( $posts_array ); |
| 101 | /* |
| 102 | * Example: |
| 103 | * |
| 104 | * Array |
| 105 | * ( |
| 106 | * [review_count] => 2 |
| 107 | * [rating_count] => 2 |
| 108 | * [rating_sum] => 10 |
| 109 | * [rating_average] => 5 |
| 110 | * [rating_detail] => Array |
| 111 | * ( |
| 112 | * [5] => 2 |
| 113 | * [4] => 0 |
| 114 | * [3] => 0 |
| 115 | * [2] => 0 |
| 116 | * [1] => 0 |
| 117 | * [0] => 0 |
| 118 | * ) |
| 119 | * ) |
| 120 | */ |
| 121 | |
| 122 | // Want to build your own HTML? Return any truthy value to short-circuit this shortcode output. |
| 123 | $html = apply_filters( 'wpmtst_average_rating_pre_html', '', $atts, $summary ); |
| 124 | if ( $html ) { |
| 125 | return $html; |
| 126 | } |
| 127 | |
| 128 | // assemble classes |
| 129 | $class_list = array_filter( array_merge( array( 'strong-rating-wrapper', 'average' ), explode( ' ', $atts['class'] ) ) ); |
| 130 | if ( $atts['block'] ) { |
| 131 | $class_list[] = 'block'; |
| 132 | } |
| 133 | if ( $atts['centered'] ) { |
| 134 | $class_list[] = 'centered'; |
| 135 | } |
| 136 | |
| 137 | // title |
| 138 | if ( isset( $parts['title'] ) ) { |
| 139 | $parts['title'] = sprintf( '<span class="strong-rating-title">%s</span>', __( 'Average Rating:', 'strong-testimonials' ) ); |
| 140 | } |
| 141 | if ( isset( $parts['title2'] ) ) { |
| 142 | /* translators: %s is a number */ |
| 143 | $count = sprintf( _n( 'Average of %s Rating:', 'Average of %s Ratings:', $summary['rating_count'], 'strong-testimonials' ), $summary['rating_count'] ); |
| 144 | $parts['title2'] = sprintf( '<span class="strong-rating-title">%s</span>', $count ); |
| 145 | } |
| 146 | |
| 147 | // stars |
| 148 | if ( isset( $parts['stars'] ) ) { |
| 149 | $parts['stars'] = $this->print_stars( wpmtst_round_to_half( $summary['rating_average'] ) ); |
| 150 | } |
| 151 | |
| 152 | // average |
| 153 | if ( isset( $parts['average'] ) ) { |
| 154 | $parts['average'] = sprintf( '<span class="strong-rating-average">%s</span>', $summary['rating_average'] ); |
| 155 | } |
| 156 | |
| 157 | // count |
| 158 | if ( isset( $parts['count'] ) ) { |
| 159 | $parts['count'] = sprintf( '<span class="strong-rating-count">%s</span>', $summary['rating_count'] ); |
| 160 | } |
| 161 | |
| 162 | // summary phrase |
| 163 | if ( isset( $parts['summary'] ) ) { |
| 164 | |
| 165 | /* translators: %s is a number */ |
| 166 | $average = sprintf( _n( '%s star', '%s stars', $summary['rating_average'], 'strong-testimonials' ), $summary['rating_average'] ); |
| 167 | $count = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $summary['rating_count'], 'strong-testimonials' ), $summary['rating_count'] ); |
| 168 | $parts['summary'] = sprintf( '<span class="strong-rating-summary">%s</span>', $average . ' ' . $count ); |
| 169 | |
| 170 | } elseif ( isset( $parts['summary2'] ) ) { |
| 171 | |
| 172 | /* translators: %s is a number */ |
| 173 | $average = sprintf( _n( '%s star', '%s stars', $summary['rating_average'], 'strong-testimonials' ), $summary['rating_average'] ); |
| 174 | $parts['summary2'] = sprintf( '<span class="strong-rating-summary">%s</span>', $average ); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | // replace tags |
| 179 | foreach ( $tag_list as $key => $tag ) { |
| 180 | $content = str_replace( $tag, $parts[ $tag_keys[ $key ] ], $content ); |
| 181 | } |
| 182 | |
| 183 | // assemble it |
| 184 | $html = sprintf( '<%s class="%s">%s</%s>', $atts['element'], join( ' ', $class_list ), $content, $atts['element'] ); |
| 185 | |
| 186 | wp_enqueue_style( 'wpmtst-rating-display' ); |
| 187 | |
| 188 | return apply_filters( 'wpmtst_average_rating_html', $html, $atts, $summary ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Calculate and return the average rating. |
| 193 | * |
| 194 | * @param $posts |
| 195 | * @since 1.1.0 |
| 196 | * @return array|null |
| 197 | */ |
| 198 | public function get_summary( $posts = null ) { |
| 199 | // Set a placeholder. |
| 200 | $average = array( |
| 201 | 'review_count' => null, |
| 202 | 'rating_count' => null, |
| 203 | 'rating_sum' => null, |
| 204 | 'rating_average' => null, |
| 205 | 'rating_detail' => null, |
| 206 | ); |
| 207 | |
| 208 | if ( $posts ) { |
| 209 | |
| 210 | // initialize totals |
| 211 | $review_count = count( $posts ); |
| 212 | $rating_count = 0; |
| 213 | $rating_sum = 0; |
| 214 | // initial values for each rating |
| 215 | $rating_detail = array_fill_keys( array( 5, 4, 3, 2, 1, 0 ), 0 ); |
| 216 | |
| 217 | foreach ( $posts as $post ) { |
| 218 | // get rating value |
| 219 | $value = $this->get_rating_value( $post ); |
| 220 | // add to detail array |
| 221 | $rating_detail[ $value ]++; |
| 222 | // add to count and sum |
| 223 | if ( $value ) { |
| 224 | $rating_sum += $value; |
| 225 | $rating_count++; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if ( $rating_count ) { |
| 230 | $average = array( |
| 231 | 'review_count' => number_format( $review_count ), |
| 232 | 'rating_count' => number_format( $rating_count ), |
| 233 | 'rating_sum' => number_format( $rating_sum ), |
| 234 | 'rating_average' => trim( number_format( $rating_sum / $rating_count, 1 ), '.0' ), |
| 235 | 'rating_detail' => $rating_detail, |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | return $average; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Return the rating value for a single post. |
| 246 | * |
| 247 | * @param $post |
| 248 | * @since 1.1.0 |
| 249 | * @return int|null |
| 250 | */ |
| 251 | private function get_rating_value( $post ) { |
| 252 | $rating_field = $this->find_first_rating_field(); |
| 253 | |
| 254 | if ( $rating_field ) { |
| 255 | $rating = intval( get_post_meta( $post->ID, $rating_field['name'], true ) ); |
| 256 | if ( ! $rating ) { |
| 257 | $rating = intval( $rating_field['default_display_value'] ); |
| 258 | } |
| 259 | } else { |
| 260 | $rating = 5; |
| 261 | } |
| 262 | |
| 263 | return $rating; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Find the first rating field. |
| 268 | * |
| 269 | * @since 1.1.0 |
| 270 | * @return bool|int|string |
| 271 | */ |
| 272 | private function find_first_rating_field() { |
| 273 | $fields = wpmtst_get_custom_fields(); |
| 274 | foreach ( $fields as $key => $field ) { |
| 275 | if ( 'rating' == $field['input_type'] ) { |
| 276 | return $field; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Print the stars. |
| 285 | * |
| 286 | * @param double $rating Rounded to half. |
| 287 | * @param string $class The container CSS class. |
| 288 | * @since 2.31.0 |
| 289 | * @return string |
| 290 | */ |
| 291 | public function print_stars( $rating = 0.0, $class = 'strong-rating' ) { |
| 292 | $rating = (double) $rating; |
| 293 | $is_zero = ( 0 == $rating ) ? ' current' : ''; |
| 294 | ob_start(); |
| 295 | ?> |
| 296 | <span class="<?php echo esc_attr( $class ); ?>"> |
| 297 | <span class="star0 star<?php echo $is_zero; ?>"></span> |
| 298 | <?php |
| 299 | if ( $is_zero ) { |
| 300 | echo str_repeat( '<span class="star"></span>', 5 ); |
| 301 | } else { |
| 302 | for ( $i = 1; $i <= 5; $i++ ) { |
| 303 | $star_class = 'star'; |
| 304 | if ( $i == round( $rating ) ) { |
| 305 | $star_class .= ' current'; |
| 306 | } |
| 307 | if ( 0.5 == $i - $rating ) { |
| 308 | $star_class .= ' half'; |
| 309 | } |
| 310 | printf( '<span class="%s"></span>', $star_class ); |
| 311 | } |
| 312 | } |
| 313 | ?> |
| 314 | </span> |
| 315 | <?php |
| 316 | $html = apply_filters( 'wpmtst_average_rating_stars_html', ob_get_clean(), $rating ); |
| 317 | |
| 318 | return $html; |
| 319 | } |
| 320 | |
| 321 | } |
| 322 |