From 718e2ce76842a868bb4a30beeecba160ac418d76 Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:47:25 +1300 Subject: [PATCH] Add search results context block (#671) * Add Search Results context block Initially built for DevHub, now required for Make Handbooks * Fix block registration path --- .../blocks/search-results-context/index.php | 80 +++++++++++++++++++ .../search-results-context/src/block.json | 44 ++++++++++ .../search-results-context/src/index.js | 19 +++++ mu-plugins/loader.php | 1 + 4 files changed, 144 insertions(+) create mode 100644 mu-plugins/blocks/search-results-context/index.php create mode 100644 mu-plugins/blocks/search-results-context/src/block.json create mode 100644 mu-plugins/blocks/search-results-context/src/index.js diff --git a/mu-plugins/blocks/search-results-context/index.php b/mu-plugins/blocks/search-results-context/index.php new file mode 100644 index 00000000..5cf33110 --- /dev/null +++ b/mu-plugins/blocks/search-results-context/index.php @@ -0,0 +1,80 @@ +found_posts; + + if ( 0 === $results_count ) { + return; + } + + $posts_per_page = get_query_var( 'posts_per_page' ); + $current_page = get_query_var( 'paged' ) ?: 1; + $first_result = ( $current_page - 1 ) * $posts_per_page + 1; + $last_result = min( $current_page * $posts_per_page, $results_count ); + + $content = sprintf( + /* translators: %1$s number of results; %2$s keyword. */ + _n( + '%1$s result found for "%2$s".', + '%1$s results found for "%2$s".', + $results_count, + 'wporg' + ), + number_format_i18n( $results_count ), + esc_html( $wp_query->query['s'] ), + ); + + $showing = sprintf( + /* translators: %1$s number of first displayed result, %2$s number of last displayed result. */ + 'Showing results %1$s to %2$s.', + number_format_i18n( $first_result ), + number_format_i18n( $last_result ), + ); + + $wrapper_attributes = get_block_wrapper_attributes(); + + return sprintf( + '<%1$s %2$s>%3$s %4$s', + esc_attr( $attributes['tagName'] ), + $wrapper_attributes, + $content, + $showing, + ); +} + +/** + * Registers the block using the metadata loaded from the `block.json` file. + * Behind the scenes, it registers also all assets so they can be enqueued + * through the block editor in the corresponding context. + * + * @see https://developer.wordpress.org/reference/functions/register_block_type/ + */ +function init() { + register_block_type( + __DIR__ . '/build', + array( + 'render_callback' => __NAMESPACE__ . '\render', + ) + ); +} diff --git a/mu-plugins/blocks/search-results-context/src/block.json b/mu-plugins/blocks/search-results-context/src/block.json new file mode 100644 index 00000000..a5554f17 --- /dev/null +++ b/mu-plugins/blocks/search-results-context/src/block.json @@ -0,0 +1,44 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "wporg/search-results-context", + "version": "0.1.0", + "title": "Search Results Context", + "category": "design", + "icon": "", + "description": "Displays context information for search results.", + "textdomain": "wporg", + "attributes": { + "tagName": { + "type": "string", + "default": "p" + } + }, + "supports": { + "align": true, + "color": true, + "html": false, + "spacing": { + "margin": true, + "padding": true, + "blockGap": false + }, + "typography": { + "fontSize": true, + "lineHeight": true, + "__experimentalFontFamily": true, + "__experimentalFontStyle": true, + "__experimentalFontWeight": true, + "__experimentalLetterSpacing": true, + "__experimentalTextTransform": true, + "__experimentalTextDecoration": true, + "__experimentalDefaultControls": { + "fontSize": true, + "fontAppearance": true, + "textTransform": true + } + } + }, + "editorScript": "file:./index.js", + "viewScript": "file:./index.js" +} \ No newline at end of file diff --git a/mu-plugins/blocks/search-results-context/src/index.js b/mu-plugins/blocks/search-results-context/src/index.js new file mode 100644 index 00000000..1981cd5f --- /dev/null +++ b/mu-plugins/blocks/search-results-context/src/index.js @@ -0,0 +1,19 @@ +/** + * WordPress dependencies + */ +import { registerBlockType } from '@wordpress/blocks'; +import { useBlockProps } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; + +function Edit() { + return
Search Results Context
; +} + +registerBlockType( metadata.name, { + edit: Edit, + save: () => null, +} ); diff --git a/mu-plugins/loader.php b/mu-plugins/loader.php index 90fab566..3d08e7ba 100644 --- a/mu-plugins/loader.php +++ b/mu-plugins/loader.php @@ -47,6 +47,7 @@ require_once __DIR__ . '/blocks/sidebar-container/index.php'; require_once __DIR__ . '/blocks/screenshot-preview/block.php'; require_once __DIR__ . '/blocks/screenshot-preview-block/block.php'; +require_once __DIR__ . '/blocks/search-results-context/index.php'; require_once __DIR__ . '/blocks/site-breadcrumbs/index.php'; require_once __DIR__ . '/blocks/table-of-contents/index.php'; require_once __DIR__ . '/blocks/time/index.php';