WooCommerce Code Reference

AssetDataRegistry
in package

Class instance for registering data used on the current view session by assets.

Holds data registered for output on the current view session when wc-settings is enqueued( directly or via dependency )

Tags
since
2.5.0

Table of Contents

$api  : API
Asset API interface for various asset registration.
$data  : array<string|int, mixed>
Contains registered data.
$handle  : string
Asset handle for registered data.
$lazy_data  : array<string|int, mixed>
Lazy data is an array of closures that will be invoked just before asset data is generated for the enqueued script.
$preloaded_api_requests  : array<string|int, mixed>
Contains preloaded API data.
__construct()  : mixed
Constructor
add()  : mixed
Interface for adding data to the registry.
enqueue_asset_data()  : mixed
Callback for enqueuing asset data via the WP api.
exists()  : bool
Allows checking whether a key exists.
hydrate_api_request()  : mixed
Hydrate from the API.
hydrate_data_from_api_request()  : mixed
Hydrate some data from the API.
register_data_script()  : void
Callback for registering the data script via WordPress API.
register_page_id()  : mixed
Adds a page permalink to the data registry.
add_data()  : mixed
See self::add() for docs.
debug()  : bool
Exposes whether the current site is in debug mode or not.
execute_lazy_data()  : void
Loops through each registered lazy data callback and adds the returned value to the data array.
format_page_resource()  : array<string|int, mixed>
Format a page object into a standard array of data.
get()  : array<string|int, mixed>
Exposes private registered data to child classes.
get_core_data()  : array<string|int, mixed>
Exposes core data via the wcSettings global. This data is shared throughout the client.
get_currency_data()  : array<string|int, mixed>
Get currency data to include in settings.
get_locale_data()  : array<string|int, mixed>
Get locale data to include in settings.
get_order_statuses()  : array<string|int, mixed>
Returns block-related data for enqueued wc-settings script.
get_products_settings()  : array<string|int, mixed>
Get product related settings.
get_store_pages()  : array<string|int, mixed>
Get store pages to include in settings.
init()  : mixed
Hook into WP asset registration for enqueueing asset data.
initialize_core_data()  : mixed
Used for on demand initialization of asset data and registering it with the internal data registry.

Properties

$lazy_data

Lazy data is an array of closures that will be invoked just before asset data is generated for the enqueued script.

private array<string|int, mixed> $lazy_data = []

Methods

add()

Interface for adding data to the registry.

public add(string $key, mixed $data[, bool $check_key_exists = false ]) : mixed

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.

Parameters
$key : string

The key used to reference the data being registered. This should use camelCase.

$data : mixed

If not a function, registered to the registry as is. If a function, then the callback is invoked right before output to the screen.

$check_key_exists : bool = false

Deprecated. If set to true, duplicate data will be ignored if the key exists. If false, duplicate data will cause an exception.

Return values
mixed

enqueue_asset_data()

Callback for enqueuing asset data via the WP api.

public enqueue_asset_data() : mixed

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.

Return values
mixed

exists()

Allows checking whether a key exists.

public exists(string $key) : bool
Parameters
$key : string

The key to check if exists.

Return values
boolWhether the key exists in the current data registry.

hydrate_api_request()

Hydrate from the API.

public hydrate_api_request(string $path) : mixed
Parameters
$path : string

REST API path to preload.

Return values
mixed

hydrate_data_from_api_request()

Hydrate some data from the API.

public hydrate_data_from_api_request(string $key, string $path[, bool $check_key_exists = false ]) : mixed
Parameters
$key : string

The key used to reference the data being registered.

$path : string

REST API path to preload.

$check_key_exists : bool = false

If set to true, duplicate data will be ignored if the key exists. If false, duplicate data will cause an exception.

Tags
throws
InvalidArgumentException

Only throws when site is in debug mode. Always logs the error.

Return values
mixed

register_page_id()

Adds a page permalink to the data registry.

public register_page_id(int $page_id) : mixed
Parameters
$page_id : int

Page ID to add to the registry.

Return values
mixed

add_data()

See self::add() for docs.

protected add_data(string $key, mixed $data) : mixed
Parameters
$key : string

Key for the data.

$data : mixed

Value for the data.

Return values
mixed

debug()

Exposes whether the current site is in debug mode or not.

protected debug() : bool
Return values
boolTrue means the site is in debug mode.

execute_lazy_data()

Loops through each registered lazy data callback and adds the returned value to the data array.

protected execute_lazy_data() : void

This method is executed right before preparing the data for printing to the rendered screen.

Return values
void

format_page_resource()

Format a page object into a standard array of data.

protected format_page_resource(WP_Post|int $page) : array<string|int, mixed>
Parameters
$page : WP_Post|int

Page object or ID.

Return values
array<string|int, mixed>

get()

Exposes private registered data to child classes.

protected get() : array<string|int, mixed>
Return values
array<string|int, mixed>The registered data on the private data property

get_core_data()

Exposes core data via the wcSettings global. This data is shared throughout the client.

protected get_core_data() : array<string|int, mixed>

Settings that are used by various components or multiple blocks should be added here. Note, that settings here are global so be sure not to add anything heavy if possible.

Return values
array<string|int, mixed>An array containing core data.

get_currency_data()

Get currency data to include in settings.

protected get_currency_data() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_locale_data()

Get locale data to include in settings.

protected get_locale_data() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_order_statuses()

Returns block-related data for enqueued wc-settings script.

protected get_order_statuses() : array<string|int, mixed>

Format order statuses by removing a leading 'wc-' if present.

Return values
array<string|int, mixed>formatted statuses.

get_products_settings()

Get product related settings.

protected get_products_settings() : array<string|int, mixed>

Note: For the time being we are exposing only the settings that are used by blocks.

Return values
array<string|int, mixed>

get_store_pages()

Get store pages to include in settings.

protected get_store_pages() : array<string|int, mixed>
Return values
array<string|int, mixed>

initialize_core_data()

Used for on demand initialization of asset data and registering it with the internal data registry.

protected initialize_core_data() : mixed

Note: core data will overwrite any externally registered data via the api.

Return values
mixed