From ee5415cd54786345f673b7f979130cd60b2292f6 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 18:25:00 +0530 Subject: [PATCH 01/16] Feat: add new query-total block --- docs/reference-guides/core-blocks.md | 10 ++ lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../block-library/src/query-total/block.json | 45 ++++++ .../block-library/src/query-total/edit.js | 135 ++++++++++++++++++ .../block-library/src/query-total/icons.js | 32 +++++ .../block-library/src/query-total/index.js | 18 +++ .../block-library/src/query-total/index.php | 82 +++++++++++ .../block-library/src/query-total/init.js | 6 + 9 files changed, 331 insertions(+) create mode 100644 packages/block-library/src/query-total/block.json create mode 100644 packages/block-library/src/query-total/edit.js create mode 100644 packages/block-library/src/query-total/icons.js create mode 100644 packages/block-library/src/query-total/index.js create mode 100644 packages/block-library/src/query-total/index.php create mode 100644 packages/block-library/src/query-total/init.js diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 34db859f71d1d..07c1c8ee17406 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -777,6 +777,16 @@ Display the query title. ([Source](https://github.com/WordPress/gutenberg/tree/t - **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ - **Attributes:** level, levelOptions, showPrefix, showSearchTerm, textAlign, type +## Query Total + +Display the total number of results in a query. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/query-total)) + +- **Name:** core/query-total +- **Category:** theme +- **Parent:** core/query-pagination, core/query +- **Supports:** align (full, wide), color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ +- **Attributes:** displayType + ## Quote Give quoted text visual emphasis. "In quoting others, we cite ourselves." — Julio Cortázar ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/quote)) diff --git a/lib/blocks.php b/lib/blocks.php index c3fdb26700c58..342cd25191e68 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -108,6 +108,7 @@ function gutenberg_reregister_core_block_types() { 'query-pagination-numbers.php' => 'core/query-pagination-numbers', 'query-pagination-previous.php' => 'core/query-pagination-previous', 'query-title.php' => 'core/query-title', + 'query-total.php' => 'core/query-total', 'read-more.php' => 'core/read-more', 'rss.php' => 'core/rss', 'search.php' => 'core/search', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 56365c87a268f..262f11de6ee22 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -99,6 +99,7 @@ import * as queryPaginationNext from './query-pagination-next'; import * as queryPaginationNumbers from './query-pagination-numbers'; import * as queryPaginationPrevious from './query-pagination-previous'; import * as queryTitle from './query-title'; +import * as queryTotal from './query-total'; import * as quote from './quote'; import * as reusableBlock from './block'; import * as readMore from './read-more'; @@ -211,6 +212,7 @@ const getAllBlocks = () => { queryPaginationNumbers, queryPaginationPrevious, queryNoResults, + queryTotal, readMore, comments, commentAuthorName, diff --git a/packages/block-library/src/query-total/block.json b/packages/block-library/src/query-total/block.json new file mode 100644 index 0000000000000..ddb31ea6febf4 --- /dev/null +++ b/packages/block-library/src/query-total/block.json @@ -0,0 +1,45 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "core/query-total", + "title": "Query Total", + "category": "theme", + "parent": [ "core/query-pagination", "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 + } + } + } +} diff --git a/packages/block-library/src/query-total/edit.js b/packages/block-library/src/query-total/edit.js new file mode 100644 index 0000000000000..7523dcca3abb7 --- /dev/null +++ b/packages/block-library/src/query-total/edit.js @@ -0,0 +1,135 @@ +/** + * External dependencies + */ +import clsx from 'clsx'; + +/** + * WordPress dependencies + */ +import { + useBlockProps, + InspectorControls, + BlockControls, +} from '@wordpress/block-editor'; +import { + PanelBody, + ToolbarGroup, + ToolbarDropdownMenu, +} from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { useEffect, useState } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { resultsFound, displayingResults } from './icons'; + +export default function QueryTotalEdit( { + attributes, + setAttributes, + className, +} ) { + const { displayType } = attributes; + + const [ totalResults, setTotalResults ] = useState( 0 ); + const [ currentPage, setCurrentPage ] = useState( 1 ); + const [ resultsPerPage, setResultsPerPage ] = useState( 10 ); + + // Fetch or calculate the total results, current page, and results per page. + useEffect( () => { + // Mock values for demonstration. Replace with actual query data. + const mockTotalResults = 12; + const mockCurrentPage = 1; + const mockResultsPerPage = 10; + + setTotalResults( mockTotalResults ); + setCurrentPage( mockCurrentPage ); + setResultsPerPage( mockResultsPerPage ); + }, [] ); + + // Helper to calculate the range for "Displaying X – Y of Z". + const calculateRange = () => { + const start = ( currentPage - 1 ) * resultsPerPage + 1; + const end = Math.min( currentPage * resultsPerPage, totalResults ); + return `${ start } – ${ end }`; + }; + + // Block properties and classes. + const blockProps = useBlockProps( { + className: clsx( className, 'query-total-block' ), + } ); + + 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 = ( + <> + + + + + + + +

+ { __( 'Choose the type of information to display.' ) } +

+
+
+ + ); + + // Render output based on the selected display type. + const renderDisplay = () => { + if ( displayType === 'total-results' ) { + return
{ `${ totalResults } results found` }
; + } + + if ( displayType === 'range-display' ) { + return ( +
{ `Displaying ${ calculateRange() } of ${ totalResults }` }
+ ); + } + + return null; + }; + + return ( +
+ { controls } + { renderDisplay() } +
+ ); +} diff --git a/packages/block-library/src/query-total/icons.js b/packages/block-library/src/query-total/icons.js new file mode 100644 index 0000000000000..c0bc2f8a3d06f --- /dev/null +++ b/packages/block-library/src/query-total/icons.js @@ -0,0 +1,32 @@ +/** + * WordPress dependencies + */ +import { SVG, Path } from '@wordpress/components'; + +export const resultsFound = ( + +); + +export const displayingResults = ( + +); + +export const queryTotal = Σ; diff --git a/packages/block-library/src/query-total/index.js b/packages/block-library/src/query-total/index.js new file mode 100644 index 0000000000000..7950897229156 --- /dev/null +++ b/packages/block-library/src/query-total/index.js @@ -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 } ); diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php new file mode 100644 index 0000000000000..57d8c20b6979e --- /dev/null +++ b/packages/block-library/src/query-total/index.php @@ -0,0 +1,82 @@ +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 'total-results': + $output = sprintf( + '

%d %s found

', + $max_rows, + _n( 'result', 'results', $max_rows, 'default' ) + ); + break; + + case 'range-display': + $range_text = $start === $end + ? sprintf( 'Displaying %d of %d', $start, $max_rows ) + : sprintf( 'Displaying %d – %d of %d', $start, $end, $max_rows ); + + $output = sprintf( '

%s

', $range_text ); + break; + + default: + $output = sprintf( + '

%d %s found

', + $max_rows, + _n( 'result', 'results', $max_rows, 'default' ) + ); + break; + } + + return sprintf( + '
%2$s
', + $wrapper_attributes, + $output + ); +} + +/** + * Registers the `query-total` block. + */ +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' ); diff --git a/packages/block-library/src/query-total/init.js b/packages/block-library/src/query-total/init.js new file mode 100644 index 0000000000000..79f0492c2cb2f --- /dev/null +++ b/packages/block-library/src/query-total/init.js @@ -0,0 +1,6 @@ +/** + * Internal dependencies + */ +import { init } from './'; + +export default init(); From 463fdc693370362014b13e4a0701e1881fb232cc Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 19:04:42 +0530 Subject: [PATCH 02/16] Fix: fixed PHP coding standards --- packages/block-library/src/query-total/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index 57d8c20b6979e..a49ad74493d27 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -19,14 +19,14 @@ function render_block_core_query_total( $attributes, $content, $block ) { $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 ) ); + $current_page = max( 1, get_query_var( 'paged', 1 ) ); } else { - $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; + $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; + $max_rows = $query_to_use->found_posts; $posts_per_page = $query_to_use->get( 'posts_per_page' ); // Calculate the range of posts being displayed. From 0e622ffe48336eff6319cb85c20ed71da6fa5819 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 20:31:08 +0530 Subject: [PATCH 03/16] Fix: simplify the mock results's logic & fix translation --- .../block-library/src/query-total/edit.js | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/query-total/edit.js b/packages/block-library/src/query-total/edit.js index 7523dcca3abb7..74ccb23175330 100644 --- a/packages/block-library/src/query-total/edit.js +++ b/packages/block-library/src/query-total/edit.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import clsx from 'clsx'; - /** * WordPress dependencies */ @@ -16,36 +11,20 @@ import { ToolbarGroup, ToolbarDropdownMenu, } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; -import { useEffect, useState } from '@wordpress/element'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies */ import { resultsFound, displayingResults } from './icons'; -export default function QueryTotalEdit( { - attributes, - setAttributes, - className, -} ) { +export default function QueryTotalEdit( { attributes, setAttributes } ) { const { displayType } = attributes; - const [ totalResults, setTotalResults ] = useState( 0 ); - const [ currentPage, setCurrentPage ] = useState( 1 ); - const [ resultsPerPage, setResultsPerPage ] = useState( 10 ); - - // Fetch or calculate the total results, current page, and results per page. - useEffect( () => { - // Mock values for demonstration. Replace with actual query data. - const mockTotalResults = 12; - const mockCurrentPage = 1; - const mockResultsPerPage = 10; - - setTotalResults( mockTotalResults ); - setCurrentPage( mockCurrentPage ); - setResultsPerPage( mockResultsPerPage ); - }, [] ); + // Mock values. + const totalResults = 12; + const currentPage = 1; + const resultsPerPage = 10; // Helper to calculate the range for "Displaying X – Y of Z". const calculateRange = () => { @@ -55,9 +34,7 @@ export default function QueryTotalEdit( { }; // Block properties and classes. - const blockProps = useBlockProps( { - className: clsx( className, 'query-total-block' ), - } ); + const blockProps = useBlockProps(); const getButtonPositionIcon = () => { switch ( displayType ) { @@ -114,12 +91,27 @@ export default function QueryTotalEdit( { // Render output based on the selected display type. const renderDisplay = () => { if ( displayType === 'total-results' ) { - return
{ `${ totalResults } results found` }
; + return ( +
+ { sprintf( + /* translators: %d is the total number of results found. */ + __( '%d results found' ), + totalResults + ) } +
+ ); } if ( displayType === 'range-display' ) { return ( -
{ `Displaying ${ calculateRange() } of ${ totalResults }` }
+
+ { sprintf( + /* translators: %1$s is the range (e.g., 1–10), %2$d is the total number of results. */ + __( 'Displaying %1$s of %2$d' ), + calculateRange(), + totalResults + ) } +
); } From e04494026574c476d184166a2c90d77e4705cd73 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 20:32:42 +0530 Subject: [PATCH 04/16] Fix: use ancestor instead of parent --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/query-total/block.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 07c1c8ee17406..6ff01707e2376 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -783,7 +783,7 @@ Display the total number of results in a query. ([Source](https://github.com/Wor - **Name:** core/query-total - **Category:** theme -- **Parent:** core/query-pagination, core/query +- **Ancestor:** core/query - **Supports:** align (full, wide), color (background, gradients, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ - **Attributes:** displayType diff --git a/packages/block-library/src/query-total/block.json b/packages/block-library/src/query-total/block.json index ddb31ea6febf4..6c2a0549ab314 100644 --- a/packages/block-library/src/query-total/block.json +++ b/packages/block-library/src/query-total/block.json @@ -4,7 +4,7 @@ "name": "core/query-total", "title": "Query Total", "category": "theme", - "parent": [ "core/query-pagination", "core/query" ], + "ancestor": [ "core/query" ], "description": "Display the total number of results in a query.", "textdomain": "default", "attributes": { From c71a62759dc0ab7461aa6fa8c507f6bdff024637 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 21:15:58 +0530 Subject: [PATCH 05/16] Feat: add test fixtures --- .../integration/fixtures/blocks/core__query-total.html | 1 + .../integration/fixtures/blocks/core__query-total.json | 10 ++++++++++ .../fixtures/blocks/core__query-total.parsed.json | 9 +++++++++ .../fixtures/blocks/core__query-total.serialized.html | 1 + 4 files changed, 21 insertions(+) create mode 100644 test/integration/fixtures/blocks/core__query-total.html create mode 100644 test/integration/fixtures/blocks/core__query-total.json create mode 100644 test/integration/fixtures/blocks/core__query-total.parsed.json create mode 100644 test/integration/fixtures/blocks/core__query-total.serialized.html diff --git a/test/integration/fixtures/blocks/core__query-total.html b/test/integration/fixtures/blocks/core__query-total.html new file mode 100644 index 0000000000000..c561ea6fcf7d8 --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-total.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/integration/fixtures/blocks/core__query-total.json b/test/integration/fixtures/blocks/core__query-total.json new file mode 100644 index 0000000000000..cf342c6a35f14 --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-total.json @@ -0,0 +1,10 @@ +[ + { + "name": "core/query-total", + "isValid": true, + "attributes": { + "displayType": "total-results" + }, + "innerBlocks": [] + } +] diff --git a/test/integration/fixtures/blocks/core__query-total.parsed.json b/test/integration/fixtures/blocks/core__query-total.parsed.json new file mode 100644 index 0000000000000..99a770a68adfb --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-total.parsed.json @@ -0,0 +1,9 @@ +[ + { + "blockName": "core/query-total", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + } +] diff --git a/test/integration/fixtures/blocks/core__query-total.serialized.html b/test/integration/fixtures/blocks/core__query-total.serialized.html new file mode 100644 index 0000000000000..059289daf7dad --- /dev/null +++ b/test/integration/fixtures/blocks/core__query-total.serialized.html @@ -0,0 +1 @@ + From a3903d719605a414cf7d33fccb328e50fe8a1a48 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 21:58:22 +0530 Subject: [PATCH 06/16] Fix: fixed PHP coding standards --- packages/block-library/src/query-total/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index a49ad74493d27..a90bdec73e783 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -7,6 +7,8 @@ /** * Renders the `query-total` block on the server. + * + * @since 6.7.0 * * @param array $attributes Block attributes. * @param string $content Block default content. @@ -70,6 +72,8 @@ function render_block_core_query_total( $attributes, $content, $block ) { /** * Registers the `query-total` block. + * + * @since 6.7.0 */ function register_block_core_query_total() { register_block_type_from_metadata( From 2f59c85cb7abab3fa63ee9bc27d0754bbecc13b5 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 22:00:23 +0530 Subject: [PATCH 07/16] Fix: removed EOL chars --- packages/block-library/src/query-total/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index a90bdec73e783..e1e08a7c389c1 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -7,7 +7,7 @@ /** * Renders the `query-total` block on the server. - * + * * @since 6.7.0 * * @param array $attributes Block attributes. @@ -72,7 +72,7 @@ function render_block_core_query_total( $attributes, $content, $block ) { /** * Registers the `query-total` block. - * + * * @since 6.7.0 */ function register_block_core_query_total() { From 088ae4d77a1d3d37c31435ec35c151a031318497 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 5 Dec 2024 22:30:24 +0530 Subject: [PATCH 08/16] Fix: add correct version --- packages/block-library/src/query-total/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index e1e08a7c389c1..dcf9a6d46fd7b 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -8,7 +8,7 @@ /** * Renders the `query-total` block on the server. * - * @since 6.7.0 + * @since 6.8.0 * * @param array $attributes Block attributes. * @param string $content Block default content. @@ -73,7 +73,7 @@ function render_block_core_query_total( $attributes, $content, $block ) { /** * Registers the `query-total` block. * - * @since 6.7.0 + * @since 6.8.0 */ function register_block_core_query_total() { register_block_type_from_metadata( From 8240be1c8616b68a63df61be1759f10de02ea2d6 Mon Sep 17 00:00:00 2001 From: Sarthak Nagoshe <83178197+sarthaknagoshe2002@users.noreply.github.com> Date: Mon, 9 Dec 2024 20:05:20 +0530 Subject: [PATCH 09/16] Fix: update the apiVersion to 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- packages/block-library/src/query-total/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-total/block.json b/packages/block-library/src/query-total/block.json index 6c2a0549ab314..02dbbbbb00f74 100644 --- a/packages/block-library/src/query-total/block.json +++ b/packages/block-library/src/query-total/block.json @@ -1,6 +1,6 @@ { "$schema": "https://schemas.wp.org/trunk/block.json", - "apiVersion": 2, + "apiVersion": 3, "name": "core/query-total", "title": "Query Total", "category": "theme", From 2f70ed3d636d472f305eec39ce49f89ee908095b Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Mon, 9 Dec 2024 21:11:11 +0530 Subject: [PATCH 10/16] Fix: make all text translatable --- .../block-library/src/query-total/index.php | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index dcf9a6d46fd7b..d8e59c10dc232 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -40,25 +40,36 @@ function render_block_core_query_total( $attributes, $content, $block ) { switch ( $attributes['displayType'] ) { case 'total-results': $output = sprintf( - '

%d %s found

', + '

%d %s

', $max_rows, - _n( 'result', 'results', $max_rows, 'default' ) + _n( 'result found', 'results found', $max_rows, 'default' ) ); break; case 'range-display': $range_text = $start === $end - ? sprintf( 'Displaying %d of %d', $start, $max_rows ) - : sprintf( 'Displaying %d – %d of %d', $start, $end, $max_rows ); + ? sprintf( + /* translators: 1: Start index of posts, 2: Total number of posts */ + __( 'Displaying %1$d of %2$d', 'default' ), + $start, + $max_rows + ) + : sprintf( + /* translators: 1: Start index of posts, 2: End index of posts, 3: Total number of posts */ + __( 'Displaying %1$d – %2$d of %3$d', 'default' ), + $start, + $end, + $max_rows + ); $output = sprintf( '

%s

', $range_text ); break; default: $output = sprintf( - '

%d %s found

', + '

%d %s

', $max_rows, - _n( 'result', 'results', $max_rows, 'default' ) + _n( 'result found', 'results found', $max_rows, 'default' ) ); break; } From 28bd5a82eec6a5d2e61ebbb7dcabf9001a667b26 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 00:02:51 +0530 Subject: [PATCH 11/16] Fix: refactor the translation --- .../block-library/src/query-total/index.php | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index d8e59c10dc232..e9923f4136ca5 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -42,25 +42,27 @@ function render_block_core_query_total( $attributes, $content, $block ) { $output = sprintf( '

%d %s

', $max_rows, - _n( 'result found', 'results found', $max_rows, 'default' ) + _n( 'result found', 'results found', $max_rows ) ); break; case 'range-display': - $range_text = $start === $end - ? sprintf( + if ( $start === $end ) { + $range_text = sprintf( /* translators: 1: Start index of posts, 2: Total number of posts */ - __( 'Displaying %1$d of %2$d', 'default' ), - $start, - $max_rows - ) - : sprintf( + __( 'Displaying %1$s of %2$s' ), + '' . $start . '', + '' . $max_rows . '' + ); + } else { + $range_text = sprintf( /* translators: 1: Start index of posts, 2: End index of posts, 3: Total number of posts */ - __( 'Displaying %1$d – %2$d of %3$d', 'default' ), - $start, - $end, - $max_rows + __( 'Displaying %1$s – %2$s of %3$s' ), + '' . $start . '', + '' . $end . '', + '' . $max_rows . '' ); + } $output = sprintf( '

%s

', $range_text ); break; @@ -69,7 +71,7 @@ function render_block_core_query_total( $attributes, $content, $block ) { $output = sprintf( '

%d %s

', $max_rows, - _n( 'result found', 'results found', $max_rows, 'default' ) + _n( 'result found', 'results found', $max_rows ) ); break; } From d5a69fbabd6f29457ccedecfe58b92d81693c33e Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 00:12:49 +0530 Subject: [PATCH 12/16] Fix: optimize the presentation & usage of mock values --- .../block-library/src/query-total/edit.js | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/packages/block-library/src/query-total/edit.js b/packages/block-library/src/query-total/edit.js index 74ccb23175330..12d352d196221 100644 --- a/packages/block-library/src/query-total/edit.js +++ b/packages/block-library/src/query-total/edit.js @@ -11,7 +11,7 @@ import { ToolbarGroup, ToolbarDropdownMenu, } from '@wordpress/components'; -import { __, sprintf } from '@wordpress/i18n'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies @@ -21,18 +21,6 @@ import { resultsFound, displayingResults } from './icons'; export default function QueryTotalEdit( { attributes, setAttributes } ) { const { displayType } = attributes; - // Mock values. - const totalResults = 12; - const currentPage = 1; - const resultsPerPage = 10; - - // Helper to calculate the range for "Displaying X – Y of Z". - const calculateRange = () => { - const start = ( currentPage - 1 ) * resultsPerPage + 1; - const end = Math.min( currentPage * resultsPerPage, totalResults ); - return `${ start } – ${ end }`; - }; - // Block properties and classes. const blockProps = useBlockProps(); @@ -91,28 +79,11 @@ export default function QueryTotalEdit( { attributes, setAttributes } ) { // Render output based on the selected display type. const renderDisplay = () => { if ( displayType === 'total-results' ) { - return ( -
- { sprintf( - /* translators: %d is the total number of results found. */ - __( '%d results found' ), - totalResults - ) } -
- ); + return
{ __( '12 results found' ) }
; } if ( displayType === 'range-display' ) { - return ( -
- { sprintf( - /* translators: %1$s is the range (e.g., 1–10), %2$d is the total number of results. */ - __( 'Displaying %1$s of %2$d' ), - calculateRange(), - totalResults - ) } -
- ); + return
{ __( 'Displaying 1 – 10 of 12' ) }
; } return null; From e284b58f1fa97875b9f2ead87013f222170eff60 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 14:08:13 +0530 Subject: [PATCH 13/16] Fix: put the block icon in a box --- packages/block-library/src/query-total/icons.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query-total/icons.js b/packages/block-library/src/query-total/icons.js index c0bc2f8a3d06f..8b285b99b1ade 100644 --- a/packages/block-library/src/query-total/icons.js +++ b/packages/block-library/src/query-total/icons.js @@ -29,4 +29,15 @@ export const displayingResults = ( ); -export const queryTotal = Σ; +export const queryTotal = ( + +); From 1a7d81f89acd9bbb38142620ca33576d751c477a Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 14:09:25 +0530 Subject: [PATCH 14/16] Fix: remove the inspector panel --- .../block-library/src/query-total/edit.js | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/packages/block-library/src/query-total/edit.js b/packages/block-library/src/query-total/edit.js index 12d352d196221..4824021ae99b0 100644 --- a/packages/block-library/src/query-total/edit.js +++ b/packages/block-library/src/query-total/edit.js @@ -1,16 +1,8 @@ /** * WordPress dependencies */ -import { - useBlockProps, - InspectorControls, - BlockControls, -} from '@wordpress/block-editor'; -import { - PanelBody, - ToolbarGroup, - ToolbarDropdownMenu, -} from '@wordpress/components'; +import { useBlockProps, BlockControls } from '@wordpress/block-editor'; +import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; /** @@ -66,13 +58,6 @@ export default function QueryTotalEdit( { attributes, setAttributes } ) { /> - - -

- { __( 'Choose the type of information to display.' ) } -

-
-
); From a3371b00523ba35a32dc0560a5128cea3631efa2 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 14:13:13 +0530 Subject: [PATCH 15/16] Fix: combine the default & cases --- packages/block-library/src/query-total/index.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index e9923f4136ca5..3aeab0a0ee441 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -39,6 +39,7 @@ function render_block_core_query_total( $attributes, $content, $block ) { $output = ''; switch ( $attributes['displayType'] ) { case 'total-results': + default: $output = sprintf( '

%d %s

', $max_rows, @@ -66,14 +67,6 @@ function render_block_core_query_total( $attributes, $content, $block ) { $output = sprintf( '

%s

', $range_text ); break; - - default: - $output = sprintf( - '

%d %s

', - $max_rows, - _n( 'result found', 'results found', $max_rows ) - ); - break; } return sprintf( From 386a8c0f5fb395b76b785d21fc2c390283d70430 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Tue, 10 Dec 2024 14:22:24 +0530 Subject: [PATCH 16/16] Fix: optimized the switch cases to ensure code reachability --- .../block-library/src/query-total/index.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/query-total/index.php b/packages/block-library/src/query-total/index.php index 3aeab0a0ee441..5a8ab76b5d1ef 100644 --- a/packages/block-library/src/query-total/index.php +++ b/packages/block-library/src/query-total/index.php @@ -38,15 +38,6 @@ function render_block_core_query_total( $attributes, $content, $block ) { // Prepare the display based on the `displayType` attribute. $output = ''; switch ( $attributes['displayType'] ) { - case 'total-results': - default: - $output = sprintf( - '

%d %s

', - $max_rows, - _n( 'result found', 'results found', $max_rows ) - ); - break; - case 'range-display': if ( $start === $end ) { $range_text = sprintf( @@ -67,6 +58,15 @@ function render_block_core_query_total( $attributes, $content, $block ) { $output = sprintf( '

%s

', $range_text ); break; + + case 'total-results': + default: + $output = sprintf( + '

%d %s

', + $max_rows, + _n( 'result found', 'results found', $max_rows ) + ); + break; } return sprintf(