Make WordPress Core

Changeset 57750


Ignore:
Timestamp:
03/02/2024 01:36:02 PM (2 years ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r57719 r57750  
    965965                $this->sql_clauses['limits']  = $limits;
    966966
    967                 $this->request = "
    968                         {$this->sql_clauses['select']}
    969                         {$this->sql_clauses['from']}
    970                         {$where}
    971                         {$this->sql_clauses['groupby']}
    972                         {$this->sql_clauses['orderby']}
    973                         {$this->sql_clauses['limits']}
    974                 ";
     967                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     968                $this->request =
     969                        "{$this->sql_clauses['select']}
     970                         {$this->sql_clauses['from']}
     971                         {$where}
     972                         {$this->sql_clauses['groupby']}
     973                         {$this->sql_clauses['orderby']}
     974                         {$this->sql_clauses['limits']}";
    975975
    976976                if ( $this->query_vars['count'] ) {
  • trunk/src/wp-includes/class-wp-network-query.php

    r57719 r57750  
    482482                $this->sql_clauses['limits']  = $limits;
    483483
    484                 $this->request = "
    485                         {$this->sql_clauses['select']}
    486                         {$this->sql_clauses['from']}
    487                         {$where}
    488                         {$this->sql_clauses['groupby']}
    489                         {$this->sql_clauses['orderby']}
    490                         {$this->sql_clauses['limits']}
    491                 ";
     484                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     485                $this->request =
     486                        "{$this->sql_clauses['select']}
     487                         {$this->sql_clauses['from']}
     488                         {$where}
     489                         {$this->sql_clauses['groupby']}
     490                         {$this->sql_clauses['orderby']}
     491                         {$this->sql_clauses['limits']}";
    492492
    493493                if ( $this->query_vars['count'] ) {
  • trunk/src/wp-includes/class-wp-query.php

    r57648 r57750  
    31053105                }
    31063106
    3107                 $old_request = "
    3108                         SELECT $found_rows $distinct $fields
    3109                         FROM {$wpdb->posts} $join
    3110                         WHERE 1=1 $where
    3111                         $groupby
    3112                         $orderby
    3113                         $limits
    3114                 ";
     3107                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     3108                $old_request =
     3109                        "SELECT $found_rows $distinct $fields
     3110                         FROM {$wpdb->posts} $join
     3111                         WHERE 1=1 $where
     3112                         $groupby
     3113                         $orderby
     3114                         $limits";
    31153115
    31163116                $this->request = $old_request;
     
    33083308                                // First get the IDs and then fill in the objects.
    33093309
    3310                                 $this->request = "
    3311                                         SELECT $found_rows $distinct {$wpdb->posts}.ID
    3312                                         FROM {$wpdb->posts} $join
    3313                                         WHERE 1=1 $where
    3314                                         $groupby
    3315                                         $orderby
    3316                                         $limits
    3317                                 ";
     3310                                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     3311                                $this->request =
     3312                                        "SELECT $found_rows $distinct {$wpdb->posts}.ID
     3313                                         FROM {$wpdb->posts} $join
     3314                                         WHERE 1=1 $where
     3315                                         $groupby
     3316                                         $orderby
     3317                                         $limits";
    33183318
    33193319                                /**
  • trunk/src/wp-includes/class-wp-site-query.php

    r57648 r57750  
    696696                $this->sql_clauses['limits']  = $limits;
    697697
    698                 $this->request = "
    699                         {$this->sql_clauses['select']}
    700                         {$this->sql_clauses['from']}
    701                         {$where}
    702                         {$this->sql_clauses['groupby']}
    703                         {$this->sql_clauses['orderby']}
    704                         {$this->sql_clauses['limits']}
    705                 ";
     698                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     699                $this->request =
     700                        "{$this->sql_clauses['select']}
     701                         {$this->sql_clauses['from']}
     702                         {$where}
     703                         {$this->sql_clauses['groupby']}
     704                         {$this->sql_clauses['orderby']}
     705                         {$this->sql_clauses['limits']}";
    706706
    707707                if ( $this->query_vars['count'] ) {
  • trunk/src/wp-includes/class-wp-term-query.php

    r57719 r57750  
    753753                $this->sql_clauses['limits']  = $limits;
    754754
    755                 $this->request = "
    756                         {$this->sql_clauses['select']}
    757                         {$this->sql_clauses['from']}
    758                         {$where}
    759                         {$this->sql_clauses['orderby']}
    760                         {$this->sql_clauses['limits']}
    761                 ";
     755                // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     756                $this->request =
     757                        "{$this->sql_clauses['select']}
     758                         {$this->sql_clauses['from']}
     759                         {$where}
     760                         {$this->sql_clauses['orderby']}
     761                         {$this->sql_clauses['limits']}";
    762762
    763763                $this->terms = null;
  • trunk/src/wp-includes/class-wp-user-query.php

    r56543 r57750  
    819819
    820820                if ( null === $this->results ) {
    821                         $this->request = "
    822                                 SELECT {$this->query_fields}
    823                                 {$this->query_from}
    824                                 {$this->query_where}
    825                                 {$this->query_orderby}
    826                                 {$this->query_limit}
    827                         ";
     821                        // Beginning of the string is on a new line to prevent leading whitespace. See https://core.trac.wordpress.org/ticket/56841.
     822                        $this->request =
     823                                "SELECT {$this->query_fields}
     824                                 {$this->query_from}
     825                                 {$this->query_where}
     826                                 {$this->query_orderby}
     827                                 {$this->query_limit}";
    828828                        $cache_value   = false;
    829829                        $cache_key     = $this->generate_cache_key( $qv, $this->request );
  • trunk/tests/phpunit/tests/comment/query.php

    r57653 r57750  
    53515351                $this->assertStringNotContainsString( ' comment_ID ', $wpdb->last_query );
    53525352        }
     5353
     5354        /**
     5355         * @ticket 56841
     5356         */
     5357        public function test_query_does_not_have_leading_whitespace() {
     5358                self::factory()->comment->create(
     5359                        array(
     5360                                'comment_post_ID' => self::$post_id,
     5361                                'user_id'         => 7,
     5362                        )
     5363                );
     5364
     5365                $q = new WP_Comment_Query();
     5366                $q->query(
     5367                        array(
     5368                                'count'   => true,
     5369                                'orderby' => 'none',
     5370                        )
     5371                );
     5372
     5373                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     5374        }
    53535375}
  • trunk/tests/phpunit/tests/multisite/wpNetworkQuery.php

    r55745 r57750  
    609609                        return array( get_network( self::$network_ids['wordpress.org/'] ) );
    610610                }
     611
     612                /**
     613                 * @ticket 56841
     614                 */
     615                public function test_wp_network_query_does_not_have_leading_whitespace() {
     616                        $q = new WP_Network_Query();
     617                        $q->query(
     618                                array(
     619                                        'fields'               => 'all',
     620                                        'number'               => 3,
     621                                        'order'                => 'ASC',
     622                                        'update_network_cache' => true,
     623                                )
     624                        );
     625
     626                        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     627                }
    611628        }
    612629
  • trunk/tests/phpunit/tests/multisite/wpSiteQuery.php

    r55745 r57750  
    11631163                        return array( get_site( self::$site_ids['wordpress.org/'] ) );
    11641164                }
     1165
     1166                /**
     1167                 * @ticket 56841
     1168                 */
     1169                public function test_wp_site_query_does_not_have_leading_whitespace() {
     1170                        $q = new WP_Site_Query();
     1171
     1172                        $q->query(
     1173                                array(
     1174                                        'fields'                 => 'ids',
     1175                                        'network_id'             => self::$network_ids['wordpress.org/'],
     1176                                        'number'                 => 3,
     1177                                        'order'                  => 'ASC',
     1178                                        'update_site_cache'      => true,
     1179                                        'update_site_meta_cache' => true,
     1180                                )
     1181                        );
     1182
     1183                        $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     1184                }
    11651185        }
    11661186
  • trunk/tests/phpunit/tests/post/query.php

    r57653 r57750  
    793793                $this->assertSame( (bool) wp_using_ext_object_cache(), $filter->get_args()[0][0] );
    794794        }
     795
     796        /**
     797         * @ticket 56841
     798         */
     799        public function test_query_does_not_have_leading_whitespace() {
     800                add_filter( 'split_the_query', '__return_false' );
     801
     802                $q = new WP_Query(
     803                        array(
     804                                'posts_per_page' => 501,
     805                        )
     806                );
     807
     808                remove_filter( 'split_the_query', '__return_false' );
     809
     810                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     811        }
     812
     813        /**
     814         * @ticket 56841
     815         */
     816        public function test_query_does_not_have_leading_whitespace_split_the_query() {
     817                add_filter( 'split_the_query', '__return_true' );
     818
     819                $q = new WP_Query(
     820                        array(
     821                                'posts_per_page' => 501,
     822                        )
     823                );
     824
     825                remove_filter( 'split_the_query', '__return_true' );
     826
     827                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     828        }
    795829}
  • trunk/tests/phpunit/tests/term/query.php

    r56555 r57750  
    11241124                );
    11251125        }
     1126
     1127        /**
     1128         * @ticket 56841
     1129         */
     1130        public function test_query_does_not_have_leading_whitespace() {
     1131                $q = new WP_Term_Query(
     1132                        array(
     1133                                'taxonomy'   => 'wptests_tax',
     1134                                'hide_empty' => true,
     1135                                'fields'     => 'ids',
     1136                        )
     1137                );
     1138
     1139                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     1140        }
    11261141}
  • trunk/tests/phpunit/tests/user/query.php

    r57325 r57750  
    23792379                );
    23802380        }
     2381
     2382        /**
     2383         * @ticket 56841
     2384         */
     2385        public function test_query_does_not_have_leading_whitespace() {
     2386                $q = new WP_User_Query(
     2387                        array(
     2388                                'number' => 2,
     2389                        )
     2390                );
     2391
     2392                $this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
     2393        }
    23812394}
Note: See TracChangeset for help on using the changeset viewer.