Edit File by line
/home/zeestwma/richards.../wp-inclu.../block-su...
File: elements.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Elements styles block support.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @since 5.8.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Gets the elements class names.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 6.0.0
[11] Fix | Delete
* @access private
[12] Fix | Delete
*
[13] Fix | Delete
* @param array $block Block object.
[14] Fix | Delete
* @return string The unique class name.
[15] Fix | Delete
*/
[16] Fix | Delete
function wp_get_elements_class_name( $block ) {
[17] Fix | Delete
return 'wp-elements-' . md5( serialize( $block ) );
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Determines whether an elements class name should be added to the block.
[22] Fix | Delete
*
[23] Fix | Delete
* @since 6.6.0
[24] Fix | Delete
* @access private
[25] Fix | Delete
*
[26] Fix | Delete
* @param array $block Block object.
[27] Fix | Delete
* @param array $options Per element type options e.g. whether to skip serialization.
[28] Fix | Delete
* @return boolean Whether the block needs an elements class name.
[29] Fix | Delete
*/
[30] Fix | Delete
function wp_should_add_elements_class_name( $block, $options ) {
[31] Fix | Delete
if ( ! isset( $block['attrs']['style']['elements'] ) ) {
[32] Fix | Delete
return false;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
$element_color_properties = array(
[36] Fix | Delete
'button' => array(
[37] Fix | Delete
'skip' => isset( $options['button']['skip'] ) ? $options['button']['skip'] : false,
[38] Fix | Delete
'paths' => array(
[39] Fix | Delete
array( 'button', 'color', 'text' ),
[40] Fix | Delete
array( 'button', 'color', 'background' ),
[41] Fix | Delete
array( 'button', 'color', 'gradient' ),
[42] Fix | Delete
),
[43] Fix | Delete
),
[44] Fix | Delete
'link' => array(
[45] Fix | Delete
'skip' => isset( $options['link']['skip'] ) ? $options['link']['skip'] : false,
[46] Fix | Delete
'paths' => array(
[47] Fix | Delete
array( 'link', 'color', 'text' ),
[48] Fix | Delete
array( 'link', ':hover', 'color', 'text' ),
[49] Fix | Delete
),
[50] Fix | Delete
),
[51] Fix | Delete
'heading' => array(
[52] Fix | Delete
'skip' => isset( $options['heading']['skip'] ) ? $options['heading']['skip'] : false,
[53] Fix | Delete
'paths' => array(
[54] Fix | Delete
array( 'heading', 'color', 'text' ),
[55] Fix | Delete
array( 'heading', 'color', 'background' ),
[56] Fix | Delete
array( 'heading', 'color', 'gradient' ),
[57] Fix | Delete
array( 'h1', 'color', 'text' ),
[58] Fix | Delete
array( 'h1', 'color', 'background' ),
[59] Fix | Delete
array( 'h1', 'color', 'gradient' ),
[60] Fix | Delete
array( 'h2', 'color', 'text' ),
[61] Fix | Delete
array( 'h2', 'color', 'background' ),
[62] Fix | Delete
array( 'h2', 'color', 'gradient' ),
[63] Fix | Delete
array( 'h3', 'color', 'text' ),
[64] Fix | Delete
array( 'h3', 'color', 'background' ),
[65] Fix | Delete
array( 'h3', 'color', 'gradient' ),
[66] Fix | Delete
array( 'h4', 'color', 'text' ),
[67] Fix | Delete
array( 'h4', 'color', 'background' ),
[68] Fix | Delete
array( 'h4', 'color', 'gradient' ),
[69] Fix | Delete
array( 'h5', 'color', 'text' ),
[70] Fix | Delete
array( 'h5', 'color', 'background' ),
[71] Fix | Delete
array( 'h5', 'color', 'gradient' ),
[72] Fix | Delete
array( 'h6', 'color', 'text' ),
[73] Fix | Delete
array( 'h6', 'color', 'background' ),
[74] Fix | Delete
array( 'h6', 'color', 'gradient' ),
[75] Fix | Delete
),
[76] Fix | Delete
),
[77] Fix | Delete
);
[78] Fix | Delete
[79] Fix | Delete
$elements_style_attributes = $block['attrs']['style']['elements'];
[80] Fix | Delete
[81] Fix | Delete
foreach ( $element_color_properties as $element_config ) {
[82] Fix | Delete
if ( $element_config['skip'] ) {
[83] Fix | Delete
continue;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
foreach ( $element_config['paths'] as $path ) {
[87] Fix | Delete
if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) {
[88] Fix | Delete
return true;
[89] Fix | Delete
}
[90] Fix | Delete
}
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
return false;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
/**
[97] Fix | Delete
* Render the elements stylesheet and adds elements class name to block as required.
[98] Fix | Delete
*
[99] Fix | Delete
* In the case of nested blocks we want the parent element styles to be rendered before their descendants.
[100] Fix | Delete
* This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
[101] Fix | Delete
* we want the descendant style to take priority, and this is done by loading it after, in DOM order.
[102] Fix | Delete
*
[103] Fix | Delete
* @since 6.0.0
[104] Fix | Delete
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
[105] Fix | Delete
* @since 6.6.0 Element block support class and styles are generated via the `render_block_data` filter instead of `pre_render_block`.
[106] Fix | Delete
* @access private
[107] Fix | Delete
*
[108] Fix | Delete
* @param array $parsed_block The parsed block.
[109] Fix | Delete
* @return array The same parsed block with elements classname added if appropriate.
[110] Fix | Delete
*/
[111] Fix | Delete
function wp_render_elements_support_styles( $parsed_block ) {
[112] Fix | Delete
/*
[113] Fix | Delete
* The generation of element styles and classname were moved to the
[114] Fix | Delete
* `render_block_data` filter in 6.6.0 to avoid filtered attributes
[115] Fix | Delete
* breaking the application of the elements CSS class.
[116] Fix | Delete
*
[117] Fix | Delete
* @see https://github.com/WordPress/gutenberg/pull/59535
[118] Fix | Delete
*
[119] Fix | Delete
* The change in filter means, the argument types for this function
[120] Fix | Delete
* have changed and require deprecating.
[121] Fix | Delete
*/
[122] Fix | Delete
if ( is_string( $parsed_block ) ) {
[123] Fix | Delete
_deprecated_argument(
[124] Fix | Delete
__FUNCTION__,
[125] Fix | Delete
'6.6.0',
[126] Fix | Delete
__( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.' )
[127] Fix | Delete
);
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );
[131] Fix | Delete
$element_block_styles = isset( $parsed_block['attrs']['style']['elements'] ) ? $parsed_block['attrs']['style']['elements'] : null;
[132] Fix | Delete
[133] Fix | Delete
if ( ! $element_block_styles ) {
[134] Fix | Delete
return $parsed_block;
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
[138] Fix | Delete
$skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' );
[139] Fix | Delete
$skip_button_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' );
[140] Fix | Delete
$skips_all_element_color_serialization = $skip_link_color_serialization &&
[141] Fix | Delete
$skip_heading_color_serialization &&
[142] Fix | Delete
$skip_button_color_serialization;
[143] Fix | Delete
[144] Fix | Delete
if ( $skips_all_element_color_serialization ) {
[145] Fix | Delete
return $parsed_block;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
$options = array(
[149] Fix | Delete
'button' => array( 'skip' => $skip_button_color_serialization ),
[150] Fix | Delete
'link' => array( 'skip' => $skip_link_color_serialization ),
[151] Fix | Delete
'heading' => array( 'skip' => $skip_heading_color_serialization ),
[152] Fix | Delete
);
[153] Fix | Delete
[154] Fix | Delete
if ( ! wp_should_add_elements_class_name( $parsed_block, $options ) ) {
[155] Fix | Delete
return $parsed_block;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
$class_name = wp_get_elements_class_name( $parsed_block );
[159] Fix | Delete
$updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name;
[160] Fix | Delete
[161] Fix | Delete
_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );
[162] Fix | Delete
[163] Fix | Delete
// Generate element styles based on selector and store in style engine for enqueuing.
[164] Fix | Delete
$element_types = array(
[165] Fix | Delete
'button' => array(
[166] Fix | Delete
'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link",
[167] Fix | Delete
'skip' => $skip_button_color_serialization,
[168] Fix | Delete
),
[169] Fix | Delete
'link' => array(
[170] Fix | Delete
'selector' => ".$class_name a:where(:not(.wp-element-button))",
[171] Fix | Delete
'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover",
[172] Fix | Delete
'skip' => $skip_link_color_serialization,
[173] Fix | Delete
),
[174] Fix | Delete
'heading' => array(
[175] Fix | Delete
'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6",
[176] Fix | Delete
'skip' => $skip_heading_color_serialization,
[177] Fix | Delete
'elements' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ),
[178] Fix | Delete
),
[179] Fix | Delete
);
[180] Fix | Delete
[181] Fix | Delete
foreach ( $element_types as $element_type => $element_config ) {
[182] Fix | Delete
if ( $element_config['skip'] ) {
[183] Fix | Delete
continue;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$element_style_object = isset( $element_block_styles[ $element_type ] ) ? $element_block_styles[ $element_type ] : null;
[187] Fix | Delete
[188] Fix | Delete
// Process primary element type styles.
[189] Fix | Delete
if ( $element_style_object ) {
[190] Fix | Delete
wp_style_engine_get_styles(
[191] Fix | Delete
$element_style_object,
[192] Fix | Delete
array(
[193] Fix | Delete
'selector' => $element_config['selector'],
[194] Fix | Delete
'context' => 'block-supports',
[195] Fix | Delete
)
[196] Fix | Delete
);
[197] Fix | Delete
[198] Fix | Delete
if ( isset( $element_style_object[':hover'] ) ) {
[199] Fix | Delete
wp_style_engine_get_styles(
[200] Fix | Delete
$element_style_object[':hover'],
[201] Fix | Delete
array(
[202] Fix | Delete
'selector' => $element_config['hover_selector'],
[203] Fix | Delete
'context' => 'block-supports',
[204] Fix | Delete
)
[205] Fix | Delete
);
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Process related elements e.g. h1-h6 for headings.
[210] Fix | Delete
if ( isset( $element_config['elements'] ) ) {
[211] Fix | Delete
foreach ( $element_config['elements'] as $element ) {
[212] Fix | Delete
$element_style_object = isset( $element_block_styles[ $element ] )
[213] Fix | Delete
? $element_block_styles[ $element ]
[214] Fix | Delete
: null;
[215] Fix | Delete
[216] Fix | Delete
if ( $element_style_object ) {
[217] Fix | Delete
wp_style_engine_get_styles(
[218] Fix | Delete
$element_style_object,
[219] Fix | Delete
array(
[220] Fix | Delete
'selector' => ".$class_name $element",
[221] Fix | Delete
'context' => 'block-supports',
[222] Fix | Delete
)
[223] Fix | Delete
);
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
return $parsed_block;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Ensure the elements block support class name generated, and added to
[234] Fix | Delete
* block attributes, in the `render_block_data` filter gets applied to the
[235] Fix | Delete
* block's markup.
[236] Fix | Delete
*
[237] Fix | Delete
* @see wp_render_elements_support_styles
[238] Fix | Delete
* @since 6.6.0
[239] Fix | Delete
*
[240] Fix | Delete
* @param string $block_content Rendered block content.
[241] Fix | Delete
* @param array $block Block object.
[242] Fix | Delete
* @return string Filtered block content.
[243] Fix | Delete
*/
[244] Fix | Delete
function wp_render_elements_class_name( $block_content, $block ) {
[245] Fix | Delete
$class_string = $block['attrs']['className'] ?? '';
[246] Fix | Delete
preg_match( '/\bwp-elements-\S+\b/', $class_string, $matches );
[247] Fix | Delete
[248] Fix | Delete
if ( empty( $matches ) ) {
[249] Fix | Delete
return $block_content;
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
$tags = new WP_HTML_Tag_Processor( $block_content );
[253] Fix | Delete
[254] Fix | Delete
if ( $tags->next_tag() ) {
[255] Fix | Delete
$tags->add_class( $matches[0] );
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
return $tags->get_updated_html();
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
add_filter( 'render_block', 'wp_render_elements_class_name', 10, 2 );
[262] Fix | Delete
add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 );
[263] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function