WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/21/2017 03:14:52 AM (8 months ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

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

    r41706 r42211  
    328328    if ( isset( $post_data['tax_input'] ) ) {
    329329        foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
    330             // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
    331             if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    332                 continue;
     330            $tax_object = get_taxonomy( $taxonomy );
     331
     332            if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
     333                $post_data['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
    333334            }
    334 
    335             /*
    336              * Assume that a 'tax_input' string is a comma-separated list of term names.
    337              * Some languages may use a character other than a comma as a delimiter, so we standardize on
    338              * commas before parsing the list.
    339              */
    340             if ( ! is_array( $terms ) ) {
    341                 $comma = _x( ',', 'tag delimiter' );
    342                 if ( ',' !== $comma ) {
    343                     $terms = str_replace( $comma, ',', $terms );
    344                 }
    345                 $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
    346             }
    347 
    348             $clean_terms = array();
    349             foreach ( $terms as $term ) {
    350                 // Empty terms are invalid input.
    351                 if ( empty( $term ) ) {
    352                     continue;
    353                 }
    354 
    355                 $_term = get_terms( $taxonomy, array(
    356                     'name' => $term,
    357                     'fields' => 'ids',
    358                     'hide_empty' => false,
    359                 ) );
    360 
    361                 if ( ! empty( $_term ) ) {
    362                     $clean_terms[] = intval( $_term[0] );
    363                 } else {
    364                     // No existing term was found, so pass the string. A new term will be created.
    365                     $clean_terms[] = $term;
    366                 }
    367             }
    368 
    369             $post_data['tax_input'][ $taxonomy ] = $clean_terms;
    370335        }
    371336    }
     
    18711836    exit;
    18721837}
     1838
     1839/**
     1840 * Sanitizes POST values from a checkbox taxonomy metabox.
     1841 *
     1842 * @since 5.0.0
     1843 *
     1844 * @param mixed $terms Raw term data from the 'tax_input' field.
     1845 * @return array
     1846 */
     1847function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonmy, $terms ) {
     1848    return array_map( 'intval', $terms );
     1849}
     1850
     1851/**
     1852 * Sanitizes POST values from an input taxonomy metabox.
     1853 *
     1854 * @since 5.0.0
     1855 *
     1856 * @param mixed $terms Raw term data from the 'tax_input' field.
     1857 * @return array
     1858 */
     1859function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
     1860    /*
     1861     * Assume that a 'tax_input' string is a comma-separated list of term names.
     1862     * Some languages may use a character other than a comma as a delimiter, so we standardize on
     1863     * commas before parsing the list.
     1864     */
     1865    if ( ! is_array( $terms ) ) {
     1866        $comma = _x( ',', 'tag delimiter' );
     1867        if ( ',' !== $comma ) {
     1868            $terms = str_replace( $comma, ',', $terms );
     1869        }
     1870        $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
     1871    }
     1872
     1873    $clean_terms = array();
     1874    foreach ( $terms as $term ) {
     1875        // Empty terms are invalid input.
     1876        if ( empty( $term ) ) {
     1877            continue;
     1878        }
     1879
     1880        $_term = get_terms( $taxonomy, array(
     1881            'name' => $term,
     1882            'fields' => 'ids',
     1883            'hide_empty' => false,
     1884        ) );
     1885
     1886        if ( ! empty( $_term ) ) {
     1887            $clean_terms[] = intval( $_term[0] );
     1888        } else {
     1889            // No existing term was found, so pass the string. A new term will be created.
     1890            $clean_terms[] = $term;
     1891        }
     1892    }
     1893
     1894    return $clean_terms;
     1895}
Note: See TracChangeset for help on using the changeset viewer.