-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
New Block: Add Query Total block for displaying total query results or ranges #67629
Merged
fabiankaegy
merged 16 commits into
WordPress:trunk
from
sarthaknagoshe2002:new-block/query-total
Dec 10, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ee5415c
Feat: add new query-total block
sarthaknagoshe2002 463fdc6
Fix: fixed PHP coding standards
sarthaknagoshe2002 0e622ff
Fix: simplify the mock results's logic & fix translation
sarthaknagoshe2002 e044940
Fix: use ancestor instead of parent
sarthaknagoshe2002 c71a627
Feat: add test fixtures
sarthaknagoshe2002 a3903d7
Fix: fixed PHP coding standards
sarthaknagoshe2002 2f59c85
Fix: removed EOL chars
sarthaknagoshe2002 088ae4d
Fix: add correct version
sarthaknagoshe2002 8240be1
Fix: update the apiVersion to 3
sarthaknagoshe2002 2f70ed3
Fix: make all text translatable
sarthaknagoshe2002 28bd5a8
Fix: refactor the translation
sarthaknagoshe2002 d5a69fb
Fix: optimize the presentation & usage of mock values
sarthaknagoshe2002 e284b58
Fix: put the block icon in a box
sarthaknagoshe2002 1a7d81f
Fix: remove the inspector panel
sarthaknagoshe2002 a3371b0
Fix: combine the default & cases
sarthaknagoshe2002 386a8c0
Fix: optimized the switch cases to ensure code reachability
sarthaknagoshe2002 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,45 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 3, | ||
"name": "core/query-total", | ||
"title": "Query Total", | ||
"category": "theme", | ||
"ancestor": [ "core/query" ], | ||
"description": "Display the total number of results in a query.", | ||
"textdomain": "default", | ||
"attributes": { | ||
"displayType": { | ||
"type": "string", | ||
"default": "total-results" | ||
} | ||
}, | ||
"usesContext": [ "queryId", "query" ], | ||
"supports": { | ||
"align": [ "wide", "full" ], | ||
"html": false, | ||
"spacing": { | ||
"margin": true, | ||
"padding": true | ||
}, | ||
"color": { | ||
"gradients": true, | ||
"text": true, | ||
"__experimentalDefaultControls": { | ||
"background": true | ||
} | ||
}, | ||
"typography": { | ||
"fontSize": true, | ||
"lineHeight": true, | ||
"__experimentalFontFamily": true, | ||
"__experimentalFontWeight": true, | ||
"__experimentalFontStyle": true, | ||
"__experimentalTextTransform": true, | ||
"__experimentalTextDecoration": true, | ||
"__experimentalLetterSpacing": true, | ||
"__experimentalDefaultControls": { | ||
"fontSize": 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,83 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps, BlockControls } from '@wordpress/block-editor'; | ||
import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { resultsFound, displayingResults } from './icons'; | ||
|
||
export default function QueryTotalEdit( { attributes, setAttributes } ) { | ||
const { displayType } = attributes; | ||
|
||
// Block properties and classes. | ||
const blockProps = useBlockProps(); | ||
|
||
const getButtonPositionIcon = () => { | ||
switch ( displayType ) { | ||
case 'total-results': | ||
return resultsFound; | ||
case 'range-display': | ||
return displayingResults; | ||
} | ||
}; | ||
|
||
const buttonPositionControls = [ | ||
{ | ||
role: 'menuitemradio', | ||
title: __( 'Total results' ), | ||
isActive: displayType === 'total-results', | ||
icon: resultsFound, | ||
onClick: () => { | ||
setAttributes( { displayType: 'total-results' } ); | ||
}, | ||
}, | ||
{ | ||
role: 'menuitemradio', | ||
title: __( 'Range display' ), | ||
isActive: displayType === 'range-display', | ||
icon: displayingResults, | ||
onClick: () => { | ||
setAttributes( { displayType: 'range-display' } ); | ||
}, | ||
}, | ||
]; | ||
|
||
// Controls for the block. | ||
const controls = ( | ||
<> | ||
<BlockControls> | ||
<ToolbarGroup> | ||
<ToolbarDropdownMenu | ||
icon={ getButtonPositionIcon() } | ||
label={ __( 'Change display type' ) } | ||
controls={ buttonPositionControls } | ||
/> | ||
</ToolbarGroup> | ||
</BlockControls> | ||
</> | ||
); | ||
|
||
// Render output based on the selected display type. | ||
const renderDisplay = () => { | ||
if ( displayType === 'total-results' ) { | ||
return <div>{ __( '12 results found' ) }</div>; | ||
} | ||
|
||
if ( displayType === 'range-display' ) { | ||
return <div>{ __( 'Displaying 1 – 10 of 12' ) }</div>; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
{ controls } | ||
{ renderDisplay() } | ||
</div> | ||
); | ||
} |
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,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SVG, Path } from '@wordpress/components'; | ||
|
||
export const resultsFound = ( | ||
<SVG | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
width="24" | ||
height="24" | ||
aria-hidden="true" | ||
focusable="false" | ||
> | ||
<Path d="M4 11h4v2H4v-2zm6 0h6v2h-6v-2zm8 0h2v2h-2v-2z" /> | ||
</SVG> | ||
); | ||
|
||
export const displayingResults = ( | ||
<SVG | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
width="24" | ||
height="24" | ||
aria-hidden="true" | ||
focusable="false" | ||
> | ||
<Path d="M4 13h2v-2H4v2zm4 0h10v-2H8v2zm12 0h2v-2h-2v2z" /> | ||
</SVG> | ||
); | ||
|
||
export const queryTotal = ( | ||
<SVG | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
width="24" | ||
height="24" | ||
aria-hidden="true" | ||
focusable="false" | ||
> | ||
<Path d="M18 4H6c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2Zm.5 14c0 .3-.2.5-.5.5H6c-.3 0-.5-.2-.5-.5V6c0-.3.2-.5.5-.5h12c.3 0 .5.2.5.5v12Zm-7-6-4.1 5h8.8v-3h-1.5v1.5h-4.2l2.9-3.5-2.9-3.5h4.2V10h1.5V7H7.4l4.1 5Z" /> | ||
</SVG> | ||
); |
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,18 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import initBlock from '../utils/init-block'; | ||
import { queryTotal } from './icons'; | ||
|
||
/* Block settings */ | ||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
icon: queryTotal, | ||
edit, | ||
}; | ||
|
||
export const init = () => initBlock( { name, metadata, settings } ); |
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,92 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/query-total` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `query-total` block on the server. | ||
* | ||
* @since 6.8.0 | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return string The rendered block content. | ||
*/ | ||
function render_block_core_query_total( $attributes, $content, $block ) { | ||
global $wp_query; | ||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { | ||
$query_to_use = $wp_query; | ||
$current_page = max( 1, get_query_var( 'paged', 1 ) ); | ||
} else { | ||
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; | ||
$current_page = isset( $_GET[ $page_key ] ) ? (int) $_GET[ $page_key ] : 1; | ||
$query_to_use = new WP_Query( build_query_vars_from_query_block( $block, $current_page ) ); | ||
} | ||
|
||
$max_rows = $query_to_use->found_posts; | ||
$posts_per_page = $query_to_use->get( 'posts_per_page' ); | ||
|
||
// Calculate the range of posts being displayed. | ||
$start = ( $current_page - 1 ) * $posts_per_page + 1; | ||
$end = min( $start + $posts_per_page - 1, $max_rows ); | ||
|
||
// Prepare the display based on the `displayType` attribute. | ||
$output = ''; | ||
switch ( $attributes['displayType'] ) { | ||
case 'range-display': | ||
if ( $start === $end ) { | ||
$range_text = sprintf( | ||
/* translators: 1: Start index of posts, 2: Total number of posts */ | ||
__( 'Displaying %1$s of %2$s' ), | ||
'<strong>' . $start . '</strong>', | ||
'<strong>' . $max_rows . '</strong>' | ||
); | ||
} else { | ||
$range_text = sprintf( | ||
/* translators: 1: Start index of posts, 2: End index of posts, 3: Total number of posts */ | ||
__( 'Displaying %1$s – %2$s of %3$s' ), | ||
'<strong>' . $start . '</strong>', | ||
'<strong>' . $end . '</strong>', | ||
'<strong>' . $max_rows . '</strong>' | ||
); | ||
} | ||
|
||
$output = sprintf( '<p>%s</p>', $range_text ); | ||
break; | ||
|
||
case 'total-results': | ||
default: | ||
$output = sprintf( | ||
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'<p><strong>%d</strong> %s</p>', | ||
$max_rows, | ||
_n( 'result found', 'results found', $max_rows ) | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
break; | ||
} | ||
|
||
return sprintf( | ||
'<div %1$s>%2$s</div>', | ||
$wrapper_attributes, | ||
$output | ||
); | ||
} | ||
|
||
/** | ||
* Registers the `query-total` block. | ||
* | ||
* @since 6.8.0 | ||
*/ | ||
function register_block_core_query_total() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/query-total', | ||
array( | ||
'render_callback' => 'render_block_core_query_total', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_query_total' ); |
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,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { init } from './'; | ||
|
||
export default init(); |
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 @@ | ||
<!-- wp:query-total /--> |
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,10 @@ | ||
[ | ||
{ | ||
"name": "core/query-total", | ||
"isValid": true, | ||
"attributes": { | ||
"displayType": "total-results" | ||
}, | ||
"innerBlocks": [] | ||
} | ||
] |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/blocks/core__query-total.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,9 @@ | ||
[ | ||
{ | ||
"blockName": "core/query-total", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__query-total.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 @@ | ||
<!-- wp:query-total /--> |
Oops, something went wrong.
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.
My last question is where the number 12 comes from. I'm not sure how much these display elements need to represent real data, but in some cases, it would be possible to show the actual number of results and the current settings for items per page.
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.
@gziolo We have a precedence for this in core. The query pagination also doesn't show the real pagination but has a good default value that showcases the block in action 🤔
I think it makes sense here also as
1 - 1 out of 1
doesn't really make senseThere 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.
Now that you shared it. What should happen for edge cases?
1 -1 out of 1
- should it simply display1
or an empty string?1-5 out of 5
- should it display1-5
?Thank you for the explanation about the number
12
. It's now documented and we can proceed further 😄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 see that it's mostly covered already in PHP 😄
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 the proper answer would still be:
1 - 1 out of 1
The format should be:
index of first item on this page
-index of last item on this page
out oftotal number of results
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.
Currently this is how it is handled on the frontend
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.
Looking at the logic, it will show
Displaying 1 of 1
which is perfectly fine.