Ticket #35991: 35991.4.patch
File 35991.4.patch, 7.3 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.8.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( 'edit_term_meta', $term_id ) ) { 417 continue; 418 } 419 420 $custom_fields[] = array( 421 "id" => $meta['meta_id'], 422 "key" => $meta['meta_key'], 423 "value" => $meta['meta_value'] 424 ); 425 } 426 427 return $custom_fields; 428 } 429 430 /** 431 * Set custom fields for term. 432 * 433 * @since 4.8.0 434 * 435 * @param int $post_id Post ID. 436 * @param array $fields Custom fields. 437 */ 438 public function set_term_custom_fields($term_id, $fields) { 439 $term_id = (int) $term_id; 440 441 foreach ( (array) $fields as $meta ) { 442 if ( isset($meta['id']) ) { 443 $meta['id'] = (int) $meta['id']; 444 $pmeta = get_metadata_by_mid( 'term', $meta['id'] ); 445 if ( isset($meta['key']) ) { 446 $meta['key'] = wp_unslash( $meta['key'] ); 447 if ( $meta['key'] !== $pmeta->meta_key ) { 448 continue; 449 } 450 $meta['value'] = wp_unslash( $meta['value'] ); 451 if ( current_user_can( 'edit_term_meta', $term_id ) ) { 452 update_metadata_by_mid( 'term', $meta['id'], $meta['value'] ); 453 } 454 } elseif ( current_user_can( 'delete_term_meta', $term_id ) ) { 455 delete_metadata_by_mid( 'term', $meta['id'] ); 456 } 457 } elseif ( current_user_can( 'add_term_meta', $term_id ) ) { 458 add_term_meta( $term_id, $meta['key'], $meta['value'] ); 459 } 460 } 461 } 462 463 /** 402 464 * Set up blog options property. 403 465 * 404 466 * Passes property through {@see 'xmlrpc_blog_options'} filter. … … 737 799 // Count we are happy to return as an integer because people really shouldn't use terms that much. 738 800 $_term['count'] = intval( $_term['count'] ); 739 801 802 // Get term meta 803 $_term['custom_fields'] = $this->get_term_custom_fields($_term['term_id']); 804 740 805 /** 741 806 * Filters XML-RPC-prepared data for the given term. 742 807 * … … 1920 1985 if ( ! $term ) 1921 1986 return new IXR_Error( 500, __( 'Sorry, your term could not be created.' ) ); 1922 1987 1988 // Add term meta 1989 if ( isset( $content_struct['custom_fields'] ) ) { 1990 $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] ); 1991 } 1992 1923 1993 return strval( $term['term_id'] ); 1924 1994 } 1925 1995 … … 2019 2089 if ( ! $term ) 2020 2090 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); 2021 2091 2092 // Update term meta 2093 if ( isset( $content_struct['custom_fields'] ) ) { 2094 $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] ); 2095 } 2096 2022 2097 return true; 2023 2098 } 2024 2099 -
src/wp-includes/taxonomy.php
1279 1279 } 1280 1280 1281 1281 /** 1282 * Get meta data for the given term ID. 1283 * 1284 * @since 4.8.0 1285 * 1286 * @global wpdb $wpdb WordPress database abstraction object. 1287 * 1288 * @param int $term_id 1289 * @return array|false 1290 */ 1291 function has_term_meta( $term_id ) { 1292 // Bail if term meta table is not installed. 1293 if ( get_option( 'db_version' ) < 34370 ) { 1294 return; 1295 } 1296 1297 global $wpdb; 1298 1299 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, term_id 1300 FROM $wpdb->termmeta WHERE term_id = %d 1301 ORDER BY meta_key,meta_id", $term_id), ARRAY_A ); 1302 } 1303 1304 /** 1282 1305 * Check if Term exists. 1283 1306 * 1284 1307 * 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 } 410 423 } -
tests/phpunit/tests/xmlrpc/wp/getTerm.php
73 73 $result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) ); 74 74 75 75 $this->assertNotInstanceOf( 'IXR_Error', $result ); 76 unset($result['custom_fields']); 76 77 $this->assertEquals( $result, $term ); 77 78 78 79 // Check DataTypes … … 95 96 $this->assertEquals( 'category', $result['taxonomy'] ); 96 97 $this->assertEquals( $term['description'], $result['description'] ); 97 98 } 99 100 /** 101 * @ticket 35991 102 */ 103 public function test_get_term_meta () { 104 $this->make_user_by_role( 'editor' ); 105 106 // Add term meta to test wp.getTerm 107 add_term_meta( self::$term_id, 'foo', 'bar' ); 108 109 $term = get_term( self::$term_id, 'category', ARRAY_A ); 110 111 $result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) ); 112 $this->assertNotInstanceOf( 'IXR_Error', $result ); 113 114 $this->assertInternalType( 'array', $result['custom_fields'] ); 115 $term_meta = get_term_meta( self::$term_id, '', true ); 116 $this->assertEquals( $term_meta['foo'][0], $result['custom_fields'][0]['value'] ); 117 } 98 118 } -
tests/phpunit/tests/xmlrpc/wp/newTerm.php
106 106 $this->assertNotInstanceOf( 'IXR_Error', $result ); 107 107 $this->assertStringMatchesFormat( '%d', $result ); 108 108 } 109 110 /** 111 * @ticket 35991 112 */ 113 public function test_add_term_meta() { 114 $this->make_user_by_role( 'editor' ); 115 $result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'category', 'name' => 'Test meta', 'custom_fields' => array( array('key' => 'key1', 'value' => 'value1') ) ) ) ); 116 $this->assertNotInstanceOf( 'IXR_Error', $result ); 117 $this->assertStringMatchesFormat( '%d', $result ); 118 } 109 119 } -
tests/phpunit/tests/xmlrpc/wp/getTerms.php
50 50 foreach( $results as $term ) { 51 51 $this->assertInternalType( 'int', $term['count'] ); 52 52 53 // Check custom term meta 54 $this->assertInternalType( 'array', $term['custom_fields'] ); 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'] ); 55 58 $this->assertStringMatchesFormat( '%d', $term['term_group'] );