Make WordPress Core

Changeset 36243


Ignore:
Timestamp:
01/09/2016 02:57:22 PM (9 years ago)
Author:
swissspidy
Message:

Taxonomy: Introduce unregister_taxonomy().

This new function can be used to completely unregister non built-in taxonomies.

Fixes #35227.

Location:
trunk
Files:
3 edited

Legend:

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

    r36214 r36243  
    474474     */
    475475    do_action( 'registered_taxonomy', $taxonomy, $object_type, $args );
     476}
     477
     478/**
     479 * Unregister a taxonomy.
     480 *
     481 * Can not be used to unregister built-in taxonomies.
     482 *
     483 * @since 4.5.0
     484 *
     485 * @global WP    $wp            Current WordPress environment instance.
     486 * @global array $wp_taxonomies List of taxonomies.
     487 *
     488 * @param string $taxonomy Taxonomy name.
     489 * @return bool|WP_Error True on success, WP_Error on failure.
     490 */
     491function unregister_taxonomy( $taxonomy ) {
     492    if ( ! taxonomy_exists( $taxonomy ) ) {
     493        return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     494    }
     495
     496    $taxonomy_args = get_taxonomy( $taxonomy );
     497
     498    // Do not allow unregistering internal taxonomies.
     499    if ( $taxonomy_args->_builtin ) {
     500        return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed' ) );
     501    }
     502
     503    global $wp, $wp_taxonomies;
     504
     505    // Remove query var.
     506    if ( false !== $taxonomy_args->query_var ) {
     507        $wp->remove_query_var( $taxonomy_args->query_var );
     508    }
     509
     510    // Remove rewrite tags and permastructs.
     511    if ( false !== $taxonomy_args->rewrite ) {
     512        remove_rewrite_tag( "%$taxonomy%" );
     513        remove_permastruct( $taxonomy );
     514    }
     515
     516    // Unregister callback handling for metabox.
     517    remove_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
     518
     519    // Remove the taxonomy.
     520    unset( $wp_taxonomies[ $taxonomy ] );
     521
     522    /**
     523     * Fires after a taxonomy is unregistered.
     524     *
     525     * @since 4.5.0
     526     *
     527     * @param string $taxonomy Taxonomy name.
     528     */
     529    do_action( 'unregistered_taxonomy', $taxonomy );
     530
     531    return true;
    476532}
    477533
  • trunk/tests/phpunit/includes/utils.php

    r35333 r36243  
    335335
    336336function _unregister_taxonomy( $taxonomy_name ) {
    337     unset( $GLOBALS['wp_taxonomies'][$taxonomy_name] );
     337    unregister_taxonomy( $taxonomy_name );
    338338}
    339339
  • trunk/tests/phpunit/tests/taxonomy.php

    r36108 r36243  
    531531        $this->assertFalse( $tax->query_var );
    532532    }
     533
     534    /**
     535     * @ticket 35227
     536     */
     537    public function test_unregister_taxonomy_unknown_taxonomy() {
     538        $this->assertWPError( unregister_taxonomy( 'foo' ) );
     539    }
     540
     541    /**
     542     * @ticket 35227
     543     */
     544    public function test_unregister_taxonomy_twice() {
     545        register_taxonomy( 'foo', 'post' );
     546        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     547        $this->assertWPError( unregister_taxonomy( 'foo' ) );
     548    }
     549
     550    /**
     551     * @ticket 35227
     552     */
     553    public function test_unregister_taxonomy_disallow_builtin_taxonomy() {
     554        $this->assertWPError( unregister_taxonomy( 'post_tag' ) );
     555        $this->assertWPError( unregister_taxonomy( 'category' ) );
     556    }
     557
     558    /**
     559     * @ticket 35227
     560     */
     561    public function test_unregister_taxonomy_removes_query_vars() {
     562        global $wp;
     563
     564        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     565
     566        $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
     567        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     568        $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
     569    }
     570
     571    /**
     572     * @ticket 35227
     573     */
     574    public function test_unregister_taxonomy_removes_permastruct() {
     575        $this->set_permalink_structure( '/%postname%' );
     576
     577        global $wp_rewrite;
     578
     579        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar', 'rewrite' => true ) );
     580
     581        $this->assertInternalType( 'array', $wp_rewrite->extra_permastructs['foo'] );
     582        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     583        $this->assertFalse( isset($wp_rewrite->extra_permastructs['foo'] ) );
     584    }
     585
     586    /**
     587     * @ticket 35227
     588     */
     589    public function test_unregister_taxonomy_removes_rewrite_rules() {
     590        $this->set_permalink_structure( '/%postname%' );
     591
     592        global $wp_rewrite;
     593
     594        register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     595
     596        $count_before = count( $wp_rewrite->rewritereplace );
     597
     598        $this->assertContains( '%foo%', $wp_rewrite->rewritecode );
     599        $this->assertContains( 'bar=', $wp_rewrite->queryreplace );
     600        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     601        $this->assertNotContains( '%foo%', $wp_rewrite->rewritecode );
     602        $this->assertNotContains( 'bar=', $wp_rewrite->queryreplace );
     603        $this->assertSame( --$count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
     604    }
     605
     606    /**
     607     * @ticket 35227
     608     */
     609    public function test_unregister_taxonomy_removes_taxonomy_from_global() {
     610        global $wp_taxonomies;
     611
     612        register_taxonomy( 'foo', 'post' );
     613
     614        $this->assertInternalType( 'object', $wp_taxonomies['foo'] );
     615        $this->assertInternalType( 'object', get_taxonomy( 'foo' ) );
     616
     617        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     618
     619        $this->assertFalse( isset( $wp_taxonomies['foo'] ) );
     620        $this->assertFalse( get_taxonomy( 'foo' ) );
     621    }
     622
     623    /**
     624     * @ticket 35227
     625     */
     626    public function test_unregister_taxonomy_removes_meta_box_callback() {
     627        global $wp_filter;
     628
     629        register_taxonomy( 'foo', 'post' );
     630
     631        $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) );
     632        $this->assertTrue( unregister_taxonomy( 'foo' ) );
     633        $this->assertSame( array(), $wp_filter['wp_ajax_add-foo'] );
     634    }
    533635}
Note: See TracChangeset for help on using the changeset viewer.