Query: Remove leading whitespace from certain database queries.

Unintended leading whitespace at the beginning of a raw MySQL query led to unexpected behavior such as broken pagination. Eliminating said whitespace avoids that.

Adds unit tests to prevent regressions.

Props wpfed, swissspidy, ironprogrammer, tadamarketing, afercia.
Fixes #56841.
Built from https://develop.svn.wordpress.org/trunk@57750


git-svn-id: http://core.svn.wordpress.org/trunk@57251 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-03-02 13:38:07 +00:00
parent 1a6de4d845
commit 02d60b4eb4
7 changed files with 55 additions and 55 deletions

View File

@ -964,14 +964,14 @@ class WP_Comment_Query {
$this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits; $this->sql_clauses['limits'] = $limits;
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
{$this->sql_clauses['select']} $this->request =
{$this->sql_clauses['from']} "{$this->sql_clauses['select']}
{$where} {$this->sql_clauses['from']}
{$this->sql_clauses['groupby']} {$where}
{$this->sql_clauses['orderby']} {$this->sql_clauses['groupby']}
{$this->sql_clauses['limits']} {$this->sql_clauses['orderby']}
"; {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
return (int) $wpdb->get_var( $this->request ); return (int) $wpdb->get_var( $this->request );

View File

@ -481,14 +481,14 @@ class WP_Network_Query {
$this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits; $this->sql_clauses['limits'] = $limits;
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
{$this->sql_clauses['select']} $this->request =
{$this->sql_clauses['from']} "{$this->sql_clauses['select']}
{$where} {$this->sql_clauses['from']}
{$this->sql_clauses['groupby']} {$where}
{$this->sql_clauses['orderby']} {$this->sql_clauses['groupby']}
{$this->sql_clauses['limits']} {$this->sql_clauses['orderby']}
"; {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
return (int) $wpdb->get_var( $this->request ); return (int) $wpdb->get_var( $this->request );

View File

@ -3104,14 +3104,14 @@ class WP_Query {
$found_rows = 'SQL_CALC_FOUND_ROWS'; $found_rows = 'SQL_CALC_FOUND_ROWS';
} }
$old_request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
SELECT $found_rows $distinct $fields $old_request =
FROM {$wpdb->posts} $join "SELECT $found_rows $distinct $fields
WHERE 1=1 $where FROM {$wpdb->posts} $join
$groupby WHERE 1=1 $where
$orderby $groupby
$limits $orderby
"; $limits";
$this->request = $old_request; $this->request = $old_request;
@ -3307,14 +3307,14 @@ class WP_Query {
if ( $split_the_query ) { if ( $split_the_query ) {
// First get the IDs and then fill in the objects. // First get the IDs and then fill in the objects.
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
SELECT $found_rows $distinct {$wpdb->posts}.ID $this->request =
FROM {$wpdb->posts} $join "SELECT $found_rows $distinct {$wpdb->posts}.ID
WHERE 1=1 $where FROM {$wpdb->posts} $join
$groupby WHERE 1=1 $where
$orderby $groupby
$limits $orderby
"; $limits";
/** /**
* Filters the Post IDs SQL request before sending. * Filters the Post IDs SQL request before sending.

View File

@ -695,14 +695,14 @@ class WP_Site_Query {
$this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits; $this->sql_clauses['limits'] = $limits;
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
{$this->sql_clauses['select']} $this->request =
{$this->sql_clauses['from']} "{$this->sql_clauses['select']}
{$where} {$this->sql_clauses['from']}
{$this->sql_clauses['groupby']} {$where}
{$this->sql_clauses['orderby']} {$this->sql_clauses['groupby']}
{$this->sql_clauses['limits']} {$this->sql_clauses['orderby']}
"; {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
return (int) $wpdb->get_var( $this->request ); return (int) $wpdb->get_var( $this->request );

View File

@ -752,13 +752,13 @@ class WP_Term_Query {
$this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : ''; $this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits; $this->sql_clauses['limits'] = $limits;
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
{$this->sql_clauses['select']} $this->request =
{$this->sql_clauses['from']} "{$this->sql_clauses['select']}
{$where} {$this->sql_clauses['from']}
{$this->sql_clauses['orderby']} {$where}
{$this->sql_clauses['limits']} {$this->sql_clauses['orderby']}
"; {$this->sql_clauses['limits']}";
$this->terms = null; $this->terms = null;

View File

@ -818,13 +818,13 @@ class WP_User_Query {
$this->results = apply_filters_ref_array( 'users_pre_query', array( null, &$this ) ); $this->results = apply_filters_ref_array( 'users_pre_query', array( null, &$this ) );
if ( null === $this->results ) { if ( null === $this->results ) {
$this->request = " // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
SELECT {$this->query_fields} $this->request =
{$this->query_from} "SELECT {$this->query_fields}
{$this->query_where} {$this->query_from}
{$this->query_orderby} {$this->query_where}
{$this->query_limit} {$this->query_orderby}
"; {$this->query_limit}";
$cache_value = false; $cache_value = false;
$cache_key = $this->generate_cache_key( $qv, $this->request ); $cache_key = $this->generate_cache_key( $qv, $this->request );
$cache_group = 'user-queries'; $cache_group = 'user-queries';

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.5-beta3-57749'; $wp_version = '6.5-beta3-57750';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.