-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Abilities API: Expand core/get-user-info with profile fields and filtering #11830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Sukhendu2002
wants to merge
6
commits into
WordPress:trunk
from
Sukhendu2002:enhance/65234-abilities-api-get-user-info-profile-fields
+186
−31
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c183110
Abilities API: Expand with profile fields and filtering
Sukhendu2002 8bad7bf
Tests: Add tests for core/get-user-info profile fields and fields fil…
Sukhendu2002 b0d26a7
Abilities API: Reuse existing i18n string for user_url field title
Sukhendu2002 47d2d67
Merge branch 'trunk' into enhance/65234-abilities-api-get-user-info-p…
Sukhendu2002 6206072
Merge branch 'trunk' into enhance/65234-abilities-api-get-user-info-p…
Sukhendu2002 c2d6329
docs: clarify roles JSON array encoding
Sukhendu2002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,57 +130,114 @@ function wp_register_core_abilities(): void { | |
| ) | ||
| ); | ||
|
|
||
| $user_info_properties = array( | ||
| 'id' => array( | ||
| 'type' => 'integer', | ||
| 'title' => __( 'User ID' ), | ||
| 'description' => __( 'The user ID.' ), | ||
| ), | ||
| 'display_name' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Display Name' ), | ||
| 'description' => __( 'The display name of the user.' ), | ||
| ), | ||
| 'user_nicename' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'User Nicename' ), | ||
| 'description' => __( 'The URL-friendly name for the user.' ), | ||
| ), | ||
| 'user_login' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Username' ), | ||
| 'description' => __( 'The login username for the user.' ), | ||
| ), | ||
| 'roles' => array( | ||
| 'type' => 'array', | ||
| 'title' => __( 'Roles' ), | ||
| 'description' => __( 'The roles assigned to the user.' ), | ||
| 'items' => array( | ||
| 'type' => 'string', | ||
| ), | ||
| ), | ||
| 'locale' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Locale' ), | ||
| 'description' => __( 'The locale string for the user, such as en_US.' ), | ||
| ), | ||
| 'first_name' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'First Name' ), | ||
| 'description' => __( 'The first name of the user.' ), | ||
| ), | ||
| 'last_name' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Last Name' ), | ||
| 'description' => __( 'The last name of the user.' ), | ||
| ), | ||
| 'nickname' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Nickname' ), | ||
| 'description' => __( 'The nickname of the user.' ), | ||
| ), | ||
| 'description' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Biographical Info' ), | ||
| 'description' => __( 'The biographical description of the user.' ), | ||
| ), | ||
| 'user_url' => array( | ||
| 'type' => 'string', | ||
| 'title' => __( 'Website URL' ), | ||
| 'description' => __( 'The URL of the user\'s website.' ), | ||
| ), | ||
| ); | ||
| $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, | ||
| 'roles' => array_values( $current_user->roles ), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a good fix to ensure it always stays a JSON array 👍🏻 Maybe worth an inline comment so it stays like that. |
||
| '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(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you verify that the provided strings match with the ones already used in the
profile.phpso that no string duplication occurs?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, you're right, four of the five new titles already match existing core strings, but 'Website URL' was a duplicate I introduced. Updated to use 'Website' to match
wp-admin/user-edit.php.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still wondering whether we should revisit some of the descriptions when they restate what the title says. It's a nitpick, and it could be tackled later as a general cleanup for input and output schema for all core abilities.