and adds the returned * value to the data array. * * This method is executed right before preparing the data for printing to * the rendered screen. * * @return void */ protected function execute_lazy_data() { foreach ( $this->lazy_data as $key => $callback ) { $this->data[ $key ] = $callback(); } } /** * Exposes private registered data to child classes. * * @return array The registered data on the private data property */ protected function get() { return $this->data; } /** * Allows checking whether a key exists. * * @param 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. * * @param string $key The key used to reference the data being registered. * You can only register data that is not already in the * registry identified by the given key. * @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. * * @throws InvalidArgumentException Only throws when site is in debug mode. * Always logs the error. */ public function add( $key, $data ) { try { $this->add_data( $key, $data ); } catch ( Exception $e ) { if ( $this->debug() ) { // bubble up. throw $e; } wc_caught_exception( $e, __METHOD__, [ $key, $data ] ); } } /** * Callback for registering the data script via WordPress API. * * @return void */ public function register_data_script() { $this->api->register_script( $this->handle, 'build/wc-settings.js', [], false ); } /** * 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 ) ); wp_add_inline_script( $this->handle, "var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );", '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; } }