Ticket #35991: 35991.5.patch
File 35991.5.patch, 7.3 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-xmlrpc-server.php
404 404 } 405 405 406 406 /** 407 * Retrieve custom fields for term. 408 * 409 * @since 4.8.0 410 * 411 * @param int $post_id Post ID. 412 * @return array Custom fields, if exist. 413 */ 414 public function get_term_custom_fields($term_id) { 415 $term_id = (int) $term_id; 416 417 $custom_fields = array(); 418 419 foreach ( (array) has_term_meta($term_id) as $meta ) { 420 421 if ( ! current_user_can( 'edit_term_meta', $term_id ) ) { 422 continue; 423 } 424 425 $custom_fields[] = array( 426 "id" => $meta['meta_id'], 427 "key" => $meta['meta_key'], 428 "value" => $meta['meta_value'] 429 ); 430 } 431 432 return $custom_fields; 433 } 434 435 /** 436 * Set custom fields for term. 437 * 438 * @since 4.8.0 439 * 440 * @param int $post_id Post ID. 441 * @param array $fields Custom fields. 442 */ 443 public function set_term_custom_fields($term_id, $fields) { 444 $term_id = (int) $term_id; 445 446 foreach ( (array) $fields as $meta ) { 447 if ( isset($meta['id']) ) { 448 $meta['id'] = (int) $meta['id']; 449 $pmeta = get_metadata_by_mid( 'term', $meta['id'] ); 450 if ( isset($meta['key']) ) { 451 $meta['key'] = wp_unslash( $meta['key'] ); 452 if ( $meta['key'] !== $pmeta->meta_key ) { 453 continue; 454 } 455 $meta['value'] = wp_unslash( $meta['value'] ); 456 if ( current_user_can( 'edit_term_meta', $term_id ) ) { 457 update_metadata_by_mid( 'term', $meta['id'], $meta['value'] ); 458 } 459 } elseif ( current_user_can( 'delete_term_meta', $term_id ) ) { 460 delete_metadata_by_mid( 'term', $meta['id'] ); 461 } 462 } elseif ( current_user_can( 'add_term_meta', $term_id ) ) { 463 add_term_meta( $term_id, $meta['key'], $meta['value'] ); 464 } 465 } 466 } 467 468 /** 407 469 * Set up blog options property. 408 470 * 409 471 * Passes property through {@see 'xmlrpc_blog_options'} filter. … … 742 804 // Count we are happy to return as an integer because people really shouldn't use terms that much. 743 805 $_term['count'] = intval( $_term['count'] ); 744 806 807 // Get term meta 808 $_term['custom_fields'] = $this->get_term_custom_fields($_term['term_id']); 809 745 810 /** 746 811 * Filters XML-RPC-prepared data for the given term. 747 812 * … … 1943 2008 if ( ! $term ) 1944 2009 return new IXR_Error( 500, __( 'Sorry, your term could not be created.' ) ); 1945 2010 2011 // Add term meta 2012 if ( isset( $content_struct['custom_fields'] ) ) { 2013 $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] ); 2014 } 2015 1946 2016 return strval( $term['term_id'] ); 1947 2017 } 1948 2018 … … 2042 2112 if ( ! $term ) 2043 2113 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); 2044 2114 2115 // Update term meta 2116 if ( isset( $content_struct['custom_fields'] ) ) { 2117 $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] ); 2118 } 2119 2045 2120 return true; 2046 2121 } 2047 2122 -
src/wp-includes/taxonomy.php
1241 1241 } 1242 1242 1243 1243 /** 1244 * Get meta data for the given term ID. 1245 * 1246 * @since 4.8.0 1247 * 1248 * @global wpdb $wpdb WordPress database abstraction object. 1249 * 1250 * @param int $term_id 1251 * @return array|false 1252 */ 1253 function has_term_meta( $term_id ) { 1254 // Bail if term meta table is not installed. 1255 if ( get_option( 'db_version' ) < 34370 ) { 1256 return; 1257 } 1258 1259 global $wpdb; 1260 1261 return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, term_id 1262 FROM $wpdb->termmeta WHERE term_id = %d 1263 ORDER BY meta_key,meta_id", $term_id), ARRAY_A ); 1264 } 1265 1266 /** 1244 1267 * Check if Term exists. 1245 1268 * 1246 1269 * 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->assertNotIXRError( $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->assertNotIXRError( $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->assertNotIXRError( $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->assertNotIXRError( $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'] );