From ed3d4a003bb0510f24915855636d5a7eb501dac6 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Mon, 9 Jan 2023 12:18:37 -0500 Subject: [PATCH] Search: Filter the search results based on block types. If the `allowed_blocks` meta filter is set, we should also pass that through to the ElasticSearch filters. See #540. Fixes #547. --- .../plugins/pattern-directory/includes/search.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/public_html/wp-content/plugins/pattern-directory/includes/search.php b/public_html/wp-content/plugins/pattern-directory/includes/search.php index c15263b91..8662af0e1 100644 --- a/public_html/wp-content/plugins/pattern-directory/includes/search.php +++ b/public_html/wp-content/plugins/pattern-directory/includes/search.php @@ -160,6 +160,18 @@ function modify_es_query_args( $es_query_args, $wp_query ) { } } + // If there is an allowed_blocks meta_query, use it to filter the ES query. + if ( isset( $meta_query['allowed_blocks'] ) && ! empty( $meta_query['allowed_blocks']['value'] ) ) { + // Parse it out of the regex format. + $regex = $meta_query['allowed_blocks']['value']; + $list = substr( $regex, 3, -6 ); + $block_types = explode( '|', $list ); + + $filter['bool']['must'][] = [ + 'terms' => [ 'meta.wpop_contains_block_types.value.raw' => $block_types ], + ]; + } + $parser->add_query( $must_query, 'must' ); $parser->add_query( $should_query, 'should' ); $parser->add_filter( $filter );