Skip to content

Commit 4eafadb

Browse files
committed
gpeb-display-stars-for-rating-fields.php: Added customizable SVG star with empty star display option for rating fields.
1 parent 876439f commit 4eafadb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

gp-entry-blocks/gpeb-display-stars-for-rating-fields.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,24 @@
77

88
// Configure star settings
99
$config = [
10-
'star_size' => '24', // Width/height of stars in pixels
10+
'star_size' => 24, // Width/height of stars in pixels
1111
'star_color' => '#FFAC33', // Color of the stars
12-
'stroke_width' => '1.5', // Thickness of star outline
12+
'stroke_width' => 1.5, // Thickness of star outline
1313
'show_empty_stars' => false, // Whether to show empty stars
1414
];
1515

16-
$star_svg = '<svg xmlns="http://www.w3.org/2000/svg" width="' . $config['star_size'] . '" height="' . $config['star_size'] . '" viewBox="0 0 24 24" stroke="' . $config['star_color'] . '" stroke-width="' . $config['stroke_width'] . '" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>';
16+
$star_size = max( 1, (int) $config['star_size'] );
17+
$stroke_width = (float) $config['stroke_width'];
18+
$star_color = sanitize_hex_color( $config['star_color'] ) ?: '#FFAC33';
19+
20+
$star_svg = '<svg xmlns="http://www.w3.org/2000/svg" width="' . esc_attr( $star_size ) . '" height="' . esc_attr( $star_size ) . '" viewBox="0 0 24 24" stroke="' . esc_attr( $star_color ) . '" stroke-width="' . esc_attr( $stroke_width ) . '" stroke-linecap="round" stroke-linejoin="round"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>';
1721

1822
foreach ( $form['fields'] as $field ) {
1923
if ( $field->get_input_type() === 'rating' ) {
2024
$selected_value = $entry[ $field->id ];
2125
foreach ( $field->choices as $index => $choice ) {
2226
if ( $choice['value'] === $selected_value ) {
23-
$filled_stars = str_repeat(str_replace('<svg ', '<svg fill="' . $config['star_color'] . '" class="gpeb-filled-star" ', $star_svg), $index + 1);
27+
$filled_stars = str_repeat(str_replace('<svg ', '<svg fill="' . esc_attr( $star_color ) . '" class="gpeb-filled-star" ', $star_svg), $index + 1);
2428
$empty_stars = $config['show_empty_stars'] ? str_repeat(str_replace('<svg ', '<svg fill="none" class="gpeb-outline-star" ', $star_svg), 5 - ($index + 1)) : '';
2529

2630
$entry[$field->id] = $filled_stars . $empty_stars;

0 commit comments

Comments
 (0)