BlockHooksTrait.php
4 weeks ago
BlockTemplateUtils.php
2 months ago
BlocksSharedState.php
3 months ago
BlocksWpQuery.php
2 years ago
CartCheckoutUtils.php
4 weeks ago
MiniCartUtils.php
1 year ago
ProductAvailabilityUtils.php
11 months ago
ProductDataUtils.php
1 year ago
ProductGalleryUtils.php
4 weeks ago
StyleAttributesUtils.php
1 year ago
Utils.php
2 years ago
BlocksSharedState.php
241 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Blocks\Utils; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use Automattic\WooCommerce\Blocks\Package; |
| 9 | use Automattic\WooCommerce\Blocks\Domain\Services\Hydration; |
| 10 | |
| 11 | /** |
| 12 | * Manages the registration of interactivity config and state that is commonly shared by WooCommerce blocks. |
| 13 | * Initialization only happens on the first call to load_store_config. |
| 14 | * |
| 15 | * This is a private API and may change in future versions. |
| 16 | */ |
| 17 | class BlocksSharedState { |
| 18 | |
| 19 | /** |
| 20 | * The consent statement for using private APIs of this class. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private static string $consent_statement = 'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WooCommerce'; |
| 25 | |
| 26 | /** |
| 27 | * The namespace for interactivity config and state. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private static string $settings_namespace = 'woocommerce'; |
| 32 | |
| 33 | /** |
| 34 | * Whether the core config has been registered. |
| 35 | * |
| 36 | * @var bool |
| 37 | */ |
| 38 | private static bool $core_config_registered = false; |
| 39 | |
| 40 | /** |
| 41 | * Cart state. |
| 42 | * |
| 43 | * @var array|null |
| 44 | */ |
| 45 | private static ?array $blocks_shared_cart_state = null; |
| 46 | |
| 47 | /** |
| 48 | * Prevent caching on certain pages. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | private static function prevent_cache(): void { |
| 53 | \WC_Cache_Helper::set_nocache_constants(); |
| 54 | nocache_headers(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check that the consent statement was passed. |
| 59 | * |
| 60 | * @param string $consent_statement The consent statement string. |
| 61 | * @return true |
| 62 | * @throws InvalidArgumentException If the statement does not match. |
| 63 | */ |
| 64 | private static function check_consent( string $consent_statement ): bool { |
| 65 | if ( $consent_statement !== self::$consent_statement ) { |
| 66 | throw new InvalidArgumentException( 'This method cannot be called without consenting the API may change.' ); |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Load store config (currency, locale, core data) into interactivity config. |
| 74 | * |
| 75 | * @param string $consent_statement The consent statement string. |
| 76 | * @return void |
| 77 | * @throws InvalidArgumentException If consent statement doesn't match. |
| 78 | */ |
| 79 | public static function load_store_config( string $consent_statement ): void { |
| 80 | self::check_consent( $consent_statement ); |
| 81 | |
| 82 | if ( self::$core_config_registered ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | self::$core_config_registered = true; |
| 87 | |
| 88 | wp_interactivity_config( self::$settings_namespace, self::get_currency_data() ); |
| 89 | wp_interactivity_config( self::$settings_namespace, self::get_locale_data() ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Load cart state into interactivity state. |
| 94 | * |
| 95 | * @param string $consent_statement The consent statement string. |
| 96 | * @return void |
| 97 | * @throws InvalidArgumentException If consent statement doesn't match. |
| 98 | */ |
| 99 | public static function load_cart_state( string $consent_statement ): void { |
| 100 | self::check_consent( $consent_statement ); |
| 101 | |
| 102 | if ( null === self::$blocks_shared_cart_state ) { |
| 103 | $cart_exists = isset( WC()->cart ); |
| 104 | $cart_has_contents = $cart_exists && ! WC()->cart->is_empty(); |
| 105 | if ( $cart_exists ) { |
| 106 | $cart_response = Package::container()->get( Hydration::class )->get_rest_api_response_data( '/wc/store/v1/cart' ); |
| 107 | self::$blocks_shared_cart_state = $cart_response['body'] ?? array(); |
| 108 | } else { |
| 109 | self::$blocks_shared_cart_state = array(); |
| 110 | } |
| 111 | |
| 112 | if ( $cart_has_contents ) { |
| 113 | self::prevent_cache(); |
| 114 | } |
| 115 | |
| 116 | wp_interactivity_config( |
| 117 | self::$settings_namespace, |
| 118 | array( 'nonOptimisticProperties' => self::get_non_optimistic_properties() ) |
| 119 | ); |
| 120 | |
| 121 | wp_interactivity_state( |
| 122 | self::$settings_namespace, |
| 123 | array( |
| 124 | 'cart' => self::$blocks_shared_cart_state, |
| 125 | 'noticeId' => '', |
| 126 | 'restUrl' => get_rest_url(), |
| 127 | ) |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get currency data to include in settings. |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | private static function get_currency_data(): array { |
| 138 | $currency = get_woocommerce_currency(); |
| 139 | |
| 140 | return array( |
| 141 | 'currency' => array( |
| 142 | 'code' => $currency, |
| 143 | 'precision' => wc_get_price_decimals(), |
| 144 | 'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ), |
| 145 | 'symbolPosition' => get_option( 'woocommerce_currency_pos' ), |
| 146 | 'decimalSeparator' => wc_get_price_decimal_separator(), |
| 147 | 'thousandSeparator' => wc_get_price_thousand_separator(), |
| 148 | 'priceFormat' => html_entity_decode( get_woocommerce_price_format() ), |
| 149 | ), |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Get locale data to include in settings. |
| 155 | * |
| 156 | * @return array |
| 157 | */ |
| 158 | private static function get_locale_data(): array { |
| 159 | global $wp_locale; |
| 160 | |
| 161 | return array( |
| 162 | 'locale' => array( |
| 163 | 'siteLocale' => get_locale(), |
| 164 | 'userLocale' => get_user_locale(), |
| 165 | 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), |
| 166 | ), |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get cart properties that cannot use optimistic UI on the frontend. |
| 172 | * |
| 173 | * Detects whether third-party code has registered callbacks on filters that |
| 174 | * modify cart property values. When callbacks are present, the corresponding |
| 175 | * property must use the server-computed value instead of a client-side |
| 176 | * optimistic computation. |
| 177 | * |
| 178 | * `@return` string[] List of cart property paths (dot-delimited) that cannot be optimistic. |
| 179 | * |
| 180 | * @return string[] List of cart property paths (dot-delimited) that cannot be optimistic. |
| 181 | */ |
| 182 | private static function get_non_optimistic_properties(): array { |
| 183 | $properties = array(); |
| 184 | |
| 185 | if ( has_filter( 'woocommerce_cart_contents_count' ) ) { |
| 186 | $properties[] = 'cart.items_count'; |
| 187 | } |
| 188 | |
| 189 | return $properties; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Load placeholder image into interactivity config. |
| 194 | * |
| 195 | * @param string $consent_statement The consent statement string. |
| 196 | * @return void |
| 197 | * @throws InvalidArgumentException If consent statement doesn't match. |
| 198 | */ |
| 199 | public static function load_placeholder_image( string $consent_statement ): void { |
| 200 | self::check_consent( $consent_statement ); |
| 201 | |
| 202 | wp_interactivity_config( |
| 203 | self::$settings_namespace, |
| 204 | array( 'placeholderImgSrc' => wc_placeholder_img_src() ) |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get cart errors formatted as notices for the store-notices interactivity store. |
| 210 | * |
| 211 | * Returns errors from the hydrated cart state in the format expected by |
| 212 | * the store-notices store context. |
| 213 | * |
| 214 | * @param string $consent_statement The consent statement string. |
| 215 | * @return array Array of notices with id, notice, type, and dismissible keys. |
| 216 | * @throws InvalidArgumentException If consent statement doesn't match. |
| 217 | */ |
| 218 | public static function get_cart_error_notices( string $consent_statement ): array { |
| 219 | self::check_consent( $consent_statement ); |
| 220 | |
| 221 | // Ensure cart state is loaded so this method works independently. |
| 222 | if ( null === self::$blocks_shared_cart_state ) { |
| 223 | self::load_cart_state( $consent_statement ); |
| 224 | } |
| 225 | |
| 226 | $errors = self::$blocks_shared_cart_state['errors'] ?? array(); |
| 227 | $notices = array(); |
| 228 | |
| 229 | foreach ( $errors as $error ) { |
| 230 | $notices[] = array( |
| 231 | 'id' => wp_unique_id( 'store-notice-' ), |
| 232 | 'notice' => $error['message'] ?? '', |
| 233 | 'type' => 'error', |
| 234 | 'dismissible' => true, |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | return $notices; |
| 239 | } |
| 240 | } |
| 241 |