Skip to content

Commit e7b9bcd

Browse files
committed
add the 'Did You Mean' block
1 parent 52a17a9 commit e7b9bcd

6 files changed

Lines changed: 196 additions & 1 deletion

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* WordPress dependencies.
3+
*/
4+
import { useBlockProps } from '@wordpress/block-editor';
5+
import { createInterpolateElement } from '@wordpress/element';
6+
import { __ } from '@wordpress/i18n';
7+
8+
/**
9+
* Edit component.
10+
*
11+
* @returns {Function} Component.
12+
*/
13+
const Edit = () => {
14+
const blockProps = useBlockProps({
15+
className: 'wp-block-elasticpress-did-you-mean',
16+
});
17+
18+
return (
19+
<div {...blockProps}>
20+
{createInterpolateElement(__('Did you mean <a>Hello</a>?', 'elasticpress'), {
21+
a: <a href="#" />, // eslint-disable-line jsx-a11y/anchor-has-content, jsx-a11y/control-has-associated-label, jsx-a11y/anchor-is-valid
22+
})}
23+
</div>
24+
);
25+
};
26+
27+
export default Edit;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"name": "elasticpress/did-you-mean",
5+
"title": "Did You Mean",
6+
"category": "elasticpress",
7+
"description": "Display ElasticPress spelling suggestions for search queries.",
8+
"textdomain": "elasticpress",
9+
"editorScript": "ep-did-you-mean-block-script"
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* WordPress dependencies.
3+
*/
4+
import { registerBlockType } from '@wordpress/blocks';
5+
import { search } from '@wordpress/icons';
6+
7+
/**
8+
* Internal dependencies.
9+
*/
10+
import Edit from './Edit';
11+
import { name } from './block.json';
12+
13+
registerBlockType(name, {
14+
icon: search,
15+
edit: () => <Edit />,
16+
save: () => null,
17+
});

includes/classes/Feature/DidYouMean/DidYouMean.php

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ElasticPress\Feature\DidYouMean;
1010

11-
use ElasticPress\{Elasticsearch, Feature, FeatureRequirementsStatus, Features };
11+
use ElasticPress\{Elasticsearch, Feature, FeatureRequirementsStatus, Utils };
1212

1313
/**
1414
* Did You Mean feature class.
@@ -61,6 +61,91 @@ public function setup() {
6161
add_filter( 'ep_integrate_search_queries', [ $this, 'set_ep_suggestion' ], 10, 2 );
6262
add_action( 'template_redirect', [ $this, 'automatically_redirect_user' ] );
6363
add_action( 'ep_suggestions', [ $this, 'the_output' ] );
64+
65+
if ( $this->is_block_enabled_in_editor() ) {
66+
add_action( 'init', [ $this, 'register_block' ] );
67+
}
68+
}
69+
70+
/**
71+
* Check if Did You Mean block should be enabled in the editor.
72+
*
73+
* @since 5.4.0
74+
* @return bool
75+
*/
76+
protected function is_block_enabled_in_editor(): bool {
77+
global $pagenow;
78+
79+
$in_editor = in_array( $pagenow, [ 'post-new.php', 'post.php' ], true );
80+
81+
/**
82+
* Filter if Did You Mean block should be enabled in the post editor.
83+
*
84+
* @since 5.4.0
85+
* @hook ep_did_you_mean_enabled_in_editor
86+
* @param {bool} $enabled If enabled or not.
87+
* @return {bool} If enabled or not.
88+
*/
89+
return ! ( $in_editor && ! apply_filters( 'ep_did_you_mean_enabled_in_editor', false ) );
90+
}
91+
92+
/**
93+
* Register Did You Mean block.
94+
*
95+
* @return void
96+
* @since 5.4.0
97+
*/
98+
public function register_block(): void {
99+
/**
100+
* Registering it here so translation works
101+
*
102+
* @see https://core.trac.wordpress.org/ticket/54797#comment:20
103+
*/
104+
wp_register_script(
105+
'ep-did-you-mean-block-script',
106+
EP_URL . 'dist/js/did-you-mean-block-script.js',
107+
Utils\get_asset_info( 'did-you-mean-block-script', 'dependencies' ),
108+
Utils\get_asset_info( 'did-you-mean-block-script', 'version' ),
109+
true
110+
);
111+
112+
wp_set_script_translations( 'ep-did-you-mean-block-script', 'elasticpress' );
113+
114+
register_block_type_from_metadata(
115+
EP_PATH . 'assets/js/blocks/did-you-mean',
116+
[
117+
'render_callback' => [ $this, 'render_block' ],
118+
]
119+
);
120+
}
121+
122+
/**
123+
* Render Did You Mean block.
124+
*
125+
* @param array $attributes Block attributes.
126+
* @return string
127+
* @since 5.4.0
128+
*/
129+
public function render_block( array $attributes = [] ): string {
130+
ob_start();
131+
do_action( 'ep_suggestions' );
132+
$output = ob_get_clean();
133+
134+
if ( empty( $output ) ) {
135+
return '';
136+
}
137+
138+
$wrapper_attributes = get_block_wrapper_attributes(
139+
[
140+
'class' => 'wp-block-elasticpress-did-you-mean',
141+
]
142+
);
143+
144+
return sprintf(
145+
'<div %1$s>%2$s</div>',
146+
wp_kses_data( $wrapper_attributes ),
147+
wp_kses_post( $output )
148+
);
64149
}
65150

66151
/**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"comments-script": "./assets/js/comments.js",
7171
"comments-block-script": "./assets/js/blocks/comments/index.js",
7272
"dashboard-script": "./assets/js/dashboard.js",
73+
"did-you-mean-block-script": "./assets/js/blocks/did-you-mean/index.js",
7374
"facets-script": "./assets/js/facets.js",
7475
"facets-block-script": "./assets/js/blocks/facets/taxonomy/index.js",
7576
"facets-date-block-script": "./assets/js/blocks/facets/date/index.js",

tests/php/features/TestDidYouMean.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,61 @@ public function testEPSuggestionsActionOtherThanMainQuery() {
380380
$this->assertStringContainsString( '<span class="result">Showing results for: </span><strong>teet</strong>', $output );
381381
}
382382

383+
/**
384+
* Test Did You Mean block rendering.
385+
*/
386+
public function testRenderBlock() {
387+
global $wp_the_query, $wp_query;
388+
389+
$this->ep_factory->post->create( [ 'post_content' => 'Test post' ] );
390+
391+
ElasticPress\Elasticsearch::factory()->refresh_indices();
392+
393+
$query = new \WP_Query(
394+
[
395+
's' => 'teet',
396+
]
397+
);
398+
399+
$wp_the_query = $query;
400+
$wp_query = $query;
401+
402+
$feature = ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' );
403+
$feature->register_block();
404+
405+
$blocks = parse_blocks( '<!-- wp:elasticpress/did-you-mean /-->' );
406+
$output = render_block( $blocks[0] );
407+
408+
$expected = sprintf( '<span class="ep-spell-suggestion">Did you mean: <a href="%s">test</a>?</span>', get_search_link( 'test' ) );
409+
410+
$this->assertStringContainsString( 'wp-block-elasticpress-did-you-mean', $output );
411+
$this->assertStringContainsString( $expected, $output );
412+
}
413+
414+
/**
415+
* Test Did You Mean block rendering when no suggestion is available.
416+
*/
417+
public function testRenderBlockWithoutSuggestion() {
418+
global $wp_the_query, $wp_query;
419+
420+
$this->ep_factory->post->create( [ 'post_content' => 'Test post' ] );
421+
422+
ElasticPress\Elasticsearch::factory()->refresh_indices();
423+
424+
$query = new \WP_Query(
425+
[
426+
's' => 'test',
427+
]
428+
);
429+
430+
$wp_the_query = $query;
431+
$wp_query = $query;
432+
433+
$output = ElasticPress\Features::factory()->get_registered_feature( 'did-you-mean' )->render_block();
434+
435+
$this->assertSame( '', $output );
436+
}
437+
383438
/**
384439
* Test maybe_sanitize_suggestion method.
385440
*/

0 commit comments

Comments
 (0)