Aucune description

rating-meta.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Utilities for the rating block.
  4. *
  5. * @since 8.0.0
  6. *
  7. * @package automattic/jetpack
  8. */
  9. if ( ! function_exists( 'jetpack_rating_meta_get_symbol_low_fidelity' ) ) {
  10. /**
  11. * Returns the low fidelity symbol for the block.
  12. *
  13. * @return string
  14. */
  15. function jetpack_rating_meta_get_symbol_low_fidelity() {
  16. return '<span aria-hidden="true">⭐</span>';
  17. }
  18. }
  19. if ( ! function_exists( 'jetpack_rating_star_get_symbol_high_fidelity' ) ) {
  20. /**
  21. * Return the high fidelity symbol for the block.
  22. *
  23. * @param string $classname_whole Name of the whole symbol class.
  24. * @param string $classname_half Name of the half symbol class.
  25. * @param string $color Color of the block.
  26. *
  27. * @return string
  28. */
  29. function jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
  30. return <<<ELO
  31. <span>
  32. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  33. <path class="{$classname_whole}" fill="{$color}" stroke="{$color}" d="M12,17.3l6.2,3.7l-1.6-7L22,9.2l-7.2-0.6L12,2L9.2,8.6L2,9.2L7.5,14l-1.6,7L12,17.3z" />
  34. </svg>
  35. </span>
  36. <span>
  37. <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
  38. <path class="{$classname_half}" fill="{$color}" stroke="{$color}" d="M12,17.3l6.2,3.7l-1.6-7L22,9.2l-7.2-0.6L12,2L9.2,8.6L2,9.2L7.5,14l-1.6,7L12,17.3z" />
  39. </svg>
  40. </span>
  41. ELO;
  42. }
  43. }
  44. if ( ! function_exists( 'jetpack_rating_meta_get_symbol_high_fidelity' ) ) {
  45. /**
  46. * Returns the high fidelity symbol for the block.
  47. *
  48. * @param array $attributes Array containing the block attributes.
  49. * @param integer $pos Value to render whole and half symbols.
  50. * @return string
  51. */
  52. function jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) {
  53. $classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled';
  54. $classname_half = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled';
  55. $color = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] );
  56. return jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
  57. }
  58. }
  59. if ( ! function_exists( 'jetpack_rating_get_schema_for_symbol' ) ) {
  60. /**
  61. * Returns an itemprop and content for rating symbols
  62. *
  63. * @param integer $position the position of the symbol.
  64. * @param integer $max_rating the maximum symbol score.
  65. *
  66. * @return string
  67. */
  68. function jetpack_rating_get_schema_for_symbol( $position, $max_rating ) {
  69. $schema = '';
  70. if ( 1 === $position ) {
  71. $schema = 'itemprop="worstRating" content="0.5"';
  72. } elseif ( $max_rating === $position ) {
  73. $schema = 'itemprop="bestRating" content="' . esc_attr( $max_rating ) . '"';
  74. }
  75. return $schema;
  76. }
  77. }
  78. if ( ! function_exists( 'jetpack_rating_meta_get_symbols' ) ) {
  79. /**
  80. * Returns the symbol for the block.
  81. *
  82. * @param array $attributes Array containing the block attributes.
  83. *
  84. * @return string
  85. */
  86. function jetpack_rating_meta_get_symbols( $attributes ) {
  87. // Output SVGs for high fidelity contexts, then color them according to rating.
  88. // These are hidden by default, then unhid when CSS loads.
  89. $symbols_hifi = array();
  90. for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) {
  91. $symbols_hifi[] = '<span style="display: none;" ' . jetpack_rating_get_schema_for_symbol( $pos, $attributes['maxRating'] ) . '>' . jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) . '</span>';
  92. }
  93. // Output fallback symbols for low fidelity contexts, like AMP,
  94. // where CSS is not loaded so the high-fidelity symbols won't be rendered.
  95. $symbols_lofi = '';
  96. for ( $i = 0; $i < $attributes['rating']; $i++ ) {
  97. $symbols_lofi .= jetpack_rating_meta_get_symbol_low_fidelity();
  98. }
  99. return '<p>' . $symbols_lofi . '</p>' . implode( $symbols_hifi );
  100. }
  101. }
  102. if ( ! function_exists( 'jetpack_rating_meta_render_block' ) ) {
  103. /**
  104. * Dynamic rendering of the block.
  105. *
  106. * @param array $attributes Array containing the block attributes.
  107. *
  108. * @return string
  109. */
  110. function jetpack_rating_meta_render_block( $attributes ) {
  111. $classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
  112. return sprintf(
  113. '<div class="%1$s" style="text-align:%4$s" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">%2$s%3$s</div>',
  114. esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ),
  115. jetpack_rating_meta_get_symbols( $attributes ),
  116. // translators: %1$s is awarded rating score, %2$s is the best possible rating.
  117. '<span itemprop="ratingValue" class="screen-reader-text" content="' . esc_attr( $attributes['rating'] ) . '">' . sprintf( __( 'Rating: %1$s out of %2$s.', 'jetpack' ), esc_attr( $attributes['rating'] ), esc_attr( $attributes['maxRating'] ) ) . '</span>',
  118. ( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : ''
  119. );
  120. }
  121. }