diff --git a/lib/block-supports/anchor.php b/lib/block-supports/anchor.php new file mode 100644 index 00000000000000..e03520440f01f6 --- /dev/null +++ b/lib/block-supports/anchor.php @@ -0,0 +1,50 @@ +supports, array( 'anchor' ), true ); + + if ( $has_anchor_support ) { + 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 to the output. + * + * @param array $attributes Comprehensive list of attributes to be applied. + * @param array $block_attributes Block attributes. + * @param WP_Block_Type $block_type Block Type. + * + * @return array Block anchor. + */ +function gutenberg_apply_anchor_support( $attributes, $block_attributes, $block_type ) { + $has_anchor_support = gutenberg_experimental_get( $block_type->supports, array( 'anchor' ), true ); + if ( $has_anchor_support ) { + $has_anchor = array_key_exists( 'anchor', $block_attributes ); + + if ( $has_anchor ) { + $attributes['anchor'] = $block_attributes['anchor']; + } + } + + return $attributes; +} diff --git a/lib/block-supports/index.php b/lib/block-supports/index.php index 4d91d6f9cc77e5..04a1594cbaa574 100644 --- a/lib/block-supports/index.php +++ b/lib/block-supports/index.php @@ -18,6 +18,7 @@ function gutenberg_register_block_supports() { gutenberg_register_colors_support( $block_type ); gutenberg_register_typography_support( $block_type ); gutenberg_register_custom_classname_support( $block_type ); + gutenberg_register_anchor_support( $block_type ); } } @@ -47,6 +48,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) { $attributes = gutenberg_apply_typography_support( $attributes, $block['attrs'], $block_type ); $attributes = gutenberg_apply_alignment_support( $attributes, $block['attrs'], $block_type ); $attributes = gutenberg_apply_custom_classname_support( $attributes, $block['attrs'], $block_type ); + $attributes = gutenberg_apply_anchor_support( $attributes, $block['attrs'], $block_type ); if ( ! count( $attributes ) ) { return $block_content; @@ -81,7 +83,7 @@ function gutenberg_apply_block_supports( $block_content, $block ) { $styles_to_add = array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array(); $new_styles = array_unique( array_map( 'gutenberg_normalize_css_rule', array_filter( array_merge( $current_styles, $styles_to_add ) ) ) ); - // Apply new styles and classes. + // Apply new styles, classes and anchor. if ( ! empty( $new_classes ) ) { $block_root->setAttribute( 'class', esc_attr( implode( ' ', $new_classes ) ) ); } @@ -90,6 +92,10 @@ function gutenberg_apply_block_supports( $block_content, $block ) { $block_root->setAttribute( 'style', esc_attr( implode( '; ', $new_styles ) . ';' ) ); } + if ( ! empty( $attributes['anchor'] ) ) { + $block_root->setAttribute( 'id', esc_attr( $attributes['anchor'] ) ); + } + return str_replace( array( $wrapper_left, $wrapper_right ), '', $dom->saveHtml() ); } add_filter( 'render_block', 'gutenberg_apply_block_supports', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index cf62c6ec05008a..06ca4cec7492d2 100644 --- a/lib/load.php +++ b/lib/load.php @@ -119,3 +119,4 @@ function gutenberg_is_experiment_enabled( $name ) { require dirname( __FILE__ ) . '/block-supports/typography.php'; require dirname( __FILE__ ) . '/block-supports/custom-classname.php'; require dirname( __FILE__ ) . '/block-supports/generated-classname.php'; +require dirname( __FILE__ ) . '/block-supports/anchor.php'; diff --git a/packages/block-editor/src/hooks/anchor.js b/packages/block-editor/src/hooks/anchor.js index a5e5bcc9febf66..c0a484eb57c5f9 100644 --- a/packages/block-editor/src/hooks/anchor.js +++ b/packages/block-editor/src/hooks/anchor.js @@ -39,14 +39,20 @@ export function addAttribute( settings ) { } if ( hasBlockSupport( settings, 'anchor' ) ) { // Gracefully handle if settings.attributes is undefined. - settings.attributes = { - ...settings.attributes, - anchor: { + let anchor = { + type: 'string', + }; + if ( settings.save.length ) { + anchor = { type: 'string', source: 'attribute', attribute: 'id', selector: '*', - }, + }; + } + settings.attributes = { + ...settings.attributes, + anchor, }; } diff --git a/packages/block-library/src/archives/block.json b/packages/block-library/src/archives/block.json index 14a9451e1b0d89..958f95111f17e0 100644 --- a/packages/block-library/src/archives/block.json +++ b/packages/block-library/src/archives/block.json @@ -13,6 +13,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/calendar/block.json b/packages/block-library/src/calendar/block.json index 698140a33354ba..2b9287d0d49d17 100644 --- a/packages/block-library/src/calendar/block.json +++ b/packages/block-library/src/calendar/block.json @@ -10,6 +10,7 @@ } }, "supports": { - "align": true + "align": true, + "anchor": true } } diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index d4fee5e0649515..72572ec2641c98 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -17,6 +17,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/latest-comments/block.json b/packages/block-library/src/latest-comments/block.json index dd208ee7266979..5d56faef51232a 100644 --- a/packages/block-library/src/latest-comments/block.json +++ b/packages/block-library/src/latest-comments/block.json @@ -23,6 +23,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index 0eef67f6d8eb20..460285218af70d 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -82,6 +82,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/rss/block.json b/packages/block-library/src/rss/block.json index 88ad78c5963852..26d6934f2fbee7 100644 --- a/packages/block-library/src/rss/block.json +++ b/packages/block-library/src/rss/block.json @@ -37,6 +37,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/search/block.json b/packages/block-library/src/search/block.json index 0a2f217aca34b7..12d9af989066bd 100644 --- a/packages/block-library/src/search/block.json +++ b/packages/block-library/src/search/block.json @@ -15,6 +15,7 @@ }, "supports": { "align": true, + "anchor": true, "html": false } } diff --git a/packages/block-library/src/tag-cloud/block.json b/packages/block-library/src/tag-cloud/block.json index 7116bb43ddf16a..3c6e3f8dea897f 100644 --- a/packages/block-library/src/tag-cloud/block.json +++ b/packages/block-library/src/tag-cloud/block.json @@ -13,6 +13,7 @@ }, "supports": { "html": false, - "align": true + "align": true, + "anchor": true } }