wp/wp-includes/block-supports/shadow.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Shadow block support flag.
       
     4  *
       
     5  * @package WordPress
       
     6  * @since 6.3.0
       
     7  */
       
     8 
       
     9 /**
       
    10  * Registers the style and shadow block attributes for block types that support it.
       
    11  *
       
    12  * @since 6.3.0
       
    13  * @access private
       
    14  *
       
    15  * @param WP_Block_Type $block_type Block Type.
       
    16  */
       
    17 function wp_register_shadow_support( $block_type ) {
       
    18 	$has_shadow_support = block_has_support( $block_type, 'shadow', false );
       
    19 
       
    20 	if ( ! $has_shadow_support ) {
       
    21 		return;
       
    22 	}
       
    23 
       
    24 	if ( ! $block_type->attributes ) {
       
    25 		$block_type->attributes = array();
       
    26 	}
       
    27 
       
    28 	if ( array_key_exists( 'style', $block_type->attributes ) ) {
       
    29 		$block_type->attributes['style'] = array(
       
    30 			'type' => 'object',
       
    31 		);
       
    32 	}
       
    33 
       
    34 	if ( array_key_exists( 'shadow', $block_type->attributes ) ) {
       
    35 		$block_type->attributes['shadow'] = array(
       
    36 			'type' => 'string',
       
    37 		);
       
    38 	}
       
    39 }
       
    40 
       
    41 /**
       
    42  * Add CSS classes and inline styles for shadow features to the incoming attributes array.
       
    43  * This will be applied to the block markup in the front-end.
       
    44  *
       
    45  * @since 6.3.0
       
    46  * @since 6.6.0 Return early if __experimentalSkipSerialization is true.
       
    47  * @access private
       
    48  *
       
    49  * @param  WP_Block_Type $block_type       Block type.
       
    50  * @param  array         $block_attributes Block attributes.
       
    51  * @return array Shadow CSS classes and inline styles.
       
    52  */
       
    53 function wp_apply_shadow_support( $block_type, $block_attributes ) {
       
    54 	$has_shadow_support = block_has_support( $block_type, 'shadow', false );
       
    55 
       
    56 	if (
       
    57 		! $has_shadow_support ||
       
    58 		wp_should_skip_block_supports_serialization( $block_type, 'shadow' )
       
    59 	) {
       
    60 		return array();
       
    61 	}
       
    62 
       
    63 	$shadow_block_styles = array();
       
    64 
       
    65 	$custom_shadow                 = $block_attributes['style']['shadow'] ?? null;
       
    66 	$shadow_block_styles['shadow'] = $custom_shadow;
       
    67 
       
    68 	$attributes = array();
       
    69 	$styles     = wp_style_engine_get_styles( $shadow_block_styles );
       
    70 
       
    71 	if ( ! empty( $styles['css'] ) ) {
       
    72 		$attributes['style'] = $styles['css'];
       
    73 	}
       
    74 
       
    75 	return $attributes;
       
    76 }
       
    77 
       
    78 // Register the block support.
       
    79 WP_Block_Supports::get_instance()->register(
       
    80 	'shadow',
       
    81 	array(
       
    82 		'register_attribute' => 'wp_register_shadow_support',
       
    83 		'apply'              => 'wp_apply_shadow_support',
       
    84 	)
       
    85 );