Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize meta query #3271

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ protected function parse_meta_queries( $args ) {
* @since 1.3
*/
$meta_queries = ( ! empty( $args['meta_query'] ) ) ? $args['meta_query'] : [];
$meta_queries = ( new \WP_Meta_Query() )->sanitize_query( $meta_queries );

/**
* Todo: Support meta_type
Expand Down
42 changes: 42 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3231,6 +3231,48 @@ public function testMetaQueryAdvanced() {
$this->assertEquals( 1, $query->found_posts );
}

/**
* Test the sanitization of an empty meta query
*
* @since 4.5.0
* @group post
*/
public function testMetaQueryEmptySanitization() {
$this->ep_factory->post->create(
array(
'post_content' => 'the post content findme',
'meta_input' => array(
'test_key' => 'value1',
'test_key2' => 'value',
),
)
);
$this->ep_factory->post->create(
array(
'post_content' => 'post content findme',
'meta_input' => array(
'test_key' => 'value',
'test_key2' => 'value2',
'test_key3' => 'value',
),
)
);

ElasticPress\Elasticsearch::factory()->refresh_indices();
$args = array(
's' => 'findme',
'meta_query' => array(
0 => array(),
),
);

$query = new \WP_Query( $args );

$this->assertTrue( $query->elasticsearch_success );
$this->assertEquals( 2, $query->post_count );
$this->assertEquals( 2, $query->found_posts );
}

/**
* Test a query that searches and filters by a meta value like the query
*
Expand Down