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


Generated : Mon Nov 23 08:20:01 2026 Cross-referenced by PHPXref