traits
1 year ago
accordion.php
1 year ago
alert.php
1 year ago
audio.php
1 year ago
button.php
1 year ago
common-base.php
1 year ago
common-optimized.php
1 year ago
common.php
1 year ago
counter.php
1 year ago
divider.php
1 year ago
google-maps.php
1 year ago
heading.php
1 year ago
html.php
1 year ago
icon-box.php
1 year ago
icon-list.php
1 year ago
icon.php
1 year ago
image-box.php
1 year ago
image-carousel.php
1 year ago
image-gallery.php
1 year ago
image.php
1 year ago
inner-section.php
2 years ago
menu-anchor.php
1 year ago
progress.php
1 year ago
rating.php
1 year ago
read-more.php
1 year ago
shortcode.php
1 year ago
sidebar.php
1 year ago
social-icons.php
1 year ago
spacer.php
1 year ago
star-rating.php
1 year ago
tabs.php
1 year ago
testimonial.php
1 year ago
text-editor.php
1 year ago
toggle.php
1 year ago
video.php
1 year ago
wordpress.php
1 year ago
star-rating.php
554 lines
| 1 | <?php |
| 2 | namespace Elementor; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; // Exit if accessed directly. |
| 6 | } |
| 7 | |
| 8 | use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 9 | use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 10 | |
| 11 | /** |
| 12 | * Elementor star rating widget. |
| 13 | * |
| 14 | * Elementor widget that displays star rating. |
| 15 | * |
| 16 | * @since 2.3.0 |
| 17 | */ |
| 18 | class Widget_Star_Rating extends Widget_Base { |
| 19 | |
| 20 | /** |
| 21 | * Get widget name. |
| 22 | * |
| 23 | * Retrieve star rating widget name. |
| 24 | * |
| 25 | * @since 2.3.0 |
| 26 | * @access public |
| 27 | * |
| 28 | * @return string Widget name. |
| 29 | */ |
| 30 | public function get_name() { |
| 31 | return 'star-rating'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get widget title. |
| 36 | * |
| 37 | * Retrieve star rating widget title. |
| 38 | * |
| 39 | * @since 2.3.0 |
| 40 | * @access public |
| 41 | * |
| 42 | * @return string Widget title. |
| 43 | */ |
| 44 | public function get_title() { |
| 45 | return esc_html__( 'Star Rating', 'elementor' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get widget icon. |
| 50 | * |
| 51 | * Retrieve star rating widget icon. |
| 52 | * |
| 53 | * @since 2.3.0 |
| 54 | * @access public |
| 55 | * |
| 56 | * @return string Widget icon. |
| 57 | */ |
| 58 | public function get_icon() { |
| 59 | return 'eicon-rating'; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get widget keywords. |
| 64 | * |
| 65 | * Retrieve the list of keywords the widget belongs to. |
| 66 | * |
| 67 | * @since 2.3.0 |
| 68 | * @access public |
| 69 | * |
| 70 | * @return array Widget keywords. |
| 71 | */ |
| 72 | public function get_keywords() { |
| 73 | return [ 'star', 'rating', 'rate', 'review' ]; |
| 74 | } |
| 75 | |
| 76 | protected function is_dynamic_content(): bool { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get style dependencies. |
| 82 | * |
| 83 | * Retrieve the list of style dependencies the widget requires. |
| 84 | * |
| 85 | * @since 3.24.0 |
| 86 | * @access public |
| 87 | * |
| 88 | * @return array Widget style dependencies. |
| 89 | */ |
| 90 | public function get_style_depends(): array { |
| 91 | return [ 'widget-star-rating' ]; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Hide widget from panel. |
| 96 | * |
| 97 | * Hide the star rating widget from the panel. |
| 98 | * |
| 99 | * @since 3.17.0 |
| 100 | * @return bool |
| 101 | */ |
| 102 | public function show_in_panel(): bool { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | public function has_widget_inner_wrapper(): bool { |
| 107 | return ! Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Register star rating widget controls. |
| 112 | * |
| 113 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 114 | * |
| 115 | * @since 3.1.0 |
| 116 | * @access protected |
| 117 | */ |
| 118 | protected function register_controls() { |
| 119 | $this->start_controls_section( |
| 120 | 'section_rating', |
| 121 | [ |
| 122 | 'label' => esc_html__( 'Star Rating', 'elementor' ), |
| 123 | ] |
| 124 | ); |
| 125 | |
| 126 | if ( Plugin::$instance->widgets_manager->get_widget_types( 'rating' ) ) { |
| 127 | $this->add_deprecation_message( |
| 128 | '3.17.0', |
| 129 | esc_html__( |
| 130 | 'You are currently editing a Star Rating widget in its old version. Drag a new Rating widget onto your page to use a newer version, providing better capabilities.', |
| 131 | 'elementor' |
| 132 | ), |
| 133 | 'rating' |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | $this->add_control( |
| 138 | 'rating_scale', |
| 139 | [ |
| 140 | 'label' => esc_html__( 'Rating Scale', 'elementor' ), |
| 141 | 'type' => Controls_Manager::SELECT, |
| 142 | 'options' => [ |
| 143 | '5' => '0-5', |
| 144 | '10' => '0-10', |
| 145 | ], |
| 146 | 'default' => '5', |
| 147 | ] |
| 148 | ); |
| 149 | |
| 150 | $this->add_control( |
| 151 | 'rating', |
| 152 | [ |
| 153 | 'label' => esc_html__( 'Rating', 'elementor' ), |
| 154 | 'type' => Controls_Manager::NUMBER, |
| 155 | 'min' => 0, |
| 156 | 'max' => 10, |
| 157 | 'step' => 0.1, |
| 158 | 'default' => 5, |
| 159 | 'dynamic' => [ |
| 160 | 'active' => true, |
| 161 | ], |
| 162 | ] |
| 163 | ); |
| 164 | |
| 165 | $this->add_control( |
| 166 | 'star_style', |
| 167 | [ |
| 168 | 'label' => esc_html__( 'Icon', 'elementor' ), |
| 169 | 'type' => Controls_Manager::SELECT, |
| 170 | 'options' => [ |
| 171 | 'star_fontawesome' => 'Font Awesome', |
| 172 | 'star_unicode' => 'Unicode', |
| 173 | ], |
| 174 | 'default' => 'star_fontawesome', |
| 175 | 'render_type' => 'template', |
| 176 | 'prefix_class' => 'elementor--star-style-', |
| 177 | 'separator' => 'before', |
| 178 | ] |
| 179 | ); |
| 180 | |
| 181 | $this->add_control( |
| 182 | 'unmarked_star_style', |
| 183 | [ |
| 184 | 'label' => esc_html__( 'Unmarked Style', 'elementor' ), |
| 185 | 'type' => Controls_Manager::CHOOSE, |
| 186 | 'options' => [ |
| 187 | 'solid' => [ |
| 188 | 'title' => esc_html__( 'Solid', 'elementor' ), |
| 189 | 'icon' => 'eicon-star', |
| 190 | ], |
| 191 | 'outline' => [ |
| 192 | 'title' => esc_html__( 'Outline', 'elementor' ), |
| 193 | 'icon' => 'eicon-star-o', |
| 194 | ], |
| 195 | ], |
| 196 | 'default' => 'solid', |
| 197 | ] |
| 198 | ); |
| 199 | |
| 200 | $this->add_control( |
| 201 | 'title', |
| 202 | [ |
| 203 | 'label' => esc_html__( 'Title', 'elementor' ), |
| 204 | 'type' => Controls_Manager::TEXT, |
| 205 | 'separator' => 'before', |
| 206 | 'dynamic' => [ |
| 207 | 'active' => true, |
| 208 | ], |
| 209 | ] |
| 210 | ); |
| 211 | |
| 212 | $this->add_responsive_control( |
| 213 | 'align', |
| 214 | [ |
| 215 | 'label' => esc_html__( 'Alignment', 'elementor' ), |
| 216 | 'type' => Controls_Manager::CHOOSE, |
| 217 | 'options' => [ |
| 218 | 'left' => [ |
| 219 | 'title' => esc_html__( 'Left', 'elementor' ), |
| 220 | 'icon' => 'eicon-text-align-left', |
| 221 | ], |
| 222 | 'center' => [ |
| 223 | 'title' => esc_html__( 'Center', 'elementor' ), |
| 224 | 'icon' => 'eicon-text-align-center', |
| 225 | ], |
| 226 | 'right' => [ |
| 227 | 'title' => esc_html__( 'Right', 'elementor' ), |
| 228 | 'icon' => 'eicon-text-align-right', |
| 229 | ], |
| 230 | 'justify' => [ |
| 231 | 'title' => esc_html__( 'Justified', 'elementor' ), |
| 232 | 'icon' => 'eicon-text-align-justify', |
| 233 | ], |
| 234 | ], |
| 235 | 'prefix_class' => 'elementor-star-rating%s--align-', |
| 236 | 'selectors' => [ |
| 237 | '{{WRAPPER}}' => 'text-align: {{VALUE}}', |
| 238 | ], |
| 239 | ] |
| 240 | ); |
| 241 | |
| 242 | $this->end_controls_section(); |
| 243 | |
| 244 | $this->start_controls_section( |
| 245 | 'section_title_style', |
| 246 | [ |
| 247 | 'label' => esc_html__( 'Title', 'elementor' ), |
| 248 | 'tab' => Controls_Manager::TAB_STYLE, |
| 249 | 'condition' => [ |
| 250 | 'title!' => '', |
| 251 | ], |
| 252 | ] |
| 253 | ); |
| 254 | |
| 255 | $this->add_control( |
| 256 | 'title_color', |
| 257 | [ |
| 258 | 'label' => esc_html__( 'Text Color', 'elementor' ), |
| 259 | 'type' => Controls_Manager::COLOR, |
| 260 | 'global' => [ |
| 261 | 'default' => Global_Colors::COLOR_TEXT, |
| 262 | ], |
| 263 | 'selectors' => [ |
| 264 | '{{WRAPPER}} .elementor-star-rating__title' => 'color: {{VALUE}}', |
| 265 | ], |
| 266 | ] |
| 267 | ); |
| 268 | |
| 269 | $this->add_group_control( |
| 270 | Group_Control_Typography::get_type(), |
| 271 | [ |
| 272 | 'name' => 'title_typography', |
| 273 | 'selector' => '{{WRAPPER}} .elementor-star-rating__title', |
| 274 | 'global' => [ |
| 275 | 'default' => Global_Typography::TYPOGRAPHY_TEXT, |
| 276 | ], |
| 277 | ] |
| 278 | ); |
| 279 | |
| 280 | $this->add_group_control( |
| 281 | Group_Control_Text_Shadow::get_type(), |
| 282 | [ |
| 283 | 'name' => 'title_shadow', |
| 284 | 'selector' => '{{WRAPPER}} .elementor-star-rating__title', |
| 285 | ] |
| 286 | ); |
| 287 | |
| 288 | $this->add_responsive_control( |
| 289 | 'title_gap', |
| 290 | [ |
| 291 | 'label' => esc_html__( 'Gap', 'elementor' ), |
| 292 | 'type' => Controls_Manager::SLIDER, |
| 293 | 'size_units' => [ 'px', 'em', 'rem', 'custom' ], |
| 294 | 'range' => [ |
| 295 | 'px' => [ |
| 296 | 'max' => 50, |
| 297 | ], |
| 298 | 'em' => [ |
| 299 | 'min' => 0, |
| 300 | 'max' => 5, |
| 301 | ], |
| 302 | 'rem' => [ |
| 303 | 'min' => 0, |
| 304 | 'max' => 5, |
| 305 | ], |
| 306 | ], |
| 307 | 'selectors' => [ |
| 308 | 'body:not(.rtl) {{WRAPPER}}:not(.elementor-star-rating--align-justify) .elementor-star-rating__title' => 'margin-right: {{SIZE}}{{UNIT}}', |
| 309 | 'body.rtl {{WRAPPER}}:not(.elementor-star-rating--align-justify) .elementor-star-rating__title' => 'margin-left: {{SIZE}}{{UNIT}}', |
| 310 | ], |
| 311 | ] |
| 312 | ); |
| 313 | |
| 314 | $this->end_controls_section(); |
| 315 | |
| 316 | $this->start_controls_section( |
| 317 | 'section_stars_style', |
| 318 | [ |
| 319 | 'label' => esc_html__( 'Stars', 'elementor' ), |
| 320 | 'tab' => Controls_Manager::TAB_STYLE, |
| 321 | ] |
| 322 | ); |
| 323 | |
| 324 | $this->add_responsive_control( |
| 325 | 'icon_size', |
| 326 | [ |
| 327 | 'label' => esc_html__( 'Size', 'elementor' ), |
| 328 | 'type' => Controls_Manager::SLIDER, |
| 329 | 'size_units' => [ 'px', 'em', 'rem', 'custom' ], |
| 330 | 'range' => [ |
| 331 | 'px' => [ |
| 332 | 'max' => 100, |
| 333 | ], |
| 334 | 'em' => [ |
| 335 | 'min' => 0, |
| 336 | 'max' => 10, |
| 337 | ], |
| 338 | 'rem' => [ |
| 339 | 'min' => 0, |
| 340 | 'max' => 10, |
| 341 | ], |
| 342 | ], |
| 343 | 'selectors' => [ |
| 344 | '{{WRAPPER}} .elementor-star-rating' => 'font-size: {{SIZE}}{{UNIT}}', |
| 345 | ], |
| 346 | ] |
| 347 | ); |
| 348 | |
| 349 | $this->add_responsive_control( |
| 350 | 'icon_space', |
| 351 | [ |
| 352 | 'label' => esc_html__( 'Spacing', 'elementor' ), |
| 353 | 'type' => Controls_Manager::SLIDER, |
| 354 | 'size_units' => [ 'px', 'em', 'rem', 'custom' ], |
| 355 | 'range' => [ |
| 356 | 'px' => [ |
| 357 | 'max' => 50, |
| 358 | ], |
| 359 | 'em' => [ |
| 360 | 'min' => 0, |
| 361 | 'max' => 5, |
| 362 | ], |
| 363 | 'rem' => [ |
| 364 | 'min' => 0, |
| 365 | 'max' => 5, |
| 366 | ], |
| 367 | ], |
| 368 | 'selectors' => [ |
| 369 | 'body:not(.rtl) {{WRAPPER}} .elementor-star-rating i:not(:last-of-type)' => 'margin-right: {{SIZE}}{{UNIT}}', |
| 370 | 'body.rtl {{WRAPPER}} .elementor-star-rating i:not(:last-of-type)' => 'margin-left: {{SIZE}}{{UNIT}}', |
| 371 | ], |
| 372 | ] |
| 373 | ); |
| 374 | |
| 375 | $this->add_control( |
| 376 | 'stars_color', |
| 377 | [ |
| 378 | 'label' => esc_html__( 'Color', 'elementor' ), |
| 379 | 'type' => Controls_Manager::COLOR, |
| 380 | 'selectors' => [ |
| 381 | '{{WRAPPER}} .elementor-star-rating i:before' => 'color: {{VALUE}}', |
| 382 | ], |
| 383 | 'separator' => 'before', |
| 384 | ] |
| 385 | ); |
| 386 | |
| 387 | $this->add_control( |
| 388 | 'stars_unmarked_color', |
| 389 | [ |
| 390 | 'label' => esc_html__( 'Unmarked Color', 'elementor' ), |
| 391 | 'type' => Controls_Manager::COLOR, |
| 392 | 'selectors' => [ |
| 393 | '{{WRAPPER}} .elementor-star-rating i' => 'color: {{VALUE}}', |
| 394 | ], |
| 395 | ] |
| 396 | ); |
| 397 | |
| 398 | $this->end_controls_section(); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @since 2.3.0 |
| 403 | * @access protected |
| 404 | */ |
| 405 | protected function get_rating() { |
| 406 | $settings = $this->get_settings_for_display(); |
| 407 | $rating_scale = (int) $settings['rating_scale']; |
| 408 | $rating = (float) $settings['rating'] > $rating_scale ? $rating_scale : $settings['rating']; |
| 409 | |
| 410 | return [ $rating, $rating_scale ]; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Print the actual stars and calculate their filling. |
| 415 | * |
| 416 | * Rating type is float to allow stars-count to be a fraction. |
| 417 | * Floored-rating type is int, to represent the rounded-down stars count. |
| 418 | * In the `for` loop, the index type is float to allow comparing with the rating value. |
| 419 | * |
| 420 | * @since 2.3.0 |
| 421 | * @access protected |
| 422 | */ |
| 423 | protected function render_stars( $icon ) { |
| 424 | $rating_data = $this->get_rating(); |
| 425 | $rating = (float) $rating_data[0]; |
| 426 | $floored_rating = floor( $rating ); |
| 427 | $stars_html = ''; |
| 428 | |
| 429 | for ( $stars = 1.0; $stars <= $rating_data[1]; $stars++ ) { |
| 430 | if ( $stars <= $floored_rating ) { |
| 431 | $stars_html .= '<i class="elementor-star-full">' . $icon . '</i>'; |
| 432 | } elseif ( $floored_rating + 1 === $stars && $rating !== $floored_rating ) { |
| 433 | $stars_html .= '<i class="elementor-star-' . ( $rating - $floored_rating ) * 10 . '">' . $icon . '</i>'; |
| 434 | } else { |
| 435 | $stars_html .= '<i class="elementor-star-empty">' . $icon . '</i>'; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | return $stars_html; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * @since 2.3.0 |
| 444 | * @access protected |
| 445 | */ |
| 446 | protected function render() { |
| 447 | $settings = $this->get_settings_for_display(); |
| 448 | $rating_data = $this->get_rating(); |
| 449 | $textual_rating = $rating_data[0] . '/' . $rating_data[1]; |
| 450 | $icon = ''; |
| 451 | |
| 452 | if ( 'star_fontawesome' === $settings['star_style'] ) { |
| 453 | if ( 'outline' === $settings['unmarked_star_style'] ) { |
| 454 | $icon = ''; |
| 455 | } |
| 456 | } elseif ( 'star_unicode' === $settings['star_style'] ) { |
| 457 | $icon = '★'; |
| 458 | |
| 459 | if ( 'outline' === $settings['unmarked_star_style'] ) { |
| 460 | $icon = '☆'; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | $this->add_render_attribute( 'icon_wrapper', [ |
| 465 | 'class' => 'elementor-star-rating', |
| 466 | 'title' => $textual_rating, |
| 467 | 'itemtype' => 'http://schema.org/Rating', |
| 468 | 'itemscope' => '', |
| 469 | 'itemprop' => 'reviewRating', |
| 470 | ] ); |
| 471 | |
| 472 | $schema_rating = '<span itemprop="ratingValue" class="elementor-screen-only">' . $textual_rating . '</span>'; |
| 473 | $stars_element = '<div ' . $this->get_render_attribute_string( 'icon_wrapper' ) . '>' . $this->render_stars( $icon ) . ' ' . $schema_rating . '</div>'; |
| 474 | ?> |
| 475 | |
| 476 | <div class="elementor-star-rating__wrapper"> |
| 477 | <?php if ( ! Utils::is_empty( $settings['title'] ) ) : ?> |
| 478 | <div class="elementor-star-rating__title"><?php echo esc_html( $settings['title'] ); ?></div> |
| 479 | <?php endif; |
| 480 | // PHPCS - $stars_element contains an HTML string that cannot be escaped. ?> |
| 481 | <?php echo $stars_element; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 482 | </div> |
| 483 | <?php |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * @since 2.9.0 |
| 488 | * @access protected |
| 489 | */ |
| 490 | protected function content_template() { |
| 491 | ?> |
| 492 | <# |
| 493 | var getRating = function() { |
| 494 | var ratingScale = parseInt( settings.rating_scale, 10 ), |
| 495 | rating = settings.rating > ratingScale ? ratingScale : settings.rating; |
| 496 | |
| 497 | return [ rating, ratingScale ]; |
| 498 | }, |
| 499 | ratingData = getRating(), |
| 500 | rating = ratingData[0], |
| 501 | textualRating = ratingData[0] + '/' + ratingData[1], |
| 502 | renderStars = function( icon ) { |
| 503 | var starsHtml = '', |
| 504 | flooredRating = Math.floor( rating ); |
| 505 | |
| 506 | for ( var stars = 1; stars <= ratingData[1]; stars++ ) { |
| 507 | if ( stars <= flooredRating ) { |
| 508 | starsHtml += '<i class="elementor-star-full">' + icon + '</i>'; |
| 509 | } else if ( flooredRating + 1 === stars && rating !== flooredRating ) { |
| 510 | starsHtml += '<i class="elementor-star-' + ( rating - flooredRating ).toFixed( 1 ) * 10 + '">' + icon + '</i>'; |
| 511 | } else { |
| 512 | starsHtml += '<i class="elementor-star-empty">' + icon + '</i>'; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return starsHtml; |
| 517 | }, |
| 518 | icon = ''; |
| 519 | |
| 520 | if ( 'star_fontawesome' === settings.star_style ) { |
| 521 | if ( 'outline' === settings.unmarked_star_style ) { |
| 522 | icon = ''; |
| 523 | } |
| 524 | } else if ( 'star_unicode' === settings.star_style ) { |
| 525 | icon = '★'; |
| 526 | |
| 527 | if ( 'outline' === settings.unmarked_star_style ) { |
| 528 | icon = '☆'; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | view.addRenderAttribute( 'iconWrapper', 'class', 'elementor-star-rating' ); |
| 533 | view.addRenderAttribute( 'iconWrapper', 'itemtype', 'http://schema.org/Rating' ); |
| 534 | view.addRenderAttribute( 'iconWrapper', 'title', textualRating ); |
| 535 | view.addRenderAttribute( 'iconWrapper', 'itemscope', '' ); |
| 536 | view.addRenderAttribute( 'iconWrapper', 'itemprop', 'reviewRating' ); |
| 537 | |
| 538 | var stars = renderStars( icon ); |
| 539 | #> |
| 540 | |
| 541 | <div class="elementor-star-rating__wrapper"> |
| 542 | <# if ( ! _.isEmpty( settings.title ) ) { #> |
| 543 | <div class="elementor-star-rating__title">{{ settings.title }}</div> |
| 544 | <# } #> |
| 545 | <div {{{ view.getRenderAttributeString( 'iconWrapper' ) }}} > |
| 546 | {{{ stars }}} |
| 547 | <span itemprop="ratingValue" class="elementor-screen-only">{{ textualRating }}</span> |
| 548 | </div> |
| 549 | </div> |
| 550 | |
| 551 | <?php |
| 552 | } |
| 553 | } |
| 554 |