Ticket #35991: 35991.3.patch
File 35991.3.patch, 7.0 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-xmlrpc-server.php
399 399 } 400 400 401 401 /** 402 * Retrieve custom fields for term. 403 * 404 * @since 4.6.0 405 * 406 * @param int $post_id Post ID. 407 * @return array Custom fields, if exist. 408 */ 409 public function get_term_custom_fields($term_id) { 410 $term_id = (int) $term_id; 411 412 $custom_fields = array(); 413 414 foreach ( (array) has_term_meta($term_id) as $meta ) { 415 416 if ( ! current_user_can( 'manage_categories' ) ) 417 continue; 418 419 $custom_fields[] = array( 420 "id" => $meta['meta_id'], 421 "key" => $meta['meta_key'], 422 "value" => $meta['meta_value'] 423 ); 424 } 425 426 return $custom_fields; 427 } 428 429 /** 430 * Set custom fields for term. 431 * 432 * @since 4.6.0 433 * 434 * @param int $post_id Post ID. 435 * @param array $fields Custom fields. 436 */ 437 public function set_term_custom_fields($term_id, $fields) { 438 $term_id = (int) $term_id; 439 440 foreach ( (array) $fields as $meta ) { 441 if ( isset($meta['id']) ) { 442 $meta['id'] = (int) $meta['id']; 443 $pmeta = get_metadata_by_mid( 'term', $meta['id'] ); 444 if ( isset($meta['key']) ) { 445 $meta['key'] = wp_unslash( $meta['key'] ); 446 if ( $meta['key'] !== $pmeta->meta_key ) 447 continue; 448 $meta['value'] = wp_unslash( $meta['value'] ); 449 if ( current_user_can( 'manage_categories' ) ) 450 update_metadata_by_mid( 'term', $meta['id'], $meta['value'] ); 451 } elseif ( current_user_can( 'manage_categories' ) ) { 452 delete_metadata_by_mid( 'term', $meta['id'] ); 453 } 454 } elseif ( current_user_can( 'manage_categories' ) ) { 455 add_term_meta( $term_id, $meta['key'], $meta['value'] ); 456 } 457 } 458 } 459 460 /** 402 461 * Set up blog options property. 403 462 * 404 463 * Passes property through {@see 'xmlrpc_blog_options'} filter. … … 737 796 // Count we are happy to return as an integer because people really shouldn't use terms that much. 738 797 $_term['count'] = intval( $_term['count'] ); 739 798 799 // Get term meta 800 $_term['custom_fields'] = $this->get_term_custom_fields($_term['term_id']); 801 740 802 /** 741 803 * Filters XML-RPC-prepared data for the given term. 742 804 * … … 1916 1978 if ( ! $term ) 1917 1979 return new IXR_Error( 500, __( 'Sorry, your term could not be created. Something wrong happened.' ) ); 1918 1980 1981 if ( isset( $content_struct['custom_fields'] ) ) { 1982 $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] ); 1983 } 1984 1919 1985 return strval( $term['term_id'] ); 1920 1986 } 1921 1987 … … 2014 2080 if ( ! $term ) 2015 2081 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); 2016 2082 2083 // Update term meta 2084 if ( isset( $content_struct['custom_fields'] ) ) { 2085 $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] ); 2086 } 2087 2017 2088 return true; 2018 2089 } 2019 2090 -
src/wp-includes/taxonomy.php
1370 1370 } 1371 1371 1372 1372 /** 1373 * Get meta data for the given term ID. 1374 * 1375 * @since 4.6.0 1376 * 1377 * @global wpdb $wpdb WordPress database abstraction object. 1378 * 1379 * @param int $term_id 1380 * @return array|false 1381 */ 1382 function has_term_meta( $term_id ) { 1383 // Bail if term meta table is not installed. 1384 if ( get_option( 'db_version' ) < 34370 ) { 1385 return; 1386 } 1387 1388 global $wpdb; 1389 1390 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, term_id 1391 FROM $wpdb->termmeta WHERE term_id = %d 1392 ORDER BY meta_key,meta_id", $term_id), ARRAY_A ); 1393 } 1394 1395 /** 1373 1396 * Check if Term exists. 1374 1397 * 1375 1398 * Formerly is_term(), introduced in 2.3.0. -
tests/phpunit/tests/term/meta.php
407 407 public static function set_cache_results( $q ) { 408 408 $q->set( 'cache_results', true ); 409 409 } 410 411 /** 412 * @ticket 35991 413 */ 414 public function test_has_term_meta() { 415 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 416 417 $term_meta_id = add_term_meta( $t, 'foo', 'bar' ); 418 $meta = has_term_meta( $t ); 419 420 $this->assertInternalType( 'array', $meta ); 421 $this->assertEquals( array( 'meta_key' => 'foo', 'meta_value' => 'bar', 'meta_id' => $term_meta_id, 'term_id' => $t ), $meta[0] ); 422 } 423 410 424 } -
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 } 110 120 } -
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'] );