Introduce meta_box_sanitize_cb taxonomy argument.

The `meta_box_cb` argument was introduced in [25572] to allow plugin
authors to provide a custom callback for rendering their taxonomy's meta
box on the post edit screen. However, the routine used to handle the saving
of these custom taxonomy meta boxes was not customizable, but was instead
based simply on whether the taxonomy was hierarchicaly. See [13535].

The new `meta_box_sanitize_cb` argument defaults to the "tag" routine for
non-hierarchical taxonomies and the "category" routine for hierarchical ones,
thereby maintaining the current default behavior. Developers can override this
when the data passed from their `meta_box_cb` differs.

Props boonebgorges, ZaneMatthew, stephenharris.
Fixes #36514.

git-svn-id: https://develop.svn.wordpress.org/trunk@42211 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2017-11-21 03:14:52 +00:00
parent 887214f7ce
commit 4ec5d65dcc
4 changed files with 128 additions and 39 deletions

View File

@@ -327,46 +327,11 @@ function edit_post( $post_data = null ) {
// 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;
$tax_object = get_taxonomy( $taxonomy );
if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
$post_data['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
}
/*
* 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 ) {
// Empty terms are invalid input.
if ( empty( $term ) ) {
continue;
}
$_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;
}
}
@@ -1870,3 +1835,61 @@ function redirect_post($post_id = '') {
wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
exit;
}
/**
* Sanitizes POST values from a checkbox taxonomy metabox.
*
* @since 5.0.0
*
* @param mixed $terms Raw term data from the 'tax_input' field.
* @return array
*/
function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonmy, $terms ) {
return array_map( 'intval', $terms );
}
/**
* Sanitizes POST values from an input taxonomy metabox.
*
* @since 5.0.0
*
* @param mixed $terms Raw term data from the 'tax_input' field.
* @return array
*/
function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
/*
* 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 ) {
// Empty terms are invalid input.
if ( empty( $term ) ) {
continue;
}
$_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;
}
}
return $clean_terms;
}