Skip to content

Commit 718e2ce

Browse files
authored
Add search results context block (#671)
* Add Search Results context block Initially built for DevHub, now required for Make Handbooks * Fix block registration path
1 parent 94f19f2 commit 718e2ce

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Block Name: Search Results Context
4+
* Description: Displays context information for search results.
5+
*
6+
* @package wporg
7+
*/
8+
9+
namespace WordPressdotorg\MU_Plugins\Search_Results_Context;
10+
11+
add_action( 'init', __NAMESPACE__ . '\init' );
12+
13+
/**
14+
* Render the block content.
15+
*
16+
* @return string Returns the block markup.
17+
*/
18+
function render( $attributes ) {
19+
global $wp_query;
20+
21+
if ( ! is_search() ) {
22+
return '';
23+
}
24+
25+
$results_count = $wp_query->found_posts;
26+
27+
if ( 0 === $results_count ) {
28+
return;
29+
}
30+
31+
$posts_per_page = get_query_var( 'posts_per_page' );
32+
$current_page = get_query_var( 'paged' ) ?: 1;
33+
$first_result = ( $current_page - 1 ) * $posts_per_page + 1;
34+
$last_result = min( $current_page * $posts_per_page, $results_count );
35+
36+
$content = sprintf(
37+
/* translators: %1$s number of results; %2$s keyword. */
38+
_n(
39+
'%1$s result found for "%2$s".',
40+
'%1$s results found for "%2$s".',
41+
$results_count,
42+
'wporg'
43+
),
44+
number_format_i18n( $results_count ),
45+
esc_html( $wp_query->query['s'] ),
46+
);
47+
48+
$showing = sprintf(
49+
/* translators: %1$s number of first displayed result, %2$s number of last displayed result. */
50+
'Showing results %1$s to %2$s.',
51+
number_format_i18n( $first_result ),
52+
number_format_i18n( $last_result ),
53+
);
54+
55+
$wrapper_attributes = get_block_wrapper_attributes();
56+
57+
return sprintf(
58+
'<%1$s %2$s>%3$s %4$s</%1$s>',
59+
esc_attr( $attributes['tagName'] ),
60+
$wrapper_attributes,
61+
$content,
62+
$showing,
63+
);
64+
}
65+
66+
/**
67+
* Registers the block using the metadata loaded from the `block.json` file.
68+
* Behind the scenes, it registers also all assets so they can be enqueued
69+
* through the block editor in the corresponding context.
70+
*
71+
* @see https://developer.wordpress.org/reference/functions/register_block_type/
72+
*/
73+
function init() {
74+
register_block_type(
75+
__DIR__ . '/build',
76+
array(
77+
'render_callback' => __NAMESPACE__ . '\render',
78+
)
79+
);
80+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 2,
4+
"name": "wporg/search-results-context",
5+
"version": "0.1.0",
6+
"title": "Search Results Context",
7+
"category": "design",
8+
"icon": "",
9+
"description": "Displays context information for search results.",
10+
"textdomain": "wporg",
11+
"attributes": {
12+
"tagName": {
13+
"type": "string",
14+
"default": "p"
15+
}
16+
},
17+
"supports": {
18+
"align": true,
19+
"color": true,
20+
"html": false,
21+
"spacing": {
22+
"margin": true,
23+
"padding": true,
24+
"blockGap": false
25+
},
26+
"typography": {
27+
"fontSize": true,
28+
"lineHeight": true,
29+
"__experimentalFontFamily": true,
30+
"__experimentalFontStyle": true,
31+
"__experimentalFontWeight": true,
32+
"__experimentalLetterSpacing": true,
33+
"__experimentalTextTransform": true,
34+
"__experimentalTextDecoration": true,
35+
"__experimentalDefaultControls": {
36+
"fontSize": true,
37+
"fontAppearance": true,
38+
"textTransform": true
39+
}
40+
}
41+
},
42+
"editorScript": "file:./index.js",
43+
"viewScript": "file:./index.js"
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { registerBlockType } from '@wordpress/blocks';
5+
import { useBlockProps } from '@wordpress/block-editor';
6+
7+
/**
8+
* Internal dependencies
9+
*/
10+
import metadata from './block.json';
11+
12+
function Edit() {
13+
return <div { ...useBlockProps() }>Search Results Context</div>;
14+
}
15+
16+
registerBlockType( metadata.name, {
17+
edit: Edit,
18+
save: () => null,
19+
} );

mu-plugins/loader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
require_once __DIR__ . '/blocks/sidebar-container/index.php';
4848
require_once __DIR__ . '/blocks/screenshot-preview/block.php';
4949
require_once __DIR__ . '/blocks/screenshot-preview-block/block.php';
50+
require_once __DIR__ . '/blocks/search-results-context/index.php';
5051
require_once __DIR__ . '/blocks/site-breadcrumbs/index.php';
5152
require_once __DIR__ . '/blocks/table-of-contents/index.php';
5253
require_once __DIR__ . '/blocks/time/index.php';

0 commit comments

Comments
 (0)