summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury German <blueknight@gentoo.org>2022-06-15 12:08:35 -0400
committerYury German <blueknight@gentoo.org>2022-06-15 12:08:35 -0400
commit36d7691c33cb64ece817246e47a779ec648d10b0 (patch)
tree08f2fb95303a1d8eeba2c8629a24b35a91fb1cac /plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php
parenttwentyfourteen upg 2.7 to 3.2 and twentysixteen from 2.0 to 2.5 (diff)
downloadblogs-gentoo-36d7691c33cb64ece817246e47a779ec648d10b0.tar.gz
blogs-gentoo-36d7691c33cb64ece817246e47a779ec648d10b0.tar.bz2
blogs-gentoo-36d7691c33cb64ece817246e47a779ec648d10b0.zip
Openid-3.6.1 and jetpack-11.0 upgrade
Signed-off-by: Yury German <blueknight@gentoo.org>
Diffstat (limited to 'plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php')
-rw-r--r--plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php239
1 files changed, 141 insertions, 98 deletions
diff --git a/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php b/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php
index b20fa6c2..10095bcb 100644
--- a/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php
+++ b/plugins/jetpack/json-endpoints/class.wpcom-json-api-site-user-endpoint.php
@@ -1,23 +1,24 @@
-<?php
-
-new WPCOM_JSON_API_Site_User_Endpoint( array(
- 'description' => 'Get details of a user of a site by ID.',
- 'group' => '__do_not_document', //'users'
- 'stat' => 'sites:1:user',
- 'method' => 'GET',
- 'path' => '/sites/%s/users/%d',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- '$user_id' => '(int) User ID',
- ),
- 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
+
+new WPCOM_JSON_API_Site_User_Endpoint(
+ array(
+ 'description' => 'Get details of a user of a site by ID.',
+ 'group' => '__do_not_document', // 'users'
+ 'stat' => 'sites:1:user',
+ 'method' => 'GET',
+ 'path' => '/sites/%s/users/%d',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$user_id' => '(int) User ID',
+ ),
+ 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
),
- ),
- 'example_response' => '{
+ 'example_response' => '{
"ID": 18342963,
"login": "binarysmash",
"email": false,
@@ -26,27 +27,29 @@ new WPCOM_JSON_API_Site_User_Endpoint( array(
"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
"roles": [ "administrator" ]
- }'
-) );
-
-new WPCOM_JSON_API_Site_User_Endpoint( array(
- 'description' => 'Get details of a user of a site by login.',
- 'group' => 'users',
- 'stat' => 'sites:1:user',
- 'method' => 'GET',
- 'path' => '/sites/%s/users/login:%s',
- 'path_labels' => array(
- '$site' => '(int|string) The site ID or domain.',
- '$user_id' => '(string) The user\'s login.',
- ),
- 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/login:binarysmash',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+ }',
+ )
+);
+
+new WPCOM_JSON_API_Site_User_Endpoint(
+ array(
+ 'description' => 'Get details of a user of a site by login.',
+ 'group' => 'users',
+ 'stat' => 'sites:1:user',
+ 'method' => 'GET',
+ 'path' => '/sites/%s/users/login:%s',
+ 'path_labels' => array(
+ '$site' => '(int|string) The site ID or domain.',
+ '$user_id' => '(string) The user\'s login.',
+ ),
+ 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/login:binarysmash',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
),
- ),
- 'example_response' => '{
+ 'example_response' => '{
"ID": 18342963,
"login": "binarysmash",
"email": false,
@@ -55,37 +58,39 @@ new WPCOM_JSON_API_Site_User_Endpoint( array(
"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
"roles": [ "administrator" ]
- }'
-) );
-
-new WPCOM_JSON_API_Site_User_Endpoint( array(
- 'description' => 'Update details of a user of a site.',
- 'group' => 'users',
- 'stat' => 'sites:1:user',
- 'method' => 'POST',
- 'path' => '/sites/%s/users/%d',
- 'path_labels' => array(
- '$site' => '(int|string) The site ID or domain.',
- '$user_id' => '(int) The user\'s ID.',
- ),
- 'request_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
- 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+ }',
+ )
+);
+
+new WPCOM_JSON_API_Site_User_Endpoint(
+ array(
+ 'description' => 'Update details of a user of a site.',
+ 'group' => 'users',
+ 'stat' => 'sites:1:user',
+ 'method' => 'POST',
+ 'path' => '/sites/%s/users/%d',
+ 'path_labels' => array(
+ '$site' => '(int|string) The site ID or domain.',
+ '$user_id' => '(int) The user\'s ID.',
),
- 'body' => array(
- 'roles' => array(
- array(
- 'administrator',
- )
+ 'request_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
+ 'response_format' => WPCOM_JSON_API_Site_User_Endpoint::$user_format,
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/user/23',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ 'body' => array(
+ 'roles' => array(
+ array(
+ 'administrator',
+ ),
+ ),
+ 'first_name' => 'Rocco',
+ 'last_name' => 'Tripaldi',
),
- 'first_name' => 'Rocco',
- 'last_name' => 'Tripaldi',
- )
- ),
- 'example_response' => '{
+ ),
+ 'example_response' => '{
"ID": 18342963,
"login": "binarysmash",
"email": false,
@@ -94,28 +99,47 @@ new WPCOM_JSON_API_Site_User_Endpoint( array(
"avatar_URL": "http:\/\/0.gravatar.com\/avatar\/a178ebb1731d432338e6bb0158720fcc?s=96&d=identicon&r=G",
"profile_URL": "http:\/\/en.gravatar.com\/binarysmash",
"roles": [ "administrator" ]
- }'
-) );
+ }',
+ )
+);
+/**
+ * Site user endpoint class.
+ *
+ * /sites/%s/users/%d -> $blog_id, $user_id
+ */
class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
+ /**
+ * User format.
+ *
+ * @var array
+ */
public static $user_format = array(
- 'ID' => '(int) The ID of the user',
- 'login' => '(string) The login username of the user',
- 'email' => '(string) The email of the user',
- 'name' => '(string) The name to display for the user',
- 'first_name' => '(string) The first name of the user',
- 'last_name' => '(string) The last name of the user',
- 'nice_name' => '(string) The nice_name to display for the user',
- 'URL' => '(string) The primary blog of the user',
- 'avatar_URL' => '(url) Gravatar image URL',
- 'profile_URL' => '(url) Gravatar Profile URL',
- 'site_ID' => '(int) ID of the user\'s primary blog',
- 'roles' => '(array|string) The role or roles of the user',
+ 'ID' => '(int) The ID of the user',
+ 'login' => '(string) The login username of the user',
+ 'email' => '(string) The email of the user',
+ 'name' => '(string) The name to display for the user',
+ 'first_name' => '(string) The first name of the user',
+ 'last_name' => '(string) The last name of the user',
+ 'nice_name' => '(string) The nice_name to display for the user',
+ 'URL' => '(string) The primary blog of the user',
+ 'avatar_URL' => '(url) Gravatar image URL',
+ 'profile_URL' => '(url) Gravatar Profile URL',
+ 'site_ID' => '(int) ID of the user\'s primary blog',
+ 'roles' => '(array|string) The role or roles of the user',
);
- // /sites/%s/users/%d -> $blog_id, $user_id
- function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
+ /**
+ * API Callback.
+ *
+ * @param string $path - the path.
+ * @param int $blog_id - the blog ID.
+ * @param int $user_id - the user ID.
+ *
+ * @return array|WP_Error
+ */
+ public function callback( $path = '', $blog_id = 0, $user_id = 0 ) {
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
@@ -126,7 +150,7 @@ class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
// Get the user by ID or login
$get_by = false !== strpos( $path, '/users/login:' ) ? 'login' : 'id';
- $user = get_user_by( $get_by, $user_id );
+ $user = get_user_by( $get_by, $user_id );
if ( ! $user ) {
return new WP_Error( 'unknown_user', 'Unknown user', 404 );
@@ -138,7 +162,7 @@ class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
if ( 'GET' === $this->api->method ) {
return $this->get_user( $user->ID );
- } else if ( 'POST' === $this->api->method ) {
+ } elseif ( 'POST' === $this->api->method ) {
if ( ! current_user_can_for_blog( $blog_id, 'promote_users' ) ) {
return new WP_Error( 'unauthorized_no_promote_cap', 'User cannot promote users for specified site', 403 );
}
@@ -148,10 +172,17 @@ class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
}
}
+ /**
+ * Get the user.
+ *
+ * @param int $user_id - the user ID.
+ *
+ * @return object
+ */
public function get_user( $user_id ) {
$the_user = $this->get_author( $user_id, true );
if ( $the_user && ! is_wp_error( $the_user ) ) {
- $userdata = get_userdata( $user_id );
+ $userdata = get_userdata( $user_id );
$the_user->roles = ! is_wp_error( $userdata ) ? array_values( $userdata->roles ) : array();
}
@@ -159,20 +190,24 @@ class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
}
/**
- * Updates user data
+ * Updates user data.
+ *
+ * @param int $user_id - the user ID.
+ * @param int $blog_id - the blog ID.
*
- * @return array
+ * @return array|WP_Error
*/
public function update_user( $user_id, $blog_id ) {
- $input = $this->input();
+ $user = array();
+ $input = $this->input();
$user['ID'] = $user_id;
- $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
+ $is_wpcom = defined( 'IS_WPCOM' ) && IS_WPCOM;
- if ( get_current_user_id() == $user_id && isset( $input['roles'] ) ) {
+ if ( get_current_user_id() === (int) $user_id && isset( $input['roles'] ) ) {
return new WP_Error( 'unauthorized', 'You cannot change your own role', 403 );
}
- if ( $is_wpcom && $user_id !== get_current_user_id() && $user_id == wpcom_get_blog_owner( $blog_id ) ) {
+ if ( $is_wpcom && $user_id !== get_current_user_id() && (int) $user_id === wpcom_get_blog_owner( $blog_id ) ) {
return new WP_Error( 'unauthorized_edit_owner', 'Current user can not edit blog owner', 403 );
}
@@ -189,25 +224,33 @@ class WPCOM_JSON_API_Site_User_Endpoint extends WPCOM_JSON_API_Endpoint {
break;
case 'display_name':
case 'name':
- $user[ 'display_name' ] = $value;
+ $user['display_name'] = $value;
break;
}
}
}
- if ( isset( $input[ 'roles' ] ) ) {
+ if ( isset( $input['roles'] ) ) {
// For now, we only use the first role in the array.
if ( is_array( $input['roles'] ) ) {
$user['role'] = $input['roles'][0];
- } else if ( is_string( $input['roles'] ) ) {
+ } elseif ( is_string( $input['roles'] ) ) {
$user['role'] = $input['roles'];
} else {
return new WP_Error( 'invalid_input', __( 'The roles property must be a string or an array.', 'jetpack' ), 400 );
}
$editable_roles = array_keys( get_editable_roles() );
- if ( ! in_array( $user['role'], $editable_roles ) ) {
- return new WP_Error( 'invalid_input', sprintf( __( '%s is not a valid role.', 'jetpack' ), $editable_roles ), 400 );
+ if ( ! in_array( $user['role'], $editable_roles, true ) ) {
+ return new WP_Error(
+ 'invalid_input',
+ sprintf(
+ /* Translators: placeholder is an invalid role name */
+ esc_html__( '%s is not a valid role.', 'jetpack' ),
+ $editable_roles
+ ),
+ 400
+ );
}
}