attributes` at * render time — a static save() would freeze whatever number the author last * typed. That is the whole point of the block: a hard-coded rating is the * least interesting version of it. * * The fractional icon is CSS, not markup: two identical icon rows, the upper * one clipped to `--airo-wp-star-rating-fill`. So the same markup serves 4, 4.5 * and 4.3 without a half-icon asset. * * `.airo-wp-star-rating` is a block-level positioning wrapper capped at the * content column; the visible element is `.airo-wp-star-rating__inner`, which * shrink-wraps. Colour, border and padding are routed there by * `airowp_route_visual_supports()` — left on the wrapper, a background * would paint across the whole column instead of hugging the stars. The CSS * custom properties therefore go on the INNER element too: that helper * rewrites the wrapper's `style` attribute wholesale, so anything set there * would be discarded. * * @package airo-wp * @since 2.8.0 * * @var array $attributes Block attributes. * @var string $content Block save content (empty — this block is server-rendered). * @var WP_Block $block Block instance. */ defined( 'ABSPATH' ) || exit; if ( ! function_exists( 'airowp_star_rating_css_color' ) ) { /** * Turn a stored colour attribute into a CSS value. * * Preset references keep their `var()` form rather than being resolved to * a hex value, so the block follows a theme's palette when it changes. * Everything else goes through the shared sanitiser, which rejects * `url()`, `expression()` and friends. * * @param string $value Stored attribute value. * @return string CSS colour, or '' when the value is unusable. */ function airowp_star_rating_css_color( $value ) { $value = is_string( $value ) ? trim( $value ) : ''; if ( '' === $value ) { return ''; } if ( preg_match( '/^var:preset\|color\|([A-Za-z0-9_-]+)$/', $value, $matches ) ) { return 'var(--wp--preset--color--' . $matches[1] . ')'; } $sanitized = airowp_sanitize_css_color( $value ); return is_string( $sanitized ) ? $sanitized : ''; } } if ( ! function_exists( 'airowp_star_rating_icon_row' ) ) { /** * One row of icons. * * Track and fill render identical rows — the fill row is clipped by width, * which is what produces a partial icon. * * @param string $row_class Row class name. * @param int $count Number of icons. * @param string $icon Icon name from the shared library. * @param string $icon_style 'filled' or 'outlined'. * @return string Row markup. SVG comes from the hardcoded library, so it is * trusted output. */ function airowp_star_rating_icon_row( $row_class, $count, $icon, $icon_style ) { $svg = airowp_render_icon_svg( $icon, $icon_style ); $stars = str_repeat( '' . $svg . '', max( 0, (int) $count ) ); return '' . $stars . ''; } } if ( ! function_exists( 'airowp_render_star_rating' ) ) { /** * Render the Star Rating block. * * @param array $attributes Block attributes. * @param string $content Inner block content (unused). * @param WP_Block $block Block instance (unused). * @return string Block markup. */ function airowp_render_star_rating( $attributes, $content, $block ) { unset( $content, $block ); $max = airowp_star_rating_clamp_max( isset( $attributes['maxRating'] ) ? $attributes['maxRating'] : 5 ); $raw = isset( $attributes['rating'] ) ? $attributes['rating'] : 4.5; $rating = airowp_star_rating_clamp( $raw, $max ); $precision = isset( $attributes['precision'] ) ? (string) $attributes['precision'] : 'half'; $fill = airowp_star_rating_fill_percent( $raw, $max, $precision ); // iconStyle and iconSize have no block.json default: left unset, they // inherit the theme tokens. Style falls back to the site-wide // settings.custom.airowp.icon.defaultStyle; size is simply not // written, so style.scss's --wp--custom--airo-wp--star-rating--default-size // applies. Only an explicit author choice is baked into the markup. $default_style = 'filled'; if ( class_exists( '\\airo-wp\\Icon_Injector' ) ) { $default_style = \airo-wp\Icon_Injector::get_icon_defaults()['style']; } $icon = ! empty( $attributes['icon'] ) ? (string) $attributes['icon'] : 'star'; $icon_style = isset( $attributes['iconStyle'] ) && in_array( $attributes['iconStyle'], array( 'filled', 'outlined' ), true ) ? (string) $attributes['iconStyle'] : $default_style; $icon_size = isset( $attributes['iconSize'] ) && is_numeric( $attributes['iconSize'] ) ? max( 1, (int) $attributes['iconSize'] ) . 'px' : ''; $icon_gap = isset( $attributes['iconGap'] ) ? (int) $attributes['iconGap'] : 4; $show_value = ! empty( $attributes['showValue'] ); $show_max = ! empty( $attributes['showMax'] ); $show_count = ! empty( $attributes['showCount'] ); $count = isset( $attributes['ratingCount'] ) && is_numeric( $attributes['ratingCount'] ) ? max( 0, (int) $attributes['ratingCount'] ) : 0; $template = isset( $attributes['countTemplate'] ) ? (string) $attributes['countTemplate'] : '(%s)'; $justification = isset( $attributes['justification'] ) ? (string) $attributes['justification'] : 'left'; $justify_class = in_array( $justification, array( 'left', 'center', 'right' ), true ) ? ' airo-wp-justify--' . $justification : ''; $rating_color = airowp_star_rating_css_color( isset( $attributes['ratingColor'] ) ? $attributes['ratingColor'] : '' ); $track_color = airowp_star_rating_css_color( isset( $attributes['trackColor'] ) ? $attributes['trackColor'] : '' ); // Custom properties, not utility classes: the values are author-set // numbers and colours, so there is no finite class set to emit. $vars = array( '--airo-wp-star-rating-fill' => $fill . '%', '--airo-wp-star-rating-size' => $icon_size, '--airo-wp-star-rating-gap' => max( 0, $icon_gap ) . 'px', '--airo-wp-star-rating-color' => $rating_color, '--airo-wp-star-rating-track-color' => $track_color, ); $inline = ''; foreach ( $vars as $property => $value ) { if ( '' !== $value ) { $inline .= $property . ':' . $value . ';'; } } $parts = array( '' . esc_html( airowp_star_rating_label( $rating, $max, $show_count ? $count : 0 ) ) . '', '', ); if ( $show_value ) { $value_html = esc_html( airowp_star_rating_format_value( $rating ) ); if ( $show_max ) { $value_html .= '/' . esc_html( airowp_star_rating_format_value( $max ) ) . ''; } $parts[] = ''; } if ( $show_count ) { $parts[] = ''; } $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'airo-wp-star-rating airo-wp-justify' . $justify_class ) ); $html = sprintf( '
%3$s
', $wrapper_attributes, esc_attr( $inline ), implode( '', $parts ) ); return airowp_route_visual_supports( $html, $attributes, 'airo-wp-star-rating__inner', array( 'color', 'border', 'spacing.padding' ) ); } } // Output directly (WordPress captures echo'd output from render callbacks). echo airowp_render_star_rating( $attributes, $content, $block ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Markup assembled above with per-part escaping; SVG comes from the hardcoded icon library.