Completed
Push — add/ratings ( 35310a...77d15f )
by Andrés
20:09 queued 12:38
created

rating-meta.php ➔ jetpack_rating_star_get_symbol_low_fidelity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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_star_get_symbol_low_fidelity' ) ) {
9
	/**
10
	 * Returns the low fidelity symbol for the block.
11
	 *
12
	 * @return string
13
	 */
14
	function jetpack_rating_star_get_symbol_low_fidelity() {
15
		return '⭐';
16
	}
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
45
if ( ! function_exists( 'jetpack_rating_meta_get_symbol_high_fidelity' ) ) {
46
	/**
47
	 * Returns the high fidelity symbol for the block.
48
	 *
49
	 * @param array   $attributes Array containing the business hours block attributes.
50
	 * @param integer $pos Value to render whole and half symbols.
51
	 * @return string
52
	 */
53
	function jetpack_rating_meta_get_symbol_high_fidelity( $attributes, $pos ) {
54
		$classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled';
55
		$classname_half  = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled';
56
		$color           = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] );
57
58
		return jetpack_rating_star_get_symbol_high_fidelity( $classname_whole, $classname_half, $color );
59
	}
60
}
61
62
if ( ! function_exists( 'jetpack_rating_meta_get_symbols' ) ) {
63
	/**
64
	 * Returns the symbol for the block.
65
	 *
66
	 * @param array $attributes Array containing the business hours block attributes.
67
	 *
68
	 * @return string
69
	 */
70
	function jetpack_rating_meta_get_symbols( $attributes ) {
71
		// Output SVGs for high fidelity contexts, then color them according to rating.
72
		// These are hidden by default, then unhid when CSS loads.
73
		$symbols_hifi = array();
74
		for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) {
75
			$symbols_hifi[] = '<span style="display: none;">' . jetpack_rating_star_get_symbol_high_fidelity( $attributes, $pos ) . '</span>';
0 ignored issues
show
Bug introduced by
The call to jetpack_rating_star_get_symbol_high_fidelity() misses a required argument $color.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$attributes is of type array<string,?,{"maxRating":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
		}
77
78
		// Output fallback symbols for low fidelity contexts, like AMP,
79
		// where CSS is not loaded so the high-fidelity symbols won't be rendered.
80
		$symbols_lofi = '';
81
		for ( $i = 0; $i < $attributes['rating']; $i++ ) {
82
			$symbols_lofi .= jetpack_rating_star_get_symbol_low_fidelity();
83
		}
84
85
		return '<p>' . $symbols_lofi . '</p>' . implode( $symbols_hifi );
86
	}
87
}
88
89
if ( ! function_exists( 'jetpack_rating_meta_render_block' ) ) {
90
	/**
91
	 * Dynamic rendering of the block.
92
	 *
93
	 * @param array $attributes Array containing the business hours block attributes.
94
	 *
95
	 * @return string
96
	 */
97
	function jetpack_rating_meta_render_block( $attributes ) {
98
		$classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
99
		return sprintf(
100
			'<div class="%1$s" style="text-align:%3$s">%2$s</div>',
101
			esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ),
102
			jetpack_rating_meta_get_symbols( $attributes ),
103
			( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : ''
104
		);
105
	}
106
}
107