From 61e231c2a5ce27992c47e3a5632fae70b00d8cda Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Thu, 7 Mar 2024 11:47:37 +0800 Subject: [PATCH 1/6] add tagName to attributes --- docs/reference-guides/core-blocks.md | 2 +- .../block-library/src/site-tagline/block.json | 4 +++ .../block-library/src/site-tagline/edit.js | 28 +++++++++++++++++-- .../block-library/src/site-tagline/index.php | 5 +++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 3ca3ffddaa9c7..f527408f63867 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -847,7 +847,7 @@ Describe in a few words what the site is about. The tagline can be used in searc - **Name:** core/site-tagline - **Category:** theme - **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** textAlign +- **Attributes:** tagName, textAlign ## Site Title diff --git a/packages/block-library/src/site-tagline/block.json b/packages/block-library/src/site-tagline/block.json index 2361be9ea3efc..d0605e3c0c21b 100644 --- a/packages/block-library/src/site-tagline/block.json +++ b/packages/block-library/src/site-tagline/block.json @@ -10,6 +10,10 @@ "attributes": { "textAlign": { "type": "string" + }, + "tagName": { + "type": "string", + "default": "p" } }, "example": {}, diff --git a/packages/block-library/src/site-tagline/edit.js b/packages/block-library/src/site-tagline/edit.js index 1ce13c6eb43c4..0b5b3c5bf6f7e 100644 --- a/packages/block-library/src/site-tagline/edit.js +++ b/packages/block-library/src/site-tagline/edit.js @@ -12,17 +12,19 @@ import { AlignmentControl, useBlockProps, BlockControls, + InspectorControls, RichText, } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; +import { SelectControl } from '@wordpress/components'; export default function SiteTaglineEdit( { attributes, setAttributes, insertBlocksAfter, } ) { - const { textAlign } = attributes; + const { textAlign, tagName: TagName } = attributes; const { canUserEdit, tagline } = useSelect( ( select ) => { const { canUser, getEntityRecord, getEditedEntityRecord } = select( coreStore ); @@ -58,7 +60,7 @@ export default function SiteTaglineEdit( { onChange={ setTagline } aria-label={ __( 'Site tagline text' ) } placeholder={ __( 'Write site taglineā€¦' ) } - tagName="p" + tagName={ TagName } value={ tagline } disableLineBreaks __unstableOnSplitAtEnd={ () => @@ -67,10 +69,30 @@ export default function SiteTaglineEdit( { { ...blockProps } /> ) : ( -

{ tagline || __( 'Site Tagline placeholder' ) }

+ + { tagline || __( 'Site Tagline placeholder' ) } + ); return ( <> + + )' ), value: 'p' }, + { label: '

', value: 'h1' }, + { label: '

', value: 'h2' }, + { label: '

', value: 'h3' }, + { label: '

', value: 'h4' }, + { label: '

', value: 'h5' }, + { label: '
', value: 'h6' }, + ] } + value={ TagName } + onChange={ ( newTagName ) => + setAttributes( { tagName: newTagName } ) + } + /> + diff --git a/packages/block-library/src/site-tagline/index.php b/packages/block-library/src/site-tagline/index.php index 75375e5bb1e79..8664dc0b55f21 100644 --- a/packages/block-library/src/site-tagline/index.php +++ b/packages/block-library/src/site-tagline/index.php @@ -17,11 +17,13 @@ function render_block_core_site_tagline( $attributes ) { if ( ! $site_tagline ) { return; } + $tag_name = empty( $attributes['tagName'] ) ? 'p' : $attributes['tagName']; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); return sprintf( - '

%2$s

', + '<%1$s %2$s>%3$s', + $tag_name, $wrapper_attributes, $site_tagline ); @@ -38,4 +40,5 @@ function register_block_core_site_tagline() { ) ); } + add_action( 'init', 'register_block_core_site_tagline' ); From 8187bf402478bc356d34cc4ef36adf7247ee9101 Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Thu, 7 Mar 2024 14:45:07 +0800 Subject: [PATCH 2/6] update fixture --- test/integration/fixtures/blocks/core__site-tagline.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/integration/fixtures/blocks/core__site-tagline.json b/test/integration/fixtures/blocks/core__site-tagline.json index 7e15e1e8bb75c..264211b5c28ff 100644 --- a/test/integration/fixtures/blocks/core__site-tagline.json +++ b/test/integration/fixtures/blocks/core__site-tagline.json @@ -2,7 +2,9 @@ { "name": "core/site-tagline", "isValid": true, - "attributes": {}, + "attributes": { + "tagName": "p" + }, "innerBlocks": [] } ] From d374617942b4d3fbaad936a45da0ab6dc740943e Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Tue, 12 Mar 2024 00:16:36 +0900 Subject: [PATCH 3/6] use HeadingLevelDropdown --- docs/reference-guides/core-blocks.md | 2 +- .../block-library/src/site-tagline/block.json | 6 ++-- .../block-library/src/site-tagline/edit.js | 31 +++++++------------ .../block-library/src/site-tagline/index.php | 7 ++++- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index f527408f63867..bc00fea57dcfa 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -847,7 +847,7 @@ Describe in a few words what the site is about. The tagline can be used in searc - **Name:** core/site-tagline - **Category:** theme - **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** tagName, textAlign +- **Attributes:** level, textAlign ## Site Title diff --git a/packages/block-library/src/site-tagline/block.json b/packages/block-library/src/site-tagline/block.json index d0605e3c0c21b..01ec727eb9053 100644 --- a/packages/block-library/src/site-tagline/block.json +++ b/packages/block-library/src/site-tagline/block.json @@ -11,9 +11,9 @@ "textAlign": { "type": "string" }, - "tagName": { - "type": "string", - "default": "p" + "level": { + "type": "number", + "default": 0 } }, "example": {}, diff --git a/packages/block-library/src/site-tagline/edit.js b/packages/block-library/src/site-tagline/edit.js index 0b5b3c5bf6f7e..e4ea535966044 100644 --- a/packages/block-library/src/site-tagline/edit.js +++ b/packages/block-library/src/site-tagline/edit.js @@ -12,19 +12,20 @@ import { AlignmentControl, useBlockProps, BlockControls, - InspectorControls, + HeadingLevelDropdown, RichText, } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; -import { SelectControl } from '@wordpress/components'; + +const HEADING_LEVELS = [ 0, 1, 2, 3, 4, 5, 6 ]; export default function SiteTaglineEdit( { attributes, setAttributes, insertBlocksAfter, } ) { - const { textAlign, tagName: TagName } = attributes; + const { textAlign, level } = attributes; const { canUserEdit, tagline } = useSelect( ( select ) => { const { canUser, getEntityRecord, getEditedEntityRecord } = select( coreStore ); @@ -40,6 +41,7 @@ export default function SiteTaglineEdit( { }; }, [] ); + const TagName = level === 0 ? 'p' : `h${ level }`; const { editEntityRecord } = useDispatch( coreStore ); function setTagline( newTagline ) { @@ -75,25 +77,14 @@ export default function SiteTaglineEdit( { ); return ( <> - - )' ), value: 'p' }, - { label: '

', value: 'h1' }, - { label: '

', value: 'h2' }, - { label: '

', value: 'h3' }, - { label: '

', value: 'h4' }, - { label: '

', value: 'h5' }, - { label: '
', value: 'h6' }, - ] } - value={ TagName } - onChange={ ( newTagName ) => - setAttributes( { tagName: newTagName } ) + + + setAttributes( { level: newLevel } ) } /> - - setAttributes( { textAlign: newAlign } ) diff --git a/packages/block-library/src/site-tagline/index.php b/packages/block-library/src/site-tagline/index.php index 8664dc0b55f21..5c1c8096b308c 100644 --- a/packages/block-library/src/site-tagline/index.php +++ b/packages/block-library/src/site-tagline/index.php @@ -17,10 +17,15 @@ function render_block_core_site_tagline( $attributes ) { if ( ! $site_tagline ) { return; } - $tag_name = empty( $attributes['tagName'] ) ? 'p' : $attributes['tagName']; + + $tag_name = 'p'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); + if ( isset( $attributes['level'] ) ) { + $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level']; + } + return sprintf( '<%1$s %2$s>%3$s', $tag_name, From fd8798737eb13ba804484eb58122327fc34fcb8e Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Tue, 12 Mar 2024 00:23:14 +0900 Subject: [PATCH 4/6] update site-tagline --- test/integration/fixtures/blocks/core__site-tagline.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/fixtures/blocks/core__site-tagline.json b/test/integration/fixtures/blocks/core__site-tagline.json index 264211b5c28ff..4ebf8b07e695d 100644 --- a/test/integration/fixtures/blocks/core__site-tagline.json +++ b/test/integration/fixtures/blocks/core__site-tagline.json @@ -3,7 +3,7 @@ "name": "core/site-tagline", "isValid": true, "attributes": { - "tagName": "p" + "level": "0" }, "innerBlocks": [] } From 70c38ff3a9bdda47313135770cc6a938867047b8 Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Tue, 12 Mar 2024 00:32:19 +0900 Subject: [PATCH 5/6] fix json --- test/integration/fixtures/blocks/core__site-tagline.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/fixtures/blocks/core__site-tagline.json b/test/integration/fixtures/blocks/core__site-tagline.json index 4ebf8b07e695d..e504bc75df30c 100644 --- a/test/integration/fixtures/blocks/core__site-tagline.json +++ b/test/integration/fixtures/blocks/core__site-tagline.json @@ -3,7 +3,7 @@ "name": "core/site-tagline", "isValid": true, "attributes": { - "level": "0" + "level": 0 }, "innerBlocks": [] } From 83290a1953ae193de8dc4b835c95e930e8c35bc4 Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Thu, 21 Mar 2024 01:28:37 +0900 Subject: [PATCH 6/6] Update packages/block-library/src/site-tagline/index.php Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com> --- packages/block-library/src/site-tagline/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/site-tagline/index.php b/packages/block-library/src/site-tagline/index.php index 5c1c8096b308c..15c3632714f4e 100644 --- a/packages/block-library/src/site-tagline/index.php +++ b/packages/block-library/src/site-tagline/index.php @@ -22,8 +22,8 @@ function render_block_core_site_tagline( $attributes ) { $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); - if ( isset( $attributes['level'] ) ) { - $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . (int) $attributes['level']; + if ( isset( $attributes['level'] ) && 0 !== $attributes['level'] ) { + $tag_name = 'h' . (int) $attributes['level']; } return sprintf(