@@ -31,29 +31,27 @@ class WP_Users_Abilities {
3131 * @return void
3232 */
3333 public static function register (): void {
34- self ::register_get_current_user ();
34+ self ::register_get_user ();
3535 }
3636
3737 /**
38- * Registers the core/get-current- user ability.
38+ * Registers the core/get-user ability.
3939 *
4040 * @since 6.9.0
4141 *
4242 * @return void
4343 */
44- private static function register_get_current_user (): void {
44+ private static function register_get_user (): void {
4545 wp_register_ability (
46- 'core/get-current- user ' ,
46+ 'core/get-user ' ,
4747 array (
48- 'label ' => __ ( 'Get Current User ' ),
49- 'description ' => __ ( 'Returns comprehensive profile details for the current authenticated user including identity, profile fields, and roles . ' ),
48+ 'label ' => __ ( 'Get User ' ),
49+ 'description ' => __ ( 'Returns comprehensive profile details for a user by id, username, or email . ' ),
5050 'category ' => 'user ' ,
51- 'input_schema ' => self ::get_current_user_input_schema (),
52- 'output_schema ' => self ::get_current_user_output_schema (),
53- 'execute_callback ' => array ( __CLASS__ , 'execute_get_current_user ' ),
54- 'permission_callback ' => static function (): bool {
55- return is_user_logged_in ();
56- },
51+ 'input_schema ' => self ::get_user_input_schema (),
52+ 'output_schema ' => self ::get_user_output_schema (),
53+ 'execute_callback ' => array ( __CLASS__ , 'execute_get_user ' ),
54+ 'permission_callback ' => array ( __CLASS__ , 'check_get_user_permission ' ),
5755 'meta ' => array (
5856 'annotations ' => array (
5957 'readonly ' => true ,
@@ -67,35 +65,51 @@ private static function register_get_current_user(): void {
6765 }
6866
6967 /**
70- * Gets the input schema for the get-current- user ability.
68+ * Gets the input schema for the get-user ability.
7169 *
7270 * @since 6.9.0
7371 *
7472 * @return array<string, mixed> The input schema.
7573 */
76- private static function get_current_user_input_schema (): array {
74+ private static function get_user_input_schema (): array {
7775 return array (
7876 'type ' => 'object ' ,
77+ 'oneOf ' => array (
78+ array ( 'required ' => array ( 'id ' ) ),
79+ array ( 'required ' => array ( 'username ' ) ),
80+ array ( 'required ' => array ( 'email ' ) ),
81+ ),
7982 'properties ' => array (
83+ 'id ' => array (
84+ 'type ' => 'integer ' ,
85+ 'description ' => __ ( 'User ID. ' ),
86+ ),
87+ 'username ' => array (
88+ 'type ' => 'string ' ,
89+ 'description ' => __ ( 'User login name. ' ),
90+ ),
91+ 'email ' => array (
92+ 'type ' => 'string ' ,
93+ 'description ' => __ ( 'User email address. ' ),
94+ ),
8095 'include_capabilities ' => array (
8196 'type ' => 'boolean ' ,
8297 'description ' => __ ( 'Whether to include the user capabilities in the response. ' ),
8398 'default ' => false ,
8499 ),
85100 ),
86101 'additionalProperties ' => false ,
87- 'default ' => array (),
88102 );
89103 }
90104
91105 /**
92- * Gets the output schema for the get-current- user ability.
106+ * Gets the output schema for the get-user ability.
93107 *
94108 * @since 6.9.0
95109 *
96110 * @return array<string, mixed> The output schema.
97111 */
98- private static function get_current_user_output_schema (): array {
112+ private static function get_user_output_schema (): array {
99113 return array (
100114 'type ' => 'object ' ,
101115 'required ' => array (
@@ -189,18 +203,72 @@ private static function get_current_user_output_schema(): array {
189203 }
190204
191205 /**
192- * Executes the get-current-user ability.
206+ * Finds a user by id, username, or email from input parameters.
207+ *
208+ * @since 6.9.0
209+ *
210+ * @param array<string, mixed> $input The input parameters.
211+ * @return WP_User|false The user object if found, false otherwise.
212+ */
213+ private static function find_user ( array $ input ) {
214+ if ( ! empty ( $ input ['id ' ] ) ) {
215+ return get_user_by ( 'ID ' , (int ) $ input ['id ' ] );
216+ }
217+
218+ if ( ! empty ( $ input ['username ' ] ) ) {
219+ return get_user_by ( 'login ' , sanitize_user ( $ input ['username ' ] ) );
220+ }
221+
222+ if ( ! empty ( $ input ['email ' ] ) ) {
223+ return get_user_by ( 'email ' , sanitize_email ( $ input ['email ' ] ) );
224+ }
225+
226+ return false ;
227+ }
228+
229+ /**
230+ * Permission callback for the get-user ability.
231+ *
232+ * @since 6.9.0
233+ *
234+ * @param array<string, mixed> $input The input parameters.
235+ * @return bool Whether the user has permission.
236+ */
237+ public static function check_get_user_permission ( array $ input = array () ): bool {
238+ // Must be logged in.
239+ if ( ! is_user_logged_in () ) {
240+ return false ;
241+ }
242+
243+ $ user = self ::find_user ( $ input );
244+ $ target_user_id = $ user ? $ user ->ID : 0 ;
245+
246+ // Users can view their own profile.
247+ if ( get_current_user_id () === $ target_user_id ) {
248+ return true ;
249+ }
250+
251+ // Otherwise require list_users capability.
252+ return current_user_can ( 'list_users ' );
253+ }
254+
255+ /**
256+ * Executes the get-user ability.
193257 *
194258 * @since 6.9.0
195259 *
196260 * @param array<string, mixed> $input The input parameters.
197- * @return array<string, mixed> The current user data.
261+ * @return array<string, mixed>|WP_Error The user data or error .
198262 */
199- public static function execute_get_current_user ( array $ input = array () ): array {
263+ public static function execute_get_user ( array $ input = array () ) {
200264 $ input = is_array ( $ input ) ? $ input : array ();
201265 $ include_capabilities = ! empty ( $ input ['include_capabilities ' ] ) && ( $ input ['include_capabilities ' ] === true || $ input ['include_capabilities ' ] === 'true ' );
202266
203- $ user = wp_get_current_user ();
267+ $ user = self ::find_user ( $ input );
268+
269+ if ( ! $ user || ! $ user ->exists () ) {
270+ return new WP_Error ( 'user_not_found ' , __ ( 'User not found. ' ), array ( 'status ' => 404 ) );
271+ }
204272
205273 $ result = array (
206274 'id ' => $ user ->ID ,
0 commit comments