Skip to content

Commit 5beef85

Browse files
committed
Block support: Add server-side processing for anchor
1 parent 3d9fde3 commit 5beef85

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Anchor block support flag.
4+
*
5+
* @package WordPress
6+
* @since 7.0.0
7+
*/
8+
9+
/**
10+
* Registers the anchor block attribute for block types that support it.
11+
*
12+
* @since 7.0.0
13+
* @access private
14+
*
15+
* @param WP_Block_Type $block_type Block Type.
16+
*/
17+
function wp_register_anchor_support( $block_type ) {
18+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
19+
20+
if ( ! $has_anchor_support ) {
21+
return;
22+
}
23+
24+
if ( ! $block_type->attributes ) {
25+
$block_type->attributes = array();
26+
}
27+
28+
if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
29+
$block_type->attributes['anchor'] = array(
30+
'type' => 'string',
31+
);
32+
}
33+
}
34+
35+
/**
36+
* Add the anchor id to the output.
37+
*
38+
* @since 7.0.0
39+
* @access private
40+
*
41+
* @param WP_Block_Type $block_type Block Type.
42+
* @param array $block_attributes Block attributes.
43+
*
44+
* @return array Block anchor id.
45+
*/
46+
function wp_apply_anchor_support( $block_type, $block_attributes ) {
47+
if ( ! $block_attributes ) {
48+
return array();
49+
}
50+
51+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
52+
if ( ! $has_anchor_support ) {
53+
return array();
54+
}
55+
56+
$has_anchor = array_key_exists( 'anchor', $block_attributes );
57+
if ( ! $has_anchor || empty( $block_attributes['anchor'] ) ) {
58+
return array();
59+
}
60+
61+
return array( 'id' => $block_attributes['anchor'] );
62+
}
63+
64+
// Register the block support.
65+
WP_Block_Supports::get_instance()->register(
66+
'anchor',
67+
array(
68+
'register_attribute' => 'wp_register_anchor_support',
69+
'apply' => 'wp_apply_anchor_support',
70+
)
71+
);

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@
404404
require ABSPATH . WPINC . '/block-supports/background.php';
405405
require ABSPATH . WPINC . '/block-supports/block-style-variations.php';
406406
require ABSPATH . WPINC . '/block-supports/aria-label.php';
407+
require ABSPATH . WPINC . '/block-supports/anchor.php';
407408
require ABSPATH . WPINC . '/block-supports/block-visibility.php';
408409
require ABSPATH . WPINC . '/style-engine.php';
409410
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @group block-supports
4+
*
5+
* @covers ::wp_apply_anchor_support
6+
*/
7+
class Tests_Block_Supports_Anchor extends WP_UnitTestCase {
8+
/**
9+
* @var string|null
10+
*/
11+
private $test_block_name;
12+
13+
public function set_up() {
14+
parent::set_up();
15+
$this->test_block_name = null;
16+
}
17+
18+
public function tear_down() {
19+
unregister_block_type( $this->test_block_name );
20+
$this->test_block_name = null;
21+
parent::tear_down();
22+
}
23+
24+
/**
25+
* Registers a new block for testing anchor support.
26+
*
27+
* @param string $block_name Name for the test block.
28+
* @param array $supports Array defining block support configuration.
29+
*
30+
* @return WP_Block_Type The block type for the newly registered test block.
31+
*/
32+
private function register_anchor_block_with_support( $block_name, $supports = array() ) {
33+
$this->test_block_name = $block_name;
34+
register_block_type(
35+
$this->test_block_name,
36+
array(
37+
'api_version' => 3,
38+
'supports' => $supports,
39+
)
40+
);
41+
$registry = WP_Block_Type_Registry::get_instance();
42+
43+
return $registry->get_registered( $this->test_block_name );
44+
}
45+
46+
/**
47+
* Tests that anchor block support works as expected.
48+
*
49+
* @dataProvider data_anchor_block_support
50+
*
51+
* @param boolean|array $support Anchor block support configuration.
52+
* @param string $value Anchor value for attribute object.
53+
* @param array $expected Expected anchor block support output.
54+
*/
55+
public function test_wp_apply_anchor_support( $support, $value, $expected ) {
56+
$block_type = self::register_anchor_block_with_support(
57+
'test/anchor-block',
58+
array( 'anchor' => $support )
59+
);
60+
$block_attrs = array( 'anchor' => $value );
61+
$actual = wp_apply_anchor_support( $block_type, $block_attrs );
62+
63+
$this->assertSame( $expected, $actual );
64+
}
65+
66+
/**
67+
* Data provider.
68+
*
69+
* @return array
70+
*/
71+
public function data_anchor_block_support() {
72+
return array(
73+
'anchor id attribute is applied' => array(
74+
'support' => true,
75+
'value' => 'my-anchor',
76+
'expected' => array( 'id' => 'my-anchor' ),
77+
),
78+
'anchor id attribute is not applied if block does not support it' => array(
79+
'support' => false,
80+
'value' => 'my-anchor',
81+
'expected' => array(),
82+
),
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)