2018-12-13 10:44:23 +01: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()
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 17:47:14 +02:00
|
|
|
#[AllowDynamicProperties]
|
2018-12-13 10:44:23 +01:00
|
|
|
class WP_Block_Type {
|
2020-06-01 14:27:08 +02:00
|
|
|
|
2020-10-20 09:54:10 +02:00
|
|
|
/**
|
|
|
|
* Block API version.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $api_version = 1;
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
/**
|
|
|
|
* Block type key.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
2020-06-01 14:27:08 +02:00
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Human-readable block type label.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $title = '';
|
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Block type category classification, used in search interfaces
|
|
|
|
* to arrange block types by category.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
2020-06-04 08:07:13 +02:00
|
|
|
* @var string|null
|
2020-06-01 14:27:08 +02:00
|
|
|
*/
|
2020-06-04 08:07:13 +02:00
|
|
|
public $category = null;
|
2020-06-01 14:27:08 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Setting parent lets a block require that it is only available
|
|
|
|
* when nested within the specified blocks.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
2022-08-11 16:03:09 +02:00
|
|
|
* @var string[]|null
|
2020-06-01 14:27:08 +02:00
|
|
|
*/
|
|
|
|
public $parent = null;
|
|
|
|
|
2022-04-06 11:47:09 +02:00
|
|
|
/**
|
|
|
|
* Setting ancestor makes a block available only inside the specified
|
|
|
|
* block types at any position of the ancestor's block subtree.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
2022-08-11 16:03:09 +02:00
|
|
|
* @var string[]|null
|
2022-04-06 11:47:09 +02:00
|
|
|
*/
|
|
|
|
public $ancestor = null;
|
|
|
|
|
2020-06-01 14:27:08 +02:00
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Block type icon.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
2020-06-04 08:07:13 +02:00
|
|
|
* @var string|null
|
2020-06-01 14:27:08 +02:00
|
|
|
*/
|
2020-06-04 08:07:13 +02:00
|
|
|
public $icon = null;
|
2020-06-01 14:27:08 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* A detailed block type description.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $description = '';
|
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Additional keywords to produce block type as result
|
|
|
|
* in search interfaces.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
2022-01-30 20:25:03 +01:00
|
|
|
* @var string[]
|
2020-06-01 14:27:08 +02:00
|
|
|
*/
|
|
|
|
public $keywords = array();
|
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* The translation textdomain.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $textdomain = null;
|
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Alternative block styles.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $styles = array();
|
|
|
|
|
2021-03-12 14:35:09 +01:00
|
|
|
/**
|
|
|
|
* Block variations.
|
2021-03-13 11:25:06 +01:00
|
|
|
*
|
2021-03-12 14:35:09 +01:00
|
|
|
* @since 5.8.0
|
2022-08-11 16:03:09 +02:00
|
|
|
* @var array[]
|
2021-03-12 14:35:09 +01:00
|
|
|
*/
|
|
|
|
public $variations = array();
|
|
|
|
|
2023-04-21 12:43:25 +02:00
|
|
|
/**
|
|
|
|
* Custom CSS selectors for theme.json style generation.
|
|
|
|
*
|
|
|
|
* @since 6.3.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $selectors = array();
|
|
|
|
|
2020-06-01 14:27:08 +02:00
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Supported features.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
2020-06-22 12:26:14 +02:00
|
|
|
* @var array|null
|
2020-06-01 14:27:08 +02:00
|
|
|
*/
|
2020-06-22 12:26:14 +02:00
|
|
|
public $supports = null;
|
2020-06-01 14:27:08 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-23 20:18:02 +01:00
|
|
|
* Structured data for the block preview.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.5.0
|
|
|
|
* @var array|null
|
|
|
|
*/
|
|
|
|
public $example = null;
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
/**
|
|
|
|
* Block type render callback.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
* @var callable
|
|
|
|
*/
|
2020-06-01 14:27:08 +02:00
|
|
|
public $render_callback = null;
|
2018-12-13 10:44:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Block type attributes property schemas.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
2020-06-01 14:27:08 +02:00
|
|
|
* @var array|null
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
2020-06-01 14:27:08 +02:00
|
|
|
public $attributes = null;
|
2018-12-13 10:44:23 +01:00
|
|
|
|
2020-06-22 12:26:14 +02:00
|
|
|
/**
|
|
|
|
* Context values inherited by blocks of this type.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
2022-08-11 16:03:09 +02:00
|
|
|
* @var string[]
|
2020-06-22 12:26:14 +02:00
|
|
|
*/
|
|
|
|
public $uses_context = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context provided by blocks of this type.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
2022-08-11 16:03:09 +02:00
|
|
|
* @var string[]|null
|
2020-06-22 12:26:14 +02:00
|
|
|
*/
|
|
|
|
public $provides_context = null;
|
|
|
|
|
2023-09-14 15:25:18 +02:00
|
|
|
/**
|
|
|
|
* Block hooks for this block type.
|
|
|
|
*
|
|
|
|
* A block hook is specified by a block type and a relative position.
|
|
|
|
* The hooked block will be automatically inserted in the given position
|
|
|
|
* next to the "anchor" block whenever the latter is encountered.
|
|
|
|
*
|
|
|
|
* @since 6.4.0
|
2023-11-27 21:20:24 +01:00
|
|
|
* @var string[]
|
2023-09-14 15:25:18 +02:00
|
|
|
*/
|
|
|
|
public $block_hooks = array();
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
/**
|
2022-09-14 12:52:08 +02:00
|
|
|
* Block type editor only script handles.
|
2018-12-13 10:44:23 +01:00
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
2022-09-14 12:52:08 +02:00
|
|
|
public $editor_script_handles = array();
|
2018-12-13 10:44:23 +01:00
|
|
|
|
|
|
|
/**
|
2022-09-14 12:52:08 +02:00
|
|
|
* Block type front end and editor script handles.
|
2018-12-13 10:44:23 +01:00
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
2022-09-14 12:52:08 +02:00
|
|
|
public $script_handles = array();
|
2018-12-13 10:44:23 +01:00
|
|
|
|
|
|
|
/**
|
2022-09-14 12:52:08 +02:00
|
|
|
* Block type front end only script handles.
|
2021-07-28 12:06:59 +02:00
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
2021-07-28 12:06:59 +02:00
|
|
|
*/
|
2022-09-14 12:52:08 +02:00
|
|
|
public $view_script_handles = array();
|
2021-07-28 12:06:59 +02:00
|
|
|
|
|
|
|
/**
|
2022-09-14 12:52:08 +02:00
|
|
|
* Block type editor only style handles.
|
2018-12-13 10:44:23 +01:00
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
2022-09-14 12:52:08 +02:00
|
|
|
public $editor_style_handles = array();
|
2018-12-13 10:44:23 +01:00
|
|
|
|
|
|
|
/**
|
2022-09-14 12:52:08 +02:00
|
|
|
* Block type front end and editor style handles.
|
2018-12-13 10:44:23 +01:00
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
public $style_handles = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated block type properties for script and style handles.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
* @var string[]
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
2022-09-14 12:52:08 +02:00
|
|
|
private $deprecated_properties = array(
|
|
|
|
'editor_script',
|
|
|
|
'script',
|
|
|
|
'view_script',
|
|
|
|
'editor_style',
|
|
|
|
'style',
|
|
|
|
);
|
2018-12-13 10:44:23 +01:00
|
|
|
|
2022-04-26 11:50:12 +02:00
|
|
|
/**
|
|
|
|
* Attributes supported by every block.
|
|
|
|
*
|
2023-11-06 15:47:18 +01:00
|
|
|
* @since 6.0.0 Added `lock`.
|
|
|
|
* @since 6.5.0 Added `metadata`.
|
2022-04-26 11:50:12 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
const GLOBAL_ATTRIBUTES = array(
|
2023-11-06 15:47:18 +01:00
|
|
|
'lock' => array( 'type' => 'object' ),
|
|
|
|
'metadata' => array( 'type' => 'object' ),
|
2022-04-26 11:50:12 +02:00
|
|
|
);
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Will populate object properties from the provided arguments.
|
|
|
|
*
|
2020-06-01 14:27:08 +02:00
|
|
|
* @since 5.0.0
|
2021-06-26 13:13:56 +02:00
|
|
|
* @since 5.5.0 Added the `title`, `category`, `parent`, `icon`, `description`,
|
|
|
|
* `keywords`, `textdomain`, `styles`, `supports`, `example`,
|
|
|
|
* `uses_context`, and `provides_context` properties.
|
|
|
|
* @since 5.6.0 Added the `api_version` property.
|
|
|
|
* @since 5.8.0 Added the `variations` property.
|
2021-07-28 12:06:59 +02:00
|
|
|
* @since 5.9.0 Added the `view_script` property.
|
2022-04-06 11:47:09 +02:00
|
|
|
* @since 6.0.0 Added the `ancestor` property.
|
2022-09-14 12:52:08 +02:00
|
|
|
* @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles,
|
|
|
|
* `editor_style_handles`, and `style_handles` properties.
|
|
|
|
* Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
|
2023-04-21 12:43:25 +02:00
|
|
|
* @since 6.3.0 Added the `selectors` property.
|
2023-09-14 15:25:18 +02:00
|
|
|
* @since 6.4.0 Added the `block_hooks` property.
|
2020-06-01 14:27:08 +02:00
|
|
|
*
|
2020-06-01 18:27:15 +02:00
|
|
|
* @see register_block_type()
|
2020-06-01 14:27:08 +02:00
|
|
|
*
|
2020-06-01 18:27:15 +02:00
|
|
|
* @param string $block_type Block type name including namespace.
|
2021-02-23 20:18:02 +01:00
|
|
|
* @param array|string $args {
|
|
|
|
* Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
|
|
|
|
* however the ones described below are supported by default. Default empty array.
|
|
|
|
*
|
2022-09-14 12:52:08 +02:00
|
|
|
* @type string $api_version Block API version.
|
|
|
|
* @type string $title Human-readable block type label.
|
|
|
|
* @type string|null $category Block type category classification, used in
|
|
|
|
* search interfaces to arrange block types by category.
|
|
|
|
* @type string[]|null $parent Setting parent lets a block require that it is only
|
|
|
|
* available when nested within the specified blocks.
|
|
|
|
* @type string[]|null $ancestor Setting ancestor makes a block available only inside the specified
|
|
|
|
* block types at any position of the ancestor's block subtree.
|
|
|
|
* @type string|null $icon Block type icon.
|
|
|
|
* @type string $description A detailed block type description.
|
|
|
|
* @type string[] $keywords Additional keywords to produce block type as
|
|
|
|
* result in search interfaces.
|
|
|
|
* @type string|null $textdomain The translation textdomain.
|
|
|
|
* @type array[] $styles Alternative block styles.
|
|
|
|
* @type array[] $variations Block variations.
|
2023-04-21 12:43:25 +02:00
|
|
|
* @type array $selectors Custom CSS selectors for theme.json style generation.
|
2022-09-14 12:52:08 +02:00
|
|
|
* @type array|null $supports Supported features.
|
|
|
|
* @type array|null $example Structured data for the block preview.
|
|
|
|
* @type callable|null $render_callback Block type render callback.
|
|
|
|
* @type array|null $attributes Block type attributes property schemas.
|
|
|
|
* @type string[] $uses_context Context values inherited by blocks of this type.
|
|
|
|
* @type string[]|null $provides_context Context provided by blocks of this type.
|
2023-11-27 21:20:24 +01:00
|
|
|
* @type string[] $block_hooks Block hooks.
|
2022-09-14 12:52:08 +02:00
|
|
|
* @type string[] $editor_script_handles Block type editor only script handles.
|
|
|
|
* @type string[] $script_handles Block type front end and editor script handles.
|
|
|
|
* @type string[] $view_script_handles Block type front end only script handles.
|
|
|
|
* @type string[] $editor_style_handles Block type editor only style handles.
|
|
|
|
* @type string[] $style_handles Block type front end and editor style handles.
|
2021-02-23 20:18:02 +01:00
|
|
|
* }
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
|
|
|
public function __construct( $block_type, $args = array() ) {
|
|
|
|
$this->name = $block_type;
|
|
|
|
|
|
|
|
$this->set_props( $args );
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:52:08 +02:00
|
|
|
/**
|
|
|
|
* Proxies getting values for deprecated properties for script and style handles for backward compatibility.
|
|
|
|
* Gets the value for the corresponding new property if the first item in the array provided.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
|
|
|
* @param string $name Deprecated property name.
|
|
|
|
*
|
Blocks: Allow arrays for deprecated asset types in block registration.
In `register_block_type`, continue to allow passing arrays as the `editor_script`, `script`, `view_script`, `editor_style`, and `style` arguments. Note that those fields were soft-deprecated in favor of their `_handles` counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to `string` or `null`.
However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in `WP_Block_Type` as a pair of `__get()` and `__set()` methods that wrap around the corresponding `_handles` members, which are arrays of strings.
It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than `string` or `null` for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding `_handles` member in `WP_Block_Type`.
Follow-up [54155].
Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb.
Fixes #56707.
Built from https://develop.svn.wordpress.org/trunk@54670
git-svn-id: http://core.svn.wordpress.org/trunk@54222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-24 16:16:12 +02:00
|
|
|
* @return string|string[]|null|void The value read from the new property if the first item in the array provided,
|
|
|
|
* null when value not found, or void when unknown property name provided.
|
2022-09-14 12:52:08 +02:00
|
|
|
*/
|
|
|
|
public function __get( $name ) {
|
2022-11-29 22:00:14 +01:00
|
|
|
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
|
2022-09-14 12:52:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_name = $name . '_handles';
|
Blocks: Allow arrays for deprecated asset types in block registration.
In `register_block_type`, continue to allow passing arrays as the `editor_script`, `script`, `view_script`, `editor_style`, and `style` arguments. Note that those fields were soft-deprecated in favor of their `_handles` counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to `string` or `null`.
However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in `WP_Block_Type` as a pair of `__get()` and `__set()` methods that wrap around the corresponding `_handles` members, which are arrays of strings.
It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than `string` or `null` for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding `_handles` member in `WP_Block_Type`.
Follow-up [54155].
Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb.
Fixes #56707.
Built from https://develop.svn.wordpress.org/trunk@54670
git-svn-id: http://core.svn.wordpress.org/trunk@54222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-24 16:16:12 +02:00
|
|
|
|
|
|
|
if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $this->{$new_name} ) > 1 ) {
|
|
|
|
return $this->{$new_name};
|
|
|
|
}
|
2022-09-14 15:53:13 +02:00
|
|
|
return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
|
2022-09-14 12:52:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxies checking for deprecated properties for script and style handles for backward compatibility.
|
|
|
|
* Checks whether the corresponding new property has the first item in the array provided.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
|
|
|
* @param string $name Deprecated property name.
|
|
|
|
*
|
2023-08-18 19:29:20 +02:00
|
|
|
* @return bool Returns true when for the new property the first item in the array exists,
|
|
|
|
* or false otherwise.
|
2022-09-14 12:52:08 +02:00
|
|
|
*/
|
|
|
|
public function __isset( $name ) {
|
2022-11-29 22:00:14 +01:00
|
|
|
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
|
2022-09-14 12:52:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_name = $name . '_handles';
|
2022-09-14 15:53:13 +02:00
|
|
|
return isset( $this->{$new_name}[0] );
|
2022-09-14 12:52:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxies setting values for deprecated properties for script and style handles for backward compatibility.
|
|
|
|
* Sets the value for the corresponding new property as the first item in the array.
|
|
|
|
* It also allows setting custom properties for backward compatibility.
|
|
|
|
*
|
|
|
|
* @since 6.1.0
|
|
|
|
*
|
|
|
|
* @param string $name Property name.
|
|
|
|
* @param mixed $value Property value.
|
|
|
|
*/
|
|
|
|
public function __set( $name, $value ) {
|
2022-11-29 22:00:14 +01:00
|
|
|
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
|
2022-09-14 12:52:08 +02:00
|
|
|
$this->{$name} = $value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Blocks: Allow arrays for deprecated asset types in block registration.
In `register_block_type`, continue to allow passing arrays as the `editor_script`, `script`, `view_script`, `editor_style`, and `style` arguments. Note that those fields were soft-deprecated in favor of their `_handles` counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to `string` or `null`.
However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in `WP_Block_Type` as a pair of `__get()` and `__set()` methods that wrap around the corresponding `_handles` members, which are arrays of strings.
It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than `string` or `null` for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding `_handles` member in `WP_Block_Type`.
Follow-up [54155].
Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb.
Fixes #56707.
Built from https://develop.svn.wordpress.org/trunk@54670
git-svn-id: http://core.svn.wordpress.org/trunk@54222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-24 16:16:12 +02:00
|
|
|
$new_name = $name . '_handles';
|
|
|
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
$filtered = array_filter( $value, 'is_string' );
|
|
|
|
|
|
|
|
if ( count( $filtered ) !== count( $value ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
sprintf(
|
|
|
|
/* translators: %s: The '$value' argument. */
|
|
|
|
__( 'The %s argument must be a string or a string array.' ),
|
|
|
|
'<code>$value</code>'
|
|
|
|
),
|
|
|
|
'6.1.0'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->{$new_name} = array_values( $filtered );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-14 12:52:08 +02:00
|
|
|
if ( ! is_string( $value ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Blocks: Allow arrays for deprecated asset types in block registration.
In `register_block_type`, continue to allow passing arrays as the `editor_script`, `script`, `view_script`, `editor_style`, and `style` arguments. Note that those fields were soft-deprecated in favor of their `_handles` counterparts in [54155], which would allow specifying multiple items. At the same time, the deprecated fields were limited to `string` or `null`.
However, this broke existing code that passed an array as one of those arguments. For backwards compatibility, this change thus restores the previous behavior. It is implemented in `WP_Block_Type` as a pair of `__get()` and `__set()` methods that wrap around the corresponding `_handles` members, which are arrays of strings.
It also affects the REST API endpoint for block types. The latter’s schema has never allowed for anything other than `string` or `null` for any of those fields. For this reason, it now returns the first element of the array stored in the corresponding `_handles` member in `WP_Block_Type`.
Follow-up [54155].
Props nendeb55, costdev, gziolo, spacedmonkey, mukesh27, sergeybiryukov, audrasjb.
Fixes #56707.
Built from https://develop.svn.wordpress.org/trunk@54670
git-svn-id: http://core.svn.wordpress.org/trunk@54222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-24 16:16:12 +02:00
|
|
|
$this->{$new_name} = array( $value );
|
2022-09-14 12:52:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2020-10-10 22:02:05 +02:00
|
|
|
* @return bool Whether block type is dynamic.
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
|
|
|
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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
* defaulted and missing values.
|
2018-12-13 10:44:23 +01:00
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
2020-06-20 14:58:10 +02:00
|
|
|
* @param array $attributes Original block attributes.
|
|
|
|
* @return array Prepared block attributes.
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
|
|
|
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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
// If there are no attribute definitions for the block type, skip
|
2020-09-10 18:34:11 +02:00
|
|
|
// processing and return verbatim.
|
2018-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +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-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
$schema = $this->attributes[ $attribute_name ];
|
2018-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +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.
|
2020-09-10 18:34:11 +02:00
|
|
|
$is_valid = rest_validate_value_from_schema( $value, $schema, $attribute_name );
|
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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
if ( is_wp_error( $is_valid ) ) {
|
|
|
|
unset( $attributes[ $attribute_name ] );
|
2018-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
}
|
2018-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +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-12-13 10:44:23 +01: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, danielbachhuber.
Merges [43918] to trunk.
See #45145 for the patch, #45098 for the original ticket.
Built from https://develop.svn.wordpress.org/trunk@44269
git-svn-id: http://core.svn.wordpress.org/trunk@44099 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-17 19:00:52 +01:00
|
|
|
return $attributes;
|
2018-12-13 10:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets block type properties.
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param array|string $args Array or string of arguments for registering a block type.
|
2021-02-23 20:18:02 +01:00
|
|
|
* See WP_Block_Type::__construct() for information on accepted arguments.
|
2018-12-13 10:44:23 +01:00
|
|
|
*/
|
|
|
|
public function set_props( $args ) {
|
|
|
|
$args = wp_parse_args(
|
|
|
|
$args,
|
|
|
|
array(
|
|
|
|
'render_callback' => null,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$args['name'] = $this->name;
|
|
|
|
|
2022-04-26 11:50:12 +02:00
|
|
|
// Setup attributes if needed.
|
|
|
|
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
|
|
|
|
$args['attributes'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register core attributes.
|
|
|
|
foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) {
|
|
|
|
if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) {
|
|
|
|
$args['attributes'][ $attr_key ] = $attr_schema;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 15:10:02 +02:00
|
|
|
/**
|
|
|
|
* Filters the arguments for registering a block type.
|
|
|
|
*
|
|
|
|
* @since 5.5.0
|
|
|
|
*
|
2020-07-02 13:30:02 +02:00
|
|
|
* @param array $args Array of arguments for registering a block type.
|
2020-07-01 15:10:02 +02:00
|
|
|
* @param string $block_type Block type name including namespace.
|
|
|
|
*/
|
|
|
|
$args = apply_filters( 'register_block_type_args', $args, $this->name );
|
|
|
|
|
2018-12-13 10:44:23 +01:00
|
|
|
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 ) ?
|
2020-06-22 14:22:08 +02:00
|
|
|
$this->attributes :
|
|
|
|
array();
|
2018-12-13 10:44:23 +01:00
|
|
|
}
|
|
|
|
}
|