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.
Built from https://develop.svn.wordpress.org/trunk@31359


git-svn-id: http://core.svn.wordpress.org/trunk@31340 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-02-06 20:32:22 +00:00
parent d7c6ac95bc
commit b9e661dcae
2 changed files with 42 additions and 1 deletions

View File

@ -314,6 +314,47 @@ function edit_post( $post_data = null ) {
$post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
}
// Convert taxonomy input to term IDs, to avoid ambiguity.
if ( isset( $post_data['tax_input'] ) ) {
foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
// Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
continue;
}
/*
* Assume that a 'tax_input' string is a comma-separated list of term names.
* Some languages may use a character other than a comma as a delimiter, so we standardize on
* commas before parsing the list.
*/
if ( ! is_array( $terms ) ) {
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$terms = str_replace( $comma, ',', $terms );
}
$terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
}
$clean_terms = array();
foreach ( $terms as $term ) {
$_term = get_terms( $taxonomy, array(
'name' => $term,
'fields' => 'ids',
'hide_empty' => false,
) );
if ( ! empty( $_term ) ) {
$clean_terms[] = intval( $_term[0] );
} else {
// No existing term was found, so pass the string. A new term will be created.
$clean_terms[] = $term;
}
}
$post_data['tax_input'][ $taxonomy ] = $clean_terms;
}
}
add_meta( $post_ID );
update_post_meta( $post_ID, '_edit_last', get_current_user_id() );

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31358';
$wp_version = '4.2-alpha-31359';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.