Ticket #35991: 35991.2.patch
File 35991.2.patch, 7.0 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-xmlrpc-server.php
386 386 } 387 387 388 388 /** 389 * Retrieve custom fields for term. 390 * 391 * @since 4.5.0 392 * 393 * @param int $post_id Post ID. 394 * @return array Custom fields, if exist. 395 */ 396 public function get_term_custom_fields($term_id) { 397 $term_id = (int) $term_id; 398 399 $custom_fields = array(); 400 401 foreach ( (array) has_term_meta($term_id) as $meta ) { 402 403 if ( ! current_user_can( 'manage_categories' ) ) 404 continue; 405 406 $custom_fields[] = array( 407 "id" => $meta['meta_id'], 408 "key" => $meta['meta_key'], 409 "value" => $meta['meta_value'] 410 ); 411 } 412 413 return $custom_fields; 414 } 415 416 /** 417 * Set custom fields for term. 418 * 419 * @since 4.5.0 420 * 421 * @param int $post_id Post ID. 422 * @param array $fields Custom fields. 423 */ 424 public function set_term_custom_fields($term_id, $fields) { 425 $term_id = (int) $term_id; 426 427 foreach ( (array) $fields as $meta ) { 428 if ( isset($meta['id']) ) { 429 $meta['id'] = (int) $meta['id']; 430 $pmeta = get_metadata_by_mid( 'term', $meta['id'] ); 431 if ( isset($meta['key']) ) { 432 $meta['key'] = wp_unslash( $meta['key'] ); 433 if ( $meta['key'] !== $pmeta->meta_key ) 434 continue; 435 $meta['value'] = wp_unslash( $meta['value'] ); 436 if ( current_user_can( 'manage_categories' ) ) 437 update_metadata_by_mid( 'term', $meta['id'], $meta['value'] ); 438 } elseif ( current_user_can( 'manage_categories' ) ) { 439 delete_metadata_by_mid( 'term', $meta['id'] ); 440 } 441 } elseif ( current_user_can( 'manage_categories' ) ) { 442 add_term_meta( $term_id, $meta['key'], $meta['value'] ); 443 } 444 } 445 } 446 447 /** 389 448 * Set up blog options property. 390 449 * 391 450 * Passes property through {@see 'xmlrpc_blog_options'} filter. … … 724 783 // Count we are happy to return as an integer because people really shouldn't use terms that much. 725 784 $_term['count'] = intval( $_term['count'] ); 726 785 786 // Get term meta 787 $_term['custom_fields'] = $this->get_term_custom_fields($_term['term_id']); 788 727 789 /** 728 790 * Filter XML-RPC-prepared data for the given term. 729 791 * … … 1897 1959 if ( ! $term ) 1898 1960 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) ); 1899 1961 1962 if ( isset( $content_struct['custom_fields'] ) ) { 1963 $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] ); 1964 } 1965 1900 1966 return strval( $term['term_id'] ); 1901 1967 } 1902 1968 … … 1995 2061 if ( ! $term ) 1996 2062 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); 1997 2063 2064 // Update term meta 2065 if ( isset( $content_struct['custom_fields'] ) ) { 2066 $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] ); 2067 } 2068 1998 2069 return true; 1999 2070 } 2000 2071 -
src/wp-includes/taxonomy.php
1877 1877 } 1878 1878 1879 1879 /** 1880 * Get meta data for the given term ID. 1881 * 1882 * @since 4.5.0 1883 * 1884 * @global wpdb $wpdb WordPress database abstraction object. 1885 * 1886 * @param int $term_id 1887 * @return array|false 1888 */ 1889 function has_term_meta( $term_id ) { 1890 // Bail if term meta table is not installed. 1891 if ( get_option( 'db_version' ) < 34370 ) { 1892 return; 1893 } 1894 1895 global $wpdb; 1896 1897 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, term_id 1898 FROM $wpdb->termmeta WHERE term_id = %d 1899 ORDER BY meta_key,meta_id", $term_id), ARRAY_A ); 1900 } 1901 1902 /** 1880 1903 * Check if Term exists. 1881 1904 * 1882 1905 * Formerly is_term(), introduced in 2.3.0. -
tests/phpunit/tests/term/meta.php
352 352 public static function set_cache_results( $q ) { 353 353 $q->set( 'cache_results', true ); 354 354 } 355 356 /** 357 * @ticket 35991 358 */ 359 public function test_has_term_meta() { 360 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 361 362 $term_meta_id = add_term_meta( $t, 'foo', 'bar' ); 363 $meta = has_term_meta( $t ); 364 365 $this->assertInternalType( 'array', $meta ); 366 $this->assertEquals( array( 'meta_key' => 'foo', 'meta_value' => 'bar', 'meta_id' => $term_meta_id, 'term_id' => $t ), $meta[0] ); 367 } 368 355 369 } -
tests/phpunit/tests/xmlrpc/wp/getTerm.php
11 11 12 12 $this->term = wp_insert_term( 'term' . rand_str() , 'category' ); 13 13 $this->assertInternalType( 'array', $this->term ); 14 15 // Add term meta to test wp.getTerm 16 add_term_meta( $this->term['term_id'], 'foo', 'bar' ); 14 17 } 15 18 16 19 function test_invalid_username_password() { … … 73 76 $result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', $this->term['term_id'] ) ); 74 77 75 78 $this->assertNotInstanceOf( 'IXR_Error', $result ); 79 80 /** 81 * Check custom term meta 82 * 83 * @ticket 35991 84 */ 85 $this->assertInternalType( 'array', $result['custom_fields'] ); 86 $term_meta = get_term_meta( $this->term['term_id'], '', true ); 87 $this->assertEquals( $term_meta['foo'][0], $result['custom_fields'][0]['value'] ); 88 unset($result['custom_fields']); 89 76 90 $this->assertEquals( $result, $term ); 77 91 78 92 // Check DataTypes -
tests/phpunit/tests/xmlrpc/wp/newTerm.php
107 107 $this->assertNotInstanceOf( 'IXR_Error', $result ); 108 108 $this->assertStringMatchesFormat( '%d', $result ); 109 109 } 110 111 /** 112 * @ticket 35991 113 */ 114 public function test_add_term_meta() { 115 $this->make_user_by_role( 'editor' ); 116 $result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'category', 'name' => 'Test meta', 'custom_fields' => array( array('key' => 'key1', 'value' => 'value1') ) ) ) ); 117 $this->assertNotInstanceOf( 'IXR_Error', $result ); 118 $this->assertStringMatchesFormat( '%d', $result ); 119 } 120 110 121 } 122 No newline at end of file -
tests/phpunit/tests/xmlrpc/wp/getTerms.php
49 49 50 50 foreach( $results as $term ) { 51 51 $this->assertInternalType( 'int', $term['count'] ); 52 53 // Check custom term meta 54 $this->assertInternalType( 'array', $term['custom_fields'] ); 52 55 53 56 // We expect all other IDs to be strings not integers so we don't return something larger than an XMLRPC integer can describe. 54 57 $this->assertStringMatchesFormat( '%d', $term['term_id'] );