WordPress.org

Make WordPress Core

Changeset 28951


Ignore:
Timestamp:
07/02/2014 01:27:07 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Clarify the docs and add more unit tests for wp_set_object_terms().

props DrewAPicture.
fixes #26570.

Location:
trunk
Files:
2 edited

Legend:

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

    r28902 r28951  
    26182618 * @uses wp_remove_object_terms()
    26192619 *
    2620  * @param int $object_id The object to relate to.
    2621  * @param array|int|string $terms The slug or id of the term, will replace all existing
    2622  * related terms in this taxonomy.
    2623  * @param array|string $taxonomy The context in which to relate the term to the object.
    2624  * @param bool $append If false will delete difference of terms.
    2625  * @return array|WP_Error Affected Term IDs
    2626  */
    2627 function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) {
     2620 * @param int              $object_id The object to relate to.
     2621 * @param array|int|string $terms     A single term slug, single term id, or array of either term slugs or ids.
     2622 *                                    Will replace all existing related terms in this taxonomy.
     2623 * @param array|string     $taxonomy The context in which to relate the term to the object.
     2624 * @param bool             $append    If false will delete difference of terms.
     2625 * @return array|WP_Error Affected Term IDs.
     2626 */
     2627function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
    26282628    global $wpdb;
    26292629
  • trunk/tests/phpunit/tests/term.php

    r28583 r28951  
    108108        // all terms should still be objects
    109109        return $terms;
     110    }
     111
     112    /**
     113     * @ticket 26570
     114     */
     115    function test_set_object_terms() {
     116        $non_hier = rand_str( 10 );
     117        $hier     = rand_str( 10 );
     118
     119        // Register taxonomies
     120        register_taxonomy( $non_hier, array() );
     121        register_taxonomy( $hier, array( 'hierarchical' => true ) );
     122
     123        // Create a post.
     124        $post_id = $this->factory->post->create();
     125
     126        /*
     127         * Set a single term (non-hierarchical) by ID.
     128         */
     129        $tag = wp_insert_term( 'Foo', $non_hier );
     130        $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
     131
     132        wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
     133        $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
     134
     135        /*
     136         * Set a single term (non-hierarchical) by slug.
     137         */
     138        $tag = wp_insert_term( 'Bar', $non_hier );
     139        $tag = get_term( $tag['term_id'], $non_hier );
     140
     141        $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) );
     142
     143        wp_set_object_terms( $post_id, $tag->slug, $non_hier );
     144        $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) );
     145
     146        /*
     147         * Set a single term (hierarchical) by ID.
     148         */
     149        $cat = wp_insert_term( 'Baz', $hier );
     150        $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) );
     151
     152        wp_set_object_terms( $post_id, $cat['term_id'], $hier );
     153        $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) );
     154
     155        /*
     156         * Set a single term (hierarchical) by slug.
     157         */
     158        $cat = wp_insert_term( 'Qux', $hier );
     159        $cat = get_term( $cat['term_id'], $hier );
     160
     161        $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
     162
     163        wp_set_object_terms( $post_id, $cat->slug, $hier );
     164        $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
     165
     166        /*
     167         * Set an array of term IDs (non-hierarchical) by ID.
     168         */
     169        $tag1 = wp_insert_term( '_tag1', $non_hier );
     170        $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
     171
     172        $tag2 = wp_insert_term( '_tag2', $non_hier );
     173        $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
     174
     175        $tag3 = wp_insert_term( '_tag3', $non_hier );
     176        $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
     177
     178        wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
     179        $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
     180
     181        /*
     182         * Set an array of term slugs (hierarchical) by slug.
     183         */
     184        $cat1 = wp_insert_term( '_cat1', $hier );
     185        $cat1 = get_term( $cat1['term_id'], $hier );
     186        $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
     187
     188        $cat2 = wp_insert_term( '_cat2', $hier );
     189        $cat2 = get_term( $cat2['term_id'], $hier );
     190        $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
     191
     192        $cat3 = wp_insert_term( '_cat3', $hier );
     193        $cat3 = get_term( $cat3['term_id'], $hier );
     194        $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
     195
     196        wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
     197        $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
    110198    }
    111199
Note: See TracChangeset for help on using the changeset viewer.