WooCommerce Code Reference

Controller extends AbstractBlock
in package

Controller class.

Table of Contents

$asset_api  : Api
Instance of the asset API.
$asset_data_registry  : AssetDataRegistry
Instance of the asset data registry.
$block_name  : string
Block name.
$collection_handler_registry  : HandlerRegistry
Instance of HandlerRegistry.
$enqueued_assets  : bool
Tracks if assets have been enqueued.
$integration_registry  : IntegrationRegistry
Instance of the integration registry.
$namespace  : string
Block namespace.
$query_builder  : QueryBuilder
Instance of QueryBuilder.
$renderer  : Renderer
Instance of Renderer.
__construct()  : mixed
Constructor.
add_product_title_click_event_directives()  : string
Add interactivity to the Product Title block within Product Collection.
add_support_for_filter_blocks()  : mixed
Add support for filter blocks: - Price filter block - Attributes filter block - Rating filter block - In stock filter block etc.
build_frontend_query()  : array<string|int, mixed>
Return a custom query based on attributes, filters and global WP_Query.
disable_enhanced_pagination()  : string
Check inner blocks of Product Collection block if there's one incompatible with the Interactivity API and if so, disable client-side navigation.
enqueue_editor_assets()  : mixed
Enqueue assets used for rendering the block in editor context.
extend_rest_query_allowed_params()  : array<string|int, mixed>
Extends allowed `collection_params` for the REST API
register_settings()  : mixed
Exposes settings used by the Product Collection block when manipulating the default query.
render_callback()  : string
The default render_callback for all blocks. This will ensure assets are enqueued just in time, then render the block (if applicable).
update_rest_query_in_editor()  : array<string|int, mixed>
Update the query for the product query block in Editor.
enqueue_data()  : mixed
Extra data passed through from server to client for block.
enqueue_scripts()  : mixed
Register/enqueue scripts used for this block on the frontend, during render.
get_block_type()  : string
Get the block type.
get_block_type_attributes()  : array<string|int, mixed>
Get block attributes.
get_block_type_editor_script()  : array<string|int, mixed>|string
Get the editor script data for this block type.
get_block_type_editor_style()  : string|null
Get the editor style handle for this block type.
get_block_type_render_callback()  : callable|null
Get the render callback for this block type.
get_block_type_script()  : null
Disable the block type script, this block uses script modules.
get_block_type_style()  : array<string|int, string>|null
Get the frontend style handle for this block type.
get_block_type_supports()  : string
Get the supports array for this block type.
get_block_type_uses_context()  : array<string|int, mixed>
Get block usesContext.
get_chunks_paths()  : array<string|int, string>
Generate an array of chunks paths for loading translation.
get_full_block_name()  : string
Get the interactivity namespace. Only used when utilizing the interactivity API.
get_routes_from_namespace()  : array<string|int, mixed>
Get routes from a REST API namespace.
initialize()  : mixed
Initialize this block type.
is_block_editor()  : mixed
Are we currently on the admin block editor screen?
parse_render_callback_attributes()  : array<string|int, mixed>
Parses block attributes from the render_callback.
register_block_type()  : array<string|int, string>
Registers the block type with WordPress.
register_block_type_assets()  : mixed
Register script and style assets for the block type before it is registered.
register_chunk_translations()  : mixed
Injects Chunk Translations into the page so translations work for lazy loaded components.
register_core_collections_and_set_handler_store()  : mixed
Registers core collections and sets the handler store.
render()  : string
Render the block. Extended by children.
is_block_compatible()  : bool
Verifies if the inner block is compatible with Interactivity API.

Properties

Methods

__construct()

Constructor.

public __construct(Api $asset_api, AssetDataRegistry $asset_data_registry, IntegrationRegistry $integration_registry[, string $block_name = '' ]) : mixed
Parameters
$asset_api : Api

Instance of the asset API.

$asset_data_registry : AssetDataRegistry

Instance of the asset data registry.

$integration_registry : IntegrationRegistry

Instance of the integration registry.

$block_name : string = ''

Optionally set block name during construct.

Return values
mixed

add_product_title_click_event_directives()

Add interactivity to the Product Title block within Product Collection.

public add_product_title_click_event_directives(string $block_content, array<string|int, mixed> $block, WP_Block $instance) : string

This enables the triggering of a custom event when the product title is clicked.

Parameters
$block_content : string

The block content.

$block : array<string|int, mixed>

The full block, including name and attributes.

$instance : WP_Block

The block instance.

Return values
stringModified block content with added interactivity.

add_support_for_filter_blocks()

Add support for filter blocks: - Price filter block - Attributes filter block - Rating filter block - In stock filter block etc.

public add_support_for_filter_blocks(array<string|int, mixed> $pre_render, array<string|int, mixed> $parsed_block) : mixed
Parameters
$pre_render : array<string|int, mixed>

The pre-rendered block.

$parsed_block : array<string|int, mixed>

The parsed block.

Return values
mixed

build_frontend_query()

Return a custom query based on attributes, filters and global WP_Query.

public build_frontend_query(WP_Query $query, WP_Block $block, int $page) : array<string|int, mixed>
Parameters
$query : WP_Query

The WordPress Query.

$block : WP_Block

The block being rendered.

$page : int

The page number.

Return values
array<string|int, mixed>

disable_enhanced_pagination()

Check inner blocks of Product Collection block if there's one incompatible with the Interactivity API and if so, disable client-side navigation.

public disable_enhanced_pagination(array<string|int, mixed> $parsed_block) : string
Parameters
$parsed_block : array<string|int, mixed>

The block being rendered.

Return values
stringReturns the parsed block, unmodified.

enqueue_editor_assets()

Enqueue assets used for rendering the block in editor context.

public enqueue_editor_assets() : mixed

This is needed if a block is not yet within the post content--render and enqueue_assets may not have ran.

Return values
mixed

extend_rest_query_allowed_params()

Extends allowed `collection_params` for the REST API

public extend_rest_query_allowed_params(array<string|int, mixed> $params) : array<string|int, mixed>

By itself, the REST API doesn't accept custom orderby values, even if they are supported by a custom post type.

Parameters
$params : array<string|int, mixed>

A list of allowed orderby values.

Return values
array<string|int, mixed>

render_callback()

The default render_callback for all blocks. This will ensure assets are enqueued just in time, then render the block (if applicable).

public render_callback([array<string|int, mixed>|WP_Block $attributes = [] ][, string $content = '' ][, WP_Block|null $block = null ]) : string
Parameters
$attributes : array<string|int, mixed>|WP_Block = []

Block attributes, or an instance of a WP_Block. Defaults to an empty array.

$content : string = ''

Block content. Default empty string.

$block : WP_Block|null = null

Block instance.

Return values
stringRendered block type output.

update_rest_query_in_editor()

Update the query for the product query block in Editor.

public update_rest_query_in_editor(array<string|int, mixed> $query, WP_REST_Request $request) : array<string|int, mixed>
Parameters
$query : array<string|int, mixed>

Query args.

$request : WP_REST_Request

Request.

Return values
array<string|int, mixed>

enqueue_data()

Extra data passed through from server to client for block.

protected enqueue_data([array<string|int, mixed> $attributes = array() ]) : mixed
Parameters
$attributes : array<string|int, mixed> = array()

Any attributes that currently are available from the block. Note, this will be empty in the editor context when the block is not in the post content on editor load.

Return values
mixed

enqueue_scripts()

Register/enqueue scripts used for this block on the frontend, during render.

protected enqueue_scripts([array<string|int, mixed> $attributes = [] ]) : mixed
Parameters
$attributes : array<string|int, mixed> = []

Any attributes that currently are available from the block.

Return values
mixed

get_block_type_attributes()

Get block attributes.

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

get_block_type_editor_script()

Get the editor script data for this block type.

protected get_block_type_editor_script([string $key = null ]) : array<string|int, mixed>|string
Parameters
$key : string = null

Data to get, or default to everything.

Tags
see

$this->register_block_type()

Return values
array<string|int, mixed>|string

get_block_type_editor_style()

Get the editor style handle for this block type.

protected get_block_type_editor_style() : string|null
Tags
see

$this->register_block_type()

Return values
string|null

get_block_type_render_callback()

Get the render callback for this block type.

protected get_block_type_render_callback() : callable|null

Dynamic blocks should return a callback, for example, return [ $this, 'render' ];

Tags
see

$this->register_block_type()

Return values
callable|null

get_block_type_script()

Disable the block type script, this block uses script modules.

protected get_block_type_script([string|null $key = null ]) : null
Parameters
$key : string|null = null

The key of the script.

Return values
null

get_block_type_style()

Get the frontend style handle for this block type.

protected get_block_type_style() : array<string|int, string>|null
Return values
array<string|int, string>|null

get_block_type_supports()

Get the supports array for this block type.

protected get_block_type_supports() : string
Tags
see

$this->register_block_type()

Return values
string

get_block_type_uses_context()

Get block usesContext.

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

get_chunks_paths()

Generate an array of chunks paths for loading translation.

protected get_chunks_paths(string $chunks_folder) : array<string|int, string>
Parameters
$chunks_folder : string

The folder to iterate over.

Return values
array<string|int, string>$chunks list of chunks to load.

get_full_block_name()

Get the interactivity namespace. Only used when utilizing the interactivity API.

protected get_full_block_name() : string
Return values
stringThe interactivity namespace, used to namespace interactivity API actions and state.

get_routes_from_namespace()

Get routes from a REST API namespace.

protected get_routes_from_namespace(string $namespace) : array<string|int, mixed>
Parameters
$namespace : string

Namespace to retrieve.

Return values
array<string|int, mixed>

parse_render_callback_attributes()

Parses block attributes from the render_callback.

protected parse_render_callback_attributes(array<string|int, mixed>|WP_Block $attributes) : array<string|int, mixed>
Parameters
$attributes : array<string|int, mixed>|WP_Block

Block attributes, or an instance of a WP_Block. Defaults to an empty array.

Return values
array<string|int, mixed>

register_block_type()

Registers the block type with WordPress.

protected register_block_type() : array<string|int, string>
Return values
array<string|int, string>Chunks paths.

register_block_type_assets()

Register script and style assets for the block type before it is registered.

protected register_block_type_assets() : mixed

This registers the scripts; it does not enqueue them.

Return values
mixed

register_chunk_translations()

Injects Chunk Translations into the page so translations work for lazy loaded components.

protected register_chunk_translations(array<string|int, string> $chunks) : mixed

The chunk names are defined when creating lazy loaded components using webpackChunkName.

Parameters
$chunks : array<string|int, string>

Array of chunk names.

Return values
mixed

render()

Render the block. Extended by children.

protected render(array<string|int, mixed> $attributes, string $content, WP_Block $block) : string
Parameters
$attributes : array<string|int, mixed>

Block attributes.

$content : string

Block content.

$block : WP_Block

Block instance.

Return values
stringRendered block type output.

is_block_compatible()

Verifies if the inner block is compatible with Interactivity API.

private is_block_compatible(string $block_name) : bool
Parameters
$block_name : string

Name of the block to verify.

Return values
bool