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


Generated : Tue Oct 13 08:20:33 2026 Cross-referenced by PHPXref