Make WordPress Core

Changeset 36525


Ignore:
Timestamp:
02/13/2016 03:50:37 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce publicly_queryable taxonomy argument.

Taxonomies registered as publicly_queryable can be queried as taxonomy
archives.

If not provided explicitly, the value of publicly_queryable is inherited
from public.

Props Chouby.
Fixes #34491.

Location:
trunk
Files:
4 edited

Legend:

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

    r36365 r36525  
    332332                $this->query_vars[$t->query_var] = str_replace( ' ', '+', $this->query_vars[$t->query_var] );
    333333
    334         // Don't allow non-public taxonomies to be queried from the front-end.
     334        // Don't allow non-publicly queryable taxonomies to be queried from the front-end.
    335335        if ( ! is_admin() ) {
    336             foreach ( get_taxonomies( array( 'public' => false ), 'objects' ) as $taxonomy => $t ) {
     336            foreach ( get_taxonomies( array( 'publicly_queryable' => false ), 'objects' ) as $taxonomy => $t ) {
    337337                /*
    338338                 * Disallow when set to the 'taxonomy' query var.
    339                  * Non-public taxonomies cannot register custom query vars. See register_taxonomy().
     339                 * Non-publicly queryable taxonomies cannot register custom query vars. See register_taxonomy().
    340340                 */
    341341                if ( isset( $this->query_vars['taxonomy'] ) && $taxonomy === $this->query_vars['taxonomy'] ) {
  • trunk/src/wp-includes/taxonomy.php

    r36516 r36525  
    289289 *                                                Default empty array.
    290290 *     @type string        $description           A short descriptive summary of what the taxonomy is for. Default empty.
    291  *     @type bool          $public                Whether the taxonomy is publicly queryable. Default true.
     291 *     @type bool          $public                Whether a taxonomy is intended for use publicly either via
     292 *                                                the admin interface or by front-end users. The default settings
     293 *                                                of `$publicly_queryable`, `$show_ui`, and `$show_in_nav_menus`
     294 *                                                are inherited from `$public`.
     295 *     @type bool          $publicly_queryable    Whether the taxonomy is publicly queryable.
     296 *                                                If not set, the default is inherited from `$public`
    292297 *     @type bool          $hierarchical          Whether the taxonomy is hierarchical. Default false.
    293298 *     @type bool          $show_ui               Whether to generate and allow a UI for managing terms in this taxonomy in
     
    363368        'description'           => '',
    364369        'public'                => true,
     370        'publicly_queryable'    => null,
    365371        'hierarchical'          => false,
    366372        'show_ui'               => null,
     
    384390    }
    385391
    386     // Non-public taxonomies should not register query vars, except in the admin.
    387     if ( false !== $args['query_var'] && ( is_admin() || false !== $args['public'] ) && ! empty( $wp ) ) {
     392    // If not set, default to the setting for public.
     393    if ( null === $args['publicly_queryable'] ) {
     394        $args['publicly_queryable'] = $args['public'];
     395    }
     396
     397    // Non-publicly queryable taxonomies should not register query vars, except in the admin.
     398    if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) && ! empty( $wp ) ) {
    388399        if ( true === $args['query_var'] )
    389400            $args['query_var'] = $taxonomy;
  • trunk/tests/phpunit/includes/utils.php

    r36316 r36525  
    363363
    364364    foreach ( get_taxonomies( array() , 'objects' ) as $t ) {
    365         if ( $t->public && ! empty( $t->query_var ) )
     365        if ( $t->publicly_queryable && ! empty( $t->query_var ) )
    366366            $GLOBALS['wp']->add_query_var( $t->query_var );
    367367    }
  • trunk/tests/phpunit/tests/taxonomy.php

    r36247 r36525  
    447447     * @ticket 21949
    448448     */
    449     public function test_nonpublic_taxonomy_should_not_be_queryable_using_taxname_query_var() {
     449    public function test_nonpublicly_queryable_taxonomy_should_not_be_queryable_using_taxname_query_var() {
    450450        register_taxonomy( 'wptests_tax', 'post', array(
    451             'public' => false,
     451            'publicly_queryable' => false,
    452452        ) );
    453453
     
    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() {
     469    public function test_it_should_be_possible_to_register_a_query_var_that_matches_the_name_of_a_nonpublicly_queryable_taxonomy() {
    470470        global $wp;
    471471
    472472        register_taxonomy( 'wptests_tax', 'post', array(
    473             'public' => false,
     473            'publicly_queryable' => false,
    474474        ) );
    475475        $t = $this->factory->term->create_and_get( array(
     
    502502     * @ticket 21949
    503503     */
    504     public function test_nonpublic_taxonomy_should_not_be_queryable_using_taxonomy_and_term_vars() {
     504    public function test_nonpublicly_queryable_taxonomy_should_not_be_queryable_using_taxonomy_and_term_vars() {
     505        register_taxonomy( 'wptests_tax', 'post', array(
     506            'publicly_queryable' => false,
     507        ) );
     508
     509        $t = self::factory()->term->create_and_get( array(
     510            'taxonomy' => 'wptests_tax',
     511        ) );
     512
     513        $p = self::factory()->post->create();
     514        wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
     515
     516        $this->go_to( '/?taxonomy=wptests_tax&term=' . $t->slug );
     517
     518        $this->assertFalse( is_tax( 'wptests_tax' ) );
     519    }
     520
     521    /**
     522     * @ticket 34491
     523     */
     524    public function test_public_taxonomy_should_be_publicly_queryable() {
     525        register_taxonomy( 'wptests_tax', 'post', array(
     526            'public' => true,
     527        ) );
     528
     529        $this->assertContains( 'wptests_tax', get_taxonomies( array( 'publicly_queryable' => true ) ) );
     530
     531        $t = self::factory()->term->create_and_get( array(
     532            'taxonomy' => 'wptests_tax',
     533        ) );
     534
     535        $p = self::factory()->post->create();
     536        wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
     537
     538        $this->go_to( '/?wptests_tax=' . $t->slug );
     539
     540        $this->assertTrue( is_tax( 'wptests_tax' ) );
     541    }
     542
     543    /**
     544     * @ticket 34491
     545     */
     546    public function test_private_taxonomy_should_not_be_publicly_queryable() {
    505547        register_taxonomy( 'wptests_tax', 'post', array(
    506548            'public' => false,
    507549        ) );
    508550
     551        $this->assertContains( 'wptests_tax', get_taxonomies( array( 'publicly_queryable' => false ) ) );
     552
    509553        $t = self::factory()->term->create_and_get( array(
    510554            'taxonomy' => 'wptests_tax',
     
    514558        wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
    515559
    516         $this->go_to( '/?taxonomy=wptests_tax&term=' . $t->slug );
     560        $this->go_to( '/?wptests_tax=' . $t->slug );
    517561
    518562        $this->assertFalse( is_tax( 'wptests_tax' ) );
     563    }
     564
     565    /**
     566     * @ticket 34491
     567     */
     568    public function test_private_taxonomy_should_be_overridden_by_publicly_queryable() {
     569        register_taxonomy( 'wptests_tax', 'post', array(
     570            'public' => false,
     571            'publicly_queryable' => true,
     572        ) );
     573
     574        $this->assertContains( 'wptests_tax', get_taxonomies( array( 'publicly_queryable' => true ) ) );
     575
     576        $t = self::factory()->term->create_and_get( array(
     577            'taxonomy' => 'wptests_tax',
     578        ) );
     579
     580        $p = self::factory()->post->create();
     581        wp_set_object_terms( $p, $t->slug, 'wptests_tax' );
     582
     583        $this->go_to( '/?wptests_tax=' . $t->slug );
     584
     585        $this->assertTrue( is_tax( 'wptests_tax' ) );
    519586    }
    520587
Note: See TracChangeset for help on using the changeset viewer.