Skip to content

Commit 6debd58

Browse files
Add: Core abiltiies registration.
1 parent 7a840d3 commit 6debd58

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
<?php
2+
/**
3+
* Core Abilities registration.
4+
*
5+
* @package WordPress
6+
* @subpackage Abilities_API
7+
* @since 0.3.0
8+
*/
9+
10+
declare( strict_types = 1 );
11+
/**
12+
* Registers the core abilities categories.
13+
*
14+
* @since 0.3.0
15+
*
16+
* @return void
17+
*/
18+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
19+
function wp_register_core_ability_categories(): void {
20+
wp_register_ability_category(
21+
'site',
22+
array(
23+
'label' => __( 'Site' ),
24+
'description' => __( 'Abilities that retrieve or modify site information and settings.' ),
25+
)
26+
);
27+
28+
wp_register_ability_category(
29+
'user',
30+
array(
31+
'label' => __( 'User' ),
32+
'description' => __( 'Abilities that retrieve or modify user information and settings.' ),
33+
)
34+
);
35+
}
36+
37+
/**
38+
* Registers the default core abilities.
39+
*
40+
* @since 0.3.0
41+
*
42+
* @return void
43+
*/
44+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
45+
function wp_register_core_abilities(): void {
46+
$category_site = 'site';
47+
$category_user = 'user';
48+
49+
$site_info_fields = array(
50+
'name',
51+
'description',
52+
'url',
53+
'wpurl',
54+
'admin_email',
55+
'charset',
56+
'language',
57+
'version',
58+
);
59+
60+
wp_register_ability(
61+
'core/get-site-info',
62+
array(
63+
'label' => __( 'Get Site Information' ),
64+
'description' => __( 'Returns site information configured in WordPress. By default returns all fields, or optionally a filtered subset.' ),
65+
'category' => $category_site,
66+
'input_schema' => array(
67+
'type' => 'object',
68+
'properties' => array(
69+
'fields' => array(
70+
'type' => 'array',
71+
'items' => array(
72+
'type' => 'string',
73+
'enum' => $site_info_fields,
74+
),
75+
'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ),
76+
),
77+
),
78+
'additionalProperties' => false,
79+
'default' => array(),
80+
),
81+
'output_schema' => array(
82+
'type' => 'object',
83+
'properties' => array(
84+
'name' => array(
85+
'type' => 'string',
86+
'description' => __( 'The site title.' ),
87+
),
88+
'description' => array(
89+
'type' => 'string',
90+
'description' => __( 'The site tagline.' ),
91+
),
92+
'url' => array(
93+
'type' => 'string',
94+
'description' => __( 'The site home URL.' ),
95+
),
96+
'wpurl' => array(
97+
'type' => 'string',
98+
'description' => __( 'The WordPress installation URL.' ),
99+
),
100+
'admin_email' => array(
101+
'type' => 'string',
102+
'description' => __( 'The site administrator email address.' ),
103+
),
104+
'charset' => array(
105+
'type' => 'string',
106+
'description' => __( 'The site character encoding.' ),
107+
),
108+
'language' => array(
109+
'type' => 'string',
110+
'description' => __( 'The site language locale code.' ),
111+
),
112+
'version' => array(
113+
'type' => 'string',
114+
'description' => __( 'The WordPress version.' ),
115+
),
116+
),
117+
'additionalProperties' => false,
118+
),
119+
'execute_callback' => static function ( $input = array() ): array {
120+
$input = is_array( $input ) ? $input : array();
121+
$all_fields = array( 'name', 'description', 'url', 'wpurl', 'admin_email', 'charset', 'language', 'version' );
122+
$requested_fields = ! empty( $input['fields'] ) ? $input['fields'] : $all_fields;
123+
124+
$result = array();
125+
foreach ( $requested_fields as $field ) {
126+
$result[ $field ] = get_bloginfo( $field );
127+
}
128+
129+
return $result;
130+
},
131+
'permission_callback' => static function (): bool {
132+
return current_user_can( 'manage_options' );
133+
},
134+
'meta' => array(
135+
'annotations' => array(
136+
'readonly' => true,
137+
'destructive' => false,
138+
'idempotent' => true,
139+
),
140+
'show_in_rest' => true,
141+
),
142+
)
143+
);
144+
145+
wp_register_ability(
146+
'core/get-user-info',
147+
array(
148+
'label' => __( 'Get User Information' ),
149+
'description' => __( 'Returns basic profile details for the current authenticated user to support personalization, auditing, and access-aware behavior.' ),
150+
'category' => $category_user,
151+
'output_schema' => array(
152+
'type' => 'object',
153+
'required' => array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ),
154+
'properties' => array(
155+
'id' => array(
156+
'type' => 'integer',
157+
'description' => __( 'The user ID.' ),
158+
),
159+
'display_name' => array(
160+
'type' => 'string',
161+
'description' => __( 'The display name of the user.' ),
162+
),
163+
'user_nicename' => array(
164+
'type' => 'string',
165+
'description' => __( 'The URL-friendly name for the user.' ),
166+
),
167+
'user_login' => array(
168+
'type' => 'string',
169+
'description' => __( 'The login username for the user.' ),
170+
),
171+
'roles' => array(
172+
'type' => 'array',
173+
'description' => __( 'The roles assigned to the user.' ),
174+
'items' => array(
175+
'type' => 'string',
176+
),
177+
),
178+
'locale' => array(
179+
'type' => 'string',
180+
'description' => __( 'The locale string for the user, such as en_US.' ),
181+
),
182+
),
183+
'additionalProperties' => false,
184+
),
185+
'execute_callback' => static function (): array {
186+
$current_user = wp_get_current_user();
187+
188+
return array(
189+
'id' => $current_user->ID,
190+
'display_name' => $current_user->display_name,
191+
'user_nicename' => $current_user->user_nicename,
192+
'user_login' => $current_user->user_login,
193+
'roles' => $current_user->roles,
194+
'locale' => get_user_locale( $current_user ),
195+
);
196+
},
197+
'permission_callback' => static function (): bool {
198+
return is_user_logged_in();
199+
},
200+
'meta' => array(
201+
'annotations' => array(
202+
'readonly' => true,
203+
'destructive' => false,
204+
'idempotent' => true,
205+
),
206+
'show_in_rest' => false,
207+
),
208+
)
209+
);
210+
211+
wp_register_ability(
212+
'core/get-environment-info',
213+
array(
214+
'label' => __( 'Get Environment Info' ),
215+
'description' => __( 'Returns core details about the site\'s runtime context for diagnostics and compatibility (environment, PHP runtime, database server info, WordPress version).' ),
216+
'category' => $category_site,
217+
'output_schema' => array(
218+
'type' => 'object',
219+
'required' => array( 'environment', 'php_version', 'db_server_info', 'wp_version' ),
220+
'properties' => array(
221+
'environment' => array(
222+
'type' => 'string',
223+
'description' => __( 'The site\'s runtime environment classification (can be one of these: production, staging, development, local).' ),
224+
'enum' => array( 'production', 'staging', 'development', 'local' ),
225+
),
226+
'php_version' => array(
227+
'type' => 'string',
228+
'description' => __( 'The PHP runtime version executing WordPress.' ),
229+
),
230+
'db_server_info' => array(
231+
'type' => 'string',
232+
'description' => __( 'The database server vendor and version string reported by the driver.' ),
233+
'examples' => array( '8.0.34', '10.11.6-MariaDB' ),
234+
),
235+
'wp_version' => array(
236+
'type' => 'string',
237+
'description' => __( 'The WordPress core version running on this site.' ),
238+
),
239+
),
240+
'additionalProperties' => false,
241+
),
242+
'execute_callback' => static function (): array {
243+
global $wpdb;
244+
245+
$env = wp_get_environment_type();
246+
$php_version = phpversion();
247+
$db_server_info = '';
248+
if ( isset( $wpdb ) && is_object( $wpdb ) && method_exists( $wpdb, 'db_server_info' ) ) {
249+
$db_server_info = $wpdb->db_server_info() ?? '';
250+
}
251+
$wp_version = get_bloginfo( 'version' );
252+
253+
return array(
254+
'environment' => $env,
255+
'php_version' => $php_version,
256+
'db_server_info' => $db_server_info,
257+
'wp_version' => $wp_version,
258+
);
259+
},
260+
'permission_callback' => static function (): bool {
261+
return current_user_can( 'manage_options' );
262+
},
263+
'meta' => array(
264+
'annotations' => array(
265+
'readonly' => true,
266+
'destructive' => false,
267+
'idempotent' => true,
268+
),
269+
'show_in_rest' => true,
270+
),
271+
)
272+
);
273+
}

src/wp-includes/default-filters.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,10 @@
532532
add_action( 'rest_api_init', 'create_initial_rest_routes', 99 );
533533
add_action( 'parse_request', 'rest_api_loaded' );
534534

535+
// Abilities API.
536+
add_action( 'wp_abilities_api_categories_init', 'wp_register_core_ability_categories' );
537+
add_action( 'wp_abilities_api_init', 'wp_register_core_abilities' );
538+
535539
// Sitemaps actions.
536540
add_action( 'init', 'wp_sitemaps_get_server' );
537541

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
require ABSPATH . WPINC . '/abilities-api/class-wp-ability.php';
291291
require ABSPATH . WPINC . '/abilities-api/class-wp-abilities-registry.php';
292292
require ABSPATH . WPINC . '/abilities-api.php';
293+
require ABSPATH . WPINC . '/abilities/wp-core-abilities.php';
293294
require ABSPATH . WPINC . '/rest-api.php';
294295
require ABSPATH . WPINC . '/rest-api/class-wp-rest-server.php';
295296
require ABSPATH . WPINC . '/rest-api/class-wp-rest-response.php';

0 commit comments

Comments
 (0)