WooCommerce Code Reference

Api
in package

The Api class provides an interface to various asset registration helpers.

Contains asset api methods

Tags
since
2.5.0

Table of Contents

$wc_version  : string
Stores the prefixed WC version. Used because the WC Blocks version has not been updated since the monorepo merge.
$disable_cache  : bool
Determines if caching is enabled for script data.
$inline_scripts  : array<string|int, mixed>
Stores inline scripts already enqueued.
$package  : Package
Reference to the Package instance
$script_data  : array<string|int, mixed>|null
Stores loaded script data for the current request
$script_data_hash  : string
Stores the hash for the script data, made up of the site url, plugin version and package path.
$script_data_transient_key  : string
Stores the transient key used to cache the script data. This will change if the site is accessed via HTTPS or HTTP.
__construct()  : mixed
Constructor for class
add_inline_script()  : mixed
Adds an inline script, once.
get_block_asset_build_path()  : string
Returns the appropriate asset path for current builds.
get_block_metadata_path()  : string|bool
Get the path to a block's metadata
get_script_data()  : array<string|int, mixed>
Get src, version and dependencies given a script relative src.
register_script()  : mixed
Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations.
register_style()  : mixed
Registers a style according to `wp_register_style`.
update_script_data_cache()  : mixed
Store all cached script data in the transient cache.
get_asset_url()  : string
Retrieve the url to an asset for this plugin.
get_file_version()  : string
Get the file modified time as a cache buster if we're in dev mode.
get_cached_script_data()  : array<string|int, mixed>
Initialize and load cached script data from the transient cache.
get_script_data_hash()  : string
Generates a hash containing the site url, plugin version and package path.

Properties

$wc_version

Stores the prefixed WC version. Used because the WC Blocks version has not been updated since the monorepo merge.

public string $wc_version

$disable_cache

Determines if caching is enabled for script data.

private bool $disable_cache = false

$inline_scripts

Stores inline scripts already enqueued.

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

$script_data

Stores loaded script data for the current request

private array<string|int, mixed>|null $script_data = null

$script_data_hash

Stores the hash for the script data, made up of the site url, plugin version and package path.

private string $script_data_hash

$script_data_transient_key

Stores the transient key used to cache the script data. This will change if the site is accessed via HTTPS or HTTP.

private string $script_data_transient_key = 'woocommerce_blocks_asset_api_script_data'

Methods

add_inline_script()

Adds an inline script, once.

public add_inline_script(string $handle, string $script) : mixed
Parameters
$handle : string

Script handle.

$script : string

Script contents.

Return values
mixed

get_block_asset_build_path()

Returns the appropriate asset path for current builds.

public get_block_asset_build_path(string $filename[, string $type = 'js' ]) : string
Parameters
$filename : string

Filename for asset path (without extension).

$type : string = 'js'

File type (.css or .js).

Return values
stringThe generated path.

get_block_metadata_path()

Get the path to a block's metadata

public get_block_metadata_path(string $block_name[, string $path = '' ]) : string|bool
Parameters
$block_name : string

The block to get metadata for.

$path : string = ''

Optional. The path to the metadata file inside the 'assets/client/blocks' folder.

Return values
string|boolFalse if metadata file is not found for the block.

get_script_data()

Get src, version and dependencies given a script relative src.

public get_script_data(string $relative_src[, array<string|int, mixed> $dependencies = [] ]) : array<string|int, mixed>
Parameters
$relative_src : string

Relative src to the script.

$dependencies : array<string|int, mixed> = []

Optional. An array of registered script handles this script depends on. Default empty array.

Return values
array<string|int, mixed>src, version and dependencies of the script.

register_script()

Registers a script according to `wp_register_script`, adding the correct prefix, and additionally loading translations.

public register_script(string $handle, string $relative_src[, array<string|int, mixed> $dependencies = [] ][, bool $has_i18n = true ]) : mixed

When creating script assets, the following rules should be followed:

  1. All asset handles should have a wc- prefix.
  2. If the asset handle is for a Block (in editor context) use the -block suffix.
  3. If the asset handle is for a Block (in frontend context) use the -block-frontend suffix.
  4. If the asset is for any other script being consumed or enqueued by the blocks plugin, use the wc-blocks- prefix.
Parameters
$handle : string

Unique name of the script.

$relative_src : string

Relative url for the script to the path from plugin root.

$dependencies : array<string|int, mixed> = []

Optional. An array of registered script handles this script depends on. Default empty array.

$has_i18n : bool = true

Optional. Whether to add a script translation call to this file. Default: true.

Tags
since
2.5.0
throws
Exception

If the registered script has a dependency on itself.

Return values
mixed

register_style()

Registers a style according to `wp_register_style`.

public register_style(string $handle, string $relative_src[, array<string|int, mixed> $deps = [] ][, string $media = 'all' ][, bool $rtl = false ]) : mixed
Parameters
$handle : string

Name of the stylesheet. Should be unique.

$relative_src : string

Relative source of the stylesheet to the plugin path.

$deps : array<string|int, mixed> = []

Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.

$media : string = 'all'

Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.

$rtl : bool = false

Optional. Whether or not to register RTL styles.

Tags
since
2.5.0
since
2.6.0

Change src to be relative source.

Return values
mixed

update_script_data_cache()

Store all cached script data in the transient cache.

public update_script_data_cache() : mixed
Return values
mixed

get_asset_url()

Retrieve the url to an asset for this plugin.

protected get_asset_url([string $relative_path = '' ]) : string
Parameters
$relative_path : string = ''

An optional relative path appended to the returned url.

Return values
string

get_file_version()

Get the file modified time as a cache buster if we're in dev mode.

protected get_file_version(string $file) : string
Parameters
$file : string

Local path to the file (relative to the plugin directory).

Return values
stringThe cache buster value to use for the given file.

get_cached_script_data()

Initialize and load cached script data from the transient cache.

private get_cached_script_data() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_script_data_hash()

Generates a hash containing the site url, plugin version and package path.

private get_script_data_hash() : string

Moving the plugin, changing the version, or changing the site url will result in a new hash and the cache will be invalidated.

Return values
stringThe generated hash.