Make WordPress Core


Ignore:
Timestamp:
01/15/2016 12:19:15 PM (10 years ago)
Author:
swissspidy
Message:

Post Types: Introduce unregister_post_type().

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

Fixes #14761.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/types.php

    r34102 r36316  
    160160        _unregister_post_type( 'foo' );
    161161    }
     162
     163    /**
     164     * @ticket 14761
     165     */
     166    public function test_unregister_post_type() {
     167        register_post_type( 'foo' );
     168        $this->assertTrue( unregister_post_type( 'foo' ) );
     169    }
     170
     171    /**
     172     * @ticket 14761
     173     */
     174    public function test_unregister_post_type_unknown_post_type() {
     175        $this->assertWPError( unregister_post_type( 'foo' ) );
     176    }
     177
     178    /**
     179     * @ticket 14761
     180     */
     181    public function test_unregister_post_type_twice() {
     182        register_post_type( 'foo' );
     183        $this->assertTrue( unregister_post_type( 'foo' ) );
     184        $this->assertWPError( unregister_post_type( 'foo' ) );
     185    }
     186
     187    /**
     188     * @ticket 14761
     189     */
     190    public function test_unregister_post_type_disallow_builtin_post_type() {
     191        $this->assertWPError( unregister_post_type( 'post' ) );
     192        $this->assertWPError( unregister_post_type( 'page' ) );
     193        $this->assertWPError( unregister_post_type( 'attachment' ) );
     194        $this->assertWPError( unregister_post_type( 'revision' ) );
     195        $this->assertWPError( unregister_post_type( 'nav_menu_item' ) );
     196    }
     197
     198    /**
     199     * @ticket 14761
     200     */
     201    public function test_unregister_post_type_removes_query_vars() {
     202        global $wp;
     203
     204        register_post_type( 'foo', array(
     205            'public'    => true,
     206            'query_var' => 'bar',
     207        ) );
     208
     209        $this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
     210        $this->assertTrue( unregister_post_type( 'foo' ) );
     211        $this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
     212    }
     213
     214    /**
     215     * @ticket 14761
     216     */
     217    public function test_unregister_post_type_removes_rewrite_tags() {
     218        $this->set_permalink_structure( '/%postname%' );
     219
     220        global $wp_rewrite;
     221
     222        register_post_type( 'foo', array(
     223            'public'    => true,
     224            'query_var' => 'bar',
     225        ) );
     226
     227        $count_before = count( $wp_rewrite->rewritereplace );
     228
     229        $this->assertContains( '%foo%', $wp_rewrite->rewritecode );
     230        $this->assertContains( 'bar=', $wp_rewrite->queryreplace );
     231        $this->assertTrue( unregister_post_type( 'foo' ) );
     232        $this->assertNotContains( '%foo%', $wp_rewrite->rewritecode );
     233        $this->assertNotContains( 'bar=', $wp_rewrite->queryreplace );
     234        $this->assertSame( -- $count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
     235    }
     236
     237    /**
     238     * @ticket 14761
     239     */
     240    public function test_unregister_post_type_removes_rewrite_rules() {
     241        $this->set_permalink_structure( '/%postname%' );
     242
     243        global $wp_rewrite;
     244
     245        register_post_type( 'foo', array(
     246            'public'      => true,
     247            'has_archive' => true,
     248        ) );
     249
     250        $this->assertContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
     251        $this->assertTrue( unregister_post_type( 'foo' ) );
     252        $this->assertNotContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
     253    }
     254
     255    /**
     256     * @ticket 14761
     257     */
     258    public function test_unregister_post_type_removes_custom_meta_capabilities() {
     259        global $post_type_meta_caps;
     260
     261        register_post_type( 'foo', array(
     262            'public'          => true,
     263            'capability_type' => 'bar',
     264            'map_meta_cap' => true,
     265        ) );
     266
     267        $this->assertSame( 'read_post', $post_type_meta_caps['read_bar'] );
     268        $this->assertSame( 'delete_post', $post_type_meta_caps['delete_bar'] );
     269        $this->assertSame( 'edit_post', $post_type_meta_caps['edit_bar'] );
     270
     271        $this->assertTrue( unregister_post_type( 'foo' ) );
     272
     273        $this->assertFalse( isset( $post_type_meta_caps['read_bar'] ) );
     274        $this->assertFalse( isset( $post_type_meta_caps['delete_bar'] ) );
     275        $this->assertFalse( isset( $post_type_meta_caps['edit_bar'] ) );
     276    }
     277
     278    /**
     279     * @ticket 14761
     280     */
     281    public function test_unregister_post_type_removes_post_type_supports() {
     282        global $_wp_post_type_features;
     283
     284        register_post_type( 'foo', array(
     285            'public'   => true,
     286            'supports' => array( 'editor', 'author', 'title' ),
     287        ) );
     288
     289        $this->assertEqualSetsWithIndex(
     290            array(
     291                'editor' => true,
     292                'author' => true,
     293                'title'  => true,
     294            ),
     295            $_wp_post_type_features['foo']
     296        );
     297        $this->assertTrue( unregister_post_type( 'foo' ) );
     298        $this->assertFalse( isset( $_wp_post_type_features['foo'] ) );
     299    }
     300
     301    /**
     302     * @ticket 14761
     303     */
     304    public function test_unregister_post_type_removes_post_type_from_taxonomies() {
     305        global $wp_taxonomies;
     306
     307        register_post_type( 'foo', array(
     308            'public'     => true,
     309            'taxonomies' => array( 'category', 'post_tag' ),
     310        ) );
     311
     312        $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
     313        $this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
     314        $this->assertTrue( unregister_post_type( 'foo' ) );
     315        $this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
     316        $this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
     317        $this->assertEmpty( get_object_taxonomies( 'foo' ) );
     318    }
     319
     320    /**
     321     * @ticket 14761
     322     */
     323    public function test_unregister_post_type_removes_the_future_post_hooks() {
     324        global $wp_filter;
     325
     326        register_post_type( 'foo', array(
     327            'public' => true,
     328        ) );
     329
     330        $this->assertSame( 1, count( $wp_filter['future_foo'] ) );
     331        $this->assertTrue( unregister_post_type( 'foo' ) );
     332        $this->assertSame( array(), $wp_filter['future_foo'] );
     333    }
     334
     335    /**
     336     * @ticket 14761
     337     */
     338    public function test_unregister_post_type_removes_meta_box_callback() {
     339        global $wp_filter;
     340
     341        register_post_type( 'foo', array(
     342            'public'               => true,
     343            'register_meta_box_cb' => '__return_empty_string',
     344        ) );
     345
     346        $this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) );
     347        $this->assertTrue( unregister_post_type( 'foo' ) );
     348        $this->assertSame( array(), $wp_filter['add_meta_boxes_foo'] );
     349    }
     350
     351    /**
     352     * @ticket 14761
     353     */
     354    public function test_unregister_post_type_removes_post_type_from_global() {
     355        global $wp_post_types;
     356
     357        register_post_type( 'foo', array(
     358            'public' => true,
     359        ) );
     360
     361        $this->assertInternalType( 'object', $wp_post_types['foo'] );
     362        $this->assertInternalType( 'object', get_post_type_object( 'foo' ) );
     363
     364        $this->assertTrue( unregister_post_type( 'foo' ) );
     365
     366        $this->assertFalse( isset( $wp_post_types['foo'] ) );
     367        $this->assertNull( get_post_type_object( 'foo' ) );
     368    }
     369
     370    /**
     371     * @ticket 14761
     372     */
     373    public function test_post_type_does_not_exist_after_unregister_post_type() {
     374        register_post_type( 'foo', array(
     375            'public' => true,
     376        ) );
     377
     378        $this->assertTrue( unregister_post_type( 'foo' ) );
     379
     380        $this->assertFalse( post_type_exists( 'foo' ) );
     381    }
    162382}
Note: See TracChangeset for help on using the changeset viewer.