Make WordPress Core

Changeset 42211


Ignore:
Timestamp:
11/21/2017 03:14:52 AM (7 years 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.

Location:
trunk
Files:
4 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}
  • trunk/src/wp-includes/class-wp-taxonomy.php

    r41162 r42211  
    127127     */
    128128    public $meta_box_cb = null;
     129
     130    /**
     131     * The callback function for sanitizing taxonomy data saved from a meta box.
     132     *
     133     * @since 5.0.0
     134     * @access public
     135     * @var callable
     136     */
     137    public $meta_box_sanitize_cb = null;
    129138
    130139    /**
     
    258267            'show_admin_column'     => false,
    259268            'meta_box_cb'           => null,
     269            'meta_box_sanitize_cb'  => null,
    260270            'capabilities'          => array(),
    261271            'rewrite'               => true,
     
    346356        $args['name'] = $this->name;
    347357
     358        // Default meta box sanitization callback depends on the value of 'meta_box_cb'.
     359        if ( null === $args['meta_box_sanitize_cb'] ) {
     360            switch ( $args['meta_box_cb'] ) {
     361                case 'post_categories_meta_box' :
     362                    $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_checkboxes';
     363                break;
     364
     365                case 'post_tags_meta_box' :
     366                default :
     367                    $args['meta_box_sanitize_cb'] = 'taxonomy_meta_box_sanitize_cb_input';
     368                break;
     369            }
     370        }
     371
    348372        foreach ( $args as $property_name => $property_value ) {
    349373            $this->$property_name = $property_value;
  • trunk/src/wp-includes/taxonomy.php

    r41987 r42211  
    298298 * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class'
    299299 *              arguments to register the Taxonomy in REST API.
     300 * @since 5.0.0 Introduced `meta_box_sanitize_cb` argument.
    300301 *
    301302 * @global array $wp_taxonomies Registered taxonomies.
     
    340341 *                                                post_tags_meta_box() is used for non-hierarchical. If false, no meta
    341342 *                                                box is shown.
     343 *     @type callable      $meta_box_sanitize_cb  Callback function for sanitizing taxonomy data saved from a meta
     344 *                                                box. If no callback is defined, an appropriate one is determined
     345 *                                                based on the value of `$meta_box_cb`.
    342346 *     @type array         $capabilities {
    343347 *         Array of capabilities for this taxonomy.
  • trunk/tests/phpunit/tests/taxonomy.php

    r41174 r42211  
    792792        $this->assertSame( 'foo', $taxonomy->name );
    793793    }
     794
     795    /**
     796     * @ticket 36514
     797     */
     798    public function test_edit_post_hierarchical_taxonomy() {
     799
     800        $taxonomy_name = 'foo';
     801        $term_name     = 'bar';
     802
     803        register_taxonomy( $taxonomy_name, array( 'post' ), array(
     804            'hierarchical' => false,
     805            'meta_box_cb'  => 'post_categories_meta_box',
     806        ) );
     807        $post = self::factory()->post->create_and_get( array(
     808            'post_type' => 'post',
     809        ) );
     810
     811        $term_id  = self::factory()->term->create_object( array(
     812            'name'     => $term_name,
     813            'taxonomy' => $taxonomy_name,
     814        ) );
     815
     816        wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     817        $updated_post_id = edit_post( array(
     818            'post_ID'   => $post->ID,
     819            'post_type' => 'post',
     820            'tax_input' => array(
     821                $taxonomy_name => array(
     822                    (string) $term_id // Cast term_id as string to match whats sent in WP Admin.
     823                ),
     824            ),
     825        ) );
     826
     827        $terms_obj = get_the_terms( $updated_post_id, $taxonomy_name );
     828        $problematic_term = current( wp_list_pluck( $terms_obj, 'name' ) );
     829
     830        $this->assertEquals( $problematic_term, $term_name );
     831    }
    794832}
Note: See TracChangeset for help on using the changeset viewer.