Block support: Add server-side processing for anchor.

Adds server-side registration for `anchor` block support and its required fields.

Props westonruter, wildworks.
Fixes #64449.
Built from https://develop.svn.wordpress.org/trunk@61437


git-svn-id: http://core.svn.wordpress.org/trunk@60749 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
wildworks
2026-01-05 06:31:29 +00:00
parent de5f29a04a
commit 4d2234f5e1
3 changed files with 68 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
<?php
/**
* Anchor block support flag.
*
* @package WordPress
* @since 7.0.0
*/
/**
* Registers the anchor block attribute for block types that support it.
*
* @since 7.0.0
* @access private
*
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_anchor_support( WP_Block_Type $block_type ) {
if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) {
return;
}
if ( ! isset( $block_type->attributes ) ) {
$block_type->attributes = array();
}
if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
);
}
}
/**
* Add the anchor id to the output.
*
* @since 7.0.0
* @access private
*
* @param WP_Block_Type $block_type Block Type.
* @param array<string, mixed> $block_attributes Block attributes.
* @return array<string, string> Attributes with block anchor id.
*/
function wp_apply_anchor_support( WP_Block_Type $block_type, array $block_attributes ): array {
if ( empty( $block_attributes ) ) {
return array();
}
if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) {
return array();
}
if ( ! isset( $block_attributes['anchor'] ) || ! is_string( $block_attributes['anchor'] ) || '' === $block_attributes['anchor'] ) {
return array();
}
return array( 'id' => $block_attributes['anchor'] );
}
// Register the block support.
WP_Block_Supports::get_instance()->register(
'anchor',
array(
'register_attribute' => 'wp_register_anchor_support',
'apply' => 'wp_apply_anchor_support',
)
);

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.0-alpha-61436';
$wp_version = '7.0-alpha-61437';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@@ -404,6 +404,7 @@ require ABSPATH . WPINC . '/block-supports/shadow.php';
require ABSPATH . WPINC . '/block-supports/background.php';
require ABSPATH . WPINC . '/block-supports/block-style-variations.php';
require ABSPATH . WPINC . '/block-supports/aria-label.php';
require ABSPATH . WPINC . '/block-supports/anchor.php';
require ABSPATH . WPINC . '/block-supports/block-visibility.php';
require ABSPATH . WPINC . '/style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';