Skip to content

Commit ade13f4

Browse files
committed
Block support: Add anchor support for dynamic blocks
1 parent 58eef62 commit ade13f4

File tree

73 files changed

+287
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+287
-70
lines changed

docs/reference-guides/block-api/block-supports.md

Lines changed: 1 addition & 1 deletion

docs/reference-guides/core-blocks.md

Lines changed: 68 additions & 68 deletions

lib/block-supports/anchor.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
15+
16+
if ( ! $has_anchor_support ) {
17+
return;
18+
}
19+
20+
if ( ! $block_type->attributes ) {
21+
$block_type->attributes = array();
22+
}
23+
24+
if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
25+
$block_type->attributes['anchor'] = array(
26+
'type' => 'string',
27+
);
28+
}
29+
}
30+
31+
/**
32+
* Add the anchor id to the output.
33+
*
34+
* @param WP_Block_Type $block_type Block Type.
35+
* @param array $block_attributes Block attributes.
36+
*
37+
* @return array Block anchor id.
38+
*/
39+
function gutenberg_apply_anchor_support( $block_type, $block_attributes ) {
40+
if ( ! $block_attributes ) {
41+
return array();
42+
}
43+
44+
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
45+
if ( ! $has_anchor_support ) {
46+
return array();
47+
}
48+
49+
$has_anchor = array_key_exists( 'anchor', $block_attributes );
50+
if ( ! $has_anchor || empty( $block_attributes['anchor'] ) ) {
51+
return array();
52+
}
53+
54+
return array( 'id' => $block_attributes['anchor'] );
55+
}
56+
57+
// Register the block support.
58+
WP_Block_Supports::get_instance()->register(
59+
'anchor',
60+
array(
61+
'register_attribute' => 'gutenberg_register_anchor_support',
62+
'apply' => 'gutenberg_apply_anchor_support',
63+
)
64+
);

lib/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ function gutenberg_is_experiment_enabled( $name ) {
186186
require __DIR__ . '/block-supports/background.php';
187187
require __DIR__ . '/block-supports/block-style-variations.php';
188188
require __DIR__ . '/block-supports/aria-label.php';
189+
require __DIR__ . '/block-supports/anchor.php';
189190
require __DIR__ . '/block-supports/block-visibility.php';
190191

191192
// Client-side media processing.

packages/block-library/src/archives/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
}
2626
},
2727
"supports": {
28+
"anchor": true,
2829
"align": true,
2930
"__experimentalBorder": {
3031
"radius": true,

packages/block-library/src/avatar/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"usesContext": [ "postType", "postId", "commentId" ],
2727
"supports": {
28+
"anchor": true,
2829
"html": false,
2930
"align": true,
3031
"alignWide": false,

packages/block-library/src/breadcrumbs/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"usesContext": [ "postId", "postType", "templateSlug" ],
3232
"supports": {
33+
"anchor": true,
3334
"html": false,
3435
"align": [ "wide", "full" ],
3536
"spacing": {

packages/block-library/src/calendar/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
}
1717
},
1818
"supports": {
19+
"anchor": true,
1920
"align": true,
2021
"html": false,
2122
"color": {

packages/block-library/src/categories/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"usesContext": [ "enhancedPagination" ],
4545
"supports": {
46+
"anchor": true,
4647
"align": true,
4748
"html": false,
4849
"spacing": {

packages/block-library/src/comment-author-name/block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"usesContext": [ "commentId" ],
2121
"supports": {
22+
"anchor": true,
2223
"html": false,
2324
"spacing": {
2425
"margin": true,

0 commit comments

Comments
 (0)