Make WordPress Core


Ignore:
Timestamp:
02/06/2015 08:31:37 PM (10 years ago)
Author:
boonebgorges
Message:

Parse non-hierarchical tag input into term IDs before sending to wp_insert_post().

When editing a post, non-hierarchical taxonomy terms are sent as the
comma-separated list entered into the tax_input metabox. Passing these
values directly to wp_update_post() meant that they were interpreted as
term slugs rather than term names, causing mismatches when a typed string
matched the slug of one term and the name of a different term. We fix the
problem by preprocessing tax_input data sent from post.php, converting it to
unambiguous term_ids before saving.

Props boonebgorges, ArminBraun.
Fixes #30615.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r31323 r31359  
    138138
    139139    /**
     140     * @ticket 30615
     141     */
     142    public function test_edit_post_should_parse_tax_input_by_name_rather_than_slug_for_nonhierarchical_taxonomies() {
     143        $u = $this->factory->user->create( array( 'role' => 'editor' ) );
     144        wp_set_current_user( $u );
     145
     146        register_taxonomy( 'wptests_tax', array( 'post' ) );
     147        $t1 = $this->factory->term->create( array(
     148            'taxonomy' => 'wptests_tax',
     149            'name' => 'foo',
     150            'slug' => 'bar',
     151        ) );
     152        $t2 = $this->factory->term->create( array(
     153            'taxonomy' => 'wptests_tax',
     154            'name' => 'bar',
     155            'slug' => 'foo',
     156        ) );
     157
     158        $p = $this->factory->post->create();
     159
     160        $post_data = array(
     161            'post_ID' => $p,
     162            'tax_input' => array(
     163                'wptests_tax' => 'foo,baz',
     164            ),
     165        );
     166
     167        edit_post( $post_data );
     168
     169        $found = wp_get_post_terms( $p, 'wptests_tax' );
     170
     171        // Should contain the term with the name 'foo', not the slug.
     172        $this->assertContains( $t1, wp_list_pluck( $found, 'term_id' ) );
     173
     174        // The 'baz' tag should have been created.
     175        $this->assertContains( 'baz', wp_list_pluck( $found, 'name' ) );
     176    }
     177
     178    /**
    140179     * @ticket 27792
    141180     */
Note: See TracChangeset for help on using the changeset viewer.