Completed
Push — add/ratings ( 61725c...f84c58 )
by Andrés
25:25 queued 18:08
created

rating-meta.php ➔ jetpack_rating_meta_get_symbols()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utilities for the rating block.
4
 *
5
 * @package Jetpack
6
 */
7
8
/**
9
 * Returns the low fidelity symbol for the block.
10
 *
11
 * @param array $attributes Array containing the business hours block attributes.
12
 * @return string
13
 */
14
function jetpack_rating_meta_get_symbol_low_fidelity( $attributes ) {
15
	switch ( $attributes['ratingStyle'] ) {
16
		case 'priciness':
17
			return '💲';
18
		case 'spiciness':
19
			return '🌶️';
20
		default:
21
			return '⭐';
22
	}
23
}
24
25
/**
26
 * Return the high fidelity symbol for the block.
27
 *
28
 * @param string $classname_whole Name of the whole symbol class.
29
 * @param string $classname_half Name of the half symbol class.
30
 * @param string $color Color of the block.
31
 *
32
 * @return string
33
 */
34
function jetpack_rating_priciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
35
	return <<<ELO
36
<span>
37
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
38
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
39
		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" />
40
</svg>
41
</span>
42
<span>
43
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
44
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
45
		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" />
46
</svg>
47
</span>
48
ELO;
49
}
50
51
/**
52
 * Return the high fidelity symbol for the block.
53
 *
54
 * @param string $classname_whole Name of the whole symbol class.
55
 * @param string $classname_half Name of the half symbol class.
56
 * @param string $color Color of the block.
57
 *
58
 * @return string
59
 */
60
function jetpack_rating_spiciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
61
	return <<<ELO
62
<span>
63
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
64
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
65
		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" />
66
</svg>
67
</span>
68
<span>
69
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
70
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
71
		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" />
72
</svg>
73
</span>
74
ELO;
75
}
76
77
/**
78
 * Return the high fidelity symbol for the block.
79
 *
80
 * @param string $classname_whole Name of the whole symbol class.
81
 * @param string $classname_half Name of the half symbol class.
82
 * @param string $color Color of the block.
83
 *
84
 * @return string
85
 */
86
function jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color ) {
87
	return <<<ELO
88
<span>
89
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
90
	<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" />
91
</svg>
92
</span>
93
<span>
94
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
95
	<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" />
96
</svg>
97
</span>
98
ELO;
99
}
100
101
/**
102
 * Returns the high fidelity symbol for the block.
103
 *
104
 * @param array   $attributes Array containing the business hours block attributes.
105
 * @param integer $pos Value to render whole and half symbols.
106
 * @return string
107
 */
108
function jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) {
109
	$classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled';
110
	$classname_half  = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled';
111
	$color           = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] );
112
113
	switch ( $attributes['ratingStyle'] ) {
114
		case 'priciness':
115
			return jetpack_rating_priciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
116
		case 'spiciness':
117
			return jetpack_rating_spiciness_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
118
		default:
119
			return jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
120
	}
121
122
}
123
124
/**
125
 * Returns the symbol for the block.
126
 *
127
 * @param array $attributes Array containing the business hours block attributes.
128
 *
129
 * @return string
130
 */
131
function jetpack_rating_meta_get_symbols( $attributes ) {
132
	// Output SVGs for high fidelity contexts, then color them according to rating.
133
	// These are hidden by default, then unhid when CSS loads.
134
	$symbols_hifi = array();
135
	for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) {
136
		$symbols_hifi[] = '<span style="display: none;">' . jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) . '</span>';
137
	}
138
139
	// Output fallback symbols for low fidelity contexts, like AMP,
140
	// where CSS is not loaded so the high-fidelity symbols won't be rendered.
141
	$symbols_lofi = '';
142
	for ( $i = 0; $i < $attributes['rating']; $i++ ) {
143
		$symbols_lofi .= jetpack_rating_meta_get_symbol_low_fidelity( $attributes );
144
	}
145
146
	return '<p>' . $symbols_lofi . '</p>' . implode( $symbols_hifi );
147
}
148
149
/**
150
 * Dynamic rendering of the block.
151
 *
152
 * @param array $attributes Array containing the business hours block attributes.
153
 *
154
 * @return string
155
 */
156
function jetpack_rating_meta_render_block( $attributes ) {
157
	$classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
158
	return sprintf(
159
		'<div class="%1$s" style="text-align:%3$s">%2$s</div>',
160
		esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ),
161
		jetpack_rating_meta_get_symbols( $attributes ),
162
		( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : ''
163
	);
164
}
165