Make WordPress Core

Ticket #33532: cat-and-tag-querystring-arrays.patch

File cat-and-tag-querystring-arrays.patch, 7.1 KB (added by Veraxus, 8 years ago)

Fixes errors when parsing querystring with arrays for categories or tags, specifically

  • src/wp-includes/query.php

     
    18911891                        }
    18921892                }
    18931893
     1894                // If querystring cat is array, implode it
     1895                if ( is_array( $q['cat'] ) ) {
     1896                        $q['cat'] = implode( ',', $q['cat'] );
     1897                }
     1898
    18941899                // Category stuff
    18951900                if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
    18961901                        $cat_in = $cat_not_in = array();
     
    19661971                        );
    19671972                }
    19681973
     1974                // If querystring tag is array, implode it
     1975                if ( is_array( $q['tag'] ) ) {
     1976                        $q['tag'] = implode( ',', $q['tag'] );
     1977                }
     1978
    19691979                // Tag stuff
    19701980                if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
    19711981                        if ( strpos($q['tag'], ',') !== false ) {
  • tests/phpunit/tests/query.php

     
    133133
    134134                $this->assertContains( "ORDER BY $wpdb->posts.post_title DESC, $wpdb->posts.post_date DESC", $q->request );
    135135        }
     136   
     137        public function test_cat_querystring_single_term() {
     138                $c1 = $this->factory->category->create( array(
     139                        'name' => 'Test Category 1',
     140                        'slug' => 'test1',
     141                ) );
     142                $c2 = $this->factory->category->create( array(
     143                        'name' => 'Test Category 2',
     144                        'slug' => 'test2',
     145                ) );
     146               
     147                $p1 = $this->factory->post->create();
     148                $p2 = $this->factory->post->create();
     149                $p3 = $this->factory->post->create();
    136150
     151                wp_set_object_terms( $p1, $c1, 'category' );
     152                wp_set_object_terms( $p2, array($c1,$c2), 'category' );
     153                wp_set_object_terms( $p3, $c2, 'category' );
     154
     155                $url = add_query_arg( array(
     156                        'cat' => $c1,
     157                ), '/' );
     158
     159                $this->go_to( $url );
     160               
     161                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     162
     163                $this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
     164        }
     165
     166        public function test_category_querystring_multiple_terms_comma_separated() {
     167                $c1 = $this->factory->category->create( array(
     168                        'name' => 'Test Category 1',
     169                        'slug' => 'test1',
     170                ) );
     171                $c2 = $this->factory->category->create( array(
     172                        'name' => 'Test Category 2',
     173                        'slug' => 'test2',
     174                ) );
     175                $c3 = $this->factory->category->create( array(
     176                        'name' => 'Test Category 3',
     177                        'slug' => 'test3',
     178                ) );
     179
     180                $p1 = $this->factory->post->create();
     181                $p2 = $this->factory->post->create();
     182                $p3 = $this->factory->post->create();
     183                $p4 = $this->factory->post->create();
     184
     185                wp_set_object_terms( $p1, $c1, 'category' );
     186                wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
     187                wp_set_object_terms( $p3, $c2, 'category' );
     188                wp_set_object_terms( $p4, $c3, 'category' );
     189
     190                $url = add_query_arg( array(
     191                        'cat' => implode(',',array($c1,$c2)),
     192                ), '/' );
     193
     194                $this->go_to( $url );
     195
     196                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     197
     198                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     199        }
     200
     201        /**
     202         * @ticket 33532
     203         */
     204        public function test_category_querystring_multiple_terms_formatted_as_array() {
     205                $c1 = $this->factory->category->create( array(
     206                        'name' => 'Test Category 1',
     207                        'slug' => 'test1',
     208                ) );
     209                $c2 = $this->factory->category->create( array(
     210                        'name' => 'Test Category 2',
     211                        'slug' => 'test2',
     212                ) );
     213                $c3 = $this->factory->category->create( array(
     214                        'name' => 'Test Category 3',
     215                        'slug' => 'test3',
     216                ) );
     217
     218                $p1 = $this->factory->post->create();
     219                $p2 = $this->factory->post->create();
     220                $p3 = $this->factory->post->create();
     221                $p4 = $this->factory->post->create();
     222
     223                wp_set_object_terms( $p1, $c1, 'category' );
     224                wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
     225                wp_set_object_terms( $p3, $c2, 'category' );
     226                wp_set_object_terms( $p4, $c3, 'category' );
     227
     228                $url = add_query_arg( array(
     229                        'cat' => array($c1,$c2),
     230                ), '/' );
     231
     232                $this->go_to( $url );
     233
     234                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     235
     236                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     237        }
     238
     239
     240        public function test_tag_querystring_single_term() {
     241                $t1 = $this->factory->tag->create_and_get( array(
     242                        'name' => 'Test Tag 1',
     243                        'slug' => 'test1',
     244                ) );
     245                $t2 = $this->factory->tag->create_and_get( array(
     246                        'name' => 'Test Tag 2',
     247                        'slug' => 'test2',
     248                ) );
     249
     250                $p1 = $this->factory->post->create();
     251                $p2 = $this->factory->post->create();
     252                $p3 = $this->factory->post->create();
     253
     254                wp_set_object_terms( $p1, $t1->slug, 'post_tag' );
     255                wp_set_object_terms( $p2, array($t1->slug,$t2->slug), 'post_tag' );
     256                wp_set_object_terms( $p3, $t2->slug, 'post_tag' );
     257
     258                $url = add_query_arg( array(
     259                        'tag' => $t1,
     260                ), '/' );
     261
     262                $this->go_to( $url );
     263
     264                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     265
     266                $this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
     267        }
     268
     269        public function test_tag_querystring_multiple_terms_comma_separated() {
     270                $c1 = $this->factory->tag->create_and_get( array(
     271                        'name' => 'Test Tag 1',
     272                        'slug' => 'test1',
     273                ) );
     274                $c2 = $this->factory->tag->create_and_get( array(
     275                        'name' => 'Test Tag 2',
     276                        'slug' => 'test2',
     277                ) );
     278                $c3 = $this->factory->tag->create_and_get( array(
     279                        'name' => 'Test Tag 3',
     280                        'slug' => 'test3',
     281                ) );
     282
     283                $p1 = $this->factory->post->create();
     284                $p2 = $this->factory->post->create();
     285                $p3 = $this->factory->post->create();
     286                $p4 = $this->factory->post->create();
     287               
     288                wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
     289                wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
     290                wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
     291                wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
     292
     293                $url = add_query_arg( array(
     294                        'tag' => implode(',',array($c1->slug,$c2->slug)),
     295                ), '/' );
     296
     297                $this->go_to( $url );
     298
     299                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     300
     301                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     302        }
     303
     304        /**
     305         * @ticket #33532
     306         */
     307        public function test_tag_querystring_multiple_terms_formatted_as_array() {
     308                $c1 = $this->factory->tag->create_and_get( array(
     309                        'name' => 'Test Tag 1',
     310                        'slug' => 'test1',
     311                ) );
     312                $c2 = $this->factory->tag->create_and_get( array(
     313                        'name' => 'Test Tag 2',
     314                        'slug' => 'test2',
     315                ) );
     316                $c3 = $this->factory->tag->create_and_get( array(
     317                        'name' => 'Test Tag 3',
     318                        'slug' => 'test3',
     319                ) );
     320
     321                $p1 = $this->factory->post->create();
     322                $p2 = $this->factory->post->create();
     323                $p3 = $this->factory->post->create();
     324                $p4 = $this->factory->post->create();
     325
     326                wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
     327                wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
     328                wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
     329                wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
     330
     331                $url = add_query_arg( array(
     332                        'tag' => array($c1->slug,$c2->slug),
     333                ), '/' );
     334
     335                $this->go_to( $url );
     336
     337                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     338
     339                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     340        }
     341
    137342        public function test_custom_taxonomy_querystring_single_term() {
    138343                register_taxonomy( 'test_tax_cat', 'post' );
    139344