Skip to content
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
merged 16 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- **Ancestor:** 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))
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -211,6 +212,7 @@ const getAllBlocks = () => {
queryPaginationNumbers,
queryPaginationPrevious,
queryNoResults,
queryTotal,
readMore,
comments,
commentAuthorName,
Expand Down
45 changes: 45 additions & 0 deletions packages/block-library/src/query-total/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
"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
}
}
}
}
127 changes: 127 additions & 0 deletions packages/block-library/src/query-total/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* WordPress dependencies
*/
import {
useBlockProps,
InspectorControls,
BlockControls,
} from '@wordpress/block-editor';
import {
PanelBody,
ToolbarGroup,
ToolbarDropdownMenu,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { resultsFound, displayingResults } from './icons';

export default function QueryTotalEdit( { attributes, setAttributes } ) {
const { displayType } = attributes;

// Mock values.
gziolo marked this conversation as resolved.
Show resolved Hide resolved
const totalResults = 12;
const currentPage = 1;
const resultsPerPage = 10;

// Helper to calculate the range for "Displaying X – Y of Z".
const calculateRange = () => {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
const start = ( currentPage - 1 ) * resultsPerPage + 1;
const end = Math.min( currentPage * resultsPerPage, totalResults );
return `${ start } – ${ end }`;
};

// 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>
<InspectorControls>
<PanelBody title={ __( 'Display Settings' ) }>
<p>
{ __( 'Choose the type of information to display.' ) }
</p>
</PanelBody>
</InspectorControls>
</>
);

// Render output based on the selected display type.
const renderDisplay = () => {
if ( displayType === 'total-results' ) {
return (
<div>
{ sprintf(
/* translators: %d is the total number of results found. */
__( '%d results found' ),
totalResults
) }
</div>
);
}

if ( displayType === 'range-display' ) {
return (
<div>
{ 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
) }
</div>
);
}

return null;
};

return (
<div { ...blockProps }>
{ controls }
{ renderDisplay() }
</div>
);
}
32 changes: 32 additions & 0 deletions packages/block-library/src/query-total/icons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 = <span>&#931;</span>;
18 changes: 18 additions & 0 deletions packages/block-library/src/query-total/index.js
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 } );
86 changes: 86 additions & 0 deletions packages/block-library/src/query-total/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 'total-results':
$output = sprintf(
'<p><strong>%d</strong> %s found</p>',
$max_rows,
_n( 'result', 'results', $max_rows, 'default' )
);
break;

case 'range-display':
$range_text = $start === $end
? sprintf( 'Displaying <strong>%d</strong> of <strong>%d</strong>', $start, $max_rows )
: sprintf( 'Displaying <strong>%d – %d</strong> of <strong>%d</strong>', $start, $end, $max_rows );

$output = sprintf( '<p>%s</p>', $range_text );
break;

default:
$output = sprintf(
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
'<p><strong>%d</strong> %s found</p>',
gziolo marked this conversation as resolved.
Show resolved Hide resolved
$max_rows,
_n( 'result', 'results', $max_rows, 'default' )
);
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' );
6 changes: 6 additions & 0 deletions packages/block-library/src/query-total/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Internal dependencies
*/
import { init } from './';

export default init();
1 change: 1 addition & 0 deletions test/integration/fixtures/blocks/core__query-total.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:query-total /-->
10 changes: 10 additions & 0 deletions test/integration/fixtures/blocks/core__query-total.json
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": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"blockName": "core/query-total",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:query-total /-->
Loading