| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Background block support flag. 4 * 5 * @package WordPress 6 * @since 6.4.0 7 */ 8 9 /** 10 * Registers the style block attribute for block types that support it. 11 * 12 * @since 6.4.0 13 * @access private 14 * 15 * @param WP_Block_Type $block_type Block Type. 16 */ 17 function wp_register_background_support( $block_type ) { 18 // Setup attributes and styles within that if needed. 19 if ( ! $block_type->attributes ) { 20 $block_type->attributes = array(); 21 } 22 23 // Check for existing style attribute definition e.g. from block.json. 24 if ( array_key_exists( 'style', $block_type->attributes ) ) { 25 return; 26 } 27 28 $has_background_support = block_has_support( $block_type, array( 'background' ), false ); 29 30 if ( $has_background_support ) { 31 $block_type->attributes['style'] = array( 32 'type' => 'object', 33 ); 34 } 35 } 36 37 /** 38 * Renders the background styles to the block wrapper. 39 * This block support uses the `render_block` hook to ensure that 40 * it is also applied to non-server-rendered blocks. 41 * 42 * @since 6.4.0 43 * @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output. 44 * @since 6.6.0 Removed requirement for `backgroundImage.source`. A file/url is the default. 45 * @since 6.7.0 Added support for `backgroundAttachment` output. 46 * @since 7.1.0 Added support for `background.gradient` output. 47 * 48 * @access private 49 * 50 * @param string $block_content Rendered block content. 51 * @param array $block Block object. 52 * @return string Filtered block content. 53 */ 54 function wp_render_background_support( $block_content, $block ) { 55 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 56 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); 57 $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false ); 58 $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); 59 60 if ( 61 ( ! $has_background_image_support && ! $has_background_gradient_support ) || 62 ! isset( $block_attributes['style']['background'] ) 63 ) { 64 return $block_content; 65 } 66 67 // Check serialization skip for each feature individually. 68 $skip_background_image = ! $has_background_image_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ); 69 $skip_background_gradient = ! $has_background_gradient_support || wp_should_skip_block_supports_serialization( $block_type, 'background', 'gradient' ); 70 71 if ( $skip_background_image && $skip_background_gradient ) { 72 return $block_content; 73 } 74 75 $background_styles = array(); 76 77 if ( ! $skip_background_image ) { 78 $background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null; 79 $background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null; 80 $background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null; 81 $background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null; 82 $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null; 83 84 if ( ! empty( $background_styles['backgroundImage'] ) ) { 85 $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover'; 86 87 // If the background size is set to `contain` and no position is set, set the position to `center`. 88 if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) { 89 $background_styles['backgroundPosition'] = '50% 50%'; 90 } 91 } 92 } 93 94 if ( ! $skip_background_gradient ) { 95 $background_styles['gradient'] = $block_attributes['style']['background']['gradient'] ?? null; 96 } 97 98 $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) ); 99 100 if ( ! empty( $styles['css'] ) ) { 101 // Inject background styles to the first element, presuming it's the wrapper, if it exists. 102 $tags = new WP_HTML_Tag_Processor( $block_content ); 103 104 if ( $tags->next_tag() ) { 105 $existing_style = $tags->get_attribute( 'style' ); 106 if ( is_string( $existing_style ) && '' !== $existing_style ) { 107 $separator = str_ends_with( $existing_style, ';' ) ? '' : ';'; 108 $updated_style = "{$existing_style}{$separator}{$styles['css']}"; 109 } else { 110 $updated_style = $styles['css']; 111 } 112 113 $tags->set_attribute( 'style', $updated_style ); 114 $tags->add_class( 'has-background' ); 115 } 116 117 return $tags->get_updated_html(); 118 } 119 120 return $block_content; 121 } 122 123 // Register the block support. 124 WP_Block_Supports::get_instance()->register( 125 'background', 126 array( 127 'register_attribute' => 'wp_register_background_support', 128 ) 129 ); 130 131 add_filter( 'render_block', 'wp_render_background_support', 10, 2 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jul 25 08:20:20 2026 | Cross-referenced by PHPXref |