Completed
Push — add/ratings ( 2c0c79 )
by Andrés
06:48
created

ratings.php ➔ jetpack_ratings_block_get_attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
function jetpack_ratings_block_get_attributes( $style = 'star', $max_rating = 5 ) {
4
	return array(
5
		'rating'      => array(
6
			'type'    => 'number',
7
			'default' => 1,
8
		),
9
		'maxRating'   => array(
10
			'type'    => 'number',
11
			'default' => $max_rating,
12
		),
13
		'color'       => array(
14
			'type' => 'text',
15
		),
16
		'ratingStyle' => array(
17
			'type'    => 'text',
18
			'default' => $style,
19
		),
20
		'className'   => array(
21
			'type' => 'text',
22
		),
23
		'align'       => array(
24
			'type'    => 'text',
25
			'default' => 'left',
26
		),
27
	);
28
}
29
30
add_action(
31
	'init',
32
	function() {
33
		jetpack_register_block(
34
			'jetpack/rating-star',
35
			array(
36
				'render_callback' => 'jetpack_ratings_render_block',
37
				'attributes'      => jetpack_ratings_block_get_attributes( 'star' ),
38
			)
39
		);
40
		jetpack_register_block(
41
			'jetpack/rating-spiciness',
42
			array(
43
				'render_callback' => 'jetpack_ratings_render_block',
44
				'attributes'      => jetpack_ratings_block_get_attributes( 'spiciness' ),
45
			)
46
		);
47
		jetpack_register_block(
48
			'jetpack/rating-priciness',
49
			array(
50
				'render_callback' => 'jetpack_ratings_render_block',
51
				'attributes'      => jetpack_ratings_block_get_attributes( 'priciness', 4 ),
52
			)
53
		);
54
	}
55
);
56
57
function jetpack_ratings_get_symbols( $attributes ) {
58
	$icons = array();
59
60
	if ( $attributes['ratingStyle'] === 'spiciness' ) {
61
		$amp_symbol = '🌶️';
62
	}
63
64
	if ( $attributes['ratingStyle'] === 'priciness' ) {
65
		$amp_symbol = '💲';
66
	}
67
68
	if ( $attributes['ratingStyle'] === 'star' ) {
69
		$amp_symbol = '⭐';
70
	}
71
72
	// Output SVGs for high fidelity contexts, then color them according to rating.
73
	// These are hidden by default, then unhid when CSS loads.
74
	for ( $pos = 1; $pos <= $attributes['maxRating']; $pos++ ) {
75
		$icons[] = '<span style="display: none;">' . jetpack_ratings_build_svg( $attributes, $pos ) . '</span>';
76
	}
77
78
	// Output fallback symbols for low fidelity contexts, like AMP.
79
	$amp_fallback = '';
80
	for ( $i = 0; $i < $attributes['rating']; $i++ ) {
81
		$amp_fallback .= $amp_symbol;
0 ignored issues
show
Bug introduced by
The variable $amp_symbol does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
82
	}
83
84
	return '<p>' . $amp_fallback . '</p>' . implode( $icons );
85
}
86
87
// The following filter is added only to support the old 0.6.2 version of the AMP plugin.
88
// This entire seciton can be removed once we're on version a newer version.
89
// Confirmed that version 1.4.1 (or presumably newer) does not need this filter.
90
add_action( 'amp_post_template_css', 'jetpack_ratings_amp_add_inline_css', 11 );
91
function jetpack_ratings_amp_add_inline_css() {
92
	echo '.wp-block-jetpack-rating-star span, .wp-block-jetpack-rating-spiciness span, .wp-block-jetpack-rating-priciness span { display: none; }';
93
}
94
95
function jetpack_ratings_render_block( $attributes ) {
96
	$classname = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
97
	return sprintf(
98
		'<div class="%1$s" style="text-align:%3$s">%2$s</div>',
99
		esc_attr( 'wp-block-jetpack-rating-' . $attributes['ratingStyle'] . $classname ),
100
		jetpack_ratings_get_symbols( $attributes ),
101
		( isset( $attributes['align'] ) ) ? esc_attr( $attributes['align'] ) : ''
102
	);
103
}
104
105
function jetpack_ratings_build_svg( $attributes, $pos ) {
106
	$classname_whole = ( $attributes['rating'] >= ( $pos - 0.5 ) ) ? '' : 'is-rating-unfilled';
107
	$classname_half  = ( $attributes['rating'] >= $pos ) ? '' : 'is-rating-unfilled';
108
109
	$color = empty( $attributes['color'] ) ? 'currentColor' : esc_attr( $attributes['color'] );
110
111
	if ( $attributes['ratingStyle'] === 'spiciness' ) {
112
		return jetpack_ratings_get_svg_spiciness( $classname_whole, $classname_half, $color );
113
	}
114
115
	if ( $attributes['ratingStyle'] === 'priciness' ) {
116
		return jetpack_ratings_get_svg_priciness( $classname_whole, $classname_half, $color );
117
	}
118
119
	return jetpack_ratings_get_svg_star( $classname_whole, $classname_half, $color );
120
}
121
122
function jetpack_ratings_get_svg_star( $classname_whole, $classname_half, $color ) {
123
	return <<<ELO
124
<span>
125
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
126
	<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" />
127
</svg>
128
</span>
129
<span>
130
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
131
	<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" />
132
</svg>
133
</span>
134
ELO;
135
}
136
137
function jetpack_ratings_get_svg_spiciness( $classname_whole, $classname_half, $color ) {
138
	return <<<ELO
139
<span>
140
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
141
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
142
		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" />
143
</svg>
144
</span>
145
<span>
146
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
147
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
148
		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" />
149
</svg>
150
</span>
151
ELO;
152
}
153
154
function jetpack_ratings_get_svg_priciness( $classname_whole, $classname_half, $color ) {
155
	return <<<ELO
156
<span>
157
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
158
	<path class="{$classname_whole}" fill="{$color}" stroke="{$color}"
159
		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" />
160
</svg>
161
</span>
162
<span>
163
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
164
	<path class="{$classname_half}" fill="{$color}" stroke="{$color}"
165
		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" />
166
</svg>
167
</span>
168
ELO;
169
}
170