<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39036] trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: Docs: Add much more complete and syntactically correct documentation throughout the `WP_REST_Users_Controller` class.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39036">39036</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39036","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>DrewAPicture</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-10-30 18:51:30 +0000 (Sun, 30 Oct 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Docs: Add much more complete and syntactically correct documentation throughout the `WP_REST_Users_Controller` class.
Props Soean, mrahmadawais, flixos90.
See <a href="https://core.trac.wordpress.org/ticket/38398">#38398</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php 2016-10-30 18:30:03 UTC (rev 39035)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php 2016-10-30 18:51:30 UTC (rev 39036)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,18 +1,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * REST API: WP_REST_Users_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 4.7.0
+ */
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Access users
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Core class used to manage users via the REST API.
+ *
+ * @since 4.7.0
+ *
+ * @see WP_REST_Controller
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_REST_Users_Controller extends WP_REST_Controller {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Instance of a user meta fields object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * @access protected
</span><span class="cx" style="display: block; padding: 0 10px"> * @var WP_REST_User_Meta_Fields
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected $meta;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Constructor.
+ *
+ * @since 4.7.0
+ * @access public
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->namespace = 'wp/v2';
</span><span class="cx" style="display: block; padding: 0 10px"> $this->rest_base = 'users';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21,45 +39,51 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Register the routes for the objects of the controller.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Registers the routes for the objects of the controller.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @see register_rest_route()
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function register_routes() {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> register_rest_route( $this->namespace, '/' . $this->rest_base, array(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_items' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_items' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'permission_callback' => array( $this, 'get_items_permissions_check' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'args' => $this->get_collection_params(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'args' => $this->get_collection_params(),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::CREATABLE,
- 'callback' => array( $this, 'create_item' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::CREATABLE,
+ 'callback' => array( $this, 'create_item' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'permission_callback' => array( $this, 'create_item_permissions_check' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'schema' => array( $this, 'get_public_item_schema' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_item' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_item' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'permission_callback' => array( $this, 'get_item_permissions_check' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'args' => array(
- 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'args' => array(
+ 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::EDITABLE,
- 'callback' => array( $this, 'update_item' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::EDITABLE,
+ 'callback' => array( $this, 'update_item' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'permission_callback' => array( $this, 'update_item_permissions_check' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::DELETABLE,
- 'callback' => array( $this, 'delete_item' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::DELETABLE,
+ 'callback' => array( $this, 'delete_item' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'args' => array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'args' => array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'force' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Required to be true, as resource does not support trashing.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -71,10 +95,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> register_rest_route( $this->namespace, '/' . $this->rest_base . '/me', array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'methods' => WP_REST_Server::READABLE,
- 'callback' => array( $this, 'get_current_item' ),
- 'args' => array(
- 'context' => array(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_current_item' ),
+ 'args' => array(
+ 'context' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'schema' => array( $this, 'get_public_item_schema' ),
</span><span class="cx" style="display: block; padding: 0 10px"> ));
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,8 +107,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Permissions check for getting all users.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return true|WP_Error True if the request has read access, otherwise WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_items_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Check if roles is specified in GET request and if user can list users.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -104,20 +131,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get all users
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves all users.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_items( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Retrieve the list of registered collection query parameters.
</span><span class="cx" style="display: block; padding: 0 10px"> $registered = $this->get_collection_params();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // This array defines mappings between public API query parameters whose
- // values are accepted as-passed, and their internal WP_Query parameter
- // name equivalents (some are the same). Only values which are also
- // present in $registered will be set.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * This array defines mappings between public API query parameters whose
+ * values are accepted as-passed, and their internal WP_Query parameter
+ * name equivalents (some are the same). Only values which are also
+ * present in $registered will be set.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> $parameter_mappings = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'exclude' => 'exclude',
</span><span class="cx" style="display: block; padding: 0 10px"> 'include' => 'include',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -129,8 +161,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // For each known parameter which is both registered and present in the request,
- // set the parameter's value on the query $prepared_args.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * For each known parameter which is both registered and present in the request,
+ * set the parameter's value on the query $prepared_args.
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $parameter_mappings as $api_param => $wp_param ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args[ $wp_param ] = $request[ $api_param ];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -170,10 +204,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter arguments, before passing to WP_User_Query, when querying users via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters WP_User_Query arguments when querying users via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @see https://developer.wordpress.org/reference/classes/wp_user_query/
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @link https://developer.wordpress.org/reference/classes/wp_user_query/
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param array $prepared_args Array of arguments for WP_User_Query.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request The current request.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -182,6 +218,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $query = new WP_User_Query( $prepared_args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $users = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $query->results as $user ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->prepare_item_for_response( $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $users[] = $this->prepare_response_for_collection( $data );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -191,33 +228,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Store pagination values for headers then unset for count query.
</span><span class="cx" style="display: block; padding: 0 10px"> $per_page = (int) $prepared_args['number'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_args['fields'] = 'ID';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $total_users = $query->get_total();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $total_users < 1 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Out-of-bounds, run the query again without LIMIT for total count
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Out-of-bounds, run the query again without LIMIT for total count.
</ins><span class="cx" style="display: block; padding: 0 10px"> unset( $prepared_args['number'], $prepared_args['offset'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $count_query = new WP_User_Query( $prepared_args );
</span><span class="cx" style="display: block; padding: 0 10px"> $total_users = $count_query->get_total();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'X-WP-Total', (int) $total_users );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $max_pages = ceil( $total_users / $per_page );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'X-WP-TotalPages', (int) $max_pages );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $page > 1 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prev_page = $page - 1;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $prev_page > $max_pages ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prev_page = $max_pages;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $prev_link = add_query_arg( 'page', $prev_page, $base );
</span><span class="cx" style="display: block; padding: 0 10px"> $response->link_header( 'prev', $prev_link );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $max_pages > $page ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $next_page = $page + 1;
</span><span class="cx" style="display: block; padding: 0 10px"> $next_link = add_query_arg( 'page', $next_page, $base );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->link_header( 'next', $next_link );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -225,10 +269,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access to read a user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to read a user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,13 +301,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get a single user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves a single user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $request['id'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = (int) $request['id'];
</ins><span class="cx" style="display: block; padding: 0 10px"> $user = get_userdata( $id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $id ) || empty( $user->ID ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -274,20 +324,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the current user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the current user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_current_item( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $current_user_id = get_current_user_id();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $current_user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user = wp_get_current_user();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = wp_get_current_user();
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $response );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $current_user_id ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $response->set_status( 302 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -295,10 +350,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access create users
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access create users.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function create_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -310,10 +368,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Create a single user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Creates a single user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function create_item( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['id'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -324,6 +385,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $check_permission = $this->check_role_update( $request['id'], $request['roles'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $check_permission ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $check_permission;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -333,6 +395,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_multisite() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $ret['errors'] ) && ! empty( $ret['errors']->errors ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $ret['errors'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -340,50 +403,61 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_multisite() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $user_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_user_create', __( 'Error creating new resource.' ), array( 'status' => 500 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $user->ID = $user_id;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user_id = wp_update_user( $user );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user_id = wp_update_user( $user );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $user_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = wp_insert_user( $user );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $user_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_user_by( 'id', $user_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> array_map( array( $user, 'add_role' ), $request['roles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_update = $this->meta->update_value( $request['meta'], $user_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $meta_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $fields_update = $this->update_additional_fields_for_object( $user, $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $fields_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Fires after a user is created or updated via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Fires immediately after a user is created or updated via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_User $user Data used to create the user.
- * @param WP_REST_Request $request Request object.
- * @param boolean $creating True when creating user, false when updating user.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param WP_User $user Data used to create the user.
+ * @param WP_REST_Request $request Request object.
+ * @param bool $creating True when creating user, false when updating user.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'rest_insert_user', $user, $request, true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $response );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response->set_status( 201 );
</span><span class="cx" style="display: block; padding: 0 10px"> $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -391,10 +465,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access update a user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access to update a user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function update_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -412,15 +489,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Update a single user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates a single user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function update_item( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $request['id'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = (int) $request['id'];
+ $user = get_userdata( $id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user = get_userdata( $id );
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! $user ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource id.' ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -439,6 +519,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['roles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $check_permission = $this->check_role_update( $id, $request['roles'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $check_permission ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $check_permission;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,28 +527,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = $this->prepare_item_for_database( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Ensure we're operating on the same user we already checked
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Ensure we're operating on the same user we already checked.
</ins><span class="cx" style="display: block; padding: 0 10px"> $user->ID = $id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user_id = wp_update_user( $user );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $user_id;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_user_by( 'id', $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $request['roles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> array_map( array( $user, 'add_role' ), $request['roles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = $this->get_item_schema();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $meta_update = $this->meta->update_value( $request['meta'], $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $meta_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $fields_update = $this->update_additional_fields_for_object( $user, $request );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $fields_update ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $fields_update;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -476,16 +562,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> do_action( 'rest_insert_user', $user, $request, false );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $response );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $response;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Check if a given request has access delete a user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Checks if a given request has access delete a user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Request $request Full details about the request.
- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function delete_item_permissions_check( $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -499,22 +590,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Delete a single user
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Deletes a single user.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|WP_REST_Response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function delete_item( $request ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $request['id'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = (int) $request['id'];
</ins><span class="cx" style="display: block; padding: 0 10px"> $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $force = isset( $request['force'] ) ? (bool) $request['force'] : false;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // We don't support trashing for this type, error out
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // We don't support trashing for this type, error out.
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $force ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_trash_not_supported', __( 'Users do not support trashing.' ), array( 'status' => 501 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_userdata( $id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $user ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource id.' ), array( 'status' => 404 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -526,6 +621,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'context', 'edit' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = $this->prepare_item_for_response( $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /** Include admin user functions to get access to wp_delete_user() */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -538,8 +634,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Fires after a user is deleted via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Fires immediately after a user is deleted via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_User $user The user data.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Response $response The response returned from the API.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request The request sent to the API.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -550,16 +648,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare a single user output for response
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares a single user output for response.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $user User object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @param WP_User $user User object.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Request object.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_REST_Response $response Response data.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_REST_Response Response object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function prepare_item_for_response( $user, $request ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $data = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $data = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> $schema = $this->get_item_schema();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['id'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $data['id'] = $user->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -634,24 +736,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->add_additional_fields_to_object( $data, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $this->filter_response_by_context( $data, $context );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Wrap the data in a response object
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Wrap the data in a response object.
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_ensure_response( $data );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $response->add_links( $this->prepare_links( $user ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter user data returned from the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters user data returned from the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_REST_Response $response The response object.
- * @param object $user User object used to create response.
- * @param WP_REST_Request $request Request object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
+ * @param WP_REST_Response $response The response object.
+ * @param object $user User object used to create response.
+ * @param WP_REST_Request $request Request object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'rest_prepare_user', $response, $user, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare links for the request.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares links for the user request.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Post $user User object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Links for the given user.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -669,8 +776,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Prepare a single user for create or update
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Prepares a single user for creation or update.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Request object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return object $prepared_user User object.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -683,9 +793,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['email'] ) && ! empty( $schema['properties']['email'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->user_email = $request['email'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['username'] ) && ! empty( $schema['properties']['username'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->user_login = $request['username'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['password'] ) && ! empty( $schema['properties']['password'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->user_pass = $request['password'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -694,21 +806,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['id'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->ID = absint( $request['id'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->display_name = $request['name'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['first_name'] ) && ! empty( $schema['properties']['first_name'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->first_name = $request['first_name'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['last_name'] ) && ! empty( $schema['properties']['last_name'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->last_name = $request['last_name'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['nickname'] ) && ! empty( $schema['properties']['nickname'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->nickname = $request['nickname'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['slug'] ) && ! empty( $schema['properties']['slug'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->user_nicename = $request['slug'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['description'] ) && ! empty( $schema['properties']['description'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $prepared_user->description = $request['description'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -723,8 +841,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Filter user data before inserting user via the REST API.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Filters user data before insertion via the REST API.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param object $prepared_user User object.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Request object.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -732,11 +852,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Determine if the current user is allowed to make the desired roles change.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Determines if the current user is allowed to make the desired roles change.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access protected
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param integer $user_id User ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $roles New user roles.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return WP_Error|boolean
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return true|WP_Error True if the current user is allowed to make the role change,
+ * otherwise a WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected function check_role_update( $user_id, $roles ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_roles;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -748,31 +872,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $potential_role = $wp_roles->role_objects[ $role ];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
- // Multisite super admins can freely edit their blog roles -- they possess all caps.
- if ( ! ( is_multisite() && current_user_can( 'manage_sites' ) ) && get_current_user_id() === $user_id && ! $potential_role->has_cap( 'edit_users' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /*
+ * Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
+ * Multisite super admins can freely edit their blog roles -- they possess all caps.
+ */
+ if ( ! ( is_multisite()
+ && current_user_can( 'manage_sites' ) )
+ && get_current_user_id() === $user_id
+ && ! $potential_role->has_cap( 'edit_users' )
+ ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_user_invalid_role', __( 'You cannot give resource that role.' ), array( 'status' => rest_authorization_required_code() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // The new role must be editable by the logged-in user.
-
</del><span class="cx" style="display: block; padding: 0 10px"> /** Include admin functions to get access to get_editable_roles() */
</span><span class="cx" style="display: block; padding: 0 10px"> require_once ABSPATH . 'wp-admin/includes/admin.php';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The new role must be editable by the logged-in user.
</ins><span class="cx" style="display: block; padding: 0 10px"> $editable_roles = get_editable_roles();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $editable_roles[ $role ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_user_invalid_role', __( 'You cannot give resource that role.' ), array( 'status' => 403 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the User's schema, conforming to JSON Schema
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the user's schema, conforming to JSON Schema.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @return array Item schema data.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_item_schema() {
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -878,7 +1011,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'password' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Password for the resource (never included).' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'context' => array(), // Password is never displayed
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'context' => array(), // Password is never displayed.
</ins><span class="cx" style="display: block; padding: 0 10px"> 'required' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'capabilities' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -900,6 +1033,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $avatar_properties = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $avatar_sizes = rest_get_avatar_sizes();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $avatar_sizes as $size ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $avatar_properties[ $size ] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.' ), $size ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -924,9 +1058,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Get the query params for collections
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Retrieves the query params for collections.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.7.0
+ * @access public
+ *
+ * @return array Collection parameters.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_collection_params() {
</span><span class="cx" style="display: block; padding: 0 10px"> $query_params = parent::get_collection_params();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -939,18 +1076,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['include'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to specific ids.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_id_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['offset'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Offset the result set by a specific number of items.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'integer',
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'absint',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['order'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => 'asc',
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Order sort attribute ascending or descending.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -959,6 +1099,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['orderby'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => 'name',
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Sort collection by object attribute.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -975,16 +1116,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['slug'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to resources with a specific slug.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'string',
</span><span class="cx" style="display: block; padding: 0 10px"> 'validate_callback' => 'rest_validate_request_arg',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_params['roles'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Limit result set to resources matching at least one specific role provided. Accepts csv list or single role.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => 'array',
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_callback' => 'wp_parse_slug_list',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $query_params;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>