Introduce register_taxonomy_for_object_type().

props leewillis77.
fixes #11058.


git-svn-id: https://develop.svn.wordpress.org/trunk@25596 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2013-09-24 02:54:00 +00:00
parent 3eea90937d
commit b0717588b7
2 changed files with 75 additions and 0 deletions

View File

@ -507,6 +507,32 @@ function register_taxonomy_for_object_type( $taxonomy, $object_type) {
return true;
}
/**
* Remove an already registered taxonomy from an object type.
*
* @since 3.7.0
*
* @param string $taxonomy Name of taxonomy object.
* @param string $object_type Name of the object type.
* @return bool True if successful, false if not.
*/
function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
global $wp_taxonomies;
if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
return false;
if ( ! get_post_type_object( $object_type ) )
return false;
$key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
if ( false === $key )
return false;
unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
return true;
}
//
// Term API
//

View File

@ -103,4 +103,53 @@ class Tests_Taxonomy extends WP_UnitTestCase {
function test_register_long_taxonomy() {
$this->assertInstanceOf( 'WP_Error', register_taxonomy( 'abcdefghijklmnopqrstuvwxyz0123456789', 'post', array() ) );
}
/**
* @ticket 11058
*/
function test_registering_taxonomies_to_object_types() {
// Create a taxonomy to test with
$tax = 'test_tax';
$this->assertFalse( taxonomy_exists($tax) );
register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
// Create a post type to test with
$post_type = 'test_cpt';
$this->assertFalse( get_post_type( $post_type ) );
$this->assertObjectHasAttribute( 'name', register_post_type( $post_type ) );
// Core taxonomy, core post type
$this->assertTrue( unregister_taxonomy_for_object_type( 'category', 'post' ) );
$this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'post' ) );
$this->assertTrue( register_taxonomy_for_object_type( 'category', 'post' ) );
// Core taxonomy, non-core post type
$this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
$this->assertTrue( unregister_taxonomy_for_object_type( 'category', $post_type ) );
$this->assertFalse( unregister_taxonomy_for_object_type( 'category', $post_type ) );
$this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
// Core taxonomies, non-post object types
$this->assertFalse( register_taxonomy_for_object_type( 'category', 'user' ) );
$this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) );
// Non-core taxonomy, core post type
$this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'post' ) );
$this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'post' ) );
$this->assertTrue( register_taxonomy_for_object_type( $tax, 'post' ) );
// Non-core taxonomy, non-core post type
$this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
$this->assertTrue( unregister_taxonomy_for_object_type( $tax, $post_type ) );
$this->assertFalse( unregister_taxonomy_for_object_type( $tax, $post_type ) );
$this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
// Non-core taxonomies, non-post object types
$this->assertFalse( register_taxonomy_for_object_type( $tax, 'user' ) );
$this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) );
unset($GLOBALS['wp_taxonomies'][$tax]);
_unregister_post_type( $post_type );
}
}