From 8891752654ed77400877cf4ce8639a8272decdfa Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Tue, 15 Feb 2022 07:48:56 +0100 Subject: [PATCH 1/6] Try: "No results" block container for the query block --- lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../src/query-no-results/block.json | 20 +++++++ .../src/query-no-results/edit.js | 18 ++++++ .../src/query-no-results/index.js | 20 +++++++ .../src/query-no-results/index.php | 58 +++++++++++++++++++ .../src/query-no-results/save.js | 8 +++ 7 files changed, 127 insertions(+) create mode 100644 packages/block-library/src/query-no-results/block.json create mode 100644 packages/block-library/src/query-no-results/edit.js create mode 100644 packages/block-library/src/query-no-results/index.js create mode 100644 packages/block-library/src/query-no-results/index.php create mode 100644 packages/block-library/src/query-no-results/save.js diff --git a/lib/blocks.php b/lib/blocks.php index 8a61b1ff8a4a48..4dda3aacfa4e26 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -97,6 +97,7 @@ function gutenberg_reregister_core_block_types() { 'post-title.php' => 'core/post-title', 'query.php' => 'core/query', 'post-template.php' => 'core/post-template', + 'query-no-results.php' => 'core/query-no-results', 'query-pagination.php' => 'core/query-pagination', 'query-pagination-next.php' => 'core/query-pagination-next', 'query-pagination-numbers.php' => 'core/query-pagination-numbers', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 52a2b4f3f50b09..6b856cf5437ace 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -77,6 +77,7 @@ import * as postTitle from './post-title'; import * as preformatted from './preformatted'; import * as pullquote from './pullquote'; import * as query from './query'; +import * as queryNoResults from './query-no-results'; import * as queryPagination from './query-pagination'; import * as queryPaginationNext from './query-pagination-next'; import * as queryPaginationNumbers from './query-pagination-numbers'; @@ -250,6 +251,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env // Experimental blocks. homeLink, postAuthorName, + queryNoResults, // Full Site Editing blocks. ...( enableFSEBlocks diff --git a/packages/block-library/src/query-no-results/block.json b/packages/block-library/src/query-no-results/block.json new file mode 100644 index 00000000000000..73ef39a24ec8cd --- /dev/null +++ b/packages/block-library/src/query-no-results/block.json @@ -0,0 +1,20 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "core/query-no-results", + "title": "No results", + "category": "theme", + "description": "Displays when no content is found.", + "parent": [ "core/query" ], + "textdomain": "default", + "usesContext": [ "queryId", "query" ], + "supports": { + "align": true, + "reusable": false, + "html": false, + "color": { + "gradients": true, + "link": true + } + } +} diff --git a/packages/block-library/src/query-no-results/edit.js b/packages/block-library/src/query-no-results/edit.js new file mode 100644 index 00000000000000..18c85e10905d25 --- /dev/null +++ b/packages/block-library/src/query-no-results/edit.js @@ -0,0 +1,18 @@ +/** + * WordPress dependencies + */ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; + +const TEMPLATE = [ [ 'core/paragraph' ] ]; + +export default function QueryNoResultsEdit() { + const blockProps = useBlockProps(); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + template: TEMPLATE, + } ); + return ( + <> +
+ + ); +} diff --git a/packages/block-library/src/query-no-results/index.js b/packages/block-library/src/query-no-results/index.js new file mode 100644 index 00000000000000..cd0c3209e00a9f --- /dev/null +++ b/packages/block-library/src/query-no-results/index.js @@ -0,0 +1,20 @@ +/** + * WordPress dependencies + */ +import { loop as icon } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; +import edit from './edit'; +import save from './save'; + +const { name } = metadata; +export { metadata, name }; + +export const settings = { + icon, + edit, + save, +}; diff --git a/packages/block-library/src/query-no-results/index.php b/packages/block-library/src/query-no-results/index.php new file mode 100644 index 00000000000000..e5f05b09d538fb --- /dev/null +++ b/packages/block-library/src/query-no-results/index.php @@ -0,0 +1,58 @@ +context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; + $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; + $query_args = build_query_vars_from_query_block( $block, $page ); + // Override the custom query with the global query if needed. + $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ); + if ( $use_global_query ) { + global $wp_query; + if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { + $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); + } + } + $query = new WP_Query( $query_args ); + + if ( $query->have_posts() ) { + return ''; + } + + wp_reset_postdata(); + + return sprintf( + '
%2$s
', + get_block_wrapper_attributes(), + $content + ); +} + +/** + * Registers the `core/query-no-results` block on the server. + */ +function register_block_core_query_no_results() { + register_block_type_from_metadata( + __DIR__ . '/query-no-results', + array( + 'render_callback' => 'render_block_core_query_no_results', + ) + ); +} +add_action( 'init', 'register_block_core_query_no_results' ); diff --git a/packages/block-library/src/query-no-results/save.js b/packages/block-library/src/query-no-results/save.js new file mode 100644 index 00000000000000..70161d85c27b16 --- /dev/null +++ b/packages/block-library/src/query-no-results/save.js @@ -0,0 +1,8 @@ +/** + * WordPress dependencies + */ +import { InnerBlocks } from '@wordpress/block-editor'; + +export default function QueryNoResultsSave() { + return ; +} From 09206d71c98632a0d1641a1149ae6a692f866897 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Tue, 15 Feb 2022 08:39:37 +0100 Subject: [PATCH 2/6] Linting and fixtures --- .../src/query-no-results/index.php | 5 +++-- .../blocks/core__query-no-results.html | 5 +++++ .../blocks/core__query-no-results.json | 18 ++++++++++++++++++ .../blocks/core__query-no-results.parsed.json | 17 +++++++++++++++++ .../core__query-no-results.serialized.html | 5 +++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/integration/fixtures/blocks/core__query-no-results.html create mode 100644 test/integration/fixtures/blocks/core__query-no-results.json create mode 100644 test/integration/fixtures/blocks/core__query-no-results.parsed.json create mode 100644 test/integration/fixtures/blocks/core__query-no-results.serialized.html diff --git a/packages/block-library/src/query-no-results/index.php b/packages/block-library/src/query-no-results/index.php index e5f05b09d538fb..2eddab9a6cb3b4 100644 --- a/packages/block-library/src/query-no-results/index.php +++ b/packages/block-library/src/query-no-results/index.php @@ -8,8 +8,9 @@ /** * Renders the `core/query-no-results` block on the server. * - * @param array $attributes Block attributes. - * @param string $content Block default content. + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. * * @return string Returns the wrapper for the no results block. */ diff --git a/test/integration/fixtures/blocks/core__query-no-results.html b/test/integration/fixtures/blocks/core__query-no-results.html new file mode 100644 index 00000000000000..c15100c8b14b23 --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-no-results.html @@ -0,0 +1,5 @@ + + +

+ + diff --git a/test/integration/fixtures/blocks/core__query-no-results.json b/test/integration/fixtures/blocks/core__query-no-results.json new file mode 100644 index 00000000000000..5aaee0f1410e0b --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-no-results.json @@ -0,0 +1,18 @@ +[ + { + "name": "core/query-no-results", + "isValid": true, + "attributes": {}, + "innerBlocks": [ + { + "name": "core/paragraph", + "isValid": true, + "attributes": { + "content": "", + "dropCap": false + }, + "innerBlocks": [] + } + ] + } +] diff --git a/test/integration/fixtures/blocks/core__query-no-results.parsed.json b/test/integration/fixtures/blocks/core__query-no-results.parsed.json new file mode 100644 index 00000000000000..6fe69aaf97fde9 --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-no-results.parsed.json @@ -0,0 +1,17 @@ +[ + { + "blockName": "core/query-no-results", + "attrs": {}, + "innerBlocks": [ + { + "blockName": "core/paragraph", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n

\n", + "innerContent": [ "\n

\n" ] + } + ], + "innerHTML": "\n\n", + "innerContent": [ "\n", null, "\n" ] + } +] diff --git a/test/integration/fixtures/blocks/core__query-no-results.serialized.html b/test/integration/fixtures/blocks/core__query-no-results.serialized.html new file mode 100644 index 00000000000000..c15100c8b14b23 --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-no-results.serialized.html @@ -0,0 +1,5 @@ + + +

+ + From c6af466b156302398ad71d4c1494969acbdd5729 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Tue, 15 Feb 2022 08:48:14 +0100 Subject: [PATCH 3/6] Update docs --- docs/reference-guides/core-blocks.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index a7bf34a80888c9..52d5a7ef1af986 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -602,6 +602,15 @@ An advanced block that allows displaying post types based on different query par - **Supports:** align (full, wide), color (background, gradients, link, text), ~~html~~ - **Attributes:** displayLayout, query, queryId, tagName +## No results + +Displays when no content is found. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/query-no-results)) + +- **Name:** core/query-no-results +- **Category:** theme +- **Supports:** align, color (background, gradients, link, text), ~~html~~, ~~reusable~~ +- **Attributes:** + ## Pagination Displays a paginated navigation to next/previous set of posts, when applicable. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/query-pagination)) From 23d7e077a0d6463275dd792e01c9c83692b6a15d Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 16 Mar 2022 12:46:27 +0200 Subject: [PATCH 4/6] Add content to the default paragraph --- packages/block-library/src/query-no-results/edit.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-no-results/edit.js b/packages/block-library/src/query-no-results/edit.js index 18c85e10905d25..cac0f2f8cb446d 100644 --- a/packages/block-library/src/query-no-results/edit.js +++ b/packages/block-library/src/query-no-results/edit.js @@ -2,8 +2,11 @@ * WordPress dependencies */ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; -const TEMPLATE = [ [ 'core/paragraph' ] ]; +const TEMPLATE = [ + [ 'core/paragraph', { content: __( 'No results found' ) } ], +]; export default function QueryNoResultsEdit() { const blockProps = useBlockProps(); From db9fdaf22e5746de48a9207cd988f9566cf0e2f7 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Wed, 16 Mar 2022 13:28:24 +0100 Subject: [PATCH 5/6] Update fixtures, try new block description --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/query-no-results/block.json | 2 +- test/integration/fixtures/blocks/core__query-no-results.html | 2 +- test/integration/fixtures/blocks/core__query-no-results.json | 2 +- .../fixtures/blocks/core__query-no-results.parsed.json | 4 ++-- .../fixtures/blocks/core__query-no-results.serialized.html | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 52d5a7ef1af986..9668735e079097 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -604,7 +604,7 @@ An advanced block that allows displaying post types based on different query par ## No results -Displays when no content is found. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/query-no-results)) +Contains the block elements used to render content when no query results are found. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/query-no-results)) - **Name:** core/query-no-results - **Category:** theme diff --git a/packages/block-library/src/query-no-results/block.json b/packages/block-library/src/query-no-results/block.json index 73ef39a24ec8cd..09a03821625a9d 100644 --- a/packages/block-library/src/query-no-results/block.json +++ b/packages/block-library/src/query-no-results/block.json @@ -4,7 +4,7 @@ "name": "core/query-no-results", "title": "No results", "category": "theme", - "description": "Displays when no content is found.", + "description": "Contains the block elements used to render content when no query results are found.", "parent": [ "core/query" ], "textdomain": "default", "usesContext": [ "queryId", "query" ], diff --git a/test/integration/fixtures/blocks/core__query-no-results.html b/test/integration/fixtures/blocks/core__query-no-results.html index c15100c8b14b23..1066a171d831de 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.html +++ b/test/integration/fixtures/blocks/core__query-no-results.html @@ -1,5 +1,5 @@ -

+

No results found

diff --git a/test/integration/fixtures/blocks/core__query-no-results.json b/test/integration/fixtures/blocks/core__query-no-results.json index 5aaee0f1410e0b..1094fc0c4d12ba 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.json +++ b/test/integration/fixtures/blocks/core__query-no-results.json @@ -8,7 +8,7 @@ "name": "core/paragraph", "isValid": true, "attributes": { - "content": "", + "content": "No results found", "dropCap": false }, "innerBlocks": [] diff --git a/test/integration/fixtures/blocks/core__query-no-results.parsed.json b/test/integration/fixtures/blocks/core__query-no-results.parsed.json index 6fe69aaf97fde9..782dbcaf8b3b5e 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.parsed.json +++ b/test/integration/fixtures/blocks/core__query-no-results.parsed.json @@ -7,8 +7,8 @@ "blockName": "core/paragraph", "attrs": {}, "innerBlocks": [], - "innerHTML": "\n

\n", - "innerContent": [ "\n

\n" ] + "innerHTML": "\n

No results found

\n", + "innerContent": [ "\n

No results found

\n" ] } ], "innerHTML": "\n\n", diff --git a/test/integration/fixtures/blocks/core__query-no-results.serialized.html b/test/integration/fixtures/blocks/core__query-no-results.serialized.html index c15100c8b14b23..1066a171d831de 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.serialized.html +++ b/test/integration/fixtures/blocks/core__query-no-results.serialized.html @@ -1,5 +1,5 @@ -

+

No results found

From 14fb2df2999dfbb1673fa604a3d338b2df9986ff Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Fri, 18 Mar 2022 14:04:53 +0100 Subject: [PATCH 6/6] Add block to query block variations, update placeholder text --- packages/block-library/src/query-no-results/edit.js | 9 ++++++++- packages/block-library/src/query/variations.js | 4 ++++ .../fixtures/blocks/core__query-no-results.html | 4 ++-- .../fixtures/blocks/core__query-no-results.json | 5 +++-- .../fixtures/blocks/core__query-no-results.parsed.json | 8 +++++--- .../blocks/core__query-no-results.serialized.html | 4 ++-- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/query-no-results/edit.js b/packages/block-library/src/query-no-results/edit.js index cac0f2f8cb446d..e2dba1152a5140 100644 --- a/packages/block-library/src/query-no-results/edit.js +++ b/packages/block-library/src/query-no-results/edit.js @@ -5,7 +5,14 @@ import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; const TEMPLATE = [ - [ 'core/paragraph', { content: __( 'No results found' ) } ], + [ + 'core/paragraph', + { + placeholder: __( + 'Add a text or blocks that will display when the query returns no results.' + ), + }, + ], ]; export default function QueryNoResultsEdit() { diff --git a/packages/block-library/src/query/variations.js b/packages/block-library/src/query/variations.js index 1d60a062aa6068..fb15645760f293 100644 --- a/packages/block-library/src/query/variations.js +++ b/packages/block-library/src/query/variations.js @@ -66,6 +66,7 @@ const variations = [ [ [ 'core/post-title' ], [ 'core/post-date' ] ], ], [ 'core/query-pagination' ], + [ 'core/query-no-results' ], ], scope: [ 'block' ], }, @@ -81,6 +82,7 @@ const variations = [ [ [ 'core/post-title' ], [ 'core/post-excerpt' ] ], ], [ 'core/query-pagination' ], + [ 'core/query-no-results' ], ], scope: [ 'block' ], }, @@ -100,6 +102,7 @@ const variations = [ ], ], [ 'core/query-pagination' ], + [ 'core/query-no-results' ], ], scope: [ 'block' ], }, @@ -119,6 +122,7 @@ const variations = [ ], ], [ 'core/query-pagination' ], + [ 'core/query-no-results' ], ], scope: [ 'block' ], }, diff --git a/test/integration/fixtures/blocks/core__query-no-results.html b/test/integration/fixtures/blocks/core__query-no-results.html index 1066a171d831de..defa48c4b45146 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.html +++ b/test/integration/fixtures/blocks/core__query-no-results.html @@ -1,5 +1,5 @@ - -

No results found

+ +

diff --git a/test/integration/fixtures/blocks/core__query-no-results.json b/test/integration/fixtures/blocks/core__query-no-results.json index 1094fc0c4d12ba..7331bcdeb58e30 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.json +++ b/test/integration/fixtures/blocks/core__query-no-results.json @@ -8,8 +8,9 @@ "name": "core/paragraph", "isValid": true, "attributes": { - "content": "No results found", - "dropCap": false + "content": "", + "dropCap": false, + "placeholder": "Add a text or blocks that will display when the query returns no results." }, "innerBlocks": [] } diff --git a/test/integration/fixtures/blocks/core__query-no-results.parsed.json b/test/integration/fixtures/blocks/core__query-no-results.parsed.json index 782dbcaf8b3b5e..f0557a42c2f54b 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.parsed.json +++ b/test/integration/fixtures/blocks/core__query-no-results.parsed.json @@ -5,10 +5,12 @@ "innerBlocks": [ { "blockName": "core/paragraph", - "attrs": {}, + "attrs": { + "placeholder": "Add a text or blocks that will display when the query returns no results." + }, "innerBlocks": [], - "innerHTML": "\n

No results found

\n", - "innerContent": [ "\n

No results found

\n" ] + "innerHTML": "\n

\n", + "innerContent": [ "\n

\n" ] } ], "innerHTML": "\n\n", diff --git a/test/integration/fixtures/blocks/core__query-no-results.serialized.html b/test/integration/fixtures/blocks/core__query-no-results.serialized.html index 1066a171d831de..defa48c4b45146 100644 --- a/test/integration/fixtures/blocks/core__query-no-results.serialized.html +++ b/test/integration/fixtures/blocks/core__query-no-results.serialized.html @@ -1,5 +1,5 @@ - -

No results found

+ +