Docs: Add much more complete and syntactically correct documentation throughout the WP_REST_Meta_Fields class.

Props Soean, mrahmadawais, flixos90.
See #38398.

Built from https://develop.svn.wordpress.org/trunk@39022


git-svn-id: http://core.svn.wordpress.org/trunk@38964 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Drew Jaynes 2016-10-30 16:33:30 +00:00
parent 718f9fe868
commit 9ce98d98ac
2 changed files with 93 additions and 36 deletions

View File

@ -1,12 +1,24 @@
<?php <?php
/**
* REST API: WP_REST_Meta_Fields class
*
* @package WordPress
* @subpackage REST_API
* @since 4.7.0
*/
/** /**
* Manage meta values for an object. * Core class to manage meta values for an object via the REST API.
*
* @since 4.7.0
*/ */
abstract class WP_REST_Meta_Fields { abstract class WP_REST_Meta_Fields {
/** /**
* Get the object type for meta. * Retrieves the object meta type.
*
* @since 4.7.0
* @access protected
* *
* @return string One of 'post', 'comment', 'term', 'user', or anything * @return string One of 'post', 'comment', 'term', 'user', or anything
* else supported by `_get_meta_table()`. * else supported by `_get_meta_table()`.
@ -14,29 +26,40 @@ abstract class WP_REST_Meta_Fields {
abstract protected function get_meta_type(); abstract protected function get_meta_type();
/** /**
* Get the object type for `register_rest_field`. * Retrieves the object type for register_rest_field().
* *
* @return string Custom post type, 'taxonomy', 'comment', or `user`. * @since 4.7.0
* @access protected
*
* @return string The REST field type, such as post type name, taxonomy name, 'comment', or `user`.
*/ */
abstract protected function get_rest_field_type(); abstract protected function get_rest_field_type();
/** /**
* Register the meta field. * Registers the meta field.
*
* @since 4.7.0
* @access public
*
* @see register_rest_field()
*/ */
public function register_field() { public function register_field() {
register_rest_field( $this->get_rest_field_type(), 'meta', array( register_rest_field( $this->get_rest_field_type(), 'meta', array(
'get_callback' => array( $this, 'get_value' ), 'get_callback' => array( $this, 'get_value' ),
'update_callback' => array( $this, 'update_value' ), 'update_callback' => array( $this, 'update_value' ),
'schema' => $this->get_field_schema(), 'schema' => $this->get_field_schema(),
)); ));
} }
/** /**
* Get the `meta` field value. * Retrieves the meta field value.
*
* @since 4.7.0
* @access public
* *
* @param int $object_id Object ID to fetch meta for. * @param int $object_id Object ID to fetch meta for.
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_Error|object * @return WP_Error|object Object containing the meta values by name, otherwise WP_Error object.
*/ */
public function get_value( $object_id, $request ) { public function get_value( $object_id, $request ) {
$fields = $this->get_registered_fields(); $fields = $this->get_registered_fields();
@ -65,13 +88,16 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Prepare value for response. * Prepares a meta value for a response.
* *
* This is required because some native types cannot be stored correctly in * This is required because some native types cannot be stored correctly
* the database, such as booleans. We need to cast back to the relevant * in the database, such as booleans. We need to cast back to the relevant
* type before passing back to JSON. * type before passing back to JSON.
* *
* @param mixed $value Value to prepare. * @since 4.7.0
* @access protected
*
* @param mixed $value Meta value to prepare.
* @param WP_REST_Request $request Current request object. * @param WP_REST_Request $request Current request object.
* @param array $args Options for the field. * @param array $args Options for the field.
* @return mixed Prepared value. * @return mixed Prepared value.
@ -85,11 +111,14 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Update meta values. * Updates meta values.
* *
* @param WP_REST_Request $request Full details about the request. * @since 4.7.0
* @param int $object_id Object ID to fetch meta for. * @access public
* @return WP_Error|null Error if one occurs, null on success. *
* @param WP_REST_Request $request Full details about the request.
* @param int $object_id Object ID to fetch meta for.
* @return WP_Error|null WP_Error if one occurs, null on success.
*/ */
public function update_value( $request, $object_id ) { public function update_value( $request, $object_id ) {
$fields = $this->get_registered_fields(); $fields = $this->get_registered_fields();
@ -99,8 +128,10 @@ abstract class WP_REST_Meta_Fields {
continue; continue;
} }
// A null value means reset the field, which is essentially deleting it /*
// from the database and then relying on the default value. * A null value means reset the field, which is essentially deleting it
* from the database and then relying on the default value.
*/
if ( is_null( $request[ $name ] ) ) { if ( is_null( $request[ $name ] ) ) {
$result = $this->delete_meta_value( $object_id, $name ); $result = $this->delete_meta_value( $object_id, $name );
} elseif ( $args['single'] ) { } elseif ( $args['single'] ) {
@ -118,11 +149,14 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Delete meta value for an object. * Deletes a meta value for an object.
*
* @since 4.7.0
* @access protected
* *
* @param int $object_id Object ID the field belongs to. * @param int $object_id Object ID the field belongs to.
* @param string $name Key for the field. * @param string $name Key for the field.
* @return bool|WP_Error True if meta field is deleted, error otherwise. * @return bool|WP_Error True if meta field is deleted, WP_Error otherwise.
*/ */
protected function delete_meta_value( $object_id, $name ) { protected function delete_meta_value( $object_id, $name ) {
if ( ! current_user_can( 'delete_post_meta', $object_id, $name ) ) { if ( ! current_user_can( 'delete_post_meta', $object_id, $name ) ) {
@ -145,14 +179,17 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Update multiple meta values for an object. * Updates multiple meta values for an object.
* *
* Alters the list of values in the database to match the list of provided values. * Alters the list of values in the database to match the list of provided values.
* *
* @since 4.7.0
* @access protected
*
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $name Key for the custom field. * @param string $name Key for the custom field.
* @param array $values List of values to update to. * @param array $values List of values to update to.
* @return bool|WP_Error True if meta fields are updated, error otherwise. * @return bool|WP_Error True if meta fields are updated, WP_Error otherwise.
*/ */
protected function update_multi_meta_value( $object_id, $name, $values ) { protected function update_multi_meta_value( $object_id, $name, $values ) {
if ( ! current_user_can( 'edit_post_meta', $object_id, $name ) ) { if ( ! current_user_can( 'edit_post_meta', $object_id, $name ) ) {
@ -166,9 +203,11 @@ abstract class WP_REST_Meta_Fields {
$current = get_metadata( $this->get_meta_type(), $object_id, $name, false ); $current = get_metadata( $this->get_meta_type(), $object_id, $name, false );
$to_remove = $current; $to_remove = $current;
$to_add = $values; $to_add = $values;
foreach ( $to_add as $add_key => $value ) { foreach ( $to_add as $add_key => $value ) {
$remove_keys = array_keys( $to_remove, $value, true ); $remove_keys = array_keys( $to_remove, $value, true );
if ( empty( $remove_keys ) ) { if ( empty( $remove_keys ) ) {
continue; continue;
} }
@ -179,13 +218,14 @@ abstract class WP_REST_Meta_Fields {
} }
$remove_key = $remove_keys[0]; $remove_key = $remove_keys[0];
unset( $to_remove[ $remove_key ] ); unset( $to_remove[ $remove_key ] );
unset( $to_add[ $add_key ] ); unset( $to_add[ $add_key ] );
} }
// `delete_metadata` removes _all_ instances of the value, so only call // `delete_metadata` removes _all_ instances of the value, so only call once.
// once.
$to_remove = array_unique( $to_remove ); $to_remove = array_unique( $to_remove );
foreach ( $to_remove as $value ) { foreach ( $to_remove as $value ) {
if ( ! delete_metadata( $this->get_meta_type(), $object_id, wp_slash( $name ), wp_slash( $value ) ) ) { if ( ! delete_metadata( $this->get_meta_type(), $object_id, wp_slash( $name ), wp_slash( $value ) ) ) {
return new WP_Error( return new WP_Error(
@ -195,6 +235,7 @@ abstract class WP_REST_Meta_Fields {
); );
} }
} }
foreach ( $to_add as $value ) { foreach ( $to_add as $value ) {
if ( ! add_metadata( $this->get_meta_type(), $object_id, wp_slash( $name ), wp_slash( $value ) ) ) { if ( ! add_metadata( $this->get_meta_type(), $object_id, wp_slash( $name ), wp_slash( $value ) ) ) {
return new WP_Error( return new WP_Error(
@ -209,12 +250,15 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Update meta value for an object. * Updates a meta value for an object.
*
* @since 4.7.0
* @access protected
* *
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $name Key for the custom field. * @param string $name Key for the custom field.
* @param mixed $value Updated value. * @param mixed $value Updated value.
* @return bool|WP_Error True if meta field is updated, error otherwise. * @return bool|WP_Error True if the meta field was updated, WP_Error otherwise.
*/ */
protected function update_meta_value( $object_id, $name, $value ) { protected function update_meta_value( $object_id, $name, $value ) {
if ( ! current_user_can( 'edit_post_meta', $object_id, $name ) ) { if ( ! current_user_can( 'edit_post_meta', $object_id, $name ) ) {
@ -231,6 +275,7 @@ abstract class WP_REST_Meta_Fields {
// Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
$old_value = get_metadata( $meta_type, $object_id, $meta_key ); $old_value = get_metadata( $meta_type, $object_id, $meta_key );
if ( 1 === count( $old_value ) ) { if ( 1 === count( $old_value ) ) {
if ( $old_value[0] === $meta_value ) { if ( $old_value[0] === $meta_value ) {
return true; return true;
@ -249,9 +294,12 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Get all the registered meta fields. * Retrieves all the registered meta fields.
* *
* @return array * @since 4.7.0
* @access protected
*
* @return array Registered fields.
*/ */
protected function get_registered_fields() { protected function get_registered_fields() {
$registered = array(); $registered = array();
@ -262,6 +310,7 @@ abstract class WP_REST_Meta_Fields {
} }
$rest_args = array(); $rest_args = array();
if ( is_array( $args['show_in_rest'] ) ) { if ( is_array( $args['show_in_rest'] ) ) {
$rest_args = $args['show_in_rest']; $rest_args = $args['show_in_rest'];
} }
@ -272,11 +321,13 @@ abstract class WP_REST_Meta_Fields {
'schema' => array(), 'schema' => array(),
'prepare_callback' => array( $this, 'prepare_value' ), 'prepare_callback' => array( $this, 'prepare_value' ),
); );
$default_schema = array( $default_schema = array(
'type' => null, 'type' => null,
'description' => empty( $args['description'] ) ? '' : $args['description'], 'description' => empty( $args['description'] ) ? '' : $args['description'],
'default' => isset( $args['default'] ) ? $args['default'] : null, 'default' => isset( $args['default'] ) ? $args['default'] : null,
); );
$rest_args = array_merge( $default_args, $rest_args ); $rest_args = array_merge( $default_args, $rest_args );
$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] ); $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );
@ -297,15 +348,18 @@ abstract class WP_REST_Meta_Fields {
} }
$registered[ $rest_args['name'] ] = $rest_args; $registered[ $rest_args['name'] ] = $rest_args;
} // End foreach(). }
return $registered; return $registered;
} }
/** /**
* Get the object's `meta` schema, conforming to JSON Schema. * Retrieves the object's meta schema, conforming to JSON Schema.
* *
* @return array * @since 4.7.0
* @access protected
*
* @return array Field schema data.
*/ */
public function get_field_schema() { public function get_field_schema() {
$fields = $this->get_registered_fields(); $fields = $this->get_registered_fields();
@ -325,15 +379,18 @@ abstract class WP_REST_Meta_Fields {
} }
/** /**
* Prepare a meta value for output. * Prepares a meta value for output.
* *
* Default preparation for meta fields. Override by passing the * Default preparation for meta fields. Override by passing the
* `prepare_callback` in your `show_in_rest` options. * `prepare_callback` in your `show_in_rest` options.
* *
* @since 4.7.0
* @access public
*
* @param mixed $value Meta value from the database. * @param mixed $value Meta value from the database.
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
* @param array $args REST-specific options for the meta key. * @param array $args REST-specific options for the meta key.
* @return mixed Value prepared for output. * @return mixed Value prepared for output. If a non-JsonSerializable object, null.
*/ */
public static function prepare_value( $value, $request, $args ) { public static function prepare_value( $value, $request, $args ) {
$type = $args['schema']['type']; $type = $args['schema']['type'];

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-beta1-39021'; $wp_version = '4.7-beta1-39022';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.