Skip to content

Commit 876439f

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

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,42 @@
44
* https://gravitywiz.com/documentation/gravity-forms-entry-blocks/
55
*/
66
add_filter( 'gpeb_entry', function( $entry, $form ) {
7+
8+
// Configure star settings
9+
$config = [
10+
'star_size' => '24', // Width/height of stars in pixels
11+
'star_color' => '#FFAC33', // Color of the stars
12+
'stroke_width' => '1.5', // Thickness of star outline
13+
'show_empty_stars' => false, // Whether to show empty stars
14+
];
15+
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>';
17+
718
foreach ( $form['fields'] as $field ) {
819
if ( $field->get_input_type() === 'rating' ) {
920
$selected_value = $entry[ $field->id ];
1021
foreach ( $field->choices as $index => $choice ) {
1122
if ( $choice['value'] === $selected_value ) {
12-
$entry[ $field->id ] = str_repeat( '', $index + 1 );
13-
break 2;
23+
$filled_stars = str_repeat(str_replace('<svg ', '<svg fill="' . $config['star_color'] . '" class="gpeb-filled-star" ', $star_svg), $index + 1);
24+
$empty_stars = $config['show_empty_stars'] ? str_repeat(str_replace('<svg ', '<svg fill="none" class="gpeb-outline-star" ', $star_svg), 5 - ($index + 1)) : '';
25+
26+
$entry[$field->id] = $filled_stars . $empty_stars;
27+
break;
1428
}
1529
}
1630
}
1731
}
1832
return $entry;
1933
}, 10, 2 );
34+
35+
// Unescape HTML-encoded SVG content for rating fields
36+
function decode_rating_field_svgs($content, $entry_form, $entry) {
37+
foreach ($entry_form['fields'] as $field) {
38+
if ($field->get_input_type() === 'rating' && isset($entry[$field->id]) && strpos($entry[$field->id], '<svg') !== false) {
39+
$content = str_replace(htmlspecialchars($entry[$field->id], ENT_QUOTES), $entry[$field->id], $content);
40+
}
41+
}
42+
return $content;
43+
}
44+
add_filter('gpeb_loop_entry_content', 'decode_rating_field_svgs', 10, 3);
45+
add_filter('gpeb_view_entry_content', 'decode_rating_field_svgs', 10, 3);

0 commit comments

Comments
 (0)