WordPress.org

Make WordPress Core

Changeset 25596


Ignore:
Timestamp:
09/24/2013 02:54:00 AM (5 years ago)
Author:
nacin
Message:

Introduce register_taxonomy_for_object_type().

props leewillis77.
fixes #11058.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r25576 r25596  
    508508}
    509509
     510/**
     511 * Remove an already registered taxonomy from an object type.
     512 *
     513 * @since 3.7.0
     514 *
     515 * @param string $taxonomy    Name of taxonomy object.
     516 * @param string $object_type Name of the object type.
     517 * @return bool True if successful, false if not.
     518 */
     519function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
     520    global $wp_taxonomies;
     521
     522    if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
     523        return false;
     524
     525    if ( ! get_post_type_object( $object_type ) )
     526        return false;
     527
     528    $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
     529    if ( false === $key )
     530        return false;
     531
     532    unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
     533    return true;
     534}
     535
    510536//
    511537// Term API
  • trunk/tests/phpunit/tests/taxonomy.php

    r25002 r25596  
    104104        $this->assertInstanceOf( 'WP_Error', register_taxonomy( 'abcdefghijklmnopqrstuvwxyz0123456789', 'post', array() ) );
    105105    }
     106
     107    /**
     108     * @ticket 11058
     109     */
     110    function test_registering_taxonomies_to_object_types() {
     111        // Create a taxonomy to test with
     112        $tax = 'test_tax';
     113        $this->assertFalse( taxonomy_exists($tax) );
     114        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     115
     116        // Create a post type to test with
     117        $post_type = 'test_cpt';
     118        $this->assertFalse( get_post_type( $post_type ) );
     119        $this->assertObjectHasAttribute( 'name', register_post_type( $post_type ) );
     120
     121        // Core taxonomy, core post type
     122        $this->assertTrue( unregister_taxonomy_for_object_type( 'category', 'post' ) );
     123        $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'post' ) );
     124        $this->assertTrue( register_taxonomy_for_object_type( 'category', 'post' ) );
     125
     126        // Core taxonomy, non-core post type
     127        $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
     128        $this->assertTrue( unregister_taxonomy_for_object_type( 'category', $post_type ) );
     129        $this->assertFalse( unregister_taxonomy_for_object_type( 'category', $post_type ) );
     130        $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
     131
     132        // Core taxonomies, non-post object types
     133        $this->assertFalse( register_taxonomy_for_object_type( 'category', 'user' ) );
     134        $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) );
     135
     136        // Non-core taxonomy, core post type
     137        $this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'post' ) );
     138        $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'post' ) );
     139        $this->assertTrue( register_taxonomy_for_object_type( $tax, 'post' ) );
     140
     141        // Non-core taxonomy, non-core post type
     142        $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
     143        $this->assertTrue( unregister_taxonomy_for_object_type( $tax, $post_type ) );
     144        $this->assertFalse( unregister_taxonomy_for_object_type( $tax, $post_type ) );
     145        $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
     146
     147        // Non-core taxonomies, non-post object types
     148        $this->assertFalse( register_taxonomy_for_object_type( $tax, 'user' ) );
     149        $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) );
     150
     151        unset($GLOBALS['wp_taxonomies'][$tax]);
     152        _unregister_post_type( $post_type );
     153
     154    }
    106155}
Note: See TracChangeset for help on using the changeset viewer.