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