| 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 |
* @return void |
| 18 |
*/ |
| 19 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 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 |
|
| 38 |
/** |
| 39 |
* Registers the default core abilities. |
| 40 |
* |
| 41 |
* @since 6.9.0 |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 46 |
function wp_register_core_abilities(): void { |
| 47 |
$category_site = 'site'; |
| 48 |
$category_user = 'user'; |
| 49 |
|
| 50 |
$site_info_properties = array( |
| 51 |
'name' => array( |
| 52 |
'type' => 'string', |
| 53 |
'description' => __( 'The site title.' ), |
| 54 |
), |
| 55 |
'description' => array( |
| 56 |
'type' => 'string', |
| 57 |
'description' => __( 'The site tagline.' ), |
| 58 |
), |
| 59 |
'url' => array( |
| 60 |
'type' => 'string', |
| 61 |
'description' => __( 'The site home URL.' ), |
| 62 |
), |
| 63 |
'wpurl' => array( |
| 64 |
'type' => 'string', |
| 65 |
'description' => __( 'The WordPress installation URL.' ), |
| 66 |
), |
| 67 |
'admin_email' => array( |
| 68 |
'type' => 'string', |
| 69 |
'description' => __( 'The site administrator email address.' ), |
| 70 |
), |
| 71 |
'charset' => array( |
| 72 |
'type' => 'string', |
| 73 |
'description' => __( 'The site character encoding.' ), |
| 74 |
), |
| 75 |
'language' => array( |
| 76 |
'type' => 'string', |
| 77 |
'description' => __( 'The site language locale code.' ), |
| 78 |
), |
| 79 |
'version' => array( |
| 80 |
'type' => 'string', |
| 81 |
'description' => __( 'The WordPress version.' ), |
| 82 |
), |
| 83 |
); |
| 84 |
$site_info_fields = array_keys( $site_info_properties ); |
| 85 |
|
| 86 |
wp_register_ability( |
| 87 |
'core/get-site-info', |
| 88 |
array( |
| 89 |
'label' => __( 'Get Site Information' ), |
| 90 |
'description' => __( 'Returns site information configured in WordPress. By default returns all fields, or optionally a filtered subset.' ), |
| 91 |
'category' => $category_site, |
| 92 |
'input_schema' => array( |
| 93 |
'type' => 'object', |
| 94 |
'properties' => array( |
| 95 |
'fields' => array( |
| 96 |
'type' => 'array', |
| 97 |
'items' => array( |
| 98 |
'type' => 'string', |
| 99 |
'enum' => $site_info_fields, |
| 100 |
), |
| 101 |
'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), |
| 102 |
), |
| 103 |
), |
| 104 |
'additionalProperties' => false, |
| 105 |
'default' => array(), |
| 106 |
), |
| 107 |
'output_schema' => array( |
| 108 |
'type' => 'object', |
| 109 |
'properties' => $site_info_properties, |
| 110 |
'additionalProperties' => false, |
| 111 |
), |
| 112 |
'execute_callback' => static function ( $input = array() ) use ( $site_info_fields ): array { |
| 113 |
$input = is_array( $input ) ? $input : array(); |
| 114 |
$requested_fields = ! empty( $input['fields'] ) ? $input['fields'] : $site_info_fields; |
| 115 |
|
| 116 |
$result = array(); |
| 117 |
foreach ( $requested_fields as $field ) { |
| 118 |
$result[ $field ] = get_bloginfo( $field ); |
| 119 |
} |
| 120 |
|
| 121 |
return $result; |
| 122 |
}, |
| 123 |
'permission_callback' => static function (): bool { |
| 124 |
return current_user_can( 'manage_options' ); |
| 125 |
}, |
| 126 |
'meta' => array( |
| 127 |
'annotations' => array( |
| 128 |
'readonly' => true, |
| 129 |
'destructive' => false, |
| 130 |
'idempotent' => true, |
| 131 |
), |
| 132 |
'show_in_rest' => true, |
| 133 |
), |
| 134 |
) |
| 135 |
); |
| 136 |
|
| 137 |
wp_register_ability( |
| 138 |
'core/get-user-info', |
| 139 |
array( |
| 140 |
'label' => __( 'Get User Information' ), |
| 141 |
'description' => __( 'Returns basic profile details for the current authenticated user to support personalization, auditing, and access-aware behavior.' ), |
| 142 |
'category' => $category_user, |
| 143 |
'output_schema' => array( |
| 144 |
'type' => 'object', |
| 145 |
'required' => array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ), |
| 146 |
'properties' => array( |
| 147 |
'id' => array( |
| 148 |
'type' => 'integer', |
| 149 |
'description' => __( 'The user ID.' ), |
| 150 |
), |
| 151 |
'display_name' => array( |
| 152 |
'type' => 'string', |
| 153 |
'description' => __( 'The display name of the user.' ), |
| 154 |
), |
| 155 |
'user_nicename' => array( |
| 156 |
'type' => 'string', |
| 157 |
'description' => __( 'The URL-friendly name for the user.' ), |
| 158 |
), |
| 159 |
'user_login' => array( |
| 160 |
'type' => 'string', |
| 161 |
'description' => __( 'The login username for the user.' ), |
| 162 |
), |
| 163 |
'roles' => array( |
| 164 |
'type' => 'array', |
| 165 |
'description' => __( 'The roles assigned to the user.' ), |
| 166 |
'items' => array( |
| 167 |
'type' => 'string', |
| 168 |
), |
| 169 |
), |
| 170 |
'locale' => array( |
| 171 |
'type' => 'string', |
| 172 |
'description' => __( 'The locale string for the user, such as en_US.' ), |
| 173 |
), |
| 174 |
), |
| 175 |
'additionalProperties' => false, |
| 176 |
), |
| 177 |
'execute_callback' => static function (): array { |
| 178 |
$current_user = wp_get_current_user(); |
| 179 |
|
| 180 |
return array( |
| 181 |
'id' => $current_user->ID, |
| 182 |
'display_name' => $current_user->display_name, |
| 183 |
'user_nicename' => $current_user->user_nicename, |
| 184 |
'user_login' => $current_user->user_login, |
| 185 |
'roles' => $current_user->roles, |
| 186 |
'locale' => get_user_locale( $current_user ), |
| 187 |
); |
| 188 |
}, |
| 189 |
'permission_callback' => static function (): bool { |
| 190 |
return is_user_logged_in(); |
| 191 |
}, |
| 192 |
'meta' => array( |
| 193 |
'annotations' => array( |
| 194 |
'readonly' => true, |
| 195 |
'destructive' => false, |
| 196 |
'idempotent' => true, |
| 197 |
), |
| 198 |
'show_in_rest' => false, |
| 199 |
), |
| 200 |
) |
| 201 |
); |
| 202 |
|
| 203 |
wp_register_ability( |
| 204 |
'core/get-environment-info', |
| 205 |
array( |
| 206 |
'label' => __( 'Get Environment Info' ), |
| 207 |
'description' => __( 'Returns core details about the site\'s runtime context for diagnostics and compatibility (environment, PHP runtime, database server info, WordPress version).' ), |
| 208 |
'category' => $category_site, |
| 209 |
'output_schema' => array( |
| 210 |
'type' => 'object', |
| 211 |
'required' => array( 'environment', 'php_version', 'db_server_info', 'wp_version' ), |
| 212 |
'properties' => array( |
| 213 |
'environment' => array( |
| 214 |
'type' => 'string', |
| 215 |
'description' => __( 'The site\'s runtime environment classification (can be one of these: production, staging, development, local).' ), |
| 216 |
'enum' => array( 'production', 'staging', 'development', 'local' ), |
| 217 |
), |
| 218 |
'php_version' => array( |
| 219 |
'type' => 'string', |
| 220 |
'description' => __( 'The PHP runtime version executing WordPress.' ), |
| 221 |
), |
| 222 |
'db_server_info' => array( |
| 223 |
'type' => 'string', |
| 224 |
'description' => __( 'The database server vendor and version string reported by the driver.' ), |
| 225 |
'examples' => array( '8.0.34', '10.11.6-MariaDB' ), |
| 226 |
), |
| 227 |
'wp_version' => array( |
| 228 |
'type' => 'string', |
| 229 |
'description' => __( 'The WordPress core version running on this site.' ), |
| 230 |
), |
| 231 |
), |
| 232 |
'additionalProperties' => false, |
| 233 |
), |
| 234 |
'execute_callback' => static function (): array { |
| 235 |
global $wpdb; |
| 236 |
|
| 237 |
$env = wp_get_environment_type(); |
| 238 |
$php_version = phpversion(); |
| 239 |
$db_server_info = ''; |
| 240 |
if ( method_exists( $wpdb, 'db_server_info' ) ) { |
| 241 |
/** @phpstan-ignore-next-line */ |
| 242 |
$db_server_info = $wpdb->db_server_info() ?? ''; |
| 243 |
} |
| 244 |
$wp_version = get_bloginfo( 'version' ); |
| 245 |
|
| 246 |
return array( |
| 247 |
'environment' => $env, |
| 248 |
'php_version' => $php_version, |
| 249 |
'db_server_info' => $db_server_info, |
| 250 |
'wp_version' => $wp_version, |
| 251 |
); |
| 252 |
}, |
| 253 |
'permission_callback' => static function (): bool { |
| 254 |
return current_user_can( 'manage_options' ); |
| 255 |
}, |
| 256 |
'meta' => array( |
| 257 |
'annotations' => array( |
| 258 |
'readonly' => true, |
| 259 |
'destructive' => false, |
| 260 |
'idempotent' => true, |
| 261 |
), |
| 262 |
'show_in_rest' => true, |
| 263 |
), |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
|