array(), 'version' => ADDRESSZEN_VERSION ); wp_register_script( 'addresszen-blocks-frontend', ADDRESSZEN_URL . 'js/blocks/checkout-blocks-frontend.js', $script_asset['dependencies'], $script_asset['version'], true ); wp_register_style( 'addresszen-blocks-style', ADDRESSZEN_URL . 'css/addresszen.css', array(), ADDRESSZEN_VERSION ); } /** * Returns an array of script handles to enqueue in the frontend context. * * @return string[] */ public function get_script_handles() { return array( 'addresszen-blocks-frontend' ); } /** * Returns an array of script handles to enqueue in the editor context. * * @return string[] */ public function get_editor_script_handles() { return array(); } /** * An array of key, value pairs of data made available to the block on the client side. * Accessible via wcSettings['addresszen_data']. * * @return array */ public function get_script_data() { return self::build_settings(); } /** * Build the full settings array for the frontend. * Uses the canonical get_options() from the integration class. * * @return array */ public static function build_settings() { if ( ! function_exists( 'WC' ) || ! WC() || ! isset( WC()->integrations ) ) { return self::get_defaults(); } $integrations = WC()->integrations->get_integrations(); if ( ! is_array( $integrations ) || ! isset( $integrations['addresszen'] ) ) { return self::get_defaults(); } $options = $integrations['addresszen']->get_options(); // Parse JSON overrides into objects for JavaScript consumption if ( ! empty( $options['autocompleteOverride'] ) && is_string( $options['autocompleteOverride'] ) ) { $options['autocompleteOverride'] = self::parse_json_override( $options['autocompleteOverride'] ); } return $options; } /** * Default settings when WooCommerce isn't available. * * @return array */ private static function get_defaults() { return [ 'enabled' => false, 'publicKey' => '', 'autocomplete' => true, 'watchCountry' => false, 'separateFinder' => false, 'separateFinderPosition' => 'above_address', 'hideAddressFields' => false, 'allowManualEntry' => true, 'removeOrganisation' => true, 'populateCounty' => true, 'autocompleteOverride' => '', 'addressFinderLabel' => '', 'addressLine3Field' => '', 'limitLineOne' => false, 'maxLineOne' => 40, 'transliterate' => false, 'asciiOnly' => false, 'fillCompany' => false, 'blockPoBoxes' => false, 'usZip5Only' => true, ]; } /** * Parse and validate a JSON override string. * * @param string $raw_override * @return mixed Parsed JSON or empty string on failure */ private static function parse_json_override( $raw_override ) { if ( empty( $raw_override ) ) { return ''; } $sanitized = wp_kses( $raw_override, array() ); $decoded = html_entity_decode( $sanitized ); $parsed = json_decode( $decoded, true ); if ( json_last_error() !== JSON_ERROR_NONE ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( '[AddressZen] Invalid JSON in override setting' ); } return ''; } return $parsed; } }