2020-10-20 15:36:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Block support flags.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class encapsulating and implementing Block Supports.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
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]
|
2020-10-20 15:36:16 +02:00
|
|
|
class WP_Block_Supports {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config.
|
|
|
|
*
|
2020-10-26 22:51:08 +01:00
|
|
|
* @since 5.6.0
|
2020-10-20 15:36:16 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $block_supports = array();
|
|
|
|
|
2020-10-26 09:31:11 +01:00
|
|
|
/**
|
|
|
|
* Tracks the current block to be rendered.
|
|
|
|
*
|
2020-10-26 22:51:08 +01:00
|
|
|
* @since 5.6.0
|
2020-10-26 09:31:11 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $block_to_render = null;
|
|
|
|
|
2020-10-20 15:36:16 +02:00
|
|
|
/**
|
|
|
|
* Container for the main instance of the class.
|
|
|
|
*
|
2020-10-26 22:51:08 +01:00
|
|
|
* @since 5.6.0
|
2020-10-20 15:36:16 +02:00
|
|
|
* @var WP_Block_Supports|null
|
|
|
|
*/
|
|
|
|
private static $instance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to retrieve the main instance of the class.
|
|
|
|
*
|
|
|
|
* The instance will be created if it does not exist yet.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
|
|
|
* @return WP_Block_Supports The main instance.
|
|
|
|
*/
|
|
|
|
public static function get_instance() {
|
|
|
|
if ( null === self::$instance ) {
|
|
|
|
self::$instance = new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-23 22:37:01 +01:00
|
|
|
* Initializes the block supports. It registers the block supports block attributes.
|
2020-10-20 15:36:16 +02:00
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
$instance = self::get_instance();
|
|
|
|
$instance->register_attributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a block support.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
2022-08-11 16:11:08 +02:00
|
|
|
* @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
|
|
|
|
*
|
2021-11-23 22:37:01 +01:00
|
|
|
* @param string $block_support_name Block support name.
|
2020-10-20 15:36:16 +02:00
|
|
|
* @param array $block_support_config Array containing the properties of the block support.
|
|
|
|
*/
|
|
|
|
public function register( $block_support_name, $block_support_config ) {
|
|
|
|
$this->block_supports[ $block_support_name ] = array_merge(
|
|
|
|
$block_support_config,
|
|
|
|
array( 'name' => $block_support_name )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an array of HTML attributes, such as classes, by applying to
|
|
|
|
* the given block all of the features that the block supports.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
2023-05-09 00:37:24 +02:00
|
|
|
* @return string[] Array of HTML attribute values keyed by their name.
|
2020-10-20 15:36:16 +02:00
|
|
|
*/
|
2020-10-26 09:31:11 +01:00
|
|
|
public function apply_block_supports() {
|
2022-10-11 20:46:13 +02:00
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
|
2020-10-26 09:31:11 +01:00
|
|
|
self::$block_to_render['blockName']
|
2020-10-20 15:36:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// If no render_callback, assume styles have been previously handled.
|
|
|
|
if ( ! $block_type || empty( $block_type ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2024-05-06 23:25:15 +02:00
|
|
|
$block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] )
|
2024-09-26 14:47:16 +02:00
|
|
|
? $block_type->prepare_attributes_for_render( self::$block_to_render['attrs'] )
|
2022-10-11 20:46:13 +02:00
|
|
|
: array();
|
|
|
|
|
2020-10-20 15:36:16 +02:00
|
|
|
$output = array();
|
2021-01-28 03:04:13 +01:00
|
|
|
foreach ( $this->block_supports as $block_support_config ) {
|
2020-10-20 15:36:16 +02:00
|
|
|
if ( ! isset( $block_support_config['apply'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_attributes = call_user_func(
|
|
|
|
$block_support_config['apply'],
|
|
|
|
$block_type,
|
|
|
|
$block_attributes
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! empty( $new_attributes ) ) {
|
|
|
|
foreach ( $new_attributes as $attribute_name => $attribute_value ) {
|
|
|
|
if ( empty( $output[ $attribute_name ] ) ) {
|
|
|
|
$output[ $attribute_name ] = $attribute_value;
|
|
|
|
} else {
|
|
|
|
$output[ $attribute_name ] .= " $attribute_value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the block attributes required by the different block supports.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*/
|
|
|
|
private function register_attributes() {
|
|
|
|
$block_registry = WP_Block_Type_Registry::get_instance();
|
|
|
|
$registered_block_types = $block_registry->get_all_registered();
|
|
|
|
foreach ( $registered_block_types as $block_type ) {
|
2023-09-29 12:20:30 +02:00
|
|
|
if ( ! ( $block_type instanceof WP_Block_Type ) ) {
|
2020-10-20 15:36:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( ! $block_type->attributes ) {
|
|
|
|
$block_type->attributes = array();
|
|
|
|
}
|
|
|
|
|
2021-01-28 03:04:13 +01:00
|
|
|
foreach ( $this->block_supports as $block_support_config ) {
|
2020-10-20 15:36:16 +02:00
|
|
|
if ( ! isset( $block_support_config['register_attribute'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func(
|
|
|
|
$block_support_config['register_attribute'],
|
|
|
|
$block_type
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a string of attributes by applying to the current block being
|
|
|
|
* rendered all of the features that the block supports.
|
|
|
|
*
|
|
|
|
* @since 5.6.0
|
|
|
|
*
|
2021-11-23 22:37:01 +01:00
|
|
|
* @param string[] $extra_attributes Optional. Array of extra attributes to render on the block wrapper.
|
|
|
|
* @return string String of HTML attributes.
|
2020-10-20 15:36:16 +02:00
|
|
|
*/
|
|
|
|
function get_block_wrapper_attributes( $extra_attributes = array() ) {
|
2020-10-26 09:31:11 +01:00
|
|
|
$new_attributes = WP_Block_Supports::get_instance()->apply_block_supports();
|
2020-10-20 15:36:16 +02:00
|
|
|
|
|
|
|
if ( empty( $new_attributes ) && empty( $extra_attributes ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is hardcoded on purpose.
|
|
|
|
// We only support a fixed list of attributes.
|
2023-02-06 16:24:16 +01:00
|
|
|
$attributes_to_merge = array( 'style', 'class', 'id' );
|
2020-10-20 15:36:16 +02:00
|
|
|
$attributes = array();
|
|
|
|
foreach ( $attributes_to_merge as $attribute_name ) {
|
|
|
|
if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $new_attributes[ $attribute_name ] ) ) {
|
|
|
|
$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $extra_attributes[ $attribute_name ] ) ) {
|
|
|
|
$attributes[ $attribute_name ] = $new_attributes[ $attribute_name ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $extra_attributes as $attribute_name => $value ) {
|
|
|
|
if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) {
|
|
|
|
$attributes[ $attribute_name ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $attributes ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$normalized_attributes = array();
|
|
|
|
foreach ( $attributes as $key => $value ) {
|
|
|
|
$normalized_attributes[] = $key . '="' . esc_attr( $value ) . '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode( ' ', $normalized_attributes );
|
|
|
|
}
|