Changeset 40916
- Timestamp:
- 06/18/2017 10:39:12 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-xmlrpc-server.php
r40692 r40916 400 400 } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) { 401 401 add_post_meta( $post_id, $meta['key'], $meta['value'] ); 402 } 403 } 404 } 405 406 /** 407 * Retrieve custom fields for a term. 408 * 409 * @since 4.9.0 410 * 411 * @param int $post_id Post ID. 412 * @return array Array of custom fields, if they 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 a term. 437 * 438 * @since 4.9.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'] ); 402 464 } 403 465 } … … 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. … … 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 } … … 2042 2112 if ( ! $term ) 2043 2113 return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) ); 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 } 2044 2119 2045 2120 return true; -
trunk/src/wp-includes/taxonomy.php
r40513 r40916 1242 1242 1243 1243 /** 1244 * Get all meta data, including meta IDs, for the given term ID. 1245 * 1246 * @since 4.9.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 false; 1257 } 1258 1259 global $wpdb; 1260 1261 return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, term_id FROM $wpdb->termmeta WHERE term_id = %d ORDER BY meta_key,meta_id", $term_id ), ARRAY_A ); 1262 } 1263 1264 /** 1244 1265 * Check if Term exists. 1245 1266 * -
trunk/tests/phpunit/tests/term/meta.php
r37589 r40916 405 405 } 406 406 407 /** 408 * @ticket 35991 409 */ 410 public function test_has_term_meta() { 411 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 412 413 $term_meta_id = add_term_meta( $t, 'foo', 'bar' ); 414 $meta = has_term_meta( $t ); 415 416 $this->assertSame( 1, count( $meta ) ); 417 418 $expected = array( 419 'meta_key' => 'foo', 420 'meta_value' => 'bar', 421 'meta_id' => $term_meta_id, 422 'term_id' => $t, 423 ); 424 425 $found = $meta[0]; 426 427 $this->assertEquals( $expected, $found ); 428 } 429 430 /** 431 * @ticket 35991 432 */ 433 public function test_has_term_meta_empty_results() { 434 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 435 436 $meta = has_term_meta( $t ); 437 438 $this->assertSame( array(), $meta ); 439 } 440 407 441 public static function set_cache_results( $q ) { 408 442 $q->set( 'cache_results', true ); -
trunk/tests/phpunit/tests/xmlrpc/wp/editTerm.php
r40417 r40916 146 146 $this->assertInternalType( 'boolean', $result ); 147 147 } 148 149 /** 150 * @ticket 35991 151 */ 152 public function test_update_term_meta() { 153 register_taxonomy( 'wptests_tax', 'post' ); 154 155 $t = self::factory()->term->create( array( 156 'taxonomy' => 'wptests_tax', 157 ) ); 158 $meta_id = add_term_meta( $t, 'foo', 'bar' ); 159 160 $this->make_user_by_role( 'editor' ); 161 162 $result = $this->myxmlrpcserver->wp_editTerm( array( 163 1, 164 'editor', 165 'editor', 166 $t, 167 array( 168 'taxonomy' => 'wptests_tax', 169 'custom_fields' => array( 170 array( 171 'id' => $meta_id, 172 'key' => 'foo', 173 'value' => 'baz', 174 ), 175 ), 176 ), 177 ) ); 178 179 $this->assertNotIXRError( $result ); 180 181 $found = get_term_meta( $t, 'foo', true ); 182 $this->assertSame( 'baz', $found ); 183 } 184 185 /** 186 * @ticket 35991 187 */ 188 public function test_delete_term_meta() { 189 register_taxonomy( 'wptests_tax', 'post' ); 190 191 $t = self::factory()->term->create( array( 192 'taxonomy' => 'wptests_tax', 193 ) ); 194 $meta_id = add_term_meta( $t, 'foo', 'bar' ); 195 196 $this->make_user_by_role( 'editor' ); 197 198 $result = $this->myxmlrpcserver->wp_editTerm( array( 199 1, 200 'editor', 201 'editor', 202 $t, 203 array( 204 'taxonomy' => 'wptests_tax', 205 'custom_fields' => array( 206 array( 207 'id' => $meta_id, 208 ), 209 ), 210 ), 211 ) ); 212 213 $this->assertNotIXRError( $result ); 214 215 $found = get_term_meta( $t, 'foo' ); 216 $this->assertSame( array(), $found ); 217 } 148 218 } -
trunk/tests/phpunit/tests/xmlrpc/wp/getTerm.php
r40417 r40916 70 70 71 71 $term = get_term( self::$term_id, 'category', ARRAY_A ); 72 $term['custom_fields'] = array(); 72 73 73 74 $result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) ); … … 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( 112 1, 113 'editor', 114 'editor', 115 'category', 116 self::$term_id, 117 ) ); 118 $this->assertNotIXRError( $result ); 119 120 $this->assertInternalType( 'array', $result['custom_fields'] ); 121 $term_meta = get_term_meta( self::$term_id, '', true ); 122 $this->assertEquals( $term_meta['foo'][0], $result['custom_fields'][0]['value'] ); 123 } 98 124 } -
trunk/tests/phpunit/tests/xmlrpc/wp/getTerms.php
r40417 r40916 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. -
trunk/tests/phpunit/tests/xmlrpc/wp/newTerm.php
r40417 r40916 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( 116 1, 117 'editor', 118 'editor', 119 array( 120 'taxonomy' => 'category', 121 'name' => 'Test meta', 122 'custom_fields' => array( 123 array( 124 'key' => 'key1', 125 'value' => 'value1', 126 ), 127 ), 128 ), 129 ) ); 130 $this->assertNotIXRError( $result ); 131 $this->assertStringMatchesFormat( '%d', $result ); 132 } 109 133 }
Note: See TracChangeset
for help on using the changeset viewer.