| 1 |
<?php |
| 2 |
/** |
| 3 |
* Anchor block support flag. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the anchor block attribute for block types that support it. |
| 10 |
* |
| 11 |
* @param WP_Block_Type $block_type Block Type. |
| 12 |
*/ |
| 13 |
function gutenberg_register_anchor_support( $block_type ) { |
| 14 |
if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! isset( $block_type->attributes ) ) { |
| 19 |
$block_type->attributes = array(); |
| 20 |
} |
| 21 |
|
| 22 |
if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) { |
| 23 |
$block_type->attributes['anchor'] = array( |
| 24 |
'type' => 'string', |
| 25 |
); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add the anchor id to the output. |
| 31 |
* |
| 32 |
* @param WP_Block_Type $block_type Block Type. |
| 33 |
* @param array<string, mixed> $block_attributes Block attributes. |
| 34 |
* @return array<string, string> Attributes with block anchor id. |
| 35 |
*/ |
| 36 |
function gutenberg_apply_anchor_support( $block_type, $block_attributes ) { |
| 37 |
if ( empty( $block_attributes ) ) { |
| 38 |
return array(); |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) { |
| 42 |
return array(); |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! isset( $block_attributes['anchor'] ) || ! is_string( $block_attributes['anchor'] ) || '' === $block_attributes['anchor'] ) { |
| 46 |
return array(); |
| 47 |
} |
| 48 |
|
| 49 |
return array( 'id' => $block_attributes['anchor'] ); |
| 50 |
} |
| 51 |
|
| 52 |
// Register the block support. |
| 53 |
WP_Block_Supports::get_instance()->register( |
| 54 |
'anchor', |
| 55 |
array( |
| 56 |
'register_attribute' => 'gutenberg_register_anchor_support', |
| 57 |
'apply' => 'gutenberg_apply_anchor_support', |
| 58 |
) |
| 59 |
); |
| 60 |
|