From b0717588b7f5bd271013dfb52a6bbec984eceae5 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 24 Sep 2013 02:54:00 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 26 +++++++++++++++++ tests/phpunit/tests/taxonomy.php | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index f43552ff92..c8191fe0b0 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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 // diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 8c2748258a..c9c6ee6fcc 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -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 ); + + } }