WooCommerce Code Reference

CouponCode extends AbstractBlock
in package

CouponCode block for displaying coupon codes in emails.

Tags
since
10.5.0

Table of Contents

COUPON_CODE_PLACEHOLDER  = 'XXXX-XXXXXX-XXXX'
Placeholder displayed in the editor and in non-email rendering for auto-generated coupons.
DEFAULT_STYLES  = array('font-size' => '1.2em', 'padding' => '12px 20px', 'display' => 'inline-block', 'border' => '2px dashed #cccccc', 'border-radius' => '4px', 'box-sizing' => 'border-box', 'color' => '#000000', 'background-color' => '#f5f5f5', 'text-align' => 'center', 'font-weight' => 'bold', 'letter-spacing' => '1px')
Default styles for the coupon code element.
$asset_api  : Api
Instance of the asset API.
$asset_data_registry  : AssetDataRegistry
Instance of the asset data registry.
$block_name  : string
Block name.
$enqueued_assets  : bool
Tracks if assets have been enqueued.
$integration_registry  : IntegrationRegistry
Instance of the integration registry.
$namespace  : string
Block namespace.
__construct()  : mixed
Constructor.
enqueue_editor_assets()  : mixed
Enqueue assets used for rendering the block in editor context.
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).
enqueue_data()  : void
Expose coupon types to the editor JS via AssetDataRegistry.
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|null
Get the editor script handle 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()  : array<string|int, mixed>|string|null
Get the frontend script handle for this block type.
get_block_type_style()  : 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.
render()  : string
Render the coupon code block.
build_coupon_html()  : string
Build the coupon code HTML element with styles.
get_alignment()  : string
Get alignment from parsed block attributes.
get_block_attributes()  : array<string|int, mixed>
Get block attributes from parsed block or fallback.
get_coupon_code()  : string
Extract coupon code from attributes.
get_rendering_context()  : Rendering_Context
Get rendering context from block or create a new one.
has_valid_background_color()  : bool
Check if declarations contain a valid CSS background color.
is_css_color_value()  : bool
Check if a string is a valid CSS color value (hex, rgb, or hsl).
resolve_background_color()  : string
Resolve background color from attributes, translating color slugs if needed.
wrap_for_email()  : string
Wrap coupon HTML in an email-compatible table structure.

Constants

COUPON_CODE_PLACEHOLDER

Placeholder displayed in the editor and in non-email rendering for auto-generated coupons.

public mixed COUPON_CODE_PLACEHOLDER = 'XXXX-XXXXXX-XXXX'

DEFAULT_STYLES

Default styles for the coupon code element.

private mixed DEFAULT_STYLES = array('font-size' => '1.2em', 'padding' => '12px 20px', 'display' => 'inline-block', 'border' => '2px dashed #cccccc', 'border-radius' => '4px', 'box-sizing' => 'border-box', 'color' => '#000000', 'background-color' => '#f5f5f5', 'text-align' => 'center', 'font-weight' => 'bold', 'letter-spacing' => '1px')

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

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

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.

enqueue_data()

Expose coupon types to the editor JS via AssetDataRegistry.

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

Block attributes.

Return values
void

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 handle for this block type.

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

Data to get. Valid keys: "handle", "path", "dependencies".

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

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()

Get the frontend script handle for this block type.

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

Data to get, or default to everything.

Tags
see

$this->register_block_type()

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

get_block_type_style()

Get the frontend style handle for this block type.

protected get_block_type_style() : null
Return values
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>

initialize()

Initialize this block type.

protected initialize() : mixed
  • Hook into WP lifecycle.
  • Register the block with WordPress.
Return values
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 coupon code block.

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

Block attributes.

$content : string

Block content.

$block : WP_Block|null

Block instance.

Return values
string

build_coupon_html()

Build the coupon code HTML element with styles.

private build_coupon_html(string $coupon_code, array<string|int, mixed> $attributes, Rendering_Context $rendering_context) : string
Parameters
$coupon_code : string

Coupon code text.

$attributes : array<string|int, mixed>

Block attributes.

$rendering_context : Rendering_Context

Rendering context for style resolution.

Return values
string

get_alignment()

Get alignment from parsed block attributes.

private get_alignment(array<string|int, mixed> $parsed_block) : string
Parameters
$parsed_block : array<string|int, mixed>

Parsed block data.

Return values
string

get_block_attributes()

Get block attributes from parsed block or fallback.

private get_block_attributes(array<string|int, mixed> $parsed_block, array<string|int, mixed> $fallback) : array<string|int, mixed>
Parameters
$parsed_block : array<string|int, mixed>

Parsed block data.

$fallback : array<string|int, mixed>

Fallback attributes.

Return values
array<string|int, mixed>

get_coupon_code()

Extract coupon code from attributes.

private get_coupon_code(array<string|int, mixed> $attributes) : string
Parameters
$attributes : array<string|int, mixed>

Block attributes.

Return values
string

has_valid_background_color()

Check if declarations contain a valid CSS background color.

private has_valid_background_color(array<string|int, mixed> $declarations) : bool
Parameters
$declarations : array<string|int, mixed>

CSS declarations.

Return values
bool

is_css_color_value()

Check if a string is a valid CSS color value (hex, rgb, or hsl).

private is_css_color_value(string $value) : bool
Parameters
$value : string

Value to check.

Return values
bool

resolve_background_color()

Resolve background color from attributes, translating color slugs if needed.

private resolve_background_color(array<string|int, mixed> $attributes, Rendering_Context $rendering_context) : string
Parameters
$attributes : array<string|int, mixed>

Block attributes.

$rendering_context : Rendering_Context

Rendering context.

Return values
stringResolved color value or default.

wrap_for_email()

Wrap coupon HTML in an email-compatible table structure.

private wrap_for_email(string $coupon_html, array<string|int, mixed> $parsed_block) : string
Parameters
$coupon_html : string

Coupon HTML content.

$parsed_block : array<string|int, mixed>

Parsed block data.

Return values
string