|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Utilities for the rating block. |
| 4 | + * |
| 5 | + * @since 8.0.0 |
| 6 | + * |
| 7 | + * @package Jetpack |
| 8 | + */ |
| 9 | + |
| 10 | +if ( ! function_exists( 'jetpack_rating_meta_get_symbol_low_fidelity' ) ) { |
| 11 | + /** |
| 12 | + * Returns the low fidelity symbol for the block. |
| 13 | + * |
| 14 | + * @return string |
| 15 | + */ |
| 16 | + function jetpack_rating_meta_get_symbol_low_fidelity() { |
| 17 | + return '⭐'; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +if ( ! function_exists( 'jetpack_rating_star_get_symbol_high_fidelity' ) ) { |
| 22 | + /** |
| 23 | + * Return the high fidelity symbol for the block. |
| 24 | + * |
| 25 | + * @param string $classname_whole Name of the whole symbol class. |
| 26 | + * @param string $classname_half Name of the half symbol class. |
| 27 | + * @param string $color Color of the block. |
| 28 | + * |
| 29 | + * @return string |
| 30 | + */ |
| 31 | + function jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) { |
| 32 | + return <<<ELO |
| 33 | +<span> |
| 34 | +<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 35 | + <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" /> |
| 36 | +</svg> |
| 37 | +</span> |
| 38 | +<span> |
| 39 | +<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> |
| 40 | + <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" /> |
| 41 | +</svg> |
| 42 | +</span> |
| 43 | +ELO; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +if ( ! function_exists( 'jetpack_rating_meta_get_symbol_high_fidelity' ) ) { |
| 48 | + /** |
| 49 | + * Returns the high fidelity symbol for the block. |
| 50 | + * |
| 51 | + * @param array $attributes Array containing the block attributes. |
| 52 | + * @param integer $pos Value to render whole and half symbols. |
| 53 | + * @return string |
| 54 | + */ |
| 55 | + function jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) { |
| 56 | + $classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled'; |
| 57 | + $classname_half = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled'; |
| 58 | + $color = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] ); |
| 59 | + |
| 60 | + return jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +if ( ! function_exists( 'jetpack_rating_meta_get_symbols' ) ) { |
| 65 | + /** |
| 66 | + * Returns the symbol for the block. |
| 67 | + * |
| 68 | + * @param array $attributes Array containing the block attributes. |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + function jetpack_rating_meta_get_symbols( $attributes ) { |
| 73 | + // Output SVGs for high fidelity contexts, then color them according to rating. |
| 74 | + // These are hidden by default, then unhid when CSS loads. |
| 75 | + $symbols_hifi = array(); |
| 76 | + for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) { |
| 77 | + $symbols_hifi[] = '<span style="display: none;">' . jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) . '</span>'; |
| 78 | + } |
| 79 | + |
| 80 | + // Output fallback symbols for low fidelity contexts, like AMP, |
| 81 | + // where CSS is not loaded so the high-fidelity symbols won't be rendered. |
| 82 | + $symbols_lofi = ''; |
| 83 | + for ( $i = 0; $i < $attributes['rating']; $i++ ) { |
| 84 | + $symbols_lofi .= jetpack_rating_meta_get_symbol_low_fidelity(); |
| 85 | + } |
| 86 | + |
| 87 | + return '<p>' . $symbols_lofi . '</p>' . implode( $symbols_hifi ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +if ( ! function_exists( 'jetpack_rating_meta_render_block' ) ) { |
| 92 | + /** |
| 93 | + * Dynamic rendering of the block. |
| 94 | + * |
| 95 | + * @param array $attributes Array containing the block attributes. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + function jetpack_rating_meta_render_block( $attributes ) { |
| 100 | + $classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className']; |
| 101 | + return sprintf( |
| 102 | + '<div class="%1$s" style="text-align:%3$s">%2$s</div>', |
| 103 | + esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ), |
| 104 | + jetpack_rating_meta_get_symbols( $attributes ), |
| 105 | + ( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : '' |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments