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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Utils / BlocksSharedState.php
woocommerce / src / Blocks / Utils Last commit date
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