|
| 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, array( '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 | + * @access private |
| 47 | + * |
| 48 | + * @param WP_Block_Type $block_type Block type. |
| 49 | + * @param array $block_attributes Block attributes. |
| 50 | + * @return array Shadow CSS classes and inline styles. |
| 51 | + */ |
| 52 | +function wp_apply_shadow_support( $block_type, $block_attributes ) { |
| 53 | + $has_shadow_support = block_has_support( $block_type, array( 'shadow' ), false ); |
| 54 | + |
| 55 | + if ( ! $has_shadow_support ) { |
| 56 | + return array(); |
| 57 | + } |
| 58 | + |
| 59 | + $shadow_block_styles = array(); |
| 60 | + |
| 61 | + $preset_shadow = array_key_exists( 'shadow', $block_attributes ) ? "var:preset|shadow|{$block_attributes['shadow']}" : null; |
| 62 | + $custom_shadow = isset( $block_attributes['style']['shadow'] ) ? $block_attributes['style']['shadow'] : null; |
| 63 | + $shadow_block_styles['shadow'] = $preset_shadow ? $preset_shadow : $custom_shadow; |
| 64 | + |
| 65 | + $attributes = array(); |
| 66 | + $styles = wp_style_engine_get_styles( $shadow_block_styles ); |
| 67 | + |
| 68 | + if ( ! empty( $styles['css'] ) ) { |
| 69 | + $attributes['style'] = $styles['css']; |
| 70 | + } |
| 71 | + |
| 72 | + return $attributes; |
| 73 | +} |
| 74 | + |
| 75 | +// Register the block support. |
| 76 | +WP_Block_Supports::get_instance()->register( |
| 77 | + 'shadow', |
| 78 | + array( |
| 79 | + 'register_attribute' => 'wp_register_shadow_support', |
| 80 | + 'apply' => 'wp_apply_shadow_support', |
| 81 | + ) |
| 82 | +); |
0 commit comments