ram string $key The key to check if exists. * @return bool Whether the key exists in the current data registry. */ public function exists( $key ) { return array_key_exists( $key, $this->data ); } /** * Interface for adding data to the registry. * * You can only register data that is not already in the registry identified by the given key. If there is a * duplicate found, unless $ignore_duplicates is true, an exception will be thrown. * * @param string $key The key used to reference the data being registered. This should use camelCase. * @param mixed $data If not a function, registered to the registry as is. If a function, then the * callback is invoked right before output to the screen. * @param boolean $check_key_exists If set to true, duplicate data will be ignored if the key exists. * If false, duplicate data will cause an exception. * * @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error. */ public function add( $key, $data, $check_key_exists = false ) { if ( $check_key_exists && $this->exists( $key ) ) { return; } try { $this->add_data( $key, $data ); } catch ( Exception $e ) { if ( $this->debug() ) { // bubble up. throw $e; } wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); } } /** * Hydrate from the API. * * @param string $path REST API path to preload. */ public function hydrate_api_request( $path ) { if ( ! isset( $this->preloaded_api_requests[ $path ] ) ) { $this->preloaded_api_requests[ $path ] = Package::container()->get( Hydration::class )->get_rest_api_response_data( $path ); } } /** * Hydrate some data from the API. * * @param string $key The key used to reference the data being registered. * @param string $path REST API path to preload. * @param boolean $check_key_exists If set to true, duplicate data will be ignored if the key exists. * If false, duplicate data will cause an exception. * * @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error. */ public function hydrate_data_from_api_request( $key, $path, $check_key_exists = false ) { $this->add( $key, function() use ( $path ) { if ( isset( $this->preloaded_api_requests[ $path ], $this->preloaded_api_requests[ $path ]['body'] ) ) { return $this->preloaded_api_requests[ $path ]['body']; } $response = Package::container()->get( Hydration::class )->get_rest_api_response_data( $path ); return $response['body'] ?? ''; }, $check_key_exists ); } /** * Adds a page permalink to the data registry. * * @param integer $page_id Page ID to add to the registry. */ public function register_page_id( $page_id ) { $permalink = $page_id ? get_permalink( $page_id ) : false; if ( $permalink ) { $this->data[ 'page-' . $page_id ] = $permalink; } } /** * Callback for registering the data script via WordPress API. * * @return void */ public function register_data_script() { $this->api->register_script( $this->handle, 'assets/client/blocks/wc-settings.js', [ 'wp-api-fetch' ], true ); } /** * Callback for enqueuing asset data via the WP api. * * Note: while this is hooked into print/admin_print_scripts, it still only * happens if the script attached to `wc-settings` handle is enqueued. This * is done to allow for any potentially expensive data generation to only * happen for routes that need it. */ public function enqueue_asset_data() { if ( wp_script_is( $this->handle, 'enqueued' ) ) { $this->initialize_core_data(); $this->execute_lazy_data(); $data = rawurlencode( wp_json_encode( $this->data ) ); $wc_settings_script = "var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );"; $preloaded_api_requests_script = ''; if ( count( $this->preloaded_api_requests ) > 0 ) { $preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) ); $preloaded_api_requests_script = "wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js( $preloaded_api_requests ) . "' ) ) ) );"; } wp_add_inline_script( $this->handle, $wc_settings_script . $preloaded_api_requests_script, 'before' ); } } /** * See self::add() for docs. * * @param string $key Key for the data. * @param mixed $data Value for the data. * * @throws InvalidArgumentException If key is not a string or already * exists in internal data cache. */ protected function add_data( $key, $data ) { if ( ! is_string( $key ) ) { if ( $this->debug() ) { throw new InvalidArgumentException( 'Key for the data being registered must be a string' ); } } if ( isset( $this->data[ $key ] ) ) { if ( $this->debug() ) { throw new InvalidArgumentException( 'Overriding existing data with an already registered key is not allowed' ); } return; } if ( \is_callable( $data ) ) { $this->lazy_data[ $key ] = $data; return; } $this->data[ $key ] = $data; } /** * Exposes whether the current site is in debug mode or not. * * @return boolean True means the site is in debug mode. */ protected function debug() { return defined( 'WP_DEBUG' ) && WP_DEBUG; } }