wpseek.com
A WordPress-centric search engine for devs and theme authors
wp_render_background_support is private and should not be used in themes or plugins directly.
wp_render_background_support › WordPress Function
Since6.4.0
Deprecatedn/a
› wp_render_background_support ( $block_content, $block )
| Access: |
|
| Parameters: (2) |
|
| Returns: |
|
| Defined at: |
|
| Codex: | |
| Change Log: |
|
Renders the background styles to the block wrapper.
This block support uses therender_block hook to ensure that
it is also applied to non-server-rendered blocks.Source
function wp_render_background_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
if ( ! $block_type ) {
return $block_content;
}
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
$has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
$has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false );
$has_background_clip_support = block_has_support( $block_type, array( 'background', 'backgroundClip' ), false );
if (
( ! $has_background_image_support && ! $has_background_gradient_support && ! $has_background_clip_support ) ||
! isset( $block_attributes['style']['background'] )
) {
return $block_content;
}
// Check serialization skip for each feature individually.
$skip_background_image = ! $has_background_image_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' );
$skip_background_gradient = ! $has_background_gradient_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'gradient' );
$skip_background_clip = ! $has_background_clip_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundClip' );
if ( $skip_background_image && $skip_background_gradient && $skip_background_clip ) {
return $block_content;
}
// unset() fatals on a string, and the helper ignores one anyway.
if ( is_array( $block_attributes['style']['background'] ) ) {
if ( $skip_background_image ) {
unset(
$block_attributes['style']['background']['backgroundImage'],
$block_attributes['style']['background']['backgroundSize'],
$block_attributes['style']['background']['backgroundPosition'],
$block_attributes['style']['background']['backgroundRepeat'],
$block_attributes['style']['background']['backgroundAttachment']
);
}
if ( $skip_background_gradient ) {
unset( $block_attributes['style']['background']['gradient'] );
}
// Serialized values only, so a skipped clip cannot drop `has-background`.
if ( $skip_background_clip ) {
unset( $block_attributes['style']['background']['backgroundClip'] );
}
}
$styles = wp_get_background_classes_and_styles( $block_attributes );
if ( ! empty( $styles['style'] ) ) {
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );
if ( $tags->next_tag() ) {
$existing_style = $tags->get_attribute( 'style' );
if ( is_string( $existing_style ) && '' !== $existing_style ) {
$separator = str_ends_with( $existing_style, ';' ) ? '' : ';';
$updated_style = "{$existing_style}{$separator}{$styles['style']}";
} else {
$updated_style = $styles['style'];
}
$tags->set_attribute( 'style', $updated_style );
if ( ! empty( $styles['class'] ) ) {
$tags->add_class( $styles['class'] );
}
}
return $tags->get_updated_html();
}
return $block_content;
}