Add term meta support to XML-RPC `addTerm` and `editTerm` endpoints.

This changeset also includes the new function `has_term_meta()`, a
counterpart to `has_meta()` (for posts).

Props enrico.sorcinelli.
Fixes #35991.
Built from https://develop.svn.wordpress.org/trunk@40916


git-svn-id: http://core.svn.wordpress.org/trunk@40766 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2017-06-18 10:40:46 +00:00
parent 395ff7aebe
commit 92175dbd33
3 changed files with 97 additions and 1 deletions

View File

@ -403,6 +403,68 @@ class wp_xmlrpc_server extends IXR_Server {
}
}
/**
* Retrieve custom fields for a term.
*
* @since 4.9.0
*
* @param int $post_id Post ID.
* @return array Array of custom fields, if they exist.
*/
public function get_term_custom_fields( $term_id ) {
$term_id = (int) $term_id;
$custom_fields = array();
foreach ( (array) has_term_meta( $term_id ) as $meta ) {
if ( ! current_user_can( 'edit_term_meta', $term_id ) ) {
continue;
}
$custom_fields[] = array(
'id' => $meta['meta_id'],
'key' => $meta['meta_key'],
'value' => $meta['meta_value'],
);
}
return $custom_fields;
}
/**
* Set custom fields for a term.
*
* @since 4.9.0
*
* @param int $post_id Post ID.
* @param array $fields Custom fields.
*/
public function set_term_custom_fields( $term_id, $fields ) {
$term_id = (int) $term_id;
foreach ( (array) $fields as $meta ) {
if ( isset( $meta['id'] ) ) {
$meta['id'] = (int) $meta['id'];
$pmeta = get_metadata_by_mid( 'term', $meta['id'] );
if ( isset( $meta['key'] ) ) {
$meta['key'] = wp_unslash( $meta['key'] );
if ( $meta['key'] !== $pmeta->meta_key ) {
continue;
}
$meta['value'] = wp_unslash( $meta['value'] );
if ( current_user_can( 'edit_term_meta', $term_id ) ) {
update_metadata_by_mid( 'term', $meta['id'], $meta['value'] );
}
} elseif ( current_user_can( 'delete_term_meta', $term_id ) ) {
delete_metadata_by_mid( 'term', $meta['id'] );
}
} elseif ( current_user_can( 'add_term_meta', $term_id ) ) {
add_term_meta( $term_id, $meta['key'], $meta['value'] );
}
}
}
/**
* Set up blog options property.
*
@ -742,6 +804,9 @@ class wp_xmlrpc_server extends IXR_Server {
// Count we are happy to return as an integer because people really shouldn't use terms that much.
$_term['count'] = intval( $_term['count'] );
// Get term meta.
$_term['custom_fields'] = $this->get_term_custom_fields( $_term['term_id'] );
/**
* Filters XML-RPC-prepared data for the given term.
*
@ -1943,6 +2008,11 @@ class wp_xmlrpc_server extends IXR_Server {
if ( ! $term )
return new IXR_Error( 500, __( 'Sorry, your term could not be created.' ) );
// Add term meta.
if ( isset( $content_struct['custom_fields'] ) ) {
$this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] );
}
return strval( $term['term_id'] );
}
@ -2042,6 +2112,11 @@ class wp_xmlrpc_server extends IXR_Server {
if ( ! $term )
return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
// Update term meta.
if ( isset( $content_struct['custom_fields'] ) ) {
$this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] );
}
return true;
}

View File

@ -1240,6 +1240,27 @@ function update_termmeta_cache( $term_ids ) {
return update_meta_cache( 'term', $term_ids );
}
/**
* Get all meta data, including meta IDs, for the given term ID.
*
* @since 4.9.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $term_id
* @return array|false
*/
function has_term_meta( $term_id ) {
// Bail if term meta table is not installed.
if ( get_option( 'db_version' ) < 34370 ) {
return false;
}
global $wpdb;
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 );
}
/**
* Check if Term exists.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.9-alpha-40915';
$wp_version = '4.9-alpha-40916';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.