Endpoint.php
1 month ago
ItemEligibility.php
4 weeks ago
Meta.php
1 month ago
Scheduler.php
4 weeks ago
StarRating.php
1 month ago
SubmissionHandler.php
4 weeks ago
StarRating.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * StarRating control class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\OrderReviews; |
| 9 | |
| 10 | /** |
| 11 | * Server-side renderer for the accessible 5-star rating control used on the |
| 12 | * Review Order page. |
| 13 | * |
| 14 | * The control degrades to native `<input type="radio">` elements without |
| 15 | * JavaScript and is enhanced by `client/legacy/js/frontend/order-review.js` |
| 16 | * for keyboard navigation, a visible focus ring, and the dynamic caption |
| 17 | * underneath the stars. |
| 18 | * |
| 19 | * @internal Just for internal use. |
| 20 | * |
| 21 | * @since 10.8.0 |
| 22 | */ |
| 23 | class StarRating { |
| 24 | |
| 25 | /** |
| 26 | * Render the rating control. Returns the HTML; does not echo. |
| 27 | * |
| 28 | * Required keys in `$args`: |
| 29 | * - `name` (string): form field name (one per item). |
| 30 | * - `id_prefix` (string): prefix used to build unique radio ids. |
| 31 | * - `label_id` (string): id of the existing label element that describes |
| 32 | * the group via `aria-labelledby`. |
| 33 | * |
| 34 | * Optional: |
| 35 | * - `selected` (int): pre-selected value 0-5; pass `0` (the default) |
| 36 | * for no pre-selection. Values outside 0-5 are treated as no selection. |
| 37 | * |
| 38 | * @since 10.8.0 |
| 39 | * |
| 40 | * @param array $args Render arguments. See description for required and optional keys. |
| 41 | * @return string |
| 42 | */ |
| 43 | public static function render( array $args ): string { |
| 44 | $name = (string) ( $args['name'] ?? '' ); |
| 45 | $id_prefix = (string) ( $args['id_prefix'] ?? '' ); |
| 46 | $label_id = (string) ( $args['label_id'] ?? '' ); |
| 47 | |
| 48 | if ( '' === $name || '' === $id_prefix || '' === $label_id ) { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | $selected = (int) ( $args['selected'] ?? 0 ); |
| 53 | if ( $selected < 0 || $selected > 5 ) { |
| 54 | $selected = 0; |
| 55 | } |
| 56 | |
| 57 | ob_start(); |
| 58 | wc_get_template( |
| 59 | 'order/star-rating.php', |
| 60 | array( |
| 61 | 'name' => $name, |
| 62 | 'id_prefix' => $id_prefix, |
| 63 | 'label_id' => $label_id, |
| 64 | 'selected' => $selected, |
| 65 | 'labels' => self::get_labels(), |
| 66 | ) |
| 67 | ); |
| 68 | return (string) ob_get_clean(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Default rating labels, indexed 1-5, after the customer-facing filter. |
| 73 | * |
| 74 | * Defaults match the long-standing WooCommerce product-review labels in |
| 75 | * `templates/single-product-reviews.php` so customers see consistent |
| 76 | * wording across the two review entry points. |
| 77 | * |
| 78 | * @since 10.8.0 |
| 79 | * |
| 80 | * @return array<int, string> |
| 81 | */ |
| 82 | public static function get_labels(): array { |
| 83 | $labels = array( |
| 84 | 1 => __( 'Very poor', 'woocommerce' ), |
| 85 | 2 => __( 'Not that bad', 'woocommerce' ), |
| 86 | 3 => __( 'Average', 'woocommerce' ), |
| 87 | 4 => __( 'Good', 'woocommerce' ), |
| 88 | 5 => __( 'Perfect', 'woocommerce' ), |
| 89 | ); |
| 90 | |
| 91 | /** |
| 92 | * Filter the labels shown under the star-rating control. |
| 93 | * |
| 94 | * @since 10.8.0 |
| 95 | * |
| 96 | * @param array<int, string> $labels Map of rating value (1-5) to label. |
| 97 | */ |
| 98 | $filtered = (array) apply_filters( 'woocommerce_review_order_rating_labels', $labels ); |
| 99 | |
| 100 | // Keep only known 1-5 keys; fall back to defaults for any the filter dropped. |
| 101 | return array_replace( $labels, array_intersect_key( $filtered, $labels ) ); |
| 102 | } |
| 103 | } |
| 104 |