Edit File by line
/home/zeestwma/ceyloniy.../wp-inclu.../block-su...
File: background.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Background block support flag.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @since 6.4.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Registers the style block attribute for block types that support it.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 6.4.0
[11] Fix | Delete
* @access private
[12] Fix | Delete
*
[13] Fix | Delete
* @param WP_Block_Type $block_type Block Type.
[14] Fix | Delete
*/
[15] Fix | Delete
function wp_register_background_support( $block_type ) {
[16] Fix | Delete
// Setup attributes and styles within that if needed.
[17] Fix | Delete
if ( ! $block_type->attributes ) {
[18] Fix | Delete
$block_type->attributes = array();
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
// Check for existing style attribute definition e.g. from block.json.
[22] Fix | Delete
if ( array_key_exists( 'style', $block_type->attributes ) ) {
[23] Fix | Delete
return;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
$has_background_support = block_has_support( $block_type, array( 'background' ), false );
[27] Fix | Delete
[28] Fix | Delete
if ( $has_background_support ) {
[29] Fix | Delete
$block_type->attributes['style'] = array(
[30] Fix | Delete
'type' => 'object',
[31] Fix | Delete
);
[32] Fix | Delete
}
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Renders the background styles to the block wrapper.
[37] Fix | Delete
* This block support uses the `render_block` hook to ensure that
[38] Fix | Delete
* it is also applied to non-server-rendered blocks.
[39] Fix | Delete
*
[40] Fix | Delete
* @since 6.4.0
[41] Fix | Delete
* @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
[42] Fix | Delete
* @since 6.6.0 Removed requirement for `backgroundImage.source`. A file/url is the default.
[43] Fix | Delete
* @since 6.7.0 Added support for `backgroundAttachment` output.
[44] Fix | Delete
*
[45] Fix | Delete
* @access private
[46] Fix | Delete
*
[47] Fix | Delete
* @param string $block_content Rendered block content.
[48] Fix | Delete
* @param array $block Block object.
[49] Fix | Delete
* @return string Filtered block content.
[50] Fix | Delete
*/
[51] Fix | Delete
function wp_render_background_support( $block_content, $block ) {
[52] Fix | Delete
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
[53] Fix | Delete
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
[54] Fix | Delete
$has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
[55] Fix | Delete
[56] Fix | Delete
if (
[57] Fix | Delete
! $has_background_image_support ||
[58] Fix | Delete
wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ) ||
[59] Fix | Delete
! isset( $block_attributes['style']['background'] )
[60] Fix | Delete
) {
[61] Fix | Delete
return $block_content;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
$background_styles = array();
[65] Fix | Delete
$background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null;
[66] Fix | Delete
$background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null;
[67] Fix | Delete
$background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null;
[68] Fix | Delete
$background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null;
[69] Fix | Delete
$background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null;
[70] Fix | Delete
[71] Fix | Delete
if ( ! empty( $background_styles['backgroundImage'] ) ) {
[72] Fix | Delete
$background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
[73] Fix | Delete
[74] Fix | Delete
// If the background size is set to `contain` and no position is set, set the position to `center`.
[75] Fix | Delete
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
[76] Fix | Delete
$background_styles['backgroundPosition'] = '50% 50%';
[77] Fix | Delete
}
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) );
[81] Fix | Delete
[82] Fix | Delete
if ( ! empty( $styles['css'] ) ) {
[83] Fix | Delete
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
[84] Fix | Delete
$tags = new WP_HTML_Tag_Processor( $block_content );
[85] Fix | Delete
[86] Fix | Delete
if ( $tags->next_tag() ) {
[87] Fix | Delete
$existing_style = $tags->get_attribute( 'style' );
[88] Fix | Delete
if ( is_string( $existing_style ) && '' !== $existing_style ) {
[89] Fix | Delete
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
[90] Fix | Delete
$updated_style = "{$existing_style}{$separator}{$styles['css']}";
[91] Fix | Delete
} else {
[92] Fix | Delete
$updated_style = $styles['css'];
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$tags->set_attribute( 'style', $updated_style );
[96] Fix | Delete
$tags->add_class( 'has-background' );
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
return $tags->get_updated_html();
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
return $block_content;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
// Register the block support.
[106] Fix | Delete
WP_Block_Supports::get_instance()->register(
[107] Fix | Delete
'background',
[108] Fix | Delete
array(
[109] Fix | Delete
'register_attribute' => 'wp_register_background_support',
[110] Fix | Delete
)
[111] Fix | Delete
);
[112] Fix | Delete
[113] Fix | Delete
add_filter( 'render_block', 'wp_render_background_support', 10, 2 );
[114] Fix | Delete
[115] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function