Completed
Push — add/ratings ( 93b123...83fca1 )
by Andrés
34:53 queued 28:17
created

rating-meta.php ➔ jetpack_rating_meta_get_symbol_low_fidelity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utilities for the rating block.
4
 *
5
 * @package Jetpack
6
 */
7
8
if ( ! function_exists( 'jetpack_rating_meta_get_symbol_low_fidelity' ) ) {
9
	/**
10
	 * Returns the low fidelity symbol for the block.
11
	 *
12
	 * @param array $attributes Array containing the business hours block attributes.
13
	 * @return string
14
	 */
15
	function jetpack_rating_meta_get_symbol_low_fidelity( $attributes ) {
16
		switch ( $attributes['ratingStyle'] ) {
17
			case 'priciness':
18
				return '💲';
19
			case 'spiciness':
20
				return '🌶️';
21
			default:
22
				return '⭐';
23
		}
24
	}
25
}
26
27
if ( ! function_exists( 'jetpack_rating_priciness_get_symbol_high_fidelity' ) ) {
28
	/**
29
	 * Return the high fidelity symbol for the block.
30
	 *
31
	 * @param string $classname_whole Name of the whole symbol class.
32
	 * @param string $classname_half Name of the half symbol class.
33
	 * @param string $color Color of the block.
34
	 *
35
	 * @return string
36
	 */
37
	function jetpack_rating_priciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
38
		return <<<ELO
39
<span>
40
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
41
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
42
		d="M11.8 10.9c-2.27-.59-3-1.2-3-2.15 0-1.09 1.01-1.85 2.7-1.85 1.78 0 2.44.85 2.5 2.1h2.21c-.07-1.72-1.12-3.3-3.21-3.81V3h-3v2.16c-1.94.42-3.5 1.68-3.5 3.61 0 2.31 1.91 3.46 4.7 4.13 2.5.6 3 1.48 3 2.41 0 .69-.49 1.79-2.7 1.79-2.06 0-2.87-.92-2.98-2.1h-2.2c.12 2.19 1.76 3.42 3.68 3.83V21h3v-2.15c1.95-.37 3.5-1.5 3.5-3.55 0-2.84-2.43-3.81-4.7-4.4z" />
43
</svg>
44
</span>
45
<span>
46
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
47
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
48
		d="M11.8 10.9c-2.27-.59-3-1.2-3-2.15 0-1.09 1.01-1.85 2.7-1.85 1.78 0 2.44.85 2.5 2.1h2.21c-.07-1.72-1.12-3.3-3.21-3.81V3h-3v2.16c-1.94.42-3.5 1.68-3.5 3.61 0 2.31 1.91 3.46 4.7 4.13 2.5.6 3 1.48 3 2.41 0 .69-.49 1.79-2.7 1.79-2.06 0-2.87-.92-2.98-2.1h-2.2c.12 2.19 1.76 3.42 3.68 3.83V21h3v-2.15c1.95-.37 3.5-1.5 3.5-3.55 0-2.84-2.43-3.81-4.7-4.4z" />
49
</svg>
50
</span>
51
ELO;
52
	}
53
}
54
55
if ( ! function_exists( 'jetpack_rating_spiciness_get_symbol_high_fidelity' ) ) {
56
	/**
57
	 * Return the high fidelity symbol for the block.
58
	 *
59
	 * @param string $classname_whole Name of the whole symbol class.
60
	 * @param string $classname_half Name of the half symbol class.
61
	 * @param string $color Color of the block.
62
	 *
63
	 * @return string
64
	 */
65
	function jetpack_rating_spiciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
66
		return <<<ELO
67
<span>
68
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
69
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
70
		d="M13.8 9l1.2-.8c.6.3 1.1 1 1.1 1.8v11.8s-8-1.8-8-10.8v-1c0-.7.4-1.4 1-1.7l1.3.7L12 8l1.8 1zM10 2c1.5 0 2.8 1.1 3 2.6 1 .3 1.8 1 2.2 2l-1.5.9-1.8-1-1.6 1-1.5-.8c.4-1 1.2-1.7 2.2-2-.2-.4-.6-.7-1-.7V2z" />
71
</svg>
72
</span>
73
<span>
74
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
75
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
76
		d="M13.8 9l1.2-.8c.6.3 1.1 1 1.1 1.8v11.8s-8-1.8-8-10.8v-1c0-.7.4-1.4 1-1.7l1.3.7L12 8l1.8 1zM10 2c1.5 0 2.8 1.1 3 2.6 1 .3 1.8 1 2.2 2l-1.5.9-1.8-1-1.6 1-1.5-.8c.4-1 1.2-1.7 2.2-2-.2-.4-.6-.7-1-.7V2z" />
77
</svg>
78
</span>
79
ELO;
80
	}
81
}
82
83
if ( ! function_exists( 'jetpack_rating_star_get_symbol_high_fidelity' ) ) {
84
	/**
85
	 * Return the high fidelity symbol for the block.
86
	 *
87
	 * @param string $classname_whole Name of the whole symbol class.
88
	 * @param string $classname_half Name of the half symbol class.
89
	 * @param string $color Color of the block.
90
	 *
91
	 * @return string
92
	 */
93
	function jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
94
		return <<<ELO
95
<span>
96
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
97
	<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" />
98
</svg>
99
</span>
100
<span>
101
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
102
	<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" />
103
</svg>
104
</span>
105
ELO;
106
	}
107
}
108
109
if ( ! function_exists( 'jetpack_rating_meta_get_symbol_high_fidelity' ) ) {
110
	/**
111
	 * Returns the high fidelity symbol for the block.
112
	 *
113
	 * @param array   $attributes Array containing the business hours block attributes.
114
	 * @param integer $pos Value to render whole and half symbols.
115
	 * @return string
116
	 */
117
	function jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) {
118
		$classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled';
119
		$classname_half  = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled';
120
		$color           = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] );
121
122
		switch ( $attributes['ratingStyle'] ) {
123
			case 'priciness':
124
				return jetpack_rating_priciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
125
			case 'spiciness':
126
				return jetpack_rating_spiciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
127
			default:
128
				return jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
129
		}
130
	}
131
}
132
133
if ( ! function_exists( 'jetpack_rating_meta_get_symbols' ) ) {
134
	/**
135
	 * Returns the symbol for the block.
136
	 *
137
	 * @param array $attributes Array containing the business hours block attributes.
138
	 *
139
	 * @return string
140
	 */
141
	function jetpack_rating_meta_get_symbols( $attributes ) {
142
		// Output SVGs for high fidelity contexts, then color them according to rating.
143
		// These are hidden by default, then unhid when CSS loads.
144
		$symbols_hifi = array();
145
		for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) {
146
			$symbols_hifi[] = '<span style="display: none;">' . jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) . '</span>';
147
		}
148
149
		// Output fallback symbols for low fidelity contexts, like AMP,
150
		// where CSS is not loaded so the high-fidelity symbols won't be rendered.
151
		$symbols_lofi = '';
152
		for ( $i = 0; $i < $attributes['rating']; $i++ ) {
153
			$symbols_lofi .= jetpack_rating_meta_get_symbol_low_fidelity( $attributes );
154
		}
155
156
		return '<p>' . $symbols_lofi . '</p>' . implode( $symbols_hifi );
157
	}
158
}
159
160
if ( ! function_exists( 'jetpack_rating_meta_render_block' ) ) {
161
	/**
162
	 * Dynamic rendering of the block.
163
	 *
164
	 * @param array $attributes Array containing the business hours block attributes.
165
	 *
166
	 * @return string
167
	 */
168
	function jetpack_rating_meta_render_block( $attributes ) {
169
		$classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
170
		return sprintf(
171
			'<div class="%1$s" style="text-align:%3$s">%2$s</div>',
172
			esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ),
173
			jetpack_rating_meta_get_symbols( $attributes ),
174
			( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : ''
175
		);
176
	}
177
}
178