Make WordPress Core

Ticket #35227: 35227.2.diff

File 35227.2.diff, 5.3 KB (added by swissspidy, 10 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 9865f42..0d21bfe 100644
    function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 
    476476}
    477477
    478478/**
     479 * Unregister a taxonomy.
     480 *
     481 * @since 4.5.0
     482 *
     483 * @global WP    $wp            Current WordPress environment instance.
     484 * @global array $wp_taxonomies List of taxonomies.
     485 *
     486 * @param string $taxonomy Taxonomy name.
     487 * @return bool|WP_Error True on success, WP_Error on failure.
     488 */
     489function unregister_taxonomy( $taxonomy ) {
     490        if ( ! taxonomy_exists( $taxonomy ) ) {
     491                return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     492        }
     493
     494        $taxonomy_args = get_taxonomy( $taxonomy );
     495
     496        // Do not allow unregistering internal taxonomies.
     497        if ( $taxonomy_args->_builtin ) {
     498                return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed' ) );
     499        }
     500
     501        global $wp_taxonomies, $wp;
     502
     503        // Remove query var.
     504        if ( false !== $taxonomy_args->query_var ) {
     505                $wp->remove_query_var( $taxonomy_args->query_var );
     506        }
     507
     508        // Remove rewrite tags and permastructs.
     509        if ( false !== $taxonomy_args->rewrite ) {
     510                remove_rewrite_tag( "%$taxonomy%" );
     511                remove_permastruct( $taxonomy );
     512        }
     513
     514        // Unregister callback handling for metabox.
     515        remove_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
     516
     517        // Remove the taxonomy.
     518        unset( $wp_taxonomies[ $taxonomy ] );
     519
     520        /**
     521         * Fires after a taxonomy is unregistered.
     522         *
     523         * @since 4.5.0
     524         *
     525         * @param string $taxonomy Taxonomy name.
     526         */
     527        do_action( 'unregistered_taxonomy', $taxonomy );
     528
     529        return true;
     530}
     531
     532/**
    479533 * Builds an object with all taxonomy labels out of a taxonomy object
    480534 *
    481535 * Accepted keys of the label array in the taxonomy object:
  • tests/phpunit/includes/utils.php

    diff --git tests/phpunit/includes/utils.php tests/phpunit/includes/utils.php
    index 0a6dfc1..00d1b32 100644
    function _unregister_post_type( $cpt_name ) { 
    334334}
    335335
    336336function _unregister_taxonomy( $taxonomy_name ) {
    337         unset( $GLOBALS['wp_taxonomies'][$taxonomy_name] );
     337        unregister_taxonomy( $taxonomy_name );
    338338}
    339339
    340340/**
  • tests/phpunit/tests/taxonomy.php

    diff --git tests/phpunit/tests/taxonomy.php tests/phpunit/tests/taxonomy.php
    index 0cf6516..0b01939 100644
    class Tests_Taxonomy extends WP_UnitTestCase { 
    530530                $tax = get_taxonomy( 'wptests_tax' );
    531531                $this->assertFalse( $tax->query_var );
    532532        }
     533
     534        /**
     535         * @ticket 35227
     536         */
     537        public function test_unregister_taxonomy() {
     538                register_taxonomy( 'foo', 'post' );
     539                $this->assertWPError( unregister_taxonomy( 'foo' ) );
     540        }
     541
     542        /**
     543         * @ticket 35227
     544         */
     545        public function test_unregister_taxonomy_unknown_taxonomy() {
     546                $this->assertWPError( unregister_taxonomy( 'foo' ) );
     547        }
     548
     549        /**
     550         * @ticket 35227
     551         */
     552        public function test_unregister_taxonomy_twice() {
     553                register_taxonomy( 'foo', 'post' );
     554                $this->assertTrue( unregister_taxonomy( 'foo' ) );
     555                $this->assertWPError( unregister_taxonomy( 'foo' ) );
     556        }
     557
     558        /**
     559         * @ticket 35227
     560         */
     561        public function test_unregister_taxonomy_disallow_builtin_taxonomy() {
     562                $this->assertWPError( unregister_taxonomy( 'post_tag' ) );
     563                $this->assertWPError( unregister_taxonomy( 'category' ) );
     564        }
     565
     566        /**
     567         * @ticket 35227
     568         */
     569        public function test_unregister_taxonomy_removes_query_vars() {
     570                global $wp;
     571
     572                register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     573
     574                $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
     575                $this->assertTrue( unregister_taxonomy( 'foo' ) );
     576                $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
     577        }
     578
     579        /**
     580         * @ticket 35227
     581         */
     582        public function test_unregister_taxonomy_removes_rewrite_rules() {
     583                $this->set_permalink_structure( '/%postname%' );
     584
     585                global $wp_rewrite;
     586
     587                register_taxonomy( 'foo', 'post', array( 'query_var' => 'bar' ) );
     588
     589                $count_before = count( $wp_rewrite->rewritereplace );
     590
     591                $this->assertInternalType( 'int', array_search( '%foo%', $wp_rewrite->rewritecode ) );
     592                $this->assertInternalType( 'int', array_search( 'bar=', $wp_rewrite->queryreplace ) );
     593                $this->assertTrue( unregister_taxonomy( 'foo' ) );
     594                $this->assertFalse( array_search( '%foo%', $wp_rewrite->rewritecode ) );
     595                $this->assertFalse( array_search( 'bar=', $wp_rewrite->queryreplace ) );
     596                $this->assertSame( --$count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
     597        }
     598
     599        /**
     600         * @ticket 35227
     601         */
     602        public function test_unregister_taxonomy_removes_taxonomy_from_global() {
     603                global $wp_taxonomies;
     604
     605                register_taxonomy( 'foo', 'post' );
     606
     607                $this->assertInternalType( 'object', $wp_taxonomies['foo'] );
     608                $this->assertInternalType( 'object', get_taxonomy( 'foo' ) );
     609
     610                $this->assertTrue( unregister_taxonomy( 'foo' ) );
     611
     612                $this->assertFalse( isset( $wp_taxonomies['foo'] ) );
     613                $this->assertFalse( get_taxonomy( 'foo' ) );
     614        }
     615
     616        /**
     617         * @ticket 35227
     618         */
     619        public function test_unregister_taxonomy_removes_meta_box_callback() {
     620                global $wp_filter;
     621
     622                register_taxonomy( 'foo', 'post' );
     623
     624                $this->assertSame( 1, count( $wp_filter['wp_ajax_add-foo'] ) );
     625                $this->assertTrue( unregister_taxonomy( 'foo' ) );
     626                $this->assertSame( array(), $wp_filter['wp_ajax_add-foo'] );
     627        }
    533628}