2021-05-21 06:14:23 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Elements styles block support.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Gets the elements class names.
|
2022-04-25 12:37:08 -04:00
|
|
|
*
|
|
|
|
* @since 6.0.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param array $block Block object.
|
2022-11-24 17:52:11 -05:00
|
|
|
* @return string The unique class name.
|
2022-04-25 12:37:08 -04:00
|
|
|
*/
|
|
|
|
function wp_get_elements_class_name( $block ) {
|
|
|
|
return 'wp-elements-' . md5( serialize( $block ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Updates the block content with elements class names.
|
2021-05-21 06:14:23 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2023-09-18 02:27:19 -04:00
|
|
|
* @since 6.4.0 Added support for button and heading element styling.
|
2021-05-21 06:14:23 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2021-07-01 17:29:56 -04:00
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
2021-05-21 06:14:23 -04:00
|
|
|
*/
|
|
|
|
function wp_render_elements_support( $block_content, $block ) {
|
2023-10-09 13:07:22 -04:00
|
|
|
if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) {
|
2021-11-08 21:17:17 -05:00
|
|
|
return $block_content;
|
2022-04-05 08:08:02 -04:00
|
|
|
}
|
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
2023-10-11 11:15:30 -04:00
|
|
|
if ( ! $block_type ) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
2023-09-18 02:27:19 -04:00
|
|
|
|
|
|
|
$element_color_properties = array(
|
|
|
|
'button' => array(
|
|
|
|
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ),
|
|
|
|
'paths' => array(
|
2023-10-09 13:07:22 -04:00
|
|
|
array( 'button', 'color', 'text' ),
|
|
|
|
array( 'button', 'color', 'background' ),
|
|
|
|
array( 'button', 'color', 'gradient' ),
|
2023-09-18 02:27:19 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'link' => array(
|
|
|
|
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ),
|
|
|
|
'paths' => array(
|
2023-10-09 13:07:22 -04:00
|
|
|
array( 'link', 'color', 'text' ),
|
|
|
|
array( 'link', ':hover', 'color', 'text' ),
|
2023-09-18 02:27:19 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'heading' => array(
|
|
|
|
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ),
|
|
|
|
'paths' => array(
|
2023-10-09 13:07:22 -04:00
|
|
|
array( 'heading', 'color', 'text' ),
|
|
|
|
array( 'heading', 'color', 'background' ),
|
|
|
|
array( 'heading', 'color', 'gradient' ),
|
|
|
|
array( 'h1', 'color', 'text' ),
|
|
|
|
array( 'h1', 'color', 'background' ),
|
|
|
|
array( 'h1', 'color', 'gradient' ),
|
|
|
|
array( 'h2', 'color', 'text' ),
|
|
|
|
array( 'h2', 'color', 'background' ),
|
|
|
|
array( 'h2', 'color', 'gradient' ),
|
|
|
|
array( 'h3', 'color', 'text' ),
|
|
|
|
array( 'h3', 'color', 'background' ),
|
|
|
|
array( 'h3', 'color', 'gradient' ),
|
|
|
|
array( 'h4', 'color', 'text' ),
|
|
|
|
array( 'h4', 'color', 'background' ),
|
|
|
|
array( 'h4', 'color', 'gradient' ),
|
|
|
|
array( 'h5', 'color', 'text' ),
|
|
|
|
array( 'h5', 'color', 'background' ),
|
|
|
|
array( 'h5', 'color', 'gradient' ),
|
|
|
|
array( 'h6', 'color', 'text' ),
|
|
|
|
array( 'h6', 'color', 'background' ),
|
|
|
|
array( 'h6', 'color', 'gradient' ),
|
2023-09-18 02:27:19 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$skip_all_element_color_serialization = $element_color_properties['button']['skip'] &&
|
|
|
|
$element_color_properties['link']['skip'] &&
|
|
|
|
$element_color_properties['heading']['skip'];
|
2022-04-05 08:08:02 -04:00
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
if ( $skip_all_element_color_serialization ) {
|
2022-04-05 08:08:02 -04:00
|
|
|
return $block_content;
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 13:07:22 -04:00
|
|
|
$elements_style_attributes = $block['attrs']['style']['elements'];
|
2023-09-18 02:27:19 -04:00
|
|
|
|
|
|
|
foreach ( $element_color_properties as $element_config ) {
|
|
|
|
if ( $element_config['skip'] ) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-21 06:14:23 -04:00
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
foreach ( $element_config['paths'] as $path ) {
|
2023-10-09 13:07:22 -04:00
|
|
|
if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) {
|
|
|
|
/*
|
|
|
|
* It only takes a single custom attribute to require that the custom
|
|
|
|
* class name be added to the block, so once one is found there's no
|
|
|
|
* need to continue looking for others.
|
|
|
|
*
|
|
|
|
* As is done with the layout hook, this code assumes that the block
|
|
|
|
* contains a single wrapper and that it's the first element in the
|
|
|
|
* rendered output. That first element, if it exists, gets the class.
|
|
|
|
*/
|
|
|
|
$tags = new WP_HTML_Tag_Processor( $block_content );
|
|
|
|
if ( $tags->next_tag() ) {
|
|
|
|
$tags->add_class( wp_get_elements_class_name( $block ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags->get_updated_html();
|
2023-09-18 02:27:19 -04:00
|
|
|
}
|
|
|
|
}
|
2023-06-25 21:15:21 -04:00
|
|
|
}
|
|
|
|
|
2023-10-09 13:07:22 -04:00
|
|
|
// If no custom attributes were found then there's nothing to modify.
|
|
|
|
return $block_content;
|
2022-04-25 12:37:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Renders the elements stylesheet.
|
2022-04-25 12:37:08 -04:00
|
|
|
*
|
|
|
|
* In the case of nested blocks we want the parent element styles to be rendered before their descendants.
|
|
|
|
* This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
|
|
|
|
* we want the descendant style to take priority, and this is done by loading it after, in DOM order.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
2022-09-19 16:14:10 -04:00
|
|
|
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
2022-04-25 12:37:08 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2022-11-24 17:52:11 -05:00
|
|
|
* @param string|null $pre_render The pre-rendered content. Default null.
|
|
|
|
* @param array $block The block being rendered.
|
2022-04-25 12:37:08 -04:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
function wp_render_elements_support_styles( $pre_render, $block ) {
|
2022-09-19 16:14:10 -04:00
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
|
|
|
$element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
|
2022-04-25 12:37:08 -04:00
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
if ( ! $element_block_styles ) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-25 12:37:08 -04:00
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
|
|
|
$skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' );
|
|
|
|
$skip_button_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' );
|
|
|
|
$skips_all_element_color_serialization = $skip_link_color_serialization &&
|
|
|
|
$skip_heading_color_serialization &&
|
|
|
|
$skip_button_color_serialization;
|
|
|
|
|
|
|
|
if ( $skips_all_element_color_serialization ) {
|
2022-09-19 16:14:10 -04:00
|
|
|
return null;
|
2022-04-25 12:37:08 -04:00
|
|
|
}
|
2023-09-18 02:27:19 -04:00
|
|
|
|
|
|
|
$class_name = wp_get_elements_class_name( $block );
|
|
|
|
|
|
|
|
$element_types = array(
|
|
|
|
'button' => array(
|
|
|
|
'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link",
|
|
|
|
'skip' => $skip_button_color_serialization,
|
|
|
|
),
|
|
|
|
'link' => array(
|
|
|
|
'selector' => ".$class_name a",
|
|
|
|
'hover_selector' => ".$class_name a:hover",
|
|
|
|
'skip' => $skip_link_color_serialization,
|
|
|
|
),
|
|
|
|
'heading' => array(
|
|
|
|
'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6",
|
|
|
|
'skip' => $skip_heading_color_serialization,
|
|
|
|
'elements' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ),
|
|
|
|
),
|
2022-09-19 16:14:10 -04:00
|
|
|
);
|
2021-05-21 06:14:23 -04:00
|
|
|
|
2023-09-18 02:27:19 -04:00
|
|
|
foreach ( $element_types as $element_type => $element_config ) {
|
|
|
|
if ( $element_config['skip'] ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Editor: Reduce the use of the `_wp_array_get()` function to improve performance.
`_wp_array_get()` is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.
This commit aims to further optimize its usage:
* In many cases, `_wp_array_get()` can be replaced with a much simpler and faster `isset()` check.
* The `isset()` function is capable of checking nested arrays, so `isset( $foo['a']['b']['c'] )` will return false even if `$foo['a']` is unset, without throwing any errors or warnings.
* When `_wp_array_get()` cannot be directly replaced with `isset()`, it would be good practice to wrap it in an `isset()` function so that `_wp_array_get()` only runs when it needs to.
Original PR from Gutenberg repository:
* [https://github.com/WordPress/gutenberg/pull/51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function]
Follow-up to [55851], [56382].
Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.
Built from https://develop.svn.wordpress.org/trunk@56709
git-svn-id: http://core.svn.wordpress.org/trunk@56221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 09:47:20 -04:00
|
|
|
$element_style_object = isset( $element_block_styles[ $element_type ] ) ? $element_block_styles[ $element_type ] : null;
|
2023-09-18 02:27:19 -04:00
|
|
|
|
|
|
|
// Process primary element type styles.
|
|
|
|
if ( $element_style_object ) {
|
|
|
|
wp_style_engine_get_styles(
|
|
|
|
$element_style_object,
|
|
|
|
array(
|
|
|
|
'selector' => $element_config['selector'],
|
|
|
|
'context' => 'block-supports',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $element_style_object[':hover'] ) ) {
|
|
|
|
wp_style_engine_get_styles(
|
|
|
|
$element_style_object[':hover'],
|
|
|
|
array(
|
|
|
|
'selector' => $element_config['hover_selector'],
|
|
|
|
'context' => 'block-supports',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process related elements e.g. h1-h6 for headings.
|
|
|
|
if ( isset( $element_config['elements'] ) ) {
|
|
|
|
foreach ( $element_config['elements'] as $element ) {
|
Editor: Reduce the use of the `_wp_array_get()` function to improve performance.
`_wp_array_get()` is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.
This commit aims to further optimize its usage:
* In many cases, `_wp_array_get()` can be replaced with a much simpler and faster `isset()` check.
* The `isset()` function is capable of checking nested arrays, so `isset( $foo['a']['b']['c'] )` will return false even if `$foo['a']` is unset, without throwing any errors or warnings.
* When `_wp_array_get()` cannot be directly replaced with `isset()`, it would be good practice to wrap it in an `isset()` function so that `_wp_array_get()` only runs when it needs to.
Original PR from Gutenberg repository:
* [https://github.com/WordPress/gutenberg/pull/51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function]
Follow-up to [55851], [56382].
Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.
Built from https://develop.svn.wordpress.org/trunk@56709
git-svn-id: http://core.svn.wordpress.org/trunk@56221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 09:47:20 -04:00
|
|
|
$element_style_object = isset( $element_block_styles[ $element ] )
|
|
|
|
? $element_block_styles[ $element ]
|
|
|
|
: null;
|
2023-09-18 02:27:19 -04:00
|
|
|
|
|
|
|
if ( $element_style_object ) {
|
|
|
|
wp_style_engine_get_styles(
|
|
|
|
$element_style_object,
|
|
|
|
array(
|
|
|
|
'selector' => ".$class_name $element",
|
|
|
|
'context' => 'block-supports',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-25 21:15:21 -04:00
|
|
|
}
|
|
|
|
|
2022-04-25 12:37:08 -04:00
|
|
|
return null;
|
2021-05-21 06:14:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|
2022-04-25 12:37:08 -04:00
|
|
|
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
|