Make WordPress Core

Changeset 55921


Ignore:
Timestamp:
06/14/2023 11:49:36 PM (18 months ago)
Author:
peterwilsoncc
Message:

Taxonomy: Prevent deprecation notices clearing terms.

Prevents wp_set_object_terms() throwing a deprecation notice in PHP 8.1+ when passing an empty value as the second parameter to clear the terms.

Props audrasjb, chouby, costdev, jrf, peterwilsoncc, prashantbhivsane, sergeybiryukov.
Fixes #57923.

Location:
trunk
Files:
2 edited

Legend:

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

    r55759 r55921  
    27382738 * @param string|int|array $terms     A single term slug, single term ID, or array of either term slugs or IDs.
    27392739 *                                    Will replace all existing related terms in this taxonomy. Passing an
    2740  *                                    empty value will remove all related terms.
     2740 *                                    empty array will remove all related terms.
    27412741 * @param string           $taxonomy  The context in which to relate the term to the object.
    27422742 * @param bool             $append    Optional. If false will delete difference of terms. Default false.
     
    27522752    }
    27532753
    2754     if ( ! is_array( $terms ) ) {
     2754    if ( empty( $terms ) ) {
     2755        $terms = array();
     2756    } elseif ( ! is_array( $terms ) ) {
    27552757        $terms = array( $terms );
    27562758    }
  • trunk/tests/phpunit/tests/term/wpSetObjectTerms.php

    r52389 r55921  
    55 */
    66class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
    7     protected $taxonomy        = 'category';
     7    protected static $taxonomy = 'category';
    88    protected static $post_ids = array();
     9    protected static $term_ids = array();
    910
    1011    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    1112        self::$post_ids = $factory->post->create_many( 5 );
     13        self::$term_ids = $factory->term->create_many( 5, array( 'taxonomy' => self::$taxonomy ) );
    1214    }
    1315
     
    106108        for ( $i = 0; $i < 3; $i++ ) {
    107109            $term   = "term_{$i}";
    108             $result = wp_insert_term( $term, $this->taxonomy );
     110            $result = wp_insert_term( $term, self::$taxonomy );
    109111            $this->assertIsArray( $result );
    110112            $term_id[ $term ] = $result['term_id'];
     
    112114
    113115        foreach ( $ids as $id ) {
    114             $tt = wp_set_object_terms( $id, array_values( $term_id ), $this->taxonomy );
     116            $tt = wp_set_object_terms( $id, array_values( $term_id ), self::$taxonomy );
    115117            // Should return three term taxonomy IDs.
    116118            $this->assertCount( 3, $tt );
     
    119121        // Each term should be associated with every post.
    120122        foreach ( $term_id as $term => $id ) {
    121             $actual = get_objects_in_term( $id, $this->taxonomy );
     123            $actual = get_objects_in_term( $id, self::$taxonomy );
    122124            $this->assertSame( $ids, array_map( 'intval', $actual ) );
    123125        }
     
    125127        // Each term should have a count of 5.
    126128        foreach ( array_keys( $term_id ) as $term ) {
    127             $t = get_term_by( 'name', $term, $this->taxonomy );
     129            $t = get_term_by( 'name', $term, self::$taxonomy );
    128130            $this->assertSame( 5, $t->count );
    129131        }
     
    140142
    141143        foreach ( $ids as $id ) {
    142             $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
     144            $tt = wp_set_object_terms( $id, $terms, self::$taxonomy );
    143145            // Should return three term taxonomy IDs.
    144146            $this->assertCount( 3, $tt );
    145147            // Remember which term has which term_id.
    146148            for ( $i = 0; $i < 3; $i++ ) {
    147                 $term                    = get_term_by( 'name', $terms[ $i ], $this->taxonomy );
     149                $term                    = get_term_by( 'name', $terms[ $i ], self::$taxonomy );
    148150                $term_id[ $terms[ $i ] ] = (int) $term->term_id;
    149151            }
     
    152154        // Each term should be associated with every post.
    153155        foreach ( $term_id as $term => $id ) {
    154             $actual = get_objects_in_term( $id, $this->taxonomy );
     156            $actual = get_objects_in_term( $id, self::$taxonomy );
    155157            $this->assertSame( $ids, array_map( 'intval', $actual ) );
    156158        }
     
    158160        // Each term should have a count of 5.
    159161        foreach ( $terms as $term ) {
    160             $t = get_term_by( 'name', $term, $this->taxonomy );
     162            $t = get_term_by( 'name', $term, self::$taxonomy );
    161163            $this->assertSame( 5, $t->count );
    162164        }
     
    254256        for ( $i = 0; $i < 3; $i++ ) {
    255257            $term   = "term_{$i}";
    256             $result = wp_insert_term( $term, $this->taxonomy );
     258            $result = wp_insert_term( $term, self::$taxonomy );
    257259            $this->assertIsArray( $result );
    258260            $terms_1[ $i ] = $result['term_id'];
     
    264266
    265267        $term       = 'term';
    266         $result     = wp_insert_term( $term, $this->taxonomy );
     268        $result     = wp_insert_term( $term, self::$taxonomy );
    267269        $terms_2[1] = $result['term_id'];
    268270
    269271        // Set the initial terms.
    270         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
     272        $tt_1 = wp_set_object_terms( $post_id, $terms_1, self::$taxonomy );
    271273        $this->assertCount( 3, $tt_1 );
    272274
     
    274276        $terms = wp_get_object_terms(
    275277            $post_id,
    276             $this->taxonomy,
     278            self::$taxonomy,
    277279            array(
    278280                'fields'  => 'ids',
     
    283285
    284286        // Change the terms.
    285         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
     287        $tt_2 = wp_set_object_terms( $post_id, $terms_2, self::$taxonomy );
    286288        $this->assertCount( 2, $tt_2 );
    287289
     
    289291        $terms = wp_get_object_terms(
    290292            $post_id,
    291             $this->taxonomy,
     293            self::$taxonomy,
    292294            array(
    293295                'fields'  => 'ids',
     
    312314
    313315        // Set the initial terms.
    314         $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
     316        $tt_1 = wp_set_object_terms( $post_id, $terms_1, self::$taxonomy );
    315317        $this->assertCount( 3, $tt_1 );
    316318
     
    318320        $terms = wp_get_object_terms(
    319321            $post_id,
    320             $this->taxonomy,
     322            self::$taxonomy,
    321323            array(
    322324                'fields'  => 'names',
     
    327329
    328330        // Change the terms.
    329         $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
     331        $tt_2 = wp_set_object_terms( $post_id, $terms_2, self::$taxonomy );
    330332        $this->assertCount( 2, $tt_2 );
    331333
     
    333335        $terms = wp_get_object_terms(
    334336            $post_id,
    335             $this->taxonomy,
     337            self::$taxonomy,
    336338            array(
    337339                'fields'  => 'names',
     
    431433        $this->assertSame( array(), $tt_ids );
    432434    }
     435
     436    /**
     437     * Tests that empty values clear an object of all terms.
     438     *
     439     * @ticket 57923
     440     *
     441     * @dataProvider data_empty_value_should_clear_terms
     442     *
     443     * @param mixed $empty_value An empty value.
     444     */
     445    public function test_empty_value_should_clear_terms( $empty_value ) {
     446        $post_id = self::$post_ids[0];
     447
     448        // Assign some terms.
     449        wp_set_object_terms( $post_id, self::$term_ids, self::$taxonomy );
     450
     451        // Make sure the terms are set.
     452        $terms = wp_get_object_terms( $post_id, self::$taxonomy, array( 'fields' => 'names' ) );
     453        $this->assertNotEmpty( $terms, 'Terms should initially be applied to post object.' );
     454
     455        // Remove terms by passing an empty value.
     456        wp_set_object_terms( $post_id, $empty_value, self::$taxonomy );
     457
     458        // Make sure the terms have been removed.
     459        $terms = wp_get_object_terms( $post_id, self::$taxonomy, array( 'fields' => 'names' ) );
     460        $this->assertEmpty( $terms, 'An empty() value should clear terms from the post object.' );
     461    }
     462
     463    /**
     464     * Data provider.
     465     *
     466     * @return array[]
     467     */
     468    public function data_empty_value_should_clear_terms() {
     469        return array(
     470            '(bool) false' => array( false ),
     471            'null'         => array( null ),
     472            '(int) 0'      => array( 0 ),
     473            '(float) 0.0'  => array( 0.0 ),
     474            'empty string' => array( '' ),
     475            '(string) 0'   => array( '0' ),
     476            'empty array'  => array( array() ),
     477        );
     478    }
    433479}
Note: See TracChangeset for help on using the changeset viewer.