2018-10-18 03:31:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Blocks API: WP_Block_Type class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Blocks
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class representing a block type.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @see register_block_type()
|
|
|
|
*/
|
|
|
|
class WP_Block_Type {
|
|
|
|
/**
|
|
|
|
* Block type key.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type render callback.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
public $render_callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type attributes property schemas.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $attributes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type editor script handle.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $editor_script;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type front end script handle.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $script;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type editor style handle.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $editor_style;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type front end style handle.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $style;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Will populate object properties from the provided arguments.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @see register_block_type()
|
|
|
|
*
|
|
|
|
* @param string $block_type Block type name including namespace.
|
|
|
|
* @param array|string $args Optional. Array or string of arguments for registering a block type.
|
|
|
|
* Default empty array.
|
|
|
|
*/
|
|
|
|
public function __construct( $block_type, $args = array() ) {
|
|
|
|
$this->name = $block_type;
|
|
|
|
|
|
|
|
$this->set_props( $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the block type output for given attributes.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param array $attributes Optional. Block attributes. Default empty array.
|
|
|
|
* @param string $content Optional. Block content. Default empty string.
|
|
|
|
* @return string Rendered block type output.
|
|
|
|
*/
|
|
|
|
public function render( $attributes = array(), $content = '' ) {
|
|
|
|
if ( ! $this->is_dynamic() ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes = $this->prepare_attributes_for_render( $attributes );
|
|
|
|
|
|
|
|
return (string) call_user_func( $this->render_callback, $attributes, $content );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the block type is dynamic, or false otherwise. A dynamic
|
|
|
|
* block is one which defers its rendering to occur on-demand at runtime.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @return boolean Whether block type is dynamic.
|
|
|
|
*/
|
|
|
|
public function is_dynamic() {
|
|
|
|
return is_callable( $this->render_callback );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates attributes against the current block schema, populating
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
* defaulted and missing values.
|
2018-10-18 03:31:26 +02:00
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param array $attributes Original block attributes.
|
|
|
|
* @return array Prepared block attributes.
|
|
|
|
*/
|
|
|
|
public function prepare_attributes_for_render( $attributes ) {
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
// If there are no attribute definitions for the block type, skip
|
|
|
|
// processing and return vebatim.
|
2018-10-18 03:31:26 +02:00
|
|
|
if ( ! isset( $this->attributes ) ) {
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
foreach ( $attributes as $attribute_name => $value ) {
|
|
|
|
// If the attribute is not defined by the block type, it cannot be
|
|
|
|
// validated.
|
|
|
|
if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-18 03:31:26 +02:00
|
|
|
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
$schema = $this->attributes[ $attribute_name ];
|
2018-10-18 03:31:26 +02:00
|
|
|
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
// Validate value by JSON schema. An invalid value should revert to
|
|
|
|
// its default, if one exists. This occurs by virtue of the missing
|
|
|
|
// attributes loop immediately following. If there is not a default
|
|
|
|
// assigned, the attribute value should remain unset.
|
|
|
|
$is_valid = rest_validate_value_from_schema( $value, $schema );
|
|
|
|
if ( is_wp_error( $is_valid ) ) {
|
|
|
|
unset( $attributes[ $attribute_name ] );
|
2018-10-18 03:31:26 +02:00
|
|
|
}
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
}
|
2018-10-18 03:31:26 +02:00
|
|
|
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
// Populate values of any missing attributes for which the block type
|
|
|
|
// defines a default.
|
|
|
|
$missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
|
|
|
|
foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
|
|
|
|
if ( isset( $schema['default'] ) ) {
|
|
|
|
$attributes[ $attribute_name ] = $schema['default'];
|
2018-10-18 03:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
REST API: Preserve unknown, respect `null` in server-side block rendering.
* Skips validation where there is no attribute definition, but keeps the attribute value. Previously, the attribute would be omitted from the attributes passed to `render_callback`. Notably, this resolves an issue where `render_callback` cannot receive a block's `align` and `customClassName` attribute values, since these are defined as a client-side filter.
* Validates `null` as a proper value in its own right. Previously, a client implementation of a block could track `{"attribute":null}` as an explicitly empty value, and the server would wrongly initiate defaulting behavior. The new behavior will now only populate a default value if the attribute is not defined at all, including when unset in its being invalid per the attribute schema.
Props aduth, noisysocks, youknowriad.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/branches/5.0@43918
git-svn-id: http://core.svn.wordpress.org/branches/5.0@43750 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-11-21 15:44:48 +01:00
|
|
|
return $attributes;
|
2018-10-18 03:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets block type properties.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param array|string $args Array or string of arguments for registering a block type.
|
|
|
|
*/
|
|
|
|
public function set_props( $args ) {
|
|
|
|
$args = wp_parse_args(
|
|
|
|
$args,
|
|
|
|
array(
|
|
|
|
'render_callback' => null,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$args['name'] = $this->name;
|
|
|
|
|
|
|
|
foreach ( $args as $property_name => $property_value ) {
|
|
|
|
$this->$property_name = $property_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available block attributes including possible layout attribute from Columns block.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @return array Array of attributes.
|
|
|
|
*/
|
|
|
|
public function get_attributes() {
|
|
|
|
return is_array( $this->attributes ) ?
|
|
|
|
array_merge(
|
|
|
|
$this->attributes,
|
|
|
|
array(
|
|
|
|
'layout' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
) :
|
|
|
|
array(
|
|
|
|
'layout' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|