From 1744a97e0e3c072126fc475d8b207a316cc05a9f Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 5 Jan 2021 12:50:44 +0200 Subject: [PATCH 01/49] Add Archive Title block --- lib/blocks.php | 1 + .../src/archive-title/block.json | 68 +++++++++++++++++++ .../block-library/src/archive-title/edit.js | 58 ++++++++++++++++ .../block-library/src/archive-title/index.js | 21 ++++++ .../block-library/src/archive-title/index.php | 48 +++++++++++++ packages/block-library/src/index.js | 2 + .../fixtures/blocks/core__archive-title.html | 1 + .../fixtures/blocks/core__archive-title.json | 12 ++++ .../blocks/core__archive-title.parsed.json | 18 +++++ .../core__archive-title.serialized.html | 1 + packages/icons/src/library/post-title.js | 5 +- 11 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 packages/block-library/src/archive-title/block.json create mode 100644 packages/block-library/src/archive-title/edit.js create mode 100644 packages/block-library/src/archive-title/index.js create mode 100644 packages/block-library/src/archive-title/index.php create mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html diff --git a/lib/blocks.php b/lib/blocks.php index f4f5a6536cee2..a5ee13c3afa8b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -50,6 +50,7 @@ function gutenberg_reregister_core_block_types() { 'block_names' => array_merge( array( 'archives.php' => 'core/archives', + 'archive-title.php' => 'core/archive-title', 'block.php' => 'core/block', 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', diff --git a/packages/block-library/src/archive-title/block.json b/packages/block-library/src/archive-title/block.json new file mode 100644 index 0000000000000..9d233d27e1ee7 --- /dev/null +++ b/packages/block-library/src/archive-title/block.json @@ -0,0 +1,68 @@ +{ + "apiVersion": 2, + "name": "core/archive-title", + "category": "design", + "attributes": { + "textAlign": { + "type": "string" + }, + "level": { + "type": "number", + "default": 2 + } + }, + "supports": { + "align": [ "wide", "full" ], + "html": false, + "color": { + "gradients": true + }, + "fontSize": true, + "lineHeight": true, + "__experimentalFontFamily": true, + "__experimentalSelector": { + "core/archive-title/h1": { + "title": "h1", + "selector": "h1.wp-block-archive-title", + "attributes": { + "level": 1 + } + }, + "core/archive-title/h2": { + "title": "h2", + "selector": "h2.wp-block-archive-title", + "attributes": { + "level": 2 + } + }, + "core/archive-title/h3": { + "title": "h3", + "selector": "h3.wp-block-archive-title", + "attributes": { + "level": 3 + } + }, + "core/archive-title/h4": { + "title": "h4", + "selector": "h4.wp-block-archive-title", + "attributes": { + "level": 4 + } + }, + "core/archive-title/h5": { + "title": "h5", + "selector": "h5.wp-block-archive-title", + "attributes": { + "level": 5 + } + }, + "core/archive-title/h6": { + "title": "h6", + "selector": "h6.wp-block-archive-title", + "attributes": { + "level": 6 + } + } + } + } +} diff --git a/packages/block-library/src/archive-title/edit.js b/packages/block-library/src/archive-title/edit.js new file mode 100644 index 0000000000000..b62f37febc7db --- /dev/null +++ b/packages/block-library/src/archive-title/edit.js @@ -0,0 +1,58 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; + +/** + * WordPress dependencies + */ +// import { useSelect, useDispatch } from '@wordpress/data'; +import { + AlignmentToolbar, + BlockControls, + useBlockProps, +} from '@wordpress/block-editor'; +import { ToolbarGroup } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import HeadingLevelDropdown from '../heading/heading-level-dropdown'; + +export default function ArchiveTitleEdit( { + attributes: { level, textAlign }, + setAttributes, +} ) { + const TagName = `h${ level }`; + const blockProps = useBlockProps( { + className: classnames( { + [ `has-text-align-${ textAlign }` ]: textAlign, + } ), + } ); + + const titleElement = ( + { __( 'Archive Title' ) } + ); + return ( + <> + + + + setAttributes( { level: newLevel } ) + } + /> + + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + + { titleElement } + + ); +} diff --git a/packages/block-library/src/archive-title/index.js b/packages/block-library/src/archive-title/index.js new file mode 100644 index 0000000000000..f9907249deacb --- /dev/null +++ b/packages/block-library/src/archive-title/index.js @@ -0,0 +1,21 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { postTitle as icon } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; +import edit from './edit'; + +const { name } = metadata; +export { metadata, name }; + +export const settings = { + title: __( 'Archive Title' ), + description: __( 'Display the archive title based on the queried object.' ), + icon, // TODO create new Icon. + edit, +}; diff --git a/packages/block-library/src/archive-title/index.php b/packages/block-library/src/archive-title/index.php new file mode 100644 index 0000000000000..db2912a07ef84 --- /dev/null +++ b/packages/block-library/src/archive-title/index.php @@ -0,0 +1,48 @@ + $align_class_name ) ); + + return sprintf( + '<%1$s %2$s>%3$s', + $tag_name, + $wrapper_attributes, + get_the_archive_title() + ); +} + +/** + * Registers the `core/archive-title` block on the server. + */ +function register_block_core_archive_title() { + register_block_type_from_metadata( + __DIR__ . '/archive-title', + array( + 'render_callback' => 'render_block_core_archive_title', + ) + ); +} +add_action( 'init', 'register_block_core_archive_title' ); + + + diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 7cfcb0e0c74a9..7e2328fb4e0cd 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -62,6 +62,7 @@ import * as socialLinks from './social-links'; import * as socialLink from './social-link'; // Full Site Editing Blocks +import * as archiveTitle from './archive-title'; import * as siteLogo from './site-logo'; import * as siteTagline from './site-tagline'; import * as siteTitle from './site-title'; @@ -208,6 +209,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = // Register Full Site Editing Blocks. ...( enableFSEBlocks ? [ + archiveTitle, siteLogo, siteTagline, siteTitle, diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.html b/packages/e2e-tests/fixtures/blocks/core__archive-title.html new file mode 100644 index 0000000000000..dfe8a473440fd --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__archive-title.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.json b/packages/e2e-tests/fixtures/blocks/core__archive-title.json new file mode 100644 index 0000000000000..cd3d3b8c9e9b4 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__archive-title.json @@ -0,0 +1,12 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/archive-title", + "isValid": true, + "attributes": { + "level": 2 + }, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json b/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json new file mode 100644 index 0000000000000..358bb0693cfa5 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json @@ -0,0 +1,18 @@ +[ + { + "blockName": "core/archive-title", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html b/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html new file mode 100644 index 0000000000000..dfe8a473440fd --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html @@ -0,0 +1 @@ + diff --git a/packages/icons/src/library/post-title.js b/packages/icons/src/library/post-title.js index d271282f2e1df..0408aa03e00a5 100644 --- a/packages/icons/src/library/post-title.js +++ b/packages/icons/src/library/post-title.js @@ -5,10 +5,7 @@ import { Path, SVG } from '@wordpress/primitives'; const postTitle = ( - + ); From a6ceb90167cca6dd122624656ded9905b1c93fe0 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 5 Jan 2021 13:02:21 +0200 Subject: [PATCH 02/49] remove extra lines --- packages/block-library/src/archive-title/index.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/block-library/src/archive-title/index.php b/packages/block-library/src/archive-title/index.php index db2912a07ef84..76a3012f6f495 100644 --- a/packages/block-library/src/archive-title/index.php +++ b/packages/block-library/src/archive-title/index.php @@ -43,6 +43,3 @@ function register_block_core_archive_title() { ); } add_action( 'init', 'register_block_core_archive_title' ); - - - From 03b2704329a1b49a8180bce1e162dbca3e4edbbb Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 5 Jan 2021 13:18:29 +0200 Subject: [PATCH 03/49] convert to int --- packages/block-library/src/archive-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/archive-title/index.php b/packages/block-library/src/archive-title/index.php index 76a3012f6f495..972915baa621c 100644 --- a/packages/block-library/src/archive-title/index.php +++ b/packages/block-library/src/archive-title/index.php @@ -19,7 +19,7 @@ function render_block_core_archive_title( $attributes, $content, $block ) { return ''; } - $tag_name = isset( $attributes['level'] ) ? 'h' . $attributes['level'] : 'h2'; + $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h2'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); From 47aae1bb0620673f226c8fa496526ee09fa186a4 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Wed, 6 Jan 2021 13:42:36 +0200 Subject: [PATCH 04/49] Add new ArchiveTitle icon --- .../block-library/src/archive-title/index.js | 4 ++-- packages/icons/src/index.js | 1 + packages/icons/src/library/archive-title.js | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 packages/icons/src/library/archive-title.js diff --git a/packages/block-library/src/archive-title/index.js b/packages/block-library/src/archive-title/index.js index f9907249deacb..843f191b27091 100644 --- a/packages/block-library/src/archive-title/index.js +++ b/packages/block-library/src/archive-title/index.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { postTitle as icon } from '@wordpress/icons'; +import { archiveTitle as icon } from '@wordpress/icons'; /** * Internal dependencies @@ -16,6 +16,6 @@ export { metadata, name }; export const settings = { title: __( 'Archive Title' ), description: __( 'Display the archive title based on the queried object.' ), - icon, // TODO create new Icon. + icon, edit, }; diff --git a/packages/icons/src/index.js b/packages/icons/src/index.js index 8a7f6dba0502a..15ce3266f6330 100644 --- a/packages/icons/src/index.js +++ b/packages/icons/src/index.js @@ -5,6 +5,7 @@ export { default as alignJustify } from './library/align-justify'; export { default as alignLeft } from './library/align-left'; export { default as alignRight } from './library/align-right'; export { default as archive } from './library/archive'; +export { default as archiveTitle } from './library/archive-title'; export { default as arrowDown } from './library/arrow-down'; export { default as arrowLeft } from './library/arrow-left'; export { default as arrowRight } from './library/arrow-right'; diff --git a/packages/icons/src/library/archive-title.js b/packages/icons/src/library/archive-title.js new file mode 100644 index 0000000000000..68d6264988942 --- /dev/null +++ b/packages/icons/src/library/archive-title.js @@ -0,0 +1,16 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/primitives'; + +const archiveTitle = ( + + + + +); + +export default archiveTitle; From 00d949f3a2febe072cf7b53e8d1a0a5fcec34495 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 18 Jan 2021 13:27:43 +0200 Subject: [PATCH 05/49] query title with block variations --- lib/blocks.php | 2 +- .../block-library/src/archive-title/index.php | 45 -------------- packages/block-library/src/index.js | 4 +- .../{archive-title => query-title}/block.json | 32 +++++----- .../{archive-title => query-title}/edit.js | 33 +++++++++-- .../{archive-title => query-title}/index.js | 6 +- .../block-library/src/query-title/index.php | 58 +++++++++++++++++++ .../src/query-title/variations.js | 58 +++++++++++++++++++ .../fixtures/blocks/core__archive-title.html | 1 - .../fixtures/blocks/core__archive-title.json | 12 ---- .../blocks/core__archive-title.parsed.json | 18 ------ .../core__archive-title.serialized.html | 1 - .../fixtures/blocks/core__query-title.html | 1 + 13 files changed, 170 insertions(+), 101 deletions(-) delete mode 100644 packages/block-library/src/archive-title/index.php rename packages/block-library/src/{archive-title => query-title}/block.json (59%) rename packages/block-library/src/{archive-title => query-title}/edit.js (61%) rename packages/block-library/src/{archive-title => query-title}/index.js (71%) create mode 100644 packages/block-library/src/query-title/index.php create mode 100644 packages/block-library/src/query-title/variations.js delete mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.html delete mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.json delete mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json delete mode 100644 packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__query-title.html diff --git a/lib/blocks.php b/lib/blocks.php index a5ee13c3afa8b..538ec09b91979 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -50,7 +50,6 @@ function gutenberg_reregister_core_block_types() { 'block_names' => array_merge( array( 'archives.php' => 'core/archives', - 'archive-title.php' => 'core/archive-title', 'block.php' => 'core/block', 'calendar.php' => 'core/calendar', 'categories.php' => 'core/categories', @@ -79,6 +78,7 @@ function gutenberg_reregister_core_block_types() { 'post-hierarchical-terms.php' => 'core/post-hierarchical-terms', 'post-tags.php' => 'core/post-tags', 'post-title.php' => 'core/post-title', + 'query-title.php' => 'core/query-title', 'query.php' => 'core/query', 'query-loop.php' => 'core/query-loop', 'query-pagination.php' => 'core/query-pagination', diff --git a/packages/block-library/src/archive-title/index.php b/packages/block-library/src/archive-title/index.php deleted file mode 100644 index 972915baa621c..0000000000000 --- a/packages/block-library/src/archive-title/index.php +++ /dev/null @@ -1,45 +0,0 @@ - $align_class_name ) ); - - return sprintf( - '<%1$s %2$s>%3$s', - $tag_name, - $wrapper_attributes, - get_the_archive_title() - ); -} - -/** - * Registers the `core/archive-title` block on the server. - */ -function register_block_core_archive_title() { - register_block_type_from_metadata( - __DIR__ . '/archive-title', - array( - 'render_callback' => 'render_block_core_archive_title', - ) - ); -} -add_action( 'init', 'register_block_core_archive_title' ); diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 7e2328fb4e0cd..a8ac7e980faa2 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -62,11 +62,11 @@ import * as socialLinks from './social-links'; import * as socialLink from './social-link'; // Full Site Editing Blocks -import * as archiveTitle from './archive-title'; import * as siteLogo from './site-logo'; import * as siteTagline from './site-tagline'; import * as siteTitle from './site-title'; import * as templatePart from './template-part'; +import * as queryTitle from './query-title'; import * as query from './query'; import * as queryLoop from './query-loop'; import * as queryPagination from './query-pagination'; @@ -209,11 +209,11 @@ export const __experimentalRegisterExperimentalCoreBlocks = // Register Full Site Editing Blocks. ...( enableFSEBlocks ? [ - archiveTitle, siteLogo, siteTagline, siteTitle, templatePart, + queryTitle, query, queryLoop, queryPagination, diff --git a/packages/block-library/src/archive-title/block.json b/packages/block-library/src/query-title/block.json similarity index 59% rename from packages/block-library/src/archive-title/block.json rename to packages/block-library/src/query-title/block.json index 9d233d27e1ee7..4a17720601af1 100644 --- a/packages/block-library/src/archive-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -1,8 +1,14 @@ { "apiVersion": 2, - "name": "core/archive-title", + "name": "core/query-title", "category": "design", "attributes": { + "content": { + "type": "string" + }, + "type": { + "type": "string" + }, "textAlign": { "type": "string" }, @@ -21,44 +27,44 @@ "lineHeight": true, "__experimentalFontFamily": true, "__experimentalSelector": { - "core/archive-title/h1": { + "core/query-title/h1": { "title": "h1", - "selector": "h1.wp-block-archive-title", + "selector": "h1.wp-block-query-title", "attributes": { "level": 1 } }, - "core/archive-title/h2": { + "core/query-title/h2": { "title": "h2", - "selector": "h2.wp-block-archive-title", + "selector": "h2.wp-block-query-title", "attributes": { "level": 2 } }, - "core/archive-title/h3": { + "core/query-title/h3": { "title": "h3", - "selector": "h3.wp-block-archive-title", + "selector": "h3.wp-block-query-title", "attributes": { "level": 3 } }, - "core/archive-title/h4": { + "core/query-title/h4": { "title": "h4", - "selector": "h4.wp-block-archive-title", + "selector": "h4.wp-block-query-title", "attributes": { "level": 4 } }, - "core/archive-title/h5": { + "core/query-title/h5": { "title": "h5", - "selector": "h5.wp-block-archive-title", + "selector": "h5.wp-block-query-title", "attributes": { "level": 5 } }, - "core/archive-title/h6": { + "core/query-title/h6": { "title": "h6", - "selector": "h6.wp-block-archive-title", + "selector": "h6.wp-block-query-title", "attributes": { "level": 6 } diff --git a/packages/block-library/src/archive-title/edit.js b/packages/block-library/src/query-title/edit.js similarity index 61% rename from packages/block-library/src/archive-title/edit.js rename to packages/block-library/src/query-title/edit.js index b62f37febc7db..c0c2e55ee29c1 100644 --- a/packages/block-library/src/archive-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -11,6 +11,7 @@ import { AlignmentToolbar, BlockControls, useBlockProps, + RichText, } from '@wordpress/block-editor'; import { ToolbarGroup } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -20,20 +21,40 @@ import { __ } from '@wordpress/i18n'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; -export default function ArchiveTitleEdit( { - attributes: { level, textAlign }, +export default function QueryTitleEdit( { + attributes: { content, type, level, textAlign }, setAttributes, } ) { const TagName = `h${ level }`; + const tagName = `h${ level }`; const blockProps = useBlockProps( { className: classnames( { [ `has-text-align-${ textAlign }` ]: textAlign, } ), } ); - - const titleElement = ( - { __( 'Archive Title' ) } - ); + let titleElement; + if ( type === 'archive' ) { + titleElement = ( + + { __( 'Archive title placeholder' ) } + + ); + } else { + titleElement = ( +
+ + setAttributes( { content: newContent } ) + } + disableLineBreaks={ true } + /> +
+ ); + } return ( <> diff --git a/packages/block-library/src/archive-title/index.js b/packages/block-library/src/query-title/index.js similarity index 71% rename from packages/block-library/src/archive-title/index.js rename to packages/block-library/src/query-title/index.js index 843f191b27091..dc0185eba25a8 100644 --- a/packages/block-library/src/archive-title/index.js +++ b/packages/block-library/src/query-title/index.js @@ -9,13 +9,15 @@ import { archiveTitle as icon } from '@wordpress/icons'; */ import metadata from './block.json'; import edit from './edit'; +import variations from './variations'; const { name } = metadata; export { metadata, name }; export const settings = { - title: __( 'Archive Title' ), - description: __( 'Display the archive title based on the queried object.' ), + title: __( 'Query Title' ), + description: __( 'Display the query title.' ), icon, edit, + variations, }; diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php new file mode 100644 index 0000000000000..662282955a9dc --- /dev/null +++ b/packages/block-library/src/query-title/index.php @@ -0,0 +1,58 @@ +found_posts, get_search_query() ); + $title = str_replace( $formats, $replacements, $title ); + } + $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h2'; + $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); + return sprintf( + '<%1$s %2$s>%3$s', + $tag_name, + $wrapper_attributes, + $title + ); +} + +/** + * Registers the `core/query-title` block on the server. + */ +function register_block_core_query_title() { + register_block_type_from_metadata( + __DIR__ . '/query-title', + array( + 'render_callback' => 'render_block_core_query_title', + ) + ); +} +add_action( 'init', 'register_block_core_query_title' ); diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js new file mode 100644 index 0000000000000..ed9a90438fb80 --- /dev/null +++ b/packages/block-library/src/query-title/variations.js @@ -0,0 +1,58 @@ +/** + * WordPress dependencies + */ +import { _x, __ } from '@wordpress/i18n'; +import { archiveTitle } from '@wordpress/icons'; +const variations = [ + { + name: 'custom-query-title', + title: __( 'Custom query Title' ), + description: __( 'Display a custom query title.' ), + icon: archiveTitle, + attributes: { type: 'custom', content: '' }, + scope: [ 'inserter', 'transform' ], + isDefault: true, + }, + { + name: 'archive-title', + title: __( 'Archive Title' ), + description: __( + 'Display the archive title based on the queried object.' + ), + icon: archiveTitle, + attributes: { + type: 'archive', + content: __( 'Archive title placeholder' ), + }, + scope: [ 'inserter', 'transform' ], + }, + { + name: 'search-title', + title: __( 'Search title' ), + description: __( + 'Displays a title in a search template, using search related format placeholders.' + ), + attributes: { + type: 'search', + // translators: Title for search template with dynamic content placeholders. + content: _x( + '%total% results found for "%search%"', + 'search template title' + ), + }, + scope: [ 'inserter', 'transform' ], + }, +]; + +/** + * Add `isActive` function to all `query-title` variations, if not defined. + * `isActive` function is used to find a variation match from a created + * Block by providing its attributes. + */ +variations.forEach( ( variation ) => { + if ( variation.isActive ) return; + variation.isActive = ( blockAttributes, variationAttributes ) => + blockAttributes.type === variationAttributes.type; +} ); + +export default variations; diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.html b/packages/e2e-tests/fixtures/blocks/core__archive-title.html deleted file mode 100644 index dfe8a473440fd..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__archive-title.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.json b/packages/e2e-tests/fixtures/blocks/core__archive-title.json deleted file mode 100644 index cd3d3b8c9e9b4..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__archive-title.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - { - "clientId": "_clientId_0", - "name": "core/archive-title", - "isValid": true, - "attributes": { - "level": 2 - }, - "innerBlocks": [], - "originalContent": "" - } -] diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json b/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json deleted file mode 100644 index 358bb0693cfa5..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__archive-title.parsed.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - "blockName": "core/archive-title", - "attrs": {}, - "innerBlocks": [], - "innerHTML": "", - "innerContent": [] - }, - { - "blockName": null, - "attrs": {}, - "innerBlocks": [], - "innerHTML": "\n", - "innerContent": [ - "\n" - ] - } -] diff --git a/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html b/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html deleted file mode 100644 index dfe8a473440fd..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__archive-title.serialized.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/fixtures/blocks/core__query-title.html b/packages/e2e-tests/fixtures/blocks/core__query-title.html new file mode 100644 index 0000000000000..2fa05648f259f --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__query-title.html @@ -0,0 +1 @@ + From 82ec6c7691df2d6ad445cced434737d052356580 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 15 Jul 2021 22:43:15 +0100 Subject: [PATCH 06/49] Fix merge errors --- packages/block-library/src/index.js | 1 - packages/block-library/src/query-title/variations.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 83b1fac5f5f49..da7fcacc8cadf 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -66,7 +66,6 @@ import * as siteLogo from './site-logo'; import * as siteTagline from './site-tagline'; import * as siteTitle from './site-title'; import * as templatePart from './template-part'; -import * as queryTitle from './query-title'; import * as query from './query'; import * as postTemplate from './post-template'; import * as queryTitle from './query-title'; diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index 2849b0053c5e3..89346f20387be 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; +import { _x, __ } from '@wordpress/i18n'; import { archiveTitle } from '@wordpress/icons'; const variations = [ { From b2fb7d7216c35cb22610fc3b7b87b57e28196ac6 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 11:31:31 +0100 Subject: [PATCH 07/49] Add support for search in query title Based on original work in #27989 --- .../block-library/src/query-title/block.json | 3 +++ .../block-library/src/query-title/edit.js | 20 +++++++++++++++++-- .../block-library/src/query-title/index.php | 2 +- .../src/query-title/variations.js | 5 +++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 3e5d43fc50846..a913d79164a83 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -6,6 +6,9 @@ "description": "Display the query title.", "textdomain": "default", "attributes": { + "content": { + "type": "string" + }, "type": { "type": "string" }, diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index c90c2627812ca..6a561c338c3f9 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -12,6 +12,7 @@ import { BlockControls, useBlockProps, Warning, + RichText, } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; @@ -20,10 +21,10 @@ import { __ } from '@wordpress/i18n'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; -const SUPPORTED_TYPES = [ 'archive' ]; +const SUPPORTED_TYPES = [ 'archive', 'search' ]; export default function QueryTitleEdit( { - attributes: { type, level, textAlign }, + attributes: { content, type, level, textAlign }, setAttributes, } ) { const TagName = `h${ level }`; @@ -48,6 +49,21 @@ export default function QueryTitleEdit( { titleElement = ( { __( 'Archive title' ) } ); + } else { + titleElement = ( +
+ + setAttributes( { content: newContent } ) + } + disableLineBreaks={ true } + /> +
+ ); } return ( <> diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 5822b6604f715..11b82398d361e 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -21,7 +21,7 @@ function render_block_core_query_title( $attributes ) { if ( ! $type || ( 'archive' === $type && ! $is_archive ) || ( 'search' === $type && ! $is_search ) ) { return ''; } - $title = ''; + $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); } diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index 89346f20387be..e2e7ec323de60 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -14,12 +14,13 @@ const variations = [ icon: archiveTitle, attributes: { type: 'archive', + content: __( 'Archive title' ), }, scope: [ 'inserter' ], }, { name: 'search-title', - title: __( 'Search title' ), + title: __( 'Search Title' ), description: __( 'Displays a title in a search template, using search related format placeholders.' ), @@ -31,7 +32,7 @@ const variations = [ 'search template title' ), }, - scope: [ 'inserter', 'transform' ], + scope: [ 'inserter' ], }, ]; From 258dc4436da61dd733046601d11d01633019c1d6 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 12:11:51 +0100 Subject: [PATCH 08/49] Add support for 404 in query title --- packages/block-library/src/query-title/edit.js | 2 +- packages/block-library/src/query-title/index.php | 6 +++++- .../block-library/src/query-title/variations.js | 13 ++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 6a561c338c3f9..4be8085315f35 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -21,7 +21,7 @@ import { __ } from '@wordpress/i18n'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; -const SUPPORTED_TYPES = [ 'archive', 'search' ]; +const SUPPORTED_TYPES = [ 'archive', 'search', '404' ]; export default function QueryTitleEdit( { attributes: { content, type, level, textAlign }, diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 11b82398d361e..c82bf9f75c634 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -18,7 +18,11 @@ function render_block_core_query_title( $attributes ) { $type = isset( $attributes['type'] ) ? $attributes['type'] : null; $is_search = is_search(); $is_archive = is_archive(); - if ( ! $type || ( 'archive' === $type && ! $is_archive ) || ( 'search' === $type && ! $is_search ) ) { + $is_404 = is_404(); + if ( ! $type || + ( 'archive' === $type && ! $is_archive ) || + ( 'search' === $type && ! $is_search ) || + ( '404' === $type && ! $is_404 ) ) { return ''; } $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index e2e7ec323de60..30cf333493c9c 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -14,7 +14,7 @@ const variations = [ icon: archiveTitle, attributes: { type: 'archive', - content: __( 'Archive title' ), + content: __( 'Archive' ), }, scope: [ 'inserter' ], }, @@ -34,6 +34,17 @@ const variations = [ }, scope: [ 'inserter' ], }, + { + name: '404-title', + title: __( '404 Title' ), + description: __( 'Displays a title in a 404 template.' ), + attributes: { + type: '404', + // translators: Title for search template with dynamic content placeholders. + content: __( 'Nothing found' ), + }, + scope: [ 'inserter' ], + }, ]; /** From be5836c66f14cb54c371cc23647bb1dd7f235034 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 12:38:02 +0100 Subject: [PATCH 09/49] Remove translators comment for 404 title --- packages/block-library/src/query-title/variations.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index 30cf333493c9c..f351f75c140a6 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -40,7 +40,6 @@ const variations = [ description: __( 'Displays a title in a 404 template.' ), attributes: { type: '404', - // translators: Title for search template with dynamic content placeholders. content: __( 'Nothing found' ), }, scope: [ 'inserter' ], From 900c95b7e1aa88c30f44a32437f5e75c71ad1585 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 17:21:06 +0100 Subject: [PATCH 10/49] Remove subhead from block folders array Merged in by mistake --- lib/blocks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 28a1b2b3fc605..cc3f4c8662254 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -41,7 +41,6 @@ function gutenberg_reregister_core_block_types() { 'separator', 'social-links', 'spacer', - 'subhead', 'table', // 'table-of-contents', 'text-columns', From 7e3f30870335c3cd9065007bb3c2cf288906bfeb Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 17:21:26 +0100 Subject: [PATCH 11/49] Add another wording option for search title --- packages/block-library/src/query-title/variations.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index f351f75c140a6..4d694fa60e38c 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -28,7 +28,8 @@ const variations = [ type: 'search', // translators: Title for search template with dynamic content placeholders. content: _x( - '%total% results found for "%search%"', + // '%total% results found for "%search%"', + 'Search results for "%search%"', 'search template title' ), }, From 3244717c92d5bd84a9846f968452d432f54ef2c5 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Fri, 16 Jul 2021 17:22:07 +0100 Subject: [PATCH 12/49] Reformat type if statement --- packages/block-library/src/query-title/index.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index c82bf9f75c634..e8a9d2c38203b 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -19,10 +19,12 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); $is_404 = is_404(); - if ( ! $type || + if ( + ! $type || ( 'archive' === $type && ! $is_archive ) || ( 'search' === $type && ! $is_search ) || - ( '404' === $type && ! $is_404 ) ) { + ( '404' === $type && ! $is_404 ) + ) { return ''; } $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; From 22d458d3e83e9090ef97d0d110f2db1dab1953c4 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 11:39:39 +0100 Subject: [PATCH 13/49] Add context to 404 template title --- packages/block-library/src/query-title/variations.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index 4d694fa60e38c..55775b10fb089 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -41,7 +41,8 @@ const variations = [ description: __( 'Displays a title in a 404 template.' ), attributes: { type: '404', - content: __( 'Nothing found' ), + // translators: Title for 404 template. + content: _x( 'Nothing found', '404 template title' ), }, scope: [ 'inserter' ], }, From 1f40aca39a648712b0de4e55be59fcef675d0b76 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 11:59:03 +0100 Subject: [PATCH 14/49] Add icons to search and 404 title blocks --- packages/block-library/src/query-title/variations.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js index 55775b10fb089..f8c8e101dd0a1 100644 --- a/packages/block-library/src/query-title/variations.js +++ b/packages/block-library/src/query-title/variations.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { _x, __ } from '@wordpress/i18n'; -import { archiveTitle } from '@wordpress/icons'; +import { archiveTitle, search, help } from '@wordpress/icons'; const variations = [ { isDefault: true, @@ -24,6 +24,7 @@ const variations = [ description: __( 'Displays a title in a search template, using search related format placeholders.' ), + icon: search, attributes: { type: 'search', // translators: Title for search template with dynamic content placeholders. @@ -39,6 +40,7 @@ const variations = [ name: '404-title', title: __( '404 Title' ), description: __( 'Displays a title in a 404 template.' ), + icon: help, attributes: { type: '404', // translators: Title for 404 template. From ecdd94bf58056fd5f743716abfe31a1461746af0 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:22:11 +0100 Subject: [PATCH 15/49] Remove Query Title variations --- .../block-library/src/query-title/index.js | 2 - .../src/query-title/variations.js | 64 ------------------- 2 files changed, 66 deletions(-) delete mode 100644 packages/block-library/src/query-title/variations.js diff --git a/packages/block-library/src/query-title/index.js b/packages/block-library/src/query-title/index.js index 5f564739f69d0..5660dbf8fd3e9 100644 --- a/packages/block-library/src/query-title/index.js +++ b/packages/block-library/src/query-title/index.js @@ -3,12 +3,10 @@ */ import metadata from './block.json'; import edit from './edit'; -import variations from './variations'; const { name } = metadata; export { metadata, name }; export const settings = { edit, - variations, }; diff --git a/packages/block-library/src/query-title/variations.js b/packages/block-library/src/query-title/variations.js deleted file mode 100644 index f8c8e101dd0a1..0000000000000 --- a/packages/block-library/src/query-title/variations.js +++ /dev/null @@ -1,64 +0,0 @@ -/** - * WordPress dependencies - */ -import { _x, __ } from '@wordpress/i18n'; -import { archiveTitle, search, help } from '@wordpress/icons'; -const variations = [ - { - isDefault: true, - name: 'archive-title', - title: __( 'Archive Title' ), - description: __( - 'Display the archive title based on the queried object.' - ), - icon: archiveTitle, - attributes: { - type: 'archive', - content: __( 'Archive' ), - }, - scope: [ 'inserter' ], - }, - { - name: 'search-title', - title: __( 'Search Title' ), - description: __( - 'Displays a title in a search template, using search related format placeholders.' - ), - icon: search, - attributes: { - type: 'search', - // translators: Title for search template with dynamic content placeholders. - content: _x( - // '%total% results found for "%search%"', - 'Search results for "%search%"', - 'search template title' - ), - }, - scope: [ 'inserter' ], - }, - { - name: '404-title', - title: __( '404 Title' ), - description: __( 'Displays a title in a 404 template.' ), - icon: help, - attributes: { - type: '404', - // translators: Title for 404 template. - content: _x( 'Nothing found', '404 template title' ), - }, - scope: [ 'inserter' ], - }, -]; - -/** - * Add `isActive` function to all `query-title` variations, if not defined. - * `isActive` function is used to find a variation match from a created - * Block by providing its attributes. - */ -variations.forEach( ( variation ) => { - if ( variation.isActive ) return; - variation.isActive = ( blockAttributes, variationAttributes ) => - blockAttributes.type === variationAttributes.type; -} ); - -export default variations; From 600204c6f5a573d7015b2505458d5757c93f00f2 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:24:34 +0100 Subject: [PATCH 16/49] Remove type attribute from Query Title --- packages/block-library/src/query-title/block.json | 6 ++---- packages/block-library/src/query-title/edit.js | 14 +------------- packages/block-library/src/query-title/index.php | 10 ---------- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index a913d79164a83..02b5f8dc2ea31 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -7,10 +7,8 @@ "textdomain": "default", "attributes": { "content": { - "type": "string" - }, - "type": { - "type": "string" + "type": "string", + "default": "Query title" }, "textAlign": { "type": "string" diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 4be8085315f35..2aee40a94fc6e 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -21,28 +21,16 @@ import { __ } from '@wordpress/i18n'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; -const SUPPORTED_TYPES = [ 'archive', 'search', '404' ]; - export default function QueryTitleEdit( { attributes: { content, type, level, textAlign }, setAttributes, } ) { const TagName = `h${ level }`; const blockProps = useBlockProps( { - className: classnames( { + className: classnames( 'wp-block-query-title__placeholder', { [ `has-text-align-${ textAlign }` ]: textAlign, - 'wp-block-query-title__placeholder': type === 'archive', } ), } ); - // The plan is to augment this block with more - // block variations like `Search Title`. - if ( ! SUPPORTED_TYPES.includes( type ) ) { - return ( -
- { __( 'Provided type is not supported.' ) } -
- ); - } let titleElement; if ( type === 'archive' ) { diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index e8a9d2c38203b..f810b38142768 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -15,18 +15,8 @@ * @return string Returns the query title based on the queried object. */ function render_block_core_query_title( $attributes ) { - $type = isset( $attributes['type'] ) ? $attributes['type'] : null; $is_search = is_search(); $is_archive = is_archive(); - $is_404 = is_404(); - if ( - ! $type || - ( 'archive' === $type && ! $is_archive ) || - ( 'search' === $type && ! $is_search ) || - ( '404' === $type && ! $is_404 ) - ) { - return ''; - } $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); From ff7179c00b7c0bca489d4ef51244b7c0fef19a78 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:24:54 +0100 Subject: [PATCH 17/49] Add templateSlug context to Query Title --- packages/block-library/src/query-title/block.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 02b5f8dc2ea31..f0f999de79ae4 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -3,8 +3,9 @@ "name": "core/query-title", "title": "Query Title", "category": "design", - "description": "Display the query title.", + "description": "Display the query title based on the queried object.", "textdomain": "default", + "usesContext": [ "templateSlug" ], "attributes": { "content": { "type": "string", From 0e17396132895db2a9db05745dbc5f988bb74b71 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:26:52 +0100 Subject: [PATCH 18/49] Change Query Title content relative to templateSlug context --- .../block-library/src/query-title/edit.js | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 2aee40a94fc6e..b526285b977bd 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -11,10 +11,9 @@ import { AlignmentControl, BlockControls, useBlockProps, - Warning, - RichText, } from '@wordpress/block-editor'; -import { __ } from '@wordpress/i18n'; +import { __, _x } from '@wordpress/i18n'; +import { useEffect } from '@wordpress/element'; /** * Internal dependencies @@ -22,8 +21,9 @@ import { __ } from '@wordpress/i18n'; import HeadingLevelDropdown from '../heading/heading-level-dropdown'; export default function QueryTitleEdit( { - attributes: { content, type, level, textAlign }, + attributes: { level, textAlign }, setAttributes, + context: { templateSlug }, } ) { const TagName = `h${ level }`; const blockProps = useBlockProps( { @@ -32,27 +32,32 @@ export default function QueryTitleEdit( { } ), } ); - let titleElement; - if ( type === 'archive' ) { - titleElement = ( - { __( 'Archive title' ) } - ); - } else { - titleElement = ( -
- - setAttributes( { content: newContent } ) - } - disableLineBreaks={ true } - /> -
- ); + let titleContent; + switch ( templateSlug ) { + case 'archive': + titleContent = __( 'Archive title' ); + break; + case 'search': + // translators: Title for search template with dynamic content placeholders. + titleContent = _x( + 'Search results for "%search%"', + 'search template title' + ); + break; + case '404': + // translators: Title for 404 template. + titleContent = _x( 'Nothing found', '404 template title' ); + break; } + + const titleElement = { titleContent }; + + useEffect( () => { + setAttributes( { + content: titleContent, + } ); + }, [] ); + return ( <> From fb20f584f503f23acb8f0b520c36f7f80cbeb8ef Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:32:41 +0100 Subject: [PATCH 19/49] Refactor title content variable --- packages/block-library/src/query-title/edit.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index b526285b977bd..2038ba8f3ada5 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -21,7 +21,7 @@ import { useEffect } from '@wordpress/element'; import HeadingLevelDropdown from '../heading/heading-level-dropdown'; export default function QueryTitleEdit( { - attributes: { level, textAlign }, + attributes: { content, level, textAlign }, setAttributes, context: { templateSlug }, } ) { @@ -32,32 +32,33 @@ export default function QueryTitleEdit( { } ), } ); - let titleContent; + // Infer title content from template slug context + // Defaults to content attribute prop switch ( templateSlug ) { case 'archive': - titleContent = __( 'Archive title' ); + content = __( 'Archive title' ); break; case 'search': // translators: Title for search template with dynamic content placeholders. - titleContent = _x( + content = _x( 'Search results for "%search%"', 'search template title' ); break; case '404': // translators: Title for 404 template. - titleContent = _x( 'Nothing found', '404 template title' ); + content = _x( 'Nothing found', '404 template title' ); break; } - const titleElement = { titleContent }; - useEffect( () => { setAttributes( { - content: titleContent, + content, } ); }, [] ); + const titleElement = { content }; + return ( <> From a571c6fafdc02478e35f1604de6ef3fd5ea30a98 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Mon, 19 Jul 2021 17:34:30 +0100 Subject: [PATCH 20/49] Use _x function for archive title Keeps title logic consistent and removes __() import --- packages/block-library/src/query-title/edit.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 2038ba8f3ada5..afc2eda295211 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -12,7 +12,7 @@ import { BlockControls, useBlockProps, } from '@wordpress/block-editor'; -import { __, _x } from '@wordpress/i18n'; +import { _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; /** @@ -36,7 +36,8 @@ export default function QueryTitleEdit( { // Defaults to content attribute prop switch ( templateSlug ) { case 'archive': - content = __( 'Archive title' ); + // translators: Title for archive template. + content = _x( 'Archive title', 'archive template title' ); break; case 'search': // translators: Title for search template with dynamic content placeholders. From ad39fe913d2a0b69b8eb93e4014df2eb48064da4 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 09:38:35 +0100 Subject: [PATCH 21/49] Add supported templates list; display warning if template is not yet supported --- packages/block-library/src/query-title/edit.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index afc2eda295211..940dedbe0c101 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -10,9 +10,10 @@ import classnames from 'classnames'; import { AlignmentControl, BlockControls, + Warning, useBlockProps, } from '@wordpress/block-editor'; -import { _x } from '@wordpress/i18n'; +import { __, _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; /** @@ -20,6 +21,8 @@ import { useEffect } from '@wordpress/element'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; +const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404' ]; + export default function QueryTitleEdit( { attributes: { content, level, textAlign }, setAttributes, @@ -60,6 +63,14 @@ export default function QueryTitleEdit( { const titleElement = { content }; + if ( ! SUPPORTED_TEMPLATES.includes( templateSlug ) ) { + return ( +
+ { __( 'Template is not supported.' ) } +
+ ); + } + return ( <> From 2420657eedc0d0036d6c9141571bc9427bd5534b Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 09:39:06 +0100 Subject: [PATCH 22/49] Add comment to useEffect --- packages/block-library/src/query-title/edit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 940dedbe0c101..774d469ffbb4d 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -55,6 +55,7 @@ export default function QueryTitleEdit( { break; } + // Update content based on current template useEffect( () => { setAttributes( { content, From 3fa11ccf9c4734d2ae09b45d6499c7b5a8ef3c3e Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 09:44:47 +0100 Subject: [PATCH 23/49] Add alternative option for search results title --- packages/block-library/src/query-title/edit.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 774d469ffbb4d..fca1b073bd340 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -45,6 +45,7 @@ export default function QueryTitleEdit( { case 'search': // translators: Title for search template with dynamic content placeholders. content = _x( + // '%total% results found for "%search%"', // Displays number of search results 'Search results for "%search%"', 'search template title' ); From e02f72da1f574327299ca9d658e02e46b88b9858 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 09:55:33 +0100 Subject: [PATCH 24/49] Add archive title icon for Query Title --- packages/block-library/src/query-title/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/block-library/src/query-title/index.js b/packages/block-library/src/query-title/index.js index 5660dbf8fd3e9..82973816b71ca 100644 --- a/packages/block-library/src/query-title/index.js +++ b/packages/block-library/src/query-title/index.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { archiveTitle as icon } from '@wordpress/icons'; + /** * Internal dependencies */ @@ -8,5 +13,6 @@ const { name } = metadata; export { metadata, name }; export const settings = { + icon, edit, }; From a527d24db3356005f73af44245acf10d6db42372 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 11:12:55 +0100 Subject: [PATCH 25/49] Remove alternative search title This was commented out anyway --- packages/block-library/src/query-title/edit.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index fca1b073bd340..774d469ffbb4d 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -45,7 +45,6 @@ export default function QueryTitleEdit( { case 'search': // translators: Title for search template with dynamic content placeholders. content = _x( - // '%total% results found for "%search%"', // Displays number of search results 'Search results for "%search%"', 'search template title' ); From 58459086a28a73544f9a1141d1b67c9293e00149 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:01:25 +0100 Subject: [PATCH 26/49] Use RichText for search title This means the search title can be edited via the editor --- .../block-library/src/query-title/edit.js | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 774d469ffbb4d..5632afee85b05 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -10,8 +10,9 @@ import classnames from 'classnames'; import { AlignmentControl, BlockControls, - Warning, useBlockProps, + Warning, + RichText, } from '@wordpress/block-editor'; import { __, _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; @@ -34,6 +35,7 @@ export default function QueryTitleEdit( { [ `has-text-align-${ textAlign }` ]: textAlign, } ), } ); + let titleElement; // Infer title content from template slug context // Defaults to content attribute prop @@ -41,17 +43,42 @@ export default function QueryTitleEdit( { case 'archive': // translators: Title for archive template. content = _x( 'Archive title', 'archive template title' ); + titleElement = { content }; break; case 'search': // translators: Title for search template with dynamic content placeholders. - content = _x( + const placeholderContent = _x( 'Search results for "%search%"', 'search template title' ); + + // Use placeholder content if new Query Title block is added + if ( content === 'Query title' ) { + content = placeholderContent; + } + + titleElement = ( +
+ + setAttributes( { content: newContent } ) + } + disableLineBreaks={ true } + /> +
+ ); break; case '404': // translators: Title for 404 template. content = _x( 'Nothing found', '404 template title' ); + titleElement = { content }; + break; + default: + titleElement = { content }; break; } @@ -62,8 +89,6 @@ export default function QueryTitleEdit( { } ); }, [] ); - const titleElement = { content }; - if ( ! SUPPORTED_TEMPLATES.includes( templateSlug ) ) { return (
From 710759e5e2259decc8ecf0d57bcf08b0edb8d523 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:01:55 +0100 Subject: [PATCH 27/49] Add more detail to Query Title description --- packages/block-library/src/query-title/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index f0f999de79ae4..5a1b4f22b26d0 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -3,7 +3,7 @@ "name": "core/query-title", "title": "Query Title", "category": "design", - "description": "Display the query title based on the queried object.", + "description": "Display the query title based on the queried object. Supports search, archive and 404 templates. Dynamic content is available on the search template for: %search%, %total%.", "textdomain": "default", "usesContext": [ "templateSlug" ], "attributes": { From 018dbb072611854a33d7803fa7cea1c57bf4c2bb Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 20 Jul 2021 12:22:00 +0100 Subject: [PATCH 28/49] Don't output anything when the title isn't set --- packages/block-library/src/query-title/block.json | 2 +- packages/block-library/src/query-title/index.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 5a1b4f22b26d0..f7bac63df32c1 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -9,7 +9,7 @@ "attributes": { "content": { "type": "string", - "default": "Query title" + "default": "" }, "textAlign": { "type": "string" diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index f810b38142768..3f89d247024f1 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -18,6 +18,9 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; + if ( empty( $title ) ) { + return; + } if ( $is_archive ) { $title = get_the_archive_title(); } From 38b767566aec6fcdcd94d15209341cca06cf988e Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 20 Jul 2021 12:26:01 +0100 Subject: [PATCH 29/49] typo fix --- packages/block-library/src/query-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 3f89d247024f1..262feda964c21 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -7,7 +7,7 @@ /** * Renders the `core/query-title` block on the server. - * For now it supports Arhive title and Search title, + * For now it supports Archive title and Search title, * using queried object information * * @param array $attributes Block attributes. From cf5f3e514275fa527a503ca81ce0e6bfc8277eb8 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 20 Jul 2021 12:27:19 +0100 Subject: [PATCH 30/49] Update comment --- packages/block-library/src/query-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 262feda964c21..86697895046e2 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -7,7 +7,7 @@ /** * Renders the `core/query-title` block on the server. - * For now it supports Archive title and Search title, + * For now it supports Archive title, Search title and 404 title, * using queried object information * * @param array $attributes Block attributes. From 64738dc9c1a136882dc4cac858e495cebfb3e31c Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:30:08 +0100 Subject: [PATCH 31/49] Fix query title unit test --- test/integration/fixtures/blocks/core__query-title.json | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/fixtures/blocks/core__query-title.json b/test/integration/fixtures/blocks/core__query-title.json index dee4358b09525..a29b2cf6839cd 100644 --- a/test/integration/fixtures/blocks/core__query-title.json +++ b/test/integration/fixtures/blocks/core__query-title.json @@ -4,6 +4,7 @@ "name": "core/query-title", "isValid": true, "attributes": { + "content": "", "level": 1 }, "innerBlocks": [], From 9ca257a20dfc00a4d1cd03095b04e3072ab99d65 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:34:34 +0100 Subject: [PATCH 32/49] Remove Query title placeholder text Placeholder is now an empty string --- packages/block-library/src/query-title/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 5632afee85b05..3446317981e31 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -53,7 +53,7 @@ export default function QueryTitleEdit( { ); // Use placeholder content if new Query Title block is added - if ( content === 'Query title' ) { + if ( content === '' ) { content = placeholderContent; } From ca7498c82be6f9e83bd811aee51ecf939bb45765 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:40:50 +0100 Subject: [PATCH 33/49] Move empty title check further down Moved to just before the final return --- packages/block-library/src/query-title/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 86697895046e2..d2f755d6ab2f4 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -18,9 +18,6 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; - if ( empty( $title ) ) { - return; - } if ( $is_archive ) { $title = get_the_archive_title(); } @@ -33,6 +30,9 @@ function render_block_core_query_title( $attributes ) { $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); + if ( empty( $title ) ) { + return; + } return sprintf( '<%1$s %2$s>%3$s', $tag_name, From b2ebbc8bc975c770fd182063fd459b76e4cfa9ad Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 12:50:50 +0100 Subject: [PATCH 34/49] Format equals sign alignment From running npm run lint-php --- packages/block-library/src/query-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index d2f755d6ab2f4..163425fe830b4 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -17,7 +17,7 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); - $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; + $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); } From 5e0eef75220cb6aa721d6d94f812853e168cafc1 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 14:05:30 +0100 Subject: [PATCH 35/49] Change if to elseif in Query Title index.php --- packages/block-library/src/query-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 163425fe830b4..071c26d5469fd 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -21,7 +21,7 @@ function render_block_core_query_title( $attributes ) { if ( $is_archive ) { $title = get_the_archive_title(); } - if ( $is_search ) { + elseif ( $is_search ) { global $wp_query; $formats = array( '%total%', '%search%' ); $replacements = array( $wp_query->found_posts, get_search_query() ); From 9ed105b77d189f06d4d3f811e50781740d077cb3 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 14:09:19 +0100 Subject: [PATCH 36/49] Remove content from archive title block Content is not used in archive --- packages/block-library/src/query-title/edit.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 3446317981e31..0e7f9b3b88fba 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -41,9 +41,14 @@ export default function QueryTitleEdit( { // Defaults to content attribute prop switch ( templateSlug ) { case 'archive': - // translators: Title for archive template. - content = _x( 'Archive title', 'archive template title' ); - titleElement = { content }; + titleElement = ( + + { + // translators: Title for archive template. + _x( 'Archive title', 'archive template title' ) + } + + ); break; case 'search': // translators: Title for search template with dynamic content placeholders. From 152103b4c8e7dda29d3ff55a9f3801acf6f6a319 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 14:20:18 +0100 Subject: [PATCH 37/49] Change default content to 'Query title' This allows the user to delete the block content and write something new --- packages/block-library/src/query-title/block.json | 2 +- packages/block-library/src/query-title/edit.js | 2 +- packages/block-library/src/query-title/index.php | 5 ++--- test/integration/fixtures/blocks/core__query-title.json | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index f7bac63df32c1..5a1b4f22b26d0 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -9,7 +9,7 @@ "attributes": { "content": { "type": "string", - "default": "" + "default": "Query title" }, "textAlign": { "type": "string" diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 0e7f9b3b88fba..6f1dae03fc8a5 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -58,7 +58,7 @@ export default function QueryTitleEdit( { ); // Use placeholder content if new Query Title block is added - if ( content === '' ) { + if ( content === 'Query title' ) { content = placeholderContent; } diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 071c26d5469fd..5fe9b4cf4ef63 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -20,8 +20,7 @@ function render_block_core_query_title( $attributes ) { $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); - } - elseif ( $is_search ) { + } elseif ( $is_search ) { global $wp_query; $formats = array( '%total%', '%search%' ); $replacements = array( $wp_query->found_posts, get_search_query() ); @@ -30,7 +29,7 @@ function render_block_core_query_title( $attributes ) { $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); - if ( empty( $title ) ) { + if ( 'Query title' === $title || empty( $title ) ) { return; } return sprintf( diff --git a/test/integration/fixtures/blocks/core__query-title.json b/test/integration/fixtures/blocks/core__query-title.json index a29b2cf6839cd..27d37f018c2b2 100644 --- a/test/integration/fixtures/blocks/core__query-title.json +++ b/test/integration/fixtures/blocks/core__query-title.json @@ -4,7 +4,7 @@ "name": "core/query-title", "isValid": true, "attributes": { - "content": "", + "content": "Query title", "level": 1 }, "innerBlocks": [], From 358cb81e5828696bc5e281daa47b95e66b5ae6a6 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Tue, 20 Jul 2021 17:08:37 +0100 Subject: [PATCH 38/49] Add __unstableMarkNextChangeAsNotPersistent to useEffect --- packages/block-library/src/query-title/edit.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 6f1dae03fc8a5..cc5e42e30a39e 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -6,13 +6,14 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -// import { useSelect, useDispatch } from '@wordpress/data'; +import { useDispatch } from '@wordpress/data'; import { AlignmentControl, BlockControls, useBlockProps, Warning, RichText, + store as blockEditorStore, } from '@wordpress/block-editor'; import { __, _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; @@ -35,6 +36,9 @@ export default function QueryTitleEdit( { [ `has-text-align-${ textAlign }` ]: textAlign, } ), } ); + const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( + blockEditorStore + ); let titleElement; // Infer title content from template slug context @@ -89,6 +93,7 @@ export default function QueryTitleEdit( { // Update content based on current template useEffect( () => { + __unstableMarkNextChangeAsNotPersistent(); setAttributes( { content, } ); From b5b52a8478d6fc4260a4e467183a1e2c1f854c33 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 21 Jul 2021 16:52:03 +0100 Subject: [PATCH 39/49] Add searchTitleContent & nothingFoundTitleContent attributes --- .../block-library/src/query-title/block.json | 6 ++ .../block-library/src/query-title/edit.js | 61 +++++++++++++------ .../block-library/src/query-title/index.php | 4 ++ 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 5a1b4f22b26d0..8d1737bc40f02 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -11,6 +11,12 @@ "type": "string", "default": "Query title" }, + "searchTitleContent": { + "type": "string" + }, + "nothingFoundTitleContent": { + "type": "string" + }, "textAlign": { "type": "string" }, diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index cc5e42e30a39e..305230a9ecc92 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -23,10 +23,16 @@ import { useEffect } from '@wordpress/element'; */ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; -const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404' ]; +const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404', 'index' ]; export default function QueryTitleEdit( { - attributes: { content, level, textAlign }, + attributes: { + content, + searchTitleContent, + nothingFoundTitleContent, + level, + textAlign, + }, setAttributes, context: { templateSlug }, } ) { @@ -41,29 +47,39 @@ export default function QueryTitleEdit( { ); let titleElement; + // translators: Title for index template. + const defaultTitle = _x( + 'Query title placeholder', + 'index template title' + ); + + // translators: Title for 404 template. + const nothingFoundTitle = _x( 'Nothing found', '404 template title' ); + + // translators: Title for archive template. + const archiveTitle = _x( + 'Archive title placeholder', + 'archive template title' + ); + + // translators: Title for search template with dynamic content placeholders. + const searchTitle = _x( + 'Search results for "%search%"', + 'search template title' + ); + // Infer title content from template slug context // Defaults to content attribute prop switch ( templateSlug ) { case 'archive': titleElement = ( - - { - // translators: Title for archive template. - _x( 'Archive title', 'archive template title' ) - } - + { archiveTitle } ); break; case 'search': - // translators: Title for search template with dynamic content placeholders. - const placeholderContent = _x( - 'Search results for "%search%"', - 'search template title' - ); - // Use placeholder content if new Query Title block is added if ( content === 'Query title' ) { - content = placeholderContent; + content = searchTitle; } titleElement = ( @@ -71,7 +87,7 @@ export default function QueryTitleEdit( { setAttributes( { content: newContent } ) @@ -82,9 +98,14 @@ export default function QueryTitleEdit( { ); break; case '404': - // translators: Title for 404 template. - content = _x( 'Nothing found', '404 template title' ); - titleElement = { content }; + titleElement = ( + { nothingFoundTitle } + ); + break; + case 'index': + titleElement = ( + { defaultTitle } + ); break; default: titleElement = { content }; @@ -96,6 +117,8 @@ export default function QueryTitleEdit( { __unstableMarkNextChangeAsNotPersistent(); setAttributes( { content, + searchTitleContent: searchTitle, + nothingFoundTitleContent: nothingFoundTitle, } ); }, [] ); diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 5fe9b4cf4ef63..740217c17c548 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -17,14 +17,18 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); + $is_404 = is_404(); $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); } elseif ( $is_search ) { + $title = isset( $attributes['searchTitleContent'] ) ? $attributes['searchTitleContent'] : ''; global $wp_query; $formats = array( '%total%', '%search%' ); $replacements = array( $wp_query->found_posts, get_search_query() ); $title = str_replace( $formats, $replacements, $title ); + } elseif ( $is_404 ) { + $title = isset( $attributes['nothingFoundTitleContent'] ) ? $attributes['nothingFoundTitleContent'] : ''; } $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; From 9df71fa197f5152f9fd18b0c57e1b63aa9104e0c Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Wed, 21 Jul 2021 16:52:25 +0100 Subject: [PATCH 40/49] Add InspectorControls for title variations --- .../block-library/src/query-title/edit.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 305230a9ecc92..bec9182803953 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -14,7 +14,9 @@ import { Warning, RichText, store as blockEditorStore, + InspectorControls, } from '@wordpress/block-editor'; +import { TextControl, PanelBody } from '@wordpress/components'; import { __, _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; @@ -146,6 +148,32 @@ export default function QueryTitleEdit( { } } /> + + + + setAttributes( { + searchTitleContent: value, + } ) + } + /> + + setAttributes( { + nothingFoundTitleContent: value, + } ) + } + /> + + { titleElement } ); From b1c26aea2ea8c1523266e58df0b6c4d6c1e277cb Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 09:25:56 +0100 Subject: [PATCH 41/49] Simplify title variable names --- .../block-library/src/query-title/block.json | 4 +- .../block-library/src/query-title/edit.js | 77 ++++++------------- .../block-library/src/query-title/index.php | 4 +- 3 files changed, 28 insertions(+), 57 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 8d1737bc40f02..0d947ec8eab24 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -11,10 +11,10 @@ "type": "string", "default": "Query title" }, - "searchTitleContent": { + "searchTitle": { "type": "string" }, - "nothingFoundTitleContent": { + "nothingFoundTitle": { "type": "string" }, "textAlign": { diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index bec9182803953..f5e432841bb17 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -28,13 +28,7 @@ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404', 'index' ]; export default function QueryTitleEdit( { - attributes: { - content, - searchTitleContent, - nothingFoundTitleContent, - level, - textAlign, - }, + attributes: { content, searchTitle, nothingFoundTitle, level, textAlign }, setAttributes, context: { templateSlug }, } ) { @@ -47,7 +41,7 @@ export default function QueryTitleEdit( { const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); - let titleElement; + let titleContent; // translators: Title for index template. const defaultTitle = _x( @@ -55,72 +49,49 @@ export default function QueryTitleEdit( { 'index template title' ); - // translators: Title for 404 template. - const nothingFoundTitle = _x( 'Nothing found', '404 template title' ); - // translators: Title for archive template. - const archiveTitle = _x( + const defaultArchiveTitle = _x( 'Archive title placeholder', 'archive template title' ); + // translators: Title for 404 template. + const defaultNothingFoundTitle = _x( + 'Nothing found', + '404 template title' + ); + // translators: Title for search template with dynamic content placeholders. - const searchTitle = _x( + const defaultSearchTitle = _x( 'Search results for "%search%"', 'search template title' ); // Infer title content from template slug context - // Defaults to content attribute prop switch ( templateSlug ) { case 'archive': - titleElement = ( - { archiveTitle } - ); + titleContent = defaultArchiveTitle; break; case 'search': - // Use placeholder content if new Query Title block is added - if ( content === 'Query title' ) { - content = searchTitle; - } - - titleElement = ( -
- - setAttributes( { content: newContent } ) - } - disableLineBreaks={ true } - /> -
- ); + titleContent = searchTitle; break; case '404': - titleElement = ( - { nothingFoundTitle } - ); - break; - case 'index': - titleElement = ( - { defaultTitle } - ); + titleContent = nothingFoundTitle; break; default: - titleElement = { content }; + titleContent = content; break; } + const titleElement = { titleContent }; + // Update content based on current template useEffect( () => { __unstableMarkNextChangeAsNotPersistent(); setAttributes( { - content, - searchTitleContent: searchTitle, - nothingFoundTitleContent: nothingFoundTitle, + content: defaultTitle, + searchTitle: defaultSearchTitle, + nothingFoundTitle: defaultNothingFoundTitle, } ); }, [] ); @@ -149,26 +120,26 @@ export default function QueryTitleEdit( { /> - + setAttributes( { - searchTitleContent: value, + searchTitle: value, } ) } /> setAttributes( { - nothingFoundTitleContent: value, + nothingFoundTitle: value, } ) } /> diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 740217c17c548..de6f933997520 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -22,13 +22,13 @@ function render_block_core_query_title( $attributes ) { if ( $is_archive ) { $title = get_the_archive_title(); } elseif ( $is_search ) { - $title = isset( $attributes['searchTitleContent'] ) ? $attributes['searchTitleContent'] : ''; + $title = isset( $attributes['searchTitle'] ) ? $attributes['searchTitle'] : ''; global $wp_query; $formats = array( '%total%', '%search%' ); $replacements = array( $wp_query->found_posts, get_search_query() ); $title = str_replace( $formats, $replacements, $title ); } elseif ( $is_404 ) { - $title = isset( $attributes['nothingFoundTitleContent'] ) ? $attributes['nothingFoundTitleContent'] : ''; + $title = isset( $attributes['nothingFoundTitle'] ) ? $attributes['nothingFoundTitle'] : ''; } $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; From 5d37503a15024a67e2bf9734dd054e06c9a56e8a Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 09:35:16 +0100 Subject: [PATCH 42/49] Remove RichText import --- packages/block-library/src/query-title/edit.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index f5e432841bb17..8ee44e7c00347 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -12,7 +12,6 @@ import { BlockControls, useBlockProps, Warning, - RichText, store as blockEditorStore, InspectorControls, } from '@wordpress/block-editor'; From 91b79cdb2d0974d917b7bb00d0b484c116fc23ae Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 09:35:44 +0100 Subject: [PATCH 43/49] Change placeholder title to 'Query title placeholder' --- packages/block-library/src/query-title/block.json | 2 +- packages/block-library/src/query-title/index.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 0d947ec8eab24..07694a86ebcd7 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -9,7 +9,7 @@ "attributes": { "content": { "type": "string", - "default": "Query title" + "default": "Query title placeholder" }, "searchTitle": { "type": "string" diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index de6f933997520..fdc7f81b2af84 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -33,7 +33,7 @@ function render_block_core_query_title( $attributes ) { $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); - if ( 'Query title' === $title || empty( $title ) ) { + if ( 'Query title placeholder' === $title || empty( $title ) ) { return; } return sprintf( From ca655a27bee31fbd78ce5aa03e322a26e23c7b93 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 11:53:22 +0100 Subject: [PATCH 44/49] Remove default placeholder Now defaults to empty string --- packages/block-library/src/query-title/block.json | 3 +-- packages/block-library/src/query-title/index.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 07694a86ebcd7..038b819a9242b 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -8,8 +8,7 @@ "usesContext": [ "templateSlug" ], "attributes": { "content": { - "type": "string", - "default": "Query title placeholder" + "type": "string" }, "searchTitle": { "type": "string" diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index fdc7f81b2af84..214415b8cfb11 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -33,7 +33,7 @@ function render_block_core_query_title( $attributes ) { $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); - if ( 'Query title placeholder' === $title || empty( $title ) ) { + if ( empty( $title ) ) { return; } return sprintf( From b88c5de93918d351d8d4e9bfb6d9feeb558b0b3c Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 11:59:50 +0100 Subject: [PATCH 45/49] Remove content attribute --- packages/block-library/src/query-title/block.json | 3 --- packages/block-library/src/query-title/edit.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 038b819a9242b..f4ccd877e6586 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -7,9 +7,6 @@ "textdomain": "default", "usesContext": [ "templateSlug" ], "attributes": { - "content": { - "type": "string" - }, "searchTitle": { "type": "string" }, diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 8ee44e7c00347..7ab08adf75df8 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -27,7 +27,7 @@ import HeadingLevelDropdown from '../heading/heading-level-dropdown'; const SUPPORTED_TEMPLATES = [ 'archive', 'search', '404', 'index' ]; export default function QueryTitleEdit( { - attributes: { content, searchTitle, nothingFoundTitle, level, textAlign }, + attributes: { searchTitle, nothingFoundTitle, level, textAlign }, setAttributes, context: { templateSlug }, } ) { From cc02de19e7bcf6e802d2f07195b4d9418dcadcf5 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 12:01:45 +0100 Subject: [PATCH 46/49] Remove Inspector Controls Allow search and 404 titles to be edited via usual block editor --- .../block-library/src/query-title/edit.js | 98 ++++++++++--------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 7ab08adf75df8..6f3119c02b295 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -12,10 +12,9 @@ import { BlockControls, useBlockProps, Warning, + RichText, store as blockEditorStore, - InspectorControls, } from '@wordpress/block-editor'; -import { TextControl, PanelBody } from '@wordpress/components'; import { __, _x } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; @@ -40,16 +39,10 @@ export default function QueryTitleEdit( { const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); - let titleContent; - - // translators: Title for index template. - const defaultTitle = _x( - 'Query title placeholder', - 'index template title' - ); + let titleElement; // translators: Title for archive template. - const defaultArchiveTitle = _x( + const archiveTitle = _x( 'Archive title placeholder', 'archive template title' ); @@ -69,29 +62,64 @@ export default function QueryTitleEdit( { // Infer title content from template slug context switch ( templateSlug ) { case 'archive': - titleContent = defaultArchiveTitle; + titleElement = ( + { archiveTitle } + ); break; case 'search': - titleContent = searchTitle; + case 'index': + titleElement = ( +
+ + setAttributes( { searchTitle: newSearchTitle } ) + } + disableLineBreaks={ true } + /> +
+ ); break; case '404': - titleContent = nothingFoundTitle; + titleElement = ( +
+ + setAttributes( { + nothingFoundTitle: newNothingFoundTitle, + } ) + } + disableLineBreaks={ true } + /> +
+ ); break; default: - titleContent = content; break; } - const titleElement = { titleContent }; - - // Update content based on current template + // Update default content based on current template useEffect( () => { __unstableMarkNextChangeAsNotPersistent(); - setAttributes( { - content: defaultTitle, - searchTitle: defaultSearchTitle, - nothingFoundTitle: defaultNothingFoundTitle, - } ); + + if ( ! searchTitle ) { + setAttributes( { + searchTitle: defaultSearchTitle, + } ); + } + + if ( ! nothingFoundTitle ) { + setAttributes( { + nothingFoundTitle: defaultNothingFoundTitle, + } ); + } }, [] ); if ( ! SUPPORTED_TEMPLATES.includes( templateSlug ) ) { @@ -118,32 +146,6 @@ export default function QueryTitleEdit( { } } /> - - - - setAttributes( { - searchTitle: value, - } ) - } - /> - - setAttributes( { - nothingFoundTitle: value, - } ) - } - /> - - { titleElement } ); From 6ccbdad3c8156c66151e14a3cdcd4229591dce01 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 13:17:52 +0100 Subject: [PATCH 47/49] Remove content attribute from block test --- test/integration/fixtures/blocks/core__query-title.json | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/fixtures/blocks/core__query-title.json b/test/integration/fixtures/blocks/core__query-title.json index 27d37f018c2b2..dee4358b09525 100644 --- a/test/integration/fixtures/blocks/core__query-title.json +++ b/test/integration/fixtures/blocks/core__query-title.json @@ -4,7 +4,6 @@ "name": "core/query-title", "isValid": true, "attributes": { - "content": "Query title", "level": 1 }, "innerBlocks": [], From 9f15ae55d8d93b2029c3360cf91c8b455e433eaa Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 13:23:20 +0100 Subject: [PATCH 48/49] Add default state for index template in editor --- packages/block-library/src/query-title/edit.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 6f3119c02b295..8bb99a1dbca20 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -41,6 +41,12 @@ export default function QueryTitleEdit( { ); let titleElement; + // translators: Title for archive template. + const defaultTitle = _x( + 'Query title placeholder', + 'query template title' + ); + // translators: Title for archive template. const archiveTitle = _x( 'Archive title placeholder', @@ -67,7 +73,6 @@ export default function QueryTitleEdit( { ); break; case 'search': - case 'index': titleElement = (
); break; + case 'index': + titleElement = ( + { defaultTitle } + ); + break; default: break; } From eff88a853fc55ff400197b08c5aa8f8c0964e694 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 22 Jul 2021 13:38:46 +0100 Subject: [PATCH 49/49] Remove title variable based on content attribute Content attribute no longer exists --- packages/block-library/src/query-title/index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 214415b8cfb11..aed0b59c0cbdf 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -18,12 +18,11 @@ function render_block_core_query_title( $attributes ) { $is_search = is_search(); $is_archive = is_archive(); $is_404 = is_404(); - $title = isset( $attributes['content'] ) ? $attributes['content'] : ''; if ( $is_archive ) { $title = get_the_archive_title(); } elseif ( $is_search ) { - $title = isset( $attributes['searchTitle'] ) ? $attributes['searchTitle'] : ''; global $wp_query; + $title = isset( $attributes['searchTitle'] ) ? $attributes['searchTitle'] : ''; $formats = array( '%total%', '%search%' ); $replacements = array( $wp_query->found_posts, get_search_query() ); $title = str_replace( $formats, $replacements, $title );