Changeset 42211
- Timestamp:
- 11/21/2017 03:14:52 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/post.php
r41706 r42211 328 328 if ( isset( $post_data['tax_input'] ) ) { 329 329 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 ) ); 333 334 } 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 on338 * 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;370 335 } 371 336 } … … 1871 1836 exit; 1872 1837 } 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 */ 1847 function 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 */ 1859 function 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 127 127 */ 128 128 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; 129 138 130 139 /** … … 258 267 'show_admin_column' => false, 259 268 'meta_box_cb' => null, 269 'meta_box_sanitize_cb' => null, 260 270 'capabilities' => array(), 261 271 'rewrite' => true, … … 346 356 $args['name'] = $this->name; 347 357 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 348 372 foreach ( $args as $property_name => $property_value ) { 349 373 $this->$property_name = $property_value; -
trunk/src/wp-includes/taxonomy.php
r41987 r42211 298 298 * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class' 299 299 * arguments to register the Taxonomy in REST API. 300 * @since 5.0.0 Introduced `meta_box_sanitize_cb` argument. 300 301 * 301 302 * @global array $wp_taxonomies Registered taxonomies. … … 340 341 * post_tags_meta_box() is used for non-hierarchical. If false, no meta 341 342 * 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`. 342 346 * @type array $capabilities { 343 347 * Array of capabilities for this taxonomy. -
trunk/tests/phpunit/tests/taxonomy.php
r41174 r42211 792 792 $this->assertSame( 'foo', $taxonomy->name ); 793 793 } 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 } 794 832 }
Note: See TracChangeset
for help on using the changeset viewer.