[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> abilities.php (source)

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


Generated : Thu Sep 3 08:20:25 2026 Cross-referenced by PHPXref