Abilities API: Expand core/get-user-info with profile fields

Extends the `core/get-user-info` ability with additional profile fields (`first_name`, `last_name`, `nickname`, `description`, `user_url`) and a new optional `fields` input parameter that lets callers limit the response to a specific subset.

The output schema now documents each property with a `title` and `description`, mirroring the user profile form labels where they apply and aligning the descriptions with the WP REST API user schema voice.

Also ensures `roles` is encoded as a JSON array regardless of the underlying PHP array keys.


Props sukhendu2002, apermo, gziolo.
Fixes #65234.



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


git-svn-id: http://core.svn.wordpress.org/trunk@61700 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo
2026-05-26 08:59:37 +00:00
parent 2c2a769281
commit fa893f1320
2 changed files with 88 additions and 30 deletions
+87 -29
View File
@@ -134,57 +134,115 @@ function wp_register_core_abilities(): void {
)
);
$user_info_properties = array(
'id' => array(
'type' => 'integer',
'title' => __( 'User ID' ),
'description' => __( 'Unique numeric identifier for the user.' ),
),
'display_name' => array(
'type' => 'string',
'title' => __( 'Display Name' ),
'description' => __( 'Public-facing name selected by the user.' ),
),
'user_nicename' => array(
'type' => 'string',
'title' => __( 'User Nicename' ),
'description' => __( 'URL-friendly slug for the user. Defaults to the username.' ),
),
'user_login' => array(
'type' => 'string',
'title' => __( 'Username' ),
'description' => __( 'Login identifier for the user. Cannot be changed once set.' ),
),
'roles' => array(
'type' => 'array',
'title' => __( 'Roles' ),
'description' => __( 'Roles assigned to the user, such as administrator, editor, author, contributor, or subscriber.' ),
'items' => array(
'type' => 'string',
),
),
'locale' => array(
'type' => 'string',
'title' => __( 'Language' ),
'description' => __( 'Locale code for the user, such as en_US.' ),
),
'first_name' => array(
'type' => 'string',
'title' => __( 'First Name' ),
'description' => __( 'Given name.' ),
),
'last_name' => array(
'type' => 'string',
'title' => __( 'Last Name' ),
'description' => __( 'Family name.' ),
),
'nickname' => array(
'type' => 'string',
'title' => __( 'Nickname' ),
'description' => __( 'Informal name. Defaults to the username.' ),
),
'description' => array(
'type' => 'string',
'title' => __( 'Biographical Info' ),
'description' => __( 'User-authored biography, often shown on author pages.' ),
),
'user_url' => array(
'type' => 'string',
'title' => __( 'Website' ),
'description' => __( 'Personal website URL.' ),
),
);
$user_info_fields = array_keys( $user_info_properties );
wp_register_ability(
'core/get-user-info',
array(
'label' => __( 'Get User Information' ),
'description' => __( 'Returns basic profile details for the current authenticated user to support personalization, auditing, and access-aware behavior.' ),
'description' => __( 'Returns profile details for the current authenticated user to support personalization, auditing, and access-aware behavior. By default returns all fields, or optionally a filtered subset.' ),
'category' => $category_user,
'output_schema' => array(
'input_schema' => array(
'type' => 'object',
'required' => array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ),
'properties' => array(
'id' => array(
'type' => 'integer',
'description' => __( 'The user ID.' ),
),
'display_name' => array(
'type' => 'string',
'description' => __( 'The display name of the user.' ),
),
'user_nicename' => array(
'type' => 'string',
'description' => __( 'The URL-friendly name for the user.' ),
),
'user_login' => array(
'type' => 'string',
'description' => __( 'The login username for the user.' ),
),
'roles' => array(
'fields' => array(
'type' => 'array',
'description' => __( 'The roles assigned to the user.' ),
'items' => array(
'type' => 'string',
'enum' => $user_info_fields,
),
),
'locale' => array(
'type' => 'string',
'description' => __( 'The locale string for the user, such as en_US.' ),
'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ),
),
),
'additionalProperties' => false,
'default' => array(),
),
'execute_callback' => static function (): array {
$current_user = wp_get_current_user();
'output_schema' => array(
'type' => 'object',
'properties' => $user_info_properties,
'additionalProperties' => false,
),
'execute_callback' => static function ( $input = array() ) use ( $user_info_fields ): array {
$input = is_array( $input ) ? $input : array();
$requested_fields = ! empty( $input['fields'] ) ? $input['fields'] : $user_info_fields;
$current_user = wp_get_current_user();
return array(
$all = array(
'id' => $current_user->ID,
'display_name' => $current_user->display_name,
'user_nicename' => $current_user->user_nicename,
'user_login' => $current_user->user_login,
'roles' => $current_user->roles,
// Ensure roles are encoded as a JSON array, regardless of their array keys.
'roles' => array_values( $current_user->roles ),
'locale' => get_user_locale( $current_user ),
'first_name' => $current_user->first_name,
'last_name' => $current_user->last_name,
'nickname' => $current_user->nickname,
'description' => $current_user->description,
'user_url' => $current_user->user_url,
);
return array_intersect_key( $all, array_flip( $requested_fields ) );
},
'permission_callback' => static function (): bool {
return is_user_logged_in();
+1 -1
View File
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.1-alpha-62418';
$wp_version = '7.1-alpha-62419';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.