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/src/wp-admin/includes/post.php

    r31323 r31359  
    313313        /** This filter is documented in wp-admin/includes/media.php */
    314314        $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
     315    }
     316
     317    // Convert taxonomy input to term IDs, to avoid ambiguity.
     318    if ( isset( $post_data['tax_input'] ) ) {
     319        foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
     320            // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
     321            if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     322                continue;
     323            }
     324
     325            /*
     326             * Assume that a 'tax_input' string is a comma-separated list of term names.
     327             * Some languages may use a character other than a comma as a delimiter, so we standardize on
     328             * commas before parsing the list.
     329             */
     330            if ( ! is_array( $terms ) ) {
     331                $comma = _x( ',', 'tag delimiter' );
     332                if ( ',' !== $comma ) {
     333                    $terms = str_replace( $comma, ',', $terms );
     334                }
     335                $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
     336            }
     337
     338            $clean_terms = array();
     339            foreach ( $terms as $term ) {
     340                $_term = get_terms( $taxonomy, array(
     341                    'name' => $term,
     342                    'fields' => 'ids',
     343                    'hide_empty' => false,
     344                ) );
     345
     346                if ( ! empty( $_term ) ) {
     347                    $clean_terms[] = intval( $_term[0] );
     348                } else {
     349                    // No existing term was found, so pass the string. A new term will be created.
     350                    $clean_terms[] = $term;
     351                }
     352            }
     353
     354            $post_data['tax_input'][ $taxonomy ] = $clean_terms;
     355        }
    315356    }
    316357
Note: See TracChangeset for help on using the changeset viewer.