Skip to content

Commit 453ad6a

Browse files
authored
gpeb-display-stars-for-rating-fields.php: Added customizable SVG star with empty star display option for rating fields.
1 parent 25ab6f2 commit 453ad6a

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,48 @@
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_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>';
21+
722
foreach ( $form['fields'] as $field ) {
823
if ( $field->get_input_type() === 'rating' ) {
924
$selected_value = $entry[ $field->id ];
1025
foreach ( $field->choices as $index => $choice ) {
1126
if ( $choice['value'] === $selected_value ) {
12-
$entry[ $field->id ] = str_repeat( '', $index + 1 );
13-
break 2;
27+
$filled_stars = str_repeat(str_replace('<svg ', '<svg fill="' . esc_attr( $star_color ) . '" class="gpeb-filled-star" ', $star_svg), $index + 1);
28+
$empty_stars = $config['show_empty_stars'] ? str_repeat(str_replace('<svg ', '<svg fill="none" class="gpeb-outline-star" ', $star_svg), 5 - ($index + 1)) : '';
29+
30+
$entry[$field->id] = $filled_stars . $empty_stars;
31+
break;
1432
}
1533
}
1634
}
1735
}
1836
return $entry;
1937
}, 10, 2 );
38+
39+
// Unescape HTML-encoded SVG content for rating fields
40+
if ( ! function_exists( 'decode_rating_field_svgs' ) ) {
41+
function decode_rating_field_svgs($content, $entry_form, $entry) {
42+
foreach ($entry_form['fields'] as $field) {
43+
if ($field->get_input_type() === 'rating' && isset($entry[$field->id]) && strpos($entry[$field->id], '<svg') !== false) {
44+
$content = str_replace(esc_html($entry[$field->id]), $entry[$field->id], $content);
45+
}
46+
}
47+
return $content;
48+
}
49+
}
50+
add_filter('gpeb_loop_entry_content', 'decode_rating_field_svgs', 10, 3);
51+
add_filter('gpeb_view_entry_content', 'decode_rating_field_svgs', 10, 3);

0 commit comments

Comments
 (0)