Make WordPress Core

Changeset 35333


Ignore:
Timestamp:
10/21/2015 04:53:51 PM (9 years ago)
Author:
boonebgorges
Message:

Prevent non-public taxonomies from registering aquery var.

[34247] made the 'public' paramater of register_taxonomy() work by blocking
requests for non-public taxonomy archives during parse_request(). Blocking
taxonomy archive requests this late means that it's impossible to register an
independent query var that matches the slug of a non-public taxonomy. By
moving the block to register_taxonomy() - not allowing these taxonomies to
register their query vars in the first place - we free up the slug for other
use. In addition, we free up a bit of processing (no need to look for the query
var in parse_request() and better parallel the way non-public post types
work. See register_post_type().

Non-public taxonomy archives that are requested using ?taxonomy=tax_name are
still blocked during parse_request. It's only custom query vars -
?tax_name=term - that are affected by this change.

Props mboynes.
Fixes #21949.

Location:
trunk
Files:
4 edited

Legend:

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

    r35205 r35333  
    312312        if ( ! is_admin() ) {
    313313            foreach ( get_taxonomies( array( 'public' => false ), 'objects' ) as $taxonomy => $t ) {
    314                 // Check first for taxonomy-specific query_var.
    315                 if ( $t->query_var && isset( $this->query_vars[ $t->query_var ] ) ) {
    316                     unset( $this->query_vars[ $t->query_var ] );
    317                 }
    318 
    319                 // Next, check the 'taxonomy' query_var.
     314                /*
     315                 * Disallow when set to the 'taxonomy' query var.
     316                 * Non-public taxonomies cannot register custom query vars. See register_taxonomy().
     317                 */
    320318                if ( isset( $this->query_vars['taxonomy'] ) && $taxonomy === $this->query_vars['taxonomy'] ) {
    321319                    unset( $this->query_vars['taxonomy'], $this->query_vars['term'] );
  • trunk/src/wp-includes/taxonomy-functions.php

    r35322 r35333  
    385385    }
    386386
    387     if ( false !== $args['query_var'] && ! empty( $wp ) ) {
     387    if ( false !== $args['query_var'] && false !== $args['public'] && ! empty( $wp ) ) {
    388388        if ( true === $args['query_var'] )
    389389            $args['query_var'] = $taxonomy;
  • trunk/tests/phpunit/includes/utils.php

    r34802 r35333  
    358358
    359359    foreach ( get_taxonomies( array() , 'objects' ) as $t ) {
    360         if ( ! empty( $t->query_var ) )
     360        if ( $t->public && ! empty( $t->query_var ) )
    361361            $GLOBALS['wp']->add_query_var( $t->query_var );
    362362    }
  • trunk/tests/phpunit/tests/taxonomy.php

    r35242 r35333  
    467467     * @ticket 21949
    468468     */
     469    public function test_it_should_be_possible_to_register_a_query_var_that_matches_the_name_of_a_nonpublic_taxonomy() {
     470        global $wp;
     471
     472        register_taxonomy( 'wptests_tax', 'post', array(
     473            'public' => false,
     474        ) );
     475        $t = $this->factory->term->create_and_get( array(
     476            'taxonomy' => 'wptests_tax',
     477        ) );
     478
     479        $p = $this->factory->post->create();
     480        wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
     481
     482        add_filter( 'do_parse_request', array( $this, 'register_query_var' ) );
     483        $this->go_to( '/?wptests_tax=foo' );
     484        remove_filter( 'do_parse_request', array( $this, 'register_query_var' ) );
     485
     486        // Not a taxonomy...
     487        $this->assertFalse( is_tax( 'wptests_tax' ) );
     488
     489        // ...but query var works.
     490        $this->assertSame( 'foo', $wp->query_vars['wptests_tax'] );
     491    }
     492
     493    public static function register_query_var( $r ) {
     494        global $wp;
     495
     496        $wp->add_query_var( 'wptests_tax' );
     497
     498        return $r;
     499    }
     500
     501    /**
     502     * @ticket 21949
     503     */
    469504    public function test_nonpublic_taxonomy_should_not_be_queryable_using_taxonomy_and_term_vars() {
    470505        register_taxonomy( 'wptests_tax', 'post', array(
Note: See TracChangeset for help on using the changeset viewer.