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

Fix a rare edge-case bug where invalid values in tax_query terms resulted in a broken query #2576

Merged
merged 1 commit into from
Jan 28, 2022

Conversation

rinatkhaziev
Copy link
Contributor

@rinatkhaziev rinatkhaziev commented Jan 28, 2022

Description of the Change

Having a tax_query with terms being an array of not valid values results in a broken query.

{
	"tax_query": [
		{
				"taxonomy": "category",
				"field": "slug",
				"terms": [
						null
				],
				"operator": "IN"
		},
		{
				"taxonomy": "category",
				"field": "term_id",
				"terms": [
						1
				],
				"operator": "NOT IN"
		}
	]
}

=> 400

{
        "error": {
            "root_cause": [
                {
                    "type": "parsing_exception",
                    "reason": "No value specified for terms query",
                    "line": 1,
                    "col": 507
                }
            ],
            "type": "parsing_exception",
            "reason": "No value specified for terms query",
            "line": 1,
            "col": 507
        }

Alternate Designs

Not sure, this seemed like a most logical place to put it to ensure no invalid terms would ever make it into the final query.

Possible Drawbacks

This is still not 100% bulletproof because in even edgier case where a non-empty array or an object instance is passed it would not get filtered by array_filter. E.g. this would likely break the query.

'terms' => [ [ 'arr' ], new YouShouldNotDoThat() ];

Verification Process

	// Without the fix
	$q = new WP_Query( [
		'ep_integrate' => true,
		'tax_query' => [
			array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => [ null ],
        ),
		]
	] );

	var_dump( $q->elasticsearch_success ); // => false
	// With the fix
	$q = new WP_Query( [
		'ep_integrate' => true,
		'tax_query' => [
			array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => [ null ],
        ),
		]
	] );

	var_dump( $q->elasticsearch_success ); // => true

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Changelog Entry

Fixed - Invalid values in tax_query terms won't result in a query failure

Credits

Props @rinatkhaziev

…lted in a broken query by passing terms array via array_filter to remove false and null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants