-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try: "No results" block container for the query block #38806
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8891752
Try: "No results" block container for the query block
carolinan 09206d7
Linting and fixtures
carolinan c6af466
Update docs
carolinan 23d7e07
Add content to the default paragraph
aristath db9fdaf
Update fixtures, try new block description
carolinan 14fb2df
Add block to query block variations, update placeholder text
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "Contains the block elements used to render content when no query results are found.", | ||
"parent": [ "core/query" ], | ||
"textdomain": "default", | ||
"usesContext": [ "queryId", "query" ], | ||
"supports": { | ||
"align": true, | ||
"reusable": false, | ||
"html": false, | ||
"color": { | ||
"gradients": true, | ||
"link": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const TEMPLATE = [ | ||
[ | ||
'core/paragraph', | ||
{ | ||
placeholder: __( | ||
'Add a text or blocks that will display when the query returns no results.' | ||
), | ||
}, | ||
], | ||
]; | ||
|
||
export default function QueryNoResultsEdit() { | ||
const blockProps = useBlockProps(); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
template: TEMPLATE, | ||
} ); | ||
return ( | ||
<> | ||
<div { ...innerBlocksProps } /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/query-no-results` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/query-no-results` block on the server. | ||
* | ||
* @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. | ||
*/ | ||
function render_block_core_query_no_results( $attributes, $content, $block ) { | ||
if ( empty( trim( $content ) ) ) { | ||
return ''; | ||
} | ||
|
||
$page_key = isset( $block->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( | ||
'<div %1$s>%2$s</div>', | ||
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export default function QueryNoResultsSave() { | ||
return <InnerBlocks.Content />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- wp:query-no-results --> | ||
<!-- wp:paragraph {"placeholder":"Add a text or blocks that will display when the query returns no results."} --> | ||
<p></p> | ||
<!-- /wp:paragraph --> | ||
<!-- /wp:query-no-results --> |
19 changes: 19 additions & 0 deletions
19
test/integration/fixtures/blocks/core__query-no-results.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[ | ||
{ | ||
"name": "core/query-no-results", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [ | ||
{ | ||
"name": "core/paragraph", | ||
"isValid": true, | ||
"attributes": { | ||
"content": "", | ||
"dropCap": false, | ||
"placeholder": "Add a text or blocks that will display when the query returns no results." | ||
}, | ||
"innerBlocks": [] | ||
} | ||
] | ||
} | ||
] |
19 changes: 19 additions & 0 deletions
19
test/integration/fixtures/blocks/core__query-no-results.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[ | ||
{ | ||
"blockName": "core/query-no-results", | ||
"attrs": {}, | ||
"innerBlocks": [ | ||
{ | ||
"blockName": "core/paragraph", | ||
"attrs": { | ||
"placeholder": "Add a text or blocks that will display when the query returns no results." | ||
}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n<p></p>\n", | ||
"innerContent": [ "\n<p></p>\n" ] | ||
} | ||
], | ||
"innerHTML": "\n\n", | ||
"innerContent": [ "\n", null, "\n" ] | ||
} | ||
] |
5 changes: 5 additions & 0 deletions
5
test/integration/fixtures/blocks/core__query-no-results.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- wp:query-no-results --> | ||
<!-- wp:paragraph {"placeholder":"Add a text or blocks that will display when the query returns no results."} --> | ||
<p></p> | ||
<!-- /wp:paragraph --> | ||
<!-- /wp:query-no-results --> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to spend a few seconds re-reading this description in order to understand what I need to do, so IMO it's sub-optimal.
However, it is consistent with the comment-template and post-template blocks which use similar wording, so if this changes it would ideally also change in other blocks as well - so not something specific to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "Contains the content shown when no query results are found" would convey the same meaning in fewer words. Maybe even just "The content shown when no query results are found" would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the first suggestion, but I would like the related block descriptions to have a "simpler" language, and not only this new block.