Make WordPress Core

Ticket #35227: 35227.3.diff

File 35227.3.diff, 5.6 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 78dd52c..3c73094 100644
    function register_taxonomy( $taxonomy, $object_type, $args = array() ) { 
    476476}
    477477
    478478/**
     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;
     532}
     533
     534/**
    479535 * Builds an object with all taxonomy labels out of a taxonomy object
    480536 *
    481537 * 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..86cf622 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_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}