Query: Ensure that the page parameter is scalar in WP_Query::get_posts().

The `page` query var only accepts a scalar value and passes the value through functions that assume a scalar value.

Adding an extra guard condition does not affect its functionality but does avoid a PHP fatal error for `trim()` when a non-scalar value such as an array is passed.

Follow-up to [2535], [53891].

Props brookedot, rlmc, mukesh27, SergeyBiryukov.
Fixes #56558.

git-svn-id: https://develop.svn.wordpress.org/trunk@56815 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-10-10 11:20:28 +00:00
parent 94d2f4e68b
commit 8c146ecb33
2 changed files with 17 additions and 2 deletions
+1 -2
View File
@@ -2020,8 +2020,7 @@ class WP_Query {
}
if ( isset( $q['page'] ) ) {
$q['page'] = trim( $q['page'], '/' );
$q['page'] = absint( $q['page'] );
$q['page'] = is_scalar( $q['page'] ) ? absint( trim( $q['page'], '/' ) ) : 0;
}
// If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
@@ -159,4 +159,20 @@ class Tests_Query_InvalidQueries extends WP_UnitTestCase {
// Only the published post should be returned.
$this->assertCount( 1, $query->posts );
}
/**
* Ensure a non-scalar page parameter does not throw a fatal error for trim().
*
* @ticket 56558
* @covers WP_Query::get_posts
*/
public function test_non_scalar_page_value() {
$query = new WP_Query(
array(
'page' => array( 1, 2, 3 ),
)
);
$this->assertSame( 0, $query->query_vars['page'] );
}
}