Post Types: Introduce unregister_post_type().

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

Fixes #14761.

git-svn-id: https://develop.svn.wordpress.org/trunk@36316 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-01-15 12:19:15 +00:00
parent 8e8cdb2ef4
commit a111eafd41
4 changed files with 313 additions and 16 deletions

View File

@@ -335,14 +335,7 @@ if ( !function_exists( 'str_getcsv' ) ) {
* Removes the post type and its taxonomy associations.
*/
function _unregister_post_type( $cpt_name ) {
unset( $GLOBALS['wp_post_types'][ $cpt_name ] );
unset( $GLOBALS['_wp_post_type_features'][ $cpt_name ] );
foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy ) {
if ( false !== $key = array_search( $cpt_name, $taxonomy->object_type ) ) {
unset( $taxonomy->object_type[$key] );
}
}
unregister_post_type( $cpt_name );
}
function _unregister_taxonomy( $taxonomy_name ) {

View File

@@ -159,4 +159,224 @@ class Tests_Post_Types extends WP_UnitTestCase {
_unregister_post_type( 'foo' );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type() {
register_post_type( 'foo' );
$this->assertTrue( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_unknown_post_type() {
$this->assertWPError( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_twice() {
register_post_type( 'foo' );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertWPError( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_disallow_builtin_post_type() {
$this->assertWPError( unregister_post_type( 'post' ) );
$this->assertWPError( unregister_post_type( 'page' ) );
$this->assertWPError( unregister_post_type( 'attachment' ) );
$this->assertWPError( unregister_post_type( 'revision' ) );
$this->assertWPError( unregister_post_type( 'nav_menu_item' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_query_vars() {
global $wp;
register_post_type( 'foo', array(
'public' => true,
'query_var' => 'bar',
) );
$this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_rewrite_tags() {
$this->set_permalink_structure( '/%postname%' );
global $wp_rewrite;
register_post_type( 'foo', array(
'public' => true,
'query_var' => 'bar',
) );
$count_before = count( $wp_rewrite->rewritereplace );
$this->assertContains( '%foo%', $wp_rewrite->rewritecode );
$this->assertContains( 'bar=', $wp_rewrite->queryreplace );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertNotContains( '%foo%', $wp_rewrite->rewritecode );
$this->assertNotContains( 'bar=', $wp_rewrite->queryreplace );
$this->assertSame( -- $count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_rewrite_rules() {
$this->set_permalink_structure( '/%postname%' );
global $wp_rewrite;
register_post_type( 'foo', array(
'public' => true,
'has_archive' => true,
) );
$this->assertContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertNotContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_custom_meta_capabilities() {
global $post_type_meta_caps;
register_post_type( 'foo', array(
'public' => true,
'capability_type' => 'bar',
'map_meta_cap' => true,
) );
$this->assertSame( 'read_post', $post_type_meta_caps['read_bar'] );
$this->assertSame( 'delete_post', $post_type_meta_caps['delete_bar'] );
$this->assertSame( 'edit_post', $post_type_meta_caps['edit_bar'] );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $post_type_meta_caps['read_bar'] ) );
$this->assertFalse( isset( $post_type_meta_caps['delete_bar'] ) );
$this->assertFalse( isset( $post_type_meta_caps['edit_bar'] ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_supports() {
global $_wp_post_type_features;
register_post_type( 'foo', array(
'public' => true,
'supports' => array( 'editor', 'author', 'title' ),
) );
$this->assertEqualSetsWithIndex(
array(
'editor' => true,
'author' => true,
'title' => true,
),
$_wp_post_type_features['foo']
);
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $_wp_post_type_features['foo'] ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_from_taxonomies() {
global $wp_taxonomies;
register_post_type( 'foo', array(
'public' => true,
'taxonomies' => array( 'category', 'post_tag' ),
) );
$this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
$this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
$this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
$this->assertEmpty( get_object_taxonomies( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_the_future_post_hooks() {
global $wp_filter;
register_post_type( 'foo', array(
'public' => true,
) );
$this->assertSame( 1, count( $wp_filter['future_foo'] ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertSame( array(), $wp_filter['future_foo'] );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_meta_box_callback() {
global $wp_filter;
register_post_type( 'foo', array(
'public' => true,
'register_meta_box_cb' => '__return_empty_string',
) );
$this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo'] ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertSame( array(), $wp_filter['add_meta_boxes_foo'] );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_from_global() {
global $wp_post_types;
register_post_type( 'foo', array(
'public' => true,
) );
$this->assertInternalType( 'object', $wp_post_types['foo'] );
$this->assertInternalType( 'object', get_post_type_object( 'foo' ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $wp_post_types['foo'] ) );
$this->assertNull( get_post_type_object( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_post_type_does_not_exist_after_unregister_post_type() {
register_post_type( 'foo', array(
'public' => true,
) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( post_type_exists( 'foo' ) );
}
}