Skip to content

Commit dc1de54

Browse files
authored
Merge branch 'develop' into fix/feature-is-active-condition
2 parents ade2c04 + c20b835 commit dc1de54

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

includes/classes/Feature/InstantResults/InstantResults.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,27 @@ public function get_facets() {
626626
$taxonomies = apply_filters( 'ep_facet_include_taxonomies', $taxonomies );
627627

628628
foreach ( $taxonomies as $slug => $taxonomy ) {
629+
if ( is_string( $taxonomy ) ) {
630+
$slug = $taxonomy;
631+
$taxonomy = get_taxonomy( $slug );
632+
}
633+
634+
if ( ! ( $taxonomy instanceof \WP_Taxonomy ) ) {
635+
_doing_it_wrong(
636+
__METHOD__,
637+
sprintf(
638+
/* translators: %s is a taxonomy slug. */
639+
esc_html__(
640+
'Invalid taxonomy "%s" returned via ep_facet_include_taxonomies filter',
641+
'elasticpress'
642+
),
643+
esc_html( $slug )
644+
),
645+
'ElasticPress 5.3.3'
646+
);
647+
continue;
648+
}
649+
629650
$name = 'tax-' . $slug;
630651
$labels = get_taxonomy_labels( $taxonomy );
631652

@@ -644,8 +665,8 @@ public function get_facets() {
644665
'type' => 'taxonomy',
645666
'post_types' => $post_types,
646667
'labels' => array(
647-
'admin' => $admin_label,
648-
'frontend' => $labels->singular_name,
668+
'admin' => wp_specialchars_decode( $admin_label, ENT_QUOTES ),
669+
'frontend' => wp_specialchars_decode( $labels->singular_name, ENT_QUOTES ),
649670
),
650671
'aggs' => array(
651672
$name => array(

tests/php/features/TestInstantResults.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,60 @@ function () {
220220
);
221221
$feature->after_update_feature( 'instant-results', [], [ 'active' => false ] );
222222
}
223+
224+
/**
225+
* Ensure invalid taxonomy values returned from ep_facet_include_taxonomies
226+
* are skipped and logged via _doing_it_wrong().
227+
*
228+
* @group instant-results
229+
* @since 5.3.3
230+
*/
231+
public function test_invalid_taxonomy_from_facet_filter_triggers_doing_it_wrong_and_is_skipped() {
232+
$this->setExpectedIncorrectUsage(
233+
\ElasticPress\Feature\InstantResults\InstantResults::class . '::get_facets'
234+
);
235+
236+
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
237+
238+
$calls = [];
239+
240+
$listener = function ( $function_name, $message, $version ) use ( &$calls ) {
241+
$calls[] = [
242+
'function' => $function_name,
243+
'message' => $message,
244+
'version' => $version,
245+
];
246+
};
247+
248+
add_action( 'doing_it_wrong_run', $listener, 10, 3 );
249+
250+
$taxonomy_filter = function ( $taxonomies ) {
251+
$taxonomies['not-a-real-taxonomy'] = 'not-a-real-taxonomy';
252+
return $taxonomies;
253+
};
254+
255+
add_filter( 'ep_facet_include_taxonomies', $taxonomy_filter, 1, 1 );
256+
257+
$feature = \ElasticPress\Features::factory()->get_registered_feature( 'instant-results' );
258+
$facets = $feature->get_facets();
259+
260+
$this->assertArrayNotHasKey( 'tax-not-a-real-taxonomy', $facets );
261+
262+
$this->assertNotEmpty( $calls );
263+
$this->assertSame( 'ElasticPress 5.3.3', $calls[0]['version'] );
264+
$this->assertSame(
265+
\ElasticPress\Feature\InstantResults\InstantResults::class . '::get_facets',
266+
$calls[0]['function']
267+
);
268+
269+
$message = html_entity_decode( $calls[0]['message'] );
270+
271+
$this->assertStringContainsString( 'not-a-real-taxonomy', $message );
272+
$this->assertStringContainsString( 'Invalid taxonomy', $message );
273+
$this->assertStringContainsString( 'returned via ep_facet_include_taxonomies', $message );
274+
275+
remove_filter( 'doing_it_wrong_trigger_error', '__return_false' );
276+
remove_action( 'doing_it_wrong_run', $listener, 10 );
277+
remove_filter( 'ep_facet_include_taxonomies', $taxonomy_filter, 1 );
278+
}
223279
}

0 commit comments

Comments
 (0)