author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
18 | 1 |
<?php |
2 |
/** |
|
3 |
* Spacing block support flag. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4 |
* |
19 | 5 |
* For backwards compatibility, this remains separate to the dimensions.php |
6 |
* block support despite both belonging under a single panel in the editor. |
|
18 | 7 |
* |
8 |
* @package WordPress |
|
9 |
* @since 5.8.0 |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* Registers the style block attribute for block types that support it. |
|
14 |
* |
|
15 |
* @since 5.8.0 |
|
16 |
* @access private |
|
17 |
* |
|
18 |
* @param WP_Block_Type $block_type Block Type. |
|
19 |
*/ |
|
20 |
function wp_register_spacing_support( $block_type ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
21 |
$has_spacing_support = block_has_support( $block_type, 'spacing', false ); |
18 | 22 |
|
23 |
// Setup attributes and styles within that if needed. |
|
24 |
if ( ! $block_type->attributes ) { |
|
25 |
$block_type->attributes = array(); |
|
26 |
} |
|
27 |
||
28 |
if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
|
29 |
$block_type->attributes['style'] = array( |
|
30 |
'type' => 'object', |
|
31 |
); |
|
32 |
} |
|
33 |
} |
|
34 |
||
35 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
* Adds CSS classes for block spacing to the incoming attributes array. |
18 | 37 |
* This will be applied to the block markup in the front-end. |
38 |
* |
|
39 |
* @since 5.8.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
40 |
* @since 6.1.0 Implemented the style engine to generate CSS and classnames. |
18 | 41 |
* @access private |
42 |
* |
|
43 |
* @param WP_Block_Type $block_type Block Type. |
|
44 |
* @param array $block_attributes Block attributes. |
|
45 |
* @return array Block spacing CSS classes and inline styles. |
|
46 |
*/ |
|
47 |
function wp_apply_spacing_support( $block_type, $block_attributes ) { |
|
19 | 48 |
if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { |
49 |
return array(); |
|
50 |
} |
|
51 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
$attributes = array(); |
19 | 53 |
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); |
54 |
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
18 | 56 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
57 |
if ( ! $block_styles ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
58 |
return $attributes; |
18 | 59 |
} |
60 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
61 |
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
62 |
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
63 |
$spacing_block_styles = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
64 |
'padding' => null, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
65 |
'margin' => null, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
66 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
67 |
if ( $has_padding_support && ! $skip_padding ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
$spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
69 |
} |
19 | 70 |
if ( $has_margin_support && ! $skip_margin ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
$spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
$styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
74 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
75 |
if ( ! empty( $styles['css'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
$attributes['style'] = $styles['css']; |
18 | 77 |
} |
78 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
return $attributes; |
18 | 80 |
} |
81 |
||
82 |
// Register the block support. |
|
83 |
WP_Block_Supports::get_instance()->register( |
|
84 |
'spacing', |
|
85 |
array( |
|
86 |
'register_attribute' => 'wp_register_spacing_support', |
|
87 |
'apply' => 'wp_apply_spacing_support', |
|
88 |
) |
|
89 |
); |