[ 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 * 47 * @access private 48 * 49 * @param string $block_content Rendered block content. 50 * @param array $block Block object. 51 * @return string Filtered block content. 52 */ 53 function wp_render_background_support( $block_content, $block ) { 54 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 55 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); 56 $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false ); 57 58 if ( 59 ! $has_background_image_support || 60 wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ) || 61 ! isset( $block_attributes['style']['background'] ) 62 ) { 63 return $block_content; 64 } 65 66 $background_styles = array(); 67 $background_styles['backgroundImage'] = $block_attributes['style']['background']['backgroundImage'] ?? null; 68 $background_styles['backgroundSize'] = $block_attributes['style']['background']['backgroundSize'] ?? null; 69 $background_styles['backgroundPosition'] = $block_attributes['style']['background']['backgroundPosition'] ?? null; 70 $background_styles['backgroundRepeat'] = $block_attributes['style']['background']['backgroundRepeat'] ?? null; 71 $background_styles['backgroundAttachment'] = $block_attributes['style']['background']['backgroundAttachment'] ?? null; 72 73 if ( ! empty( $background_styles['backgroundImage'] ) ) { 74 $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover'; 75 76 // If the background size is set to `contain` and no position is set, set the position to `center`. 77 if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) { 78 $background_styles['backgroundPosition'] = '50% 50%'; 79 } 80 } 81 82 $styles = wp_style_engine_get_styles( array( 'background' => $background_styles ) ); 83 84 if ( ! empty( $styles['css'] ) ) { 85 // Inject background styles to the first element, presuming it's the wrapper, if it exists. 86 $tags = new WP_HTML_Tag_Processor( $block_content ); 87 88 if ( $tags->next_tag() ) { 89 $existing_style = $tags->get_attribute( 'style' ); 90 $updated_style = ''; 91 92 if ( ! empty( $existing_style ) ) { 93 $updated_style = $existing_style; 94 if ( ! str_ends_with( $existing_style, ';' ) ) { 95 $updated_style .= ';'; 96 } 97 } 98 99 $updated_style .= $styles['css']; 100 $tags->set_attribute( 'style', $updated_style ); 101 $tags->add_class( 'has-background' ); 102 } 103 104 return $tags->get_updated_html(); 105 } 106 107 return $block_content; 108 } 109 110 // Register the block support. 111 WP_Block_Supports::get_instance()->register( 112 'background', 113 array( 114 'register_attribute' => 'wp_register_background_support', 115 ) 116 ); 117 118 add_filter( 'render_block', 'wp_render_background_support', 10, 2 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |