Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/wp-includes/block-supports/anchor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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( $block_type ) {
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );

if ( ! $has_anchor_support ) {
return;
}

if ( ! $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 $block_attributes Block attributes.
*
* @return array Block anchor id.
*/
function wp_apply_anchor_support( $block_type, $block_attributes ) {
if ( ! $block_attributes ) {
return array();
}

$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
if ( ! $has_anchor_support ) {
return array();
}

$has_anchor = array_key_exists( 'anchor', $block_attributes );
if ( ! $has_anchor || empty( $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',
)
);
1 change: 1 addition & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@
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';
Expand Down
85 changes: 85 additions & 0 deletions tests/phpunit/tests/block-supports/anchor..php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @group block-supports
*
* @covers ::wp_apply_anchor_support
*/
class Tests_Block_Supports_Anchor extends WP_UnitTestCase {
/**
* @var string|null
*/
private $test_block_name;

public function set_up() {
parent::set_up();
$this->test_block_name = null;
}

public function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
parent::tear_down();
}

/**
* Registers a new block for testing anchor support.
*
* @param string $block_name Name for the test block.
* @param array $supports Array defining block support configuration.
*
* @return WP_Block_Type The block type for the newly registered test block.
*/
private function register_anchor_block_with_support( $block_name, $supports = array() ) {
$this->test_block_name = $block_name;
register_block_type(
$this->test_block_name,
array(
'api_version' => 3,
'supports' => $supports,
)
);
$registry = WP_Block_Type_Registry::get_instance();

return $registry->get_registered( $this->test_block_name );
}

/**
* Tests that anchor block support works as expected.
*
* @dataProvider data_anchor_block_support
*
* @param boolean|array $support Anchor block support configuration.
* @param string $value Anchor value for attribute object.
* @param array $expected Expected anchor block support output.
*/
public function test_wp_apply_anchor_support( $support, $value, $expected ) {
$block_type = self::register_anchor_block_with_support(
'test/anchor-block',
array( 'anchor' => $support )
);
$block_attrs = array( 'anchor' => $value );
$actual = wp_apply_anchor_support( $block_type, $block_attrs );

$this->assertSame( $expected, $actual );
}

/**
* Data provider.
*
* @return array
*/
public function data_anchor_block_support() {
return array(
'anchor id attribute is applied' => array(
'support' => true,
'value' => 'my-anchor',
'expected' => array( 'id' => 'my-anchor' ),
),
'anchor id attribute is not applied if block does not support it' => array(
'support' => false,
'value' => 'my-anchor',
'expected' => array(),
),
);
}
}
Loading