Make WordPress Core

Changeset 47317


Ignore:
Timestamp:
02/19/2020 03:50:38 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Discard tags_input parameter in wp_update_post() if it's the same as existing post tags.

This ensures that wp_update_post() does not unintentionally modify post tags if the post has several tags with the same name but different slugs.

Tags should only be modified if tags_input parameter was explicitly provided, and is different from the existing tags.

Props kaggdesign, SergeyBiryukov.
Fixes #45121.

Location:
trunk
Files:
2 edited

Legend:

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

    r47274 r47317  
    42184218    }
    42194219
     4220    // Discard 'tags_input' parameter if it's the same as existing post tags.
     4221    if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
     4222        $tags      = get_the_terms( $postarr['ID'], 'post_tag' );
     4223        $tag_names = array();
     4224
     4225        if ( $tags && ! is_wp_error( $tags ) ) {
     4226            $tag_names = wp_list_pluck( $tags, 'name' );
     4227        }
     4228
     4229        if ( $postarr['tags_input'] === $tag_names ) {
     4230            unset( $postarr['tags_input'] );
     4231        }
     4232    }
     4233
    42204234    return wp_insert_post( $postarr, $wp_error );
    42214235}
  • trunk/tests/phpunit/tests/post.php

    r47198 r47317  
    11191119
    11201120    /**
    1121      * If a post is sticky and is updated by a user that does not have the publish_post capability, it should _stay_
    1122      * sticky.
     1121     * If a post is sticky and is updated by a user that does not have the publish_post capability,
     1122     * it should _stay_ sticky.
    11231123     *
    11241124     * @ticket 24153
     
    11571157
    11581158    /**
    1159      * If the `edit_post()` method is invoked by a user without publish_posts permission, the sticky status of the post
    1160      * should not be changed.
     1159     * If the `edit_post()` method is invoked by a user without publish_posts permission,
     1160     * the sticky status of the post should not be changed.
    11611161     *
    11621162     * @ticket 24153
     
    14271427        self::assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date_gmt ), 'The dates should be equal', 2 );
    14281428    }
     1429
     1430    /**
     1431     * Test ensuring that wp_update_post() does not unintentionally modify post tags
     1432     * if the post has several tags with the same name but different slugs.
     1433     *
     1434     * Tags should only be modified if 'tags_input' parameter was explicitly provided,
     1435     * and is different from the existing tags.
     1436     *
     1437     * @ticket 45121
     1438     */
     1439    public function test_update_post_should_only_modify_post_tags_if_different_tags_input_was_provided() {
     1440        $tag_1 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_1' ) );
     1441        $tag_2 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_2' ) );
     1442        $tag_3 = wp_insert_term( 'wp_update_post_tag', 'post_tag', array( 'slug' => 'wp_update_post_tag_3' ) );
     1443
     1444        $post_id = self::factory()->post->create(
     1445            array(
     1446                'tags_input' => array( $tag_1['term_id'], $tag_2['term_id'] ),
     1447            )
     1448        );
     1449
     1450        $post = get_post( $post_id );
     1451
     1452        $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
     1453        $this->assertEqualSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags );
     1454
     1455        wp_update_post( $post );
     1456
     1457        $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
     1458        $this->assertEqualSets( array( $tag_1['term_id'], $tag_2['term_id'] ), $tags );
     1459
     1460        wp_update_post(
     1461            array(
     1462                'ID'         => $post->ID,
     1463                'tags_input' => array( $tag_2['term_id'], $tag_3['term_id'] ),
     1464            )
     1465        );
     1466
     1467        $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
     1468        $this->assertEqualSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
     1469    }
    14291470}
Note: See TracChangeset for help on using the changeset viewer.