/home/techb158/balacoffee.com/wp-content/plugins/woocommerce/src/Blocks/Utils
Edit: /home/techb158/balacoffee.com/wp-content/plugins/woocommerce/src/Blocks/Utils/BlocksSharedState.php (7022B)
cart );
$cart_has_contents = $cart_exists && ! WC()->cart->is_empty();
if ( $cart_exists ) {
$cart_response = Package::container()->get( Hydration::class )->get_rest_api_response_data( '/wc/store/v1/cart' );
self::$blocks_shared_cart_state = $cart_response['body'] ?? array();
} else {
self::$blocks_shared_cart_state = array();
}
if ( $cart_has_contents ) {
self::prevent_cache();
}
wp_interactivity_config(
self::$settings_namespace,
array( 'nonOptimisticProperties' => self::get_non_optimistic_properties() )
);
wp_interactivity_state(
self::$settings_namespace,
array(
'cart' => self::$blocks_shared_cart_state,
'noticeId' => '',
'restUrl' => get_rest_url(),
)
);
}
}
/**
* Get currency data to include in settings.
*
* @return array
*/
private static function get_currency_data(): array {
$currency = get_woocommerce_currency();
return array(
'currency' => array(
'code' => $currency,
'precision' => wc_get_price_decimals(),
'symbol' => html_entity_decode( get_woocommerce_currency_symbol( $currency ) ),
'symbolPosition' => get_option( 'woocommerce_currency_pos' ),
'decimalSeparator' => wc_get_price_decimal_separator(),
'thousandSeparator' => wc_get_price_thousand_separator(),
'priceFormat' => html_entity_decode( get_woocommerce_price_format() ),
),
);
}
/**
* Get locale data to include in settings.
*
* @return array
*/
private static function get_locale_data(): array {
global $wp_locale;
return array(
'locale' => array(
'siteLocale' => get_locale(),
'userLocale' => get_user_locale(),
'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
),
);
}
/**
* Get cart properties that cannot use optimistic UI on the frontend.
*
* Detects whether third-party code has registered callbacks on filters that
* modify cart property values. When callbacks are present, the corresponding
* property must use the server-computed value instead of a client-side
* optimistic computation.
*
* `@return` string[] List of cart property paths (dot-delimited) that cannot be optimistic.
*
* @return string[] List of cart property paths (dot-delimited) that cannot be optimistic.
*/
private static function get_non_optimistic_properties(): array {
$properties = array();
if ( has_filter( 'woocommerce_cart_contents_count' ) ) {
$properties[] = 'cart.items_count';
}
return $properties;
}
/**
* Load placeholder image into interactivity config.
*
* @param string $consent_statement The consent statement string.
* @return void
* @throws InvalidArgumentException If consent statement doesn't match.
*/
public static function load_placeholder_image( string $consent_statement ): void {
self::check_consent( $consent_statement );
wp_interactivity_config(
self::$settings_namespace,
array( 'placeholderImgSrc' => wc_placeholder_img_src() )
);
}
/**
* Get cart errors formatted as notices for the store-notices interactivity store.
*
* Returns errors from the hydrated cart state in the format expected by
* the store-notices store context.
*
* @param string $consent_statement The consent statement string.
* @return array Array of notices with id, notice, type, and dismissible keys.
* @throws InvalidArgumentException If consent statement doesn't match.
*/
public static function get_cart_error_notices( string $consent_statement ): array {
self::check_consent( $consent_statement );
// Ensure cart state is loaded so this method works independently.
if ( null === self::$blocks_shared_cart_state ) {
self::load_cart_state( $consent_statement );
}
$errors = self::$blocks_shared_cart_state['errors'] ?? array();
$notices = array();
foreach ( $errors as $error ) {
$notices[] = array(
'id' => wp_unique_id( 'store-notice-' ),
'notice' => $error['message'] ?? '',
'type' => 'error',
'dismissible' => true,
);
}
return $notices;
}
}