| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Core Abilities registration. 4 * 5 * @package WordPress 6 * @subpackage Abilities_API 7 * @since 6.9.0 8 */ 9 10 declare( strict_types = 1 ); 11 12 require_once __DIR__ . '/abilities/class-wp-abilities-content.php'; 13 require_once __DIR__ . '/abilities/class-wp-abilities-settings.php'; 14 require_once __DIR__ . '/abilities/class-wp-abilities-users.php'; 15 16 /** 17 * Registers the core ability categories. 18 * 19 * @since 6.9.0 20 * @since 7.2.0 Added the `content` and `uncategorized` categories. 21 */ 22 function wp_register_core_ability_categories(): void { 23 wp_register_ability_category( 24 'site', 25 array( 26 'label' => __( 'Site' ), 27 'description' => __( 'Abilities that retrieve or modify site information and settings.' ), 28 ) 29 ); 30 31 wp_register_ability_category( 32 'user', 33 array( 34 'label' => __( 'User' ), 35 'description' => __( 'Abilities that retrieve or modify user information and settings.' ), 36 ) 37 ); 38 39 wp_register_ability_category( 40 'content', 41 array( 42 'label' => __( 'Content' ), 43 'description' => __( 'Abilities that retrieve or manage posts and other content.' ), 44 ) 45 ); 46 47 wp_register_ability_category( 48 'uncategorized', 49 array( 50 'label' => __( 'Uncategorized' ), 51 'description' => __( 'Abilities that have not been assigned to a specific category.' ), 52 ) 53 ); 54 } 55 56 /** 57 * Registers the default core abilities. 58 * 59 * @since 6.9.0 60 * @since 7.2.0 Added the `core/content-query`, `core/settings-get`, and `core/users-query` abilities. 61 * 62 * @global wpdb $wpdb WordPress database abstraction object. 63 */ 64 function wp_register_core_abilities(): void { 65 $category_site = 'site'; 66 $category_user = 'user'; 67 68 $site_info_properties = array( 69 'name' => array( 70 'type' => 'string', 71 'title' => __( 'Site Title' ), 72 'description' => __( 'The site title.' ), 73 ), 74 'description' => array( 75 'type' => 'string', 76 'title' => __( 'Tagline' ), 77 'description' => __( 'The site tagline.' ), 78 ), 79 'url' => array( 80 'type' => 'string', 81 'title' => __( 'Site Address (URL)' ), 82 'description' => __( 'The public URL where visitors access the site. May differ from the WordPress installation URL.' ), 83 ), 84 'wpurl' => array( 85 'type' => 'string', 86 'title' => __( 'WordPress Address (URL)' ), 87 'description' => __( 'The URL where WordPress core files are served. May differ from the public site URL.' ), 88 ), 89 'admin_email' => array( 90 'type' => 'string', 91 'title' => __( 'Administration Email Address' ), 92 'description' => __( 'The site administrator email address.' ), 93 ), 94 'charset' => array( 95 'type' => 'string', 96 'title' => __( 'Site Charset' ), 97 'description' => __( 'The site character encoding.' ), 98 ), 99 'language' => array( 100 'type' => 'string', 101 'title' => __( 'Site Language' ), 102 'description' => __( 'The site locale in dash form (e.g. en-US).' ), 103 ), 104 'version' => array( 105 'type' => 'string', 106 'title' => __( 'WordPress Version' ), 107 'description' => __( 'The WordPress core version running on this site.' ), 108 ), 109 ); 110 $site_info_fields = array_keys( $site_info_properties ); 111 112 wp_register_ability( 113 'core/get-site-info', 114 array( 115 'label' => __( 'Get Site Information' ), 116 'description' => __( 'Returns site information configured in WordPress. By default returns all fields, or optionally a filtered subset.' ), 117 'category' => $category_site, 118 'input_schema' => array( 119 'type' => 'object', 120 'properties' => array( 121 'fields' => array( 122 'type' => 'array', 123 'items' => array( 124 'type' => 'string', 125 'enum' => $site_info_fields, 126 ), 127 'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), 128 ), 129 ), 130 'additionalProperties' => false, 131 'default' => array(), 132 ), 133 'output_schema' => array( 134 'type' => 'object', 135 'properties' => $site_info_properties, 136 'additionalProperties' => false, 137 ), 138 'execute_callback' => static function ( $input = array() ) use ( $site_info_fields ): array { 139 $input = rest_sanitize_object( $input ); 140 $requested_fields = rest_sanitize_array( $input['fields'] ?? array() ); 141 if ( empty( $requested_fields ) ) { 142 $requested_fields = $site_info_fields; 143 } 144 145 $result = array(); 146 foreach ( $requested_fields as $field ) { 147 if ( 'language' === $field ) { 148 $result[ $field ] = str_replace( '_', '-', get_locale() ); 149 } else { 150 $result[ $field ] = get_bloginfo( $field ); 151 } 152 } 153 154 return $result; 155 }, 156 'required_capabilities' => array( 'manage_options' ), 157 'meta' => array( 158 'annotations' => array( 159 'readonly' => true, 160 'destructive' => false, 161 'idempotent' => true, 162 ), 163 'public' => true, 164 ), 165 ) 166 ); 167 168 $user_info_properties = array( 169 'id' => array( 170 'type' => 'integer', 171 'title' => __( 'User ID' ), 172 'description' => __( 'Unique identifier for the user.' ), 173 ), 174 'display_name' => array( 175 'type' => 'string', 176 'title' => __( 'Display Name' ), 177 'description' => __( 'Public-facing name selected by the user.' ), 178 ), 179 'user_nicename' => array( 180 'type' => 'string', 181 'title' => __( 'User Nicename' ), 182 'description' => __( 'URL-friendly slug for the user. Defaults to the username.' ), 183 ), 184 'user_login' => array( 185 'type' => 'string', 186 'title' => __( 'Username' ), 187 'description' => __( 'Login identifier for the user. Cannot be changed once set.' ), 188 ), 189 'roles' => array( 190 'type' => 'array', 191 'title' => __( 'Roles' ), 192 'description' => __( 'Roles assigned to the user, such as administrator, editor, author, contributor, or subscriber.' ), 193 'items' => array( 194 'type' => 'string', 195 ), 196 ), 197 'locale' => array( 198 'type' => 'string', 199 'title' => __( 'Language' ), 200 'description' => __( 'Locale code for the user, such as en_US.' ), 201 ), 202 'first_name' => array( 203 'type' => 'string', 204 'title' => __( 'First Name' ), 205 'description' => __( 'Given name.' ), 206 ), 207 'last_name' => array( 208 'type' => 'string', 209 'title' => __( 'Last Name' ), 210 'description' => __( 'Family name.' ), 211 ), 212 'nickname' => array( 213 'type' => 'string', 214 'title' => __( 'Nickname' ), 215 'description' => __( 'Informal name. Defaults to the username.' ), 216 ), 217 'description' => array( 218 'type' => 'string', 219 'title' => __( 'Biographical Info' ), 220 'description' => __( 'User-authored biography. May be empty.' ), 221 ), 222 'user_url' => array( 223 'type' => 'string', 224 'title' => __( 'Website' ), 225 'description' => __( 'Personal website URL.' ), 226 ), 227 ); 228 $user_info_fields = array_keys( $user_info_properties ); 229 230 wp_register_ability( 231 'core/get-user-info', 232 array( 233 'label' => __( 'Get User Information' ), 234 '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.' ), 235 'category' => $category_user, 236 'input_schema' => array( 237 'type' => 'object', 238 'properties' => array( 239 'fields' => array( 240 'type' => 'array', 241 'items' => array( 242 'type' => 'string', 243 'enum' => $user_info_fields, 244 ), 245 'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), 246 ), 247 ), 248 'additionalProperties' => false, 249 'default' => array(), 250 ), 251 'output_schema' => array( 252 'type' => 'object', 253 'properties' => $user_info_properties, 254 'additionalProperties' => false, 255 ), 256 'execute_callback' => static function ( $input = array() ) use ( $user_info_fields ): array { 257 $input = rest_sanitize_object( $input ); 258 $requested_fields = rest_sanitize_array( $input['fields'] ?? array() ); 259 if ( empty( $requested_fields ) ) { 260 $requested_fields = $user_info_fields; 261 } 262 $current_user = wp_get_current_user(); 263 264 $all = array( 265 'id' => $current_user->ID, 266 'display_name' => $current_user->display_name, 267 'user_nicename' => $current_user->user_nicename, 268 'user_login' => $current_user->user_login, 269 // Ensure roles are encoded as a JSON array, regardless of their array keys. 270 'roles' => array_values( $current_user->roles ), 271 'locale' => get_user_locale( $current_user ), 272 'first_name' => $current_user->first_name, 273 'last_name' => $current_user->last_name, 274 'nickname' => $current_user->nickname, 275 'description' => $current_user->description, 276 'user_url' => $current_user->user_url, 277 ); 278 279 return array_intersect_key( $all, array_flip( $requested_fields ) ); 280 }, 281 'permission_callback' => static function (): bool { 282 return is_user_logged_in(); 283 }, 284 'meta' => array( 285 'annotations' => array( 286 'readonly' => true, 287 'destructive' => false, 288 'idempotent' => true, 289 ), 290 'public' => true, 291 ), 292 ) 293 ); 294 295 $environment_info_properties = array( 296 'environment' => array( 297 'type' => 'string', 298 'title' => __( 'Environment Type' ), 299 'description' => __( 'The site\'s runtime environment classification.' ), 300 'enum' => array( 'production', 'staging', 'development', 'local' ), 301 ), 302 'php_version' => array( 303 'type' => 'string', 304 'title' => __( 'PHP Version' ), 305 'description' => __( 'The PHP runtime version executing WordPress.' ), 306 ), 307 'db_server_info' => array( 308 'type' => 'string', 309 'title' => __( 'Database Server Info' ), 310 'description' => __( 'The database server vendor and version string reported by the driver.' ), 311 ), 312 'wp_version' => array( 313 'type' => 'string', 314 'title' => __( 'WordPress Version' ), 315 'description' => __( 'The WordPress core version running on this site.' ), 316 ), 317 ); 318 $environment_info_fields = array_keys( $environment_info_properties ); 319 320 wp_register_ability( 321 'core/get-environment-info', 322 array( 323 'label' => __( 'Get Environment Info' ), 324 'description' => __( 'Returns core details about the site\'s runtime context for diagnostics and compatibility (environment, PHP runtime, database server info, WordPress version). By default returns all fields, or optionally a filtered subset.' ), 325 'category' => $category_site, 326 'input_schema' => array( 327 'type' => 'object', 328 'properties' => array( 329 'fields' => array( 330 'type' => 'array', 331 'items' => array( 332 'type' => 'string', 333 'enum' => $environment_info_fields, 334 ), 335 'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), 336 ), 337 ), 338 'additionalProperties' => false, 339 'default' => array(), 340 ), 341 'output_schema' => array( 342 'type' => 'object', 343 'properties' => $environment_info_properties, 344 'additionalProperties' => false, 345 ), 346 'execute_callback' => static function ( $input = array() ) use ( $environment_info_fields ): array { 347 global $wpdb; 348 349 $input = rest_sanitize_object( $input ); 350 /** @var array{ fields?: string[]|string } $input */ 351 $requested_fields = rest_sanitize_array( $input['fields'] ?? array() ); 352 if ( empty( $requested_fields ) ) { 353 $requested_fields = $environment_info_fields; 354 } 355 356 $db_server_info = ''; 357 if ( method_exists( $wpdb, 'db_server_info' ) ) { 358 $db_server_info = $wpdb->db_server_info() ?? ''; 359 } 360 361 $all = array( 362 'environment' => wp_get_environment_type(), 363 'php_version' => phpversion(), 364 'db_server_info' => $db_server_info, 365 'wp_version' => get_bloginfo( 'version' ), 366 ); 367 368 return array_intersect_key( $all, array_flip( $requested_fields ) ); 369 }, 370 'required_capabilities' => array( 'view_site_health_checks' ), 371 'meta' => array( 372 'annotations' => array( 373 'readonly' => true, 374 'destructive' => false, 375 'idempotent' => true, 376 ), 377 'public' => true, 378 ), 379 ) 380 ); 381 382 // Register the settings abilities (currently the read-only `core/settings-get`). 383 ( new WP_Abilities_Settings() )->register(); 384 385 // Register the user abilities (currently the read-only `core/users-query`). 386 ( new WP_Abilities_Users() )->register(); 387 388 // Register the content abilities (currently the read-only `core/content-query`). 389 ( new WP_Abilities_Content() )->register(); 390 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 3 08:20:36 2026 | Cross-referenced by PHPXref |