diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c7f62..a624f49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ the initial snapshot of the prototype. ### Added +- Backport the three info abilities introduced in WordPress 6.9. Sites that already register them keep their native implementation. + - Repository scaffold and tooling: Composer contract for `wordpress/abilities` (type `library`, zero runtime dependencies, PSR-4 `WordPress\Abilities\` → `src/`), WPCS and PHPStan lint configs (`composer run lint:phpcs`, diff --git a/README.md b/README.md index 573a98e..4938d6f 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,9 @@ Full rules, the promotion path, and the rename protocol are in | Name | Tier | Category | Notes | |---|---|---|---| +| `core/get-site-info` | core | site | Reads site settings, optionally limited to selected fields. Requires `manage_options`. | +| `core/get-user-info` | core | user | Reads the signed-in user's own profile. The 6.9 package copy is excluded from REST. | +| `core/get-environment-info` | core | site | Reads the runtime and database versions and the WordPress environment type. Requires `manage_options`. | | `core/content-query` | core | content | Backport of the readonly content query ability landed in WordPress core 7.2 (`core_since: 7.2`). On 7.2+ core provides it and the Registrar skips it; on 6.9–7.1 the package serves it under the same name. Requires the `show_in_abilities` support feature (polyfilled where core lacks it). | | `core/settings-get` | core | site | Backport of the readonly settings get ability landed in WordPress core 7.2 (`core_since: 7.2`). On 7.2+ core provides it and the Registrar skips it; on 6.9–7.1 the package serves it under the same name. Requires the `setting_show_in_abilities` support feature (polyfilled where core lacks it: core's own settings are flagged and registered before the ability reads them, as on 7.2). | | `core/users-query` | core | user | Backport of the readonly users query ability landed in WordPress core 7.2 (`core_since: 7.2`). On 7.2+ core provides it and the Registrar skips it; on 6.9–7.1 the package serves it under the same name. Needs no support feature beyond the Abilities API. | diff --git a/src/Info/Abstract_Info_Ability.php b/src/Info/Abstract_Info_Ability.php new file mode 100644 index 0000000..1c4644d --- /dev/null +++ b/src/Info/Abstract_Info_Ability.php @@ -0,0 +1,72 @@ + + */ + private array $args; + + /** + * Returns the arguments from the original wp_register_ability() call. + * + * @return array + */ + abstract protected function registration_args(): array; + + protected function prepare(): void { + $this->args = $this->registration_args(); + } + + protected function label(): string { + return $this->args['label']; + } + + protected function description(): string { + return $this->args['description']; + } + + protected function category_slug(): string { + return $this->args['category']; + } + + protected function input_schema(): array { + return $this->args['input_schema'] ?? array(); + } + + protected function output_schema(): array { + return $this->args['output_schema']; + } + + protected function make_execute_callback(): callable { + return $this->args['execute_callback']; + } + + protected function make_permission_callback(): callable { + return $this->args['permission_callback']; + } + + protected function required_capabilities(): array { + return array(); + } + + protected function ability_meta(): array { + return $this->args['meta']; + } +} diff --git a/src/Info/Get_Environment_Info.php b/src/Info/Get_Environment_Info.php new file mode 100644 index 0000000..de59c10 --- /dev/null +++ b/src/Info/Get_Environment_Info.php @@ -0,0 +1,79 @@ + __( 'Get Environment Info' ), + 'description' => __( 'Returns core details about the site\'s runtime context for diagnostics and compatibility (environment, PHP runtime, database server info, WordPress version).' ), + 'category' => $category_site, + 'output_schema' => array( + 'type' => 'object', + 'required' => array( 'environment', 'php_version', 'db_server_info', 'wp_version' ), + 'properties' => array( + 'environment' => array( + 'type' => 'string', + 'description' => __( 'The site\'s runtime environment classification (can be one of these: production, staging, development, local).' ), + 'enum' => array( 'production', 'staging', 'development', 'local' ), + ), + 'php_version' => array( + 'type' => 'string', + 'description' => __( 'The PHP runtime version executing WordPress.' ), + ), + 'db_server_info' => array( + 'type' => 'string', + 'description' => __( 'The database server vendor and version string reported by the driver.' ), + ), + 'wp_version' => array( + 'type' => 'string', + 'description' => __( 'The WordPress core version running on this site.' ), + ), + ), + 'additionalProperties' => false, + ), + 'execute_callback' => static function (): array { + global $wpdb; + + $env = wp_get_environment_type(); + $php_version = phpversion(); + $db_server_info = ''; + if ( method_exists( $wpdb, 'db_server_info' ) ) { + $db_server_info = $wpdb->db_server_info() ?? ''; + } + $wp_version = get_bloginfo( 'version' ); + + return array( + 'environment' => $env, + 'php_version' => $php_version, + 'db_server_info' => $db_server_info, + 'wp_version' => $wp_version, + ); + }, + 'permission_callback' => static function (): bool { + return current_user_can( 'manage_options' ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => true, + ), + ); + } +} diff --git a/src/Info/Get_Site_Info.php b/src/Info/Get_Site_Info.php new file mode 100644 index 0000000..47cced5 --- /dev/null +++ b/src/Info/Get_Site_Info.php @@ -0,0 +1,103 @@ + array( + 'type' => 'string', + 'description' => __( 'The site title.' ), + ), + 'description' => array( + 'type' => 'string', + 'description' => __( 'The site tagline.' ), + ), + 'url' => array( + 'type' => 'string', + 'description' => __( 'The site home URL.' ), + ), + 'wpurl' => array( + 'type' => 'string', + 'description' => __( 'The WordPress installation URL.' ), + ), + 'admin_email' => array( + 'type' => 'string', + 'description' => __( 'The site administrator email address.' ), + ), + 'charset' => array( + 'type' => 'string', + 'description' => __( 'The site character encoding.' ), + ), + 'language' => array( + 'type' => 'string', + 'description' => __( 'The site language locale code.' ), + ), + 'version' => array( + 'type' => 'string', + 'description' => __( 'The WordPress version.' ), + ), + ); + $site_info_fields = array_keys( $site_info_properties ); + return array( + 'label' => __( 'Get Site Information' ), + 'description' => __( 'Returns site information configured in WordPress. By default returns all fields, or optionally a filtered subset.' ), + 'category' => $category_site, + 'input_schema' => array( + 'type' => 'object', + 'properties' => array( + 'fields' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + 'enum' => $site_info_fields, + ), + 'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), + ), + ), + 'additionalProperties' => false, + 'default' => array(), + ), + 'output_schema' => array( + 'type' => 'object', + 'properties' => $site_info_properties, + 'additionalProperties' => false, + ), + 'execute_callback' => static function ( $input = array() ) use ( $site_info_fields ): array { + $input = is_array( $input ) ? $input : array(); + $requested_fields = ! empty( $input['fields'] ) ? $input['fields'] : $site_info_fields; + + $result = array(); + foreach ( $requested_fields as $field ) { + $result[ $field ] = get_bloginfo( $field ); + } + + return $result; + }, + 'permission_callback' => static function (): bool { + return current_user_can( 'manage_options' ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => true, + ), + ); + } +} diff --git a/src/Info/Get_User_Info.php b/src/Info/Get_User_Info.php new file mode 100644 index 0000000..aba1859 --- /dev/null +++ b/src/Info/Get_User_Info.php @@ -0,0 +1,83 @@ + __( 'Get User Information' ), + 'description' => __( 'Returns basic profile details for the current authenticated user to support personalization, auditing, and access-aware behavior.' ), + 'category' => $category_user, + 'output_schema' => array( + 'type' => 'object', + 'required' => array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ), + 'properties' => array( + 'id' => array( + 'type' => 'integer', + 'description' => __( 'The user ID.' ), + ), + 'display_name' => array( + 'type' => 'string', + 'description' => __( 'The display name of the user.' ), + ), + 'user_nicename' => array( + 'type' => 'string', + 'description' => __( 'The URL-friendly name for the user.' ), + ), + 'user_login' => array( + 'type' => 'string', + 'description' => __( 'The login username for the user.' ), + ), + 'roles' => array( + 'type' => 'array', + 'description' => __( 'The roles assigned to the user.' ), + 'items' => array( + 'type' => 'string', + ), + ), + 'locale' => array( + 'type' => 'string', + 'description' => __( 'The locale string for the user, such as en_US.' ), + ), + ), + 'additionalProperties' => false, + ), + 'execute_callback' => static function (): array { + $current_user = wp_get_current_user(); + + return array( + 'id' => $current_user->ID, + 'display_name' => $current_user->display_name, + 'user_nicename' => $current_user->user_nicename, + 'user_login' => $current_user->user_login, + 'roles' => $current_user->roles, + 'locale' => get_user_locale( $current_user ), + ); + }, + 'permission_callback' => static function (): bool { + return is_user_logged_in(); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => false, + ), + ); + } +} diff --git a/src/Manifest.php b/src/Manifest.php index ebbd618..9dd9d74 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -42,6 +42,39 @@ private function __construct() {} */ public static function all(): array { return array( + 'core/get-site-info' => array( + 'name' => 'core/get-site-info', + 'class' => Info\Get_Site_Info::class, + 'category' => 'site', + 'tier' => self::TIER_CORE, + 'min_wp_version' => '6.9', + 'core_since' => '6.9', + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), + 'core/get-user-info' => array( + 'name' => 'core/get-user-info', + 'class' => Info\Get_User_Info::class, + 'category' => 'user', + 'tier' => self::TIER_CORE, + 'min_wp_version' => '6.9', + 'core_since' => '6.9', + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), + 'core/get-environment-info' => array( + 'name' => 'core/get-environment-info', + 'class' => Info\Get_Environment_Info::class, + 'category' => 'site', + 'tier' => self::TIER_CORE, + 'min_wp_version' => '6.9', + 'core_since' => '6.9', + 'requires' => array(), + 'renamed_from' => null, + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), 'core/content-query' => array( 'name' => 'core/content-query', /* diff --git a/tests/Abilities/CoreInfoTest.php b/tests/Abilities/CoreInfoTest.php new file mode 100644 index 0000000..2c642d1 --- /dev/null +++ b/tests/Abilities/CoreInfoTest.php @@ -0,0 +1,120 @@ + 'core-info-' . wp_generate_uuid4(), + 'user_pass' => wp_generate_password(), + 'role' => 'subscriber', + ) + ); + self::assertIsInt( $user ); + $this->subscriber = $user; + } + + public function tear_down(): void { + wp_set_current_user( 0 ); + if ( ! function_exists( 'wp_delete_user' ) ) { + require_once ABSPATH . 'wp-admin/includes/user.php'; + } + wp_delete_user( $this->subscriber ); + parent::tear_down(); + } + + public function test_site_fields_and_access(): void { + $ability = new Get_Site_Info(); + $fields = array( 'name', 'description', 'url', 'wpurl', 'admin_email', 'charset', 'language', 'version' ); + self::assertSame( $fields, $ability->get_input_schema()['properties']['fields']['items']['enum'] ); + self::assertSame( $fields, array_keys( $ability->get_output_schema()['properties'] ) ); + self::assertTrue( $ability->get_meta_item( 'show_in_rest' ) ); + + wp_set_current_user( $this->subscriber ); + self::assertFalse( $ability->check_permissions() ); + self::assertInstanceOf( WP_Error::class, $ability->execute() ); + + wp_set_current_user( 1 ); + self::assertTrue( $ability->check_permissions() ); + $title = static fn () => 'Backported site title'; + add_filter( 'pre_option_blogname', $title ); + try { + self::assertSame( array( 'name' => 'Backported site title' ), $ability->execute( array( 'fields' => array( 'name' ) ) ) ); + self::assertSame( array_combine( $fields, array_map( 'get_bloginfo', $fields ) ), $ability->execute() ); + self::assertInstanceOf( WP_Error::class, $ability->execute( array( 'fields' => array( 'unknown' ) ) ) ); + } finally { + remove_filter( 'pre_option_blogname', $title ); + } + } + + public function test_user_info_returns_only_the_current_user(): void { + $ability = new Get_User_Info(); + $fields = array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ); + self::assertSame( array(), $ability->get_input_schema() ); + self::assertSame( $fields, $ability->get_output_schema()['required'] ); + self::assertFalse( $ability->get_meta_item( 'show_in_rest' ) ); + + wp_set_current_user( 0 ); + self::assertFalse( $ability->check_permissions() ); + self::assertInstanceOf( WP_Error::class, $ability->execute() ); + + $user = wp_set_current_user( $this->subscriber ); + self::assertTrue( $ability->check_permissions() ); + self::assertSame( + array( + 'id' => $user->ID, + 'display_name' => $user->display_name, + 'user_nicename' => $user->user_nicename, + 'user_login' => $user->user_login, + 'roles' => $user->roles, + 'locale' => get_user_locale( $user ), + ), + $ability->execute() + ); + } + + public function test_environment_info_requires_an_administrator(): void { + global $wpdb; + $ability = new Get_Environment_Info(); + self::assertSame( array(), $ability->get_input_schema() ); + self::assertSame( array( 'environment', 'php_version', 'db_server_info', 'wp_version' ), $ability->get_output_schema()['required'] ); + self::assertTrue( $ability->get_meta_item( 'show_in_rest' ) ); + wp_set_current_user( $this->subscriber ); + self::assertFalse( $ability->check_permissions() ); + self::assertInstanceOf( WP_Error::class, $ability->execute() ); + + wp_set_current_user( 1 ); + self::assertTrue( $ability->check_permissions() ); + self::assertSame( + array( + 'environment' => wp_get_environment_type(), + 'php_version' => phpversion(), + 'db_server_info' => $wpdb->db_server_info(), + 'wp_version' => get_bloginfo( 'version' ), + ), + $ability->execute() + ); + } +} diff --git a/tests/Fixtures/abilities-api/6.9/get-environment-info.php b/tests/Fixtures/abilities-api/6.9/get-environment-info.php new file mode 100644 index 0000000..d512046 --- /dev/null +++ b/tests/Fixtures/abilities-api/6.9/get-environment-info.php @@ -0,0 +1,68 @@ + __( 'Get Environment Info' ), + 'description' => __( 'Returns core details about the site\'s runtime context for diagnostics and compatibility (environment, PHP runtime, database server info, WordPress version).' ), + 'category' => $category_site, + 'output_schema' => array( + 'type' => 'object', + 'required' => array( 'environment', 'php_version', 'db_server_info', 'wp_version' ), + 'properties' => array( + 'environment' => array( + 'type' => 'string', + 'description' => __( 'The site\'s runtime environment classification (can be one of these: production, staging, development, local).' ), + 'enum' => array( 'production', 'staging', 'development', 'local' ), + ), + 'php_version' => array( + 'type' => 'string', + 'description' => __( 'The PHP runtime version executing WordPress.' ), + ), + 'db_server_info' => array( + 'type' => 'string', + 'description' => __( 'The database server vendor and version string reported by the driver.' ), + ), + 'wp_version' => array( + 'type' => 'string', + 'description' => __( 'The WordPress core version running on this site.' ), + ), + ), + 'additionalProperties' => false, + ), + 'execute_callback' => static function (): array { + global $wpdb; + + $env = wp_get_environment_type(); + $php_version = phpversion(); + $db_server_info = ''; + if ( method_exists( $wpdb, 'db_server_info' ) ) { + $db_server_info = $wpdb->db_server_info() ?? ''; + } + $wp_version = get_bloginfo( 'version' ); + + return array( + 'environment' => $env, + 'php_version' => $php_version, + 'db_server_info' => $db_server_info, + 'wp_version' => $wp_version, + ); + }, + 'permission_callback' => static function (): bool { + return current_user_can( 'manage_options' ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => true, + ), + ) +); diff --git a/tests/Fixtures/abilities-api/6.9/get-site-info.php b/tests/Fixtures/abilities-api/6.9/get-site-info.php new file mode 100644 index 0000000..5f35297 --- /dev/null +++ b/tests/Fixtures/abilities-api/6.9/get-site-info.php @@ -0,0 +1,93 @@ + array( + 'type' => 'string', + 'description' => __( 'The site title.' ), + ), + 'description' => array( + 'type' => 'string', + 'description' => __( 'The site tagline.' ), + ), + 'url' => array( + 'type' => 'string', + 'description' => __( 'The site home URL.' ), + ), + 'wpurl' => array( + 'type' => 'string', + 'description' => __( 'The WordPress installation URL.' ), + ), + 'admin_email' => array( + 'type' => 'string', + 'description' => __( 'The site administrator email address.' ), + ), + 'charset' => array( + 'type' => 'string', + 'description' => __( 'The site character encoding.' ), + ), + 'language' => array( + 'type' => 'string', + 'description' => __( 'The site language locale code.' ), + ), + 'version' => array( + 'type' => 'string', + 'description' => __( 'The WordPress version.' ), + ), +); +$site_info_fields = array_keys( $site_info_properties ); + +wp_register_ability( + 'core/get-site-info', + array( + 'label' => __( 'Get Site Information' ), + 'description' => __( 'Returns site information configured in WordPress. By default returns all fields, or optionally a filtered subset.' ), + 'category' => $category_site, + 'input_schema' => array( + 'type' => 'object', + 'properties' => array( + 'fields' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + 'enum' => $site_info_fields, + ), + 'description' => __( 'Optional: Limit response to specific fields. If omitted, all fields are returned.' ), + ), + ), + 'additionalProperties' => false, + 'default' => array(), + ), + 'output_schema' => array( + 'type' => 'object', + 'properties' => $site_info_properties, + 'additionalProperties' => false, + ), + 'execute_callback' => static function ( $input = array() ) use ( $site_info_fields ): array { + $input = is_array( $input ) ? $input : array(); + $requested_fields = ! empty( $input['fields'] ) ? $input['fields'] : $site_info_fields; + + $result = array(); + foreach ( $requested_fields as $field ) { + $result[ $field ] = get_bloginfo( $field ); + } + + return $result; + }, + 'permission_callback' => static function (): bool { + return current_user_can( 'manage_options' ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => true, + ), + ) +); diff --git a/tests/Fixtures/abilities-api/6.9/get-user-info.php b/tests/Fixtures/abilities-api/6.9/get-user-info.php new file mode 100644 index 0000000..fd48ff2 --- /dev/null +++ b/tests/Fixtures/abilities-api/6.9/get-user-info.php @@ -0,0 +1,72 @@ + __( 'Get User Information' ), + 'description' => __( 'Returns basic profile details for the current authenticated user to support personalization, auditing, and access-aware behavior.' ), + 'category' => $category_user, + 'output_schema' => array( + 'type' => 'object', + 'required' => array( 'id', 'display_name', 'user_nicename', 'user_login', 'roles', 'locale' ), + 'properties' => array( + 'id' => array( + 'type' => 'integer', + 'description' => __( 'The user ID.' ), + ), + 'display_name' => array( + 'type' => 'string', + 'description' => __( 'The display name of the user.' ), + ), + 'user_nicename' => array( + 'type' => 'string', + 'description' => __( 'The URL-friendly name for the user.' ), + ), + 'user_login' => array( + 'type' => 'string', + 'description' => __( 'The login username for the user.' ), + ), + 'roles' => array( + 'type' => 'array', + 'description' => __( 'The roles assigned to the user.' ), + 'items' => array( + 'type' => 'string', + ), + ), + 'locale' => array( + 'type' => 'string', + 'description' => __( 'The locale string for the user, such as en_US.' ), + ), + ), + 'additionalProperties' => false, + ), + 'execute_callback' => static function (): array { + $current_user = wp_get_current_user(); + + return array( + 'id' => $current_user->ID, + 'display_name' => $current_user->display_name, + 'user_nicename' => $current_user->user_nicename, + 'user_login' => $current_user->user_login, + 'roles' => $current_user->roles, + 'locale' => get_user_locale( $current_user ), + ); + }, + 'permission_callback' => static function (): bool { + return is_user_logged_in(); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => true, + 'destructive' => false, + 'idempotent' => true, + ), + 'show_in_rest' => false, + ), + ) +); diff --git a/tests/RegistrarTest.php b/tests/RegistrarTest.php index e89e0ba..85abeb7 100644 --- a/tests/RegistrarTest.php +++ b/tests/RegistrarTest.php @@ -656,6 +656,45 @@ public function test_consumer_filter_cannot_veto_core_provided_name(): void { self::assert_no_wrong_usage(); } + /** + * Check both paths for the original Core info abilities. The standard + * PHPUnit bootstrap unhooks Core registration, so replay it explicitly. + */ + public function test_core_info_registrations(): void { + $names = array( 'core/get-site-info', 'core/get-user-info', 'core/get-environment-info' ); + $core = static function (): void { + wp_register_core_abilities(); + }; + + self::open_fresh_categories_window(); + self::open_fresh_init_window(); + add_action( 'wp_abilities_api_init', $core, 2 ); + Registrar::register_all(); + try { + WP_Abilities_Registry::get_instance(); + foreach ( $names as $name ) { + $native = wp_get_ability( $name ); + self::assertInstanceOf( \WP_Ability::class, $native ); + self::assertNotInstanceOf( Abstract_Core_Ability::class, $native ); + self::assertTrue( Registrar::register( $name ) ); + self::assertSame( $native, wp_get_ability( $name ) ); + } + } finally { + remove_action( 'wp_abilities_api_init', $core, 2 ); + self::open_fresh_init_window(); + Registrar::register_all(); + WP_Abilities_Registry::get_instance(); + } + + foreach ( $names as $name ) { + $entry = Manifest::get( $name ); + $ability = wp_get_ability( $name ); + self::assertInstanceOf( $entry['class'], $ability ); + self::assertSame( 'wordpress/abilities', $ability->get_meta_item( 'source' ) ); + self::assertSame( '6.9', $entry['core_since'] ); + } + } + /** * 7. P8: the Gate-8 rejection for a missing ability category is LOUD. When * `wp_register_ability()` returns null because the ability's category is diff --git a/tools/coreify.php b/tools/coreify.php index dff8629..b9991b1 100644 --- a/tools/coreify.php +++ b/tools/coreify.php @@ -136,6 +136,33 @@ function fail( string $msg ): void { * - category / requires / proposal: the reverse manifest entry skeleton. */ const PROFILES = array( + 'core/get-site-info' => array( + 'fixture' => 'tests/Fixtures/abilities-api/6.9/get-site-info.php', + 'package' => 'src/Info/Get_Site_Info.php', + 'procedural' => true, + 'core_since' => '6.9', + 'category' => 'site', + 'requires' => array(), + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), + 'core/get-user-info' => array( + 'fixture' => 'tests/Fixtures/abilities-api/6.9/get-user-info.php', + 'package' => 'src/Info/Get_User_Info.php', + 'procedural' => true, + 'core_since' => '6.9', + 'category' => 'user', + 'requires' => array(), + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), + 'core/get-environment-info' => array( + 'fixture' => 'tests/Fixtures/abilities-api/6.9/get-environment-info.php', + 'package' => 'src/Info/Get_Environment_Info.php', + 'procedural' => true, + 'core_since' => '6.9', + 'category' => 'site', + 'requires' => array(), + 'proposal' => 'https://core.trac.wordpress.org/ticket/64146', + ), 'core/content-query' => array( 'fixture' => 'tests/Fixtures/abilities-api/abilities/class-wp-abilities-content.php', 'package' => 'src/Content/Content_Query.php', @@ -1008,6 +1035,9 @@ function transform_forward( string $pkgText, string $absText ): string { $abname = unquote( return_expr( $tokens, $by['ability_name'] ) ); $prof = profile( $abname ); + if ( ! empty( $prof['procedural'] ) ) { + return forward_procedural_registration( $tokens, $by, $abname ); + } list( $dir, $pkgClass, $coreClass, $regMethod ) = slug_parts( $abname ); if ( $cls['name'] !== $pkgClass ) { fail( 'unexpected class name ' . $cls['name'] . ' for ability ' . $abname . ' (expected ' . $pkgClass . ')' ); @@ -1460,13 +1490,77 @@ function parse_registration( array $tokens, array $stmt ): array { $entries[ $key ] = span_trim( $tokens, $arrow + 1, $vend ); $i = $vend + 2; // skip ',' } - return array( $abname, $entries ); + return array( $abname, $entries, span_text( $tokens, $arrTok, $close ) ); +} + +/** Procedural fixtures begin with local setup or the registration call itself. */ +function is_procedural_registration( array $tokens ): bool { + $first = next_meaningful_token( $tokens, 1 ); + return null !== $first && ( T_VARIABLE === $tokens[ $first ][0] || 'wp_register_ability' === $tokens[ $first ][1] ); +} + +/** Split an original procedural registration into its setup, name and argument array. */ +function procedural_registration( array $tokens ): array { + $setup = split_statements( $tokens, 1, count( $tokens ) - 1 ); + $call = array_pop( $setup ); + $call !== null || fail( 'missing procedural registration' ); + list( $name, , $values ) = parse_registration( $tokens, $call ); + return array( $name, $setup, $values ); +} + +/** Turn the selected 6.9 registration into a package class without splitting closure scope. */ +function reverse_procedural_registration( array $tokens ): string { + list( $name, $setup, $values ) = procedural_registration( $tokens ); + $prof = profile( $name ); + $class = basename( $prof['package'], '.php' ); + $body = ''; + foreach ( $setup as $statement ) { + $body .= "\t\t" . reindent( stmt_text( $tokens, $statement ), 2 ) . "\n"; + } + $body .= "\t\treturn " . reindent( $values, 1 ) . ';'; + $template = <<<'PHP' + $name, 'CLASS_NAME' => $class, 'METHOD_BODY' => $body ) ) . "\n"; +} + +/** Restore the original setup and wp_register_ability() call from the package definition. */ +function forward_procedural_registration( array $tokens, array $by, string $name ): string { + $method = $by['registration_args']; + $setup = body_statements( $tokens, $method['open'], $method['close'] ); + $return = array_pop( $setup ); + $return !== null && T_RETURN === $tokens[ stmt_start( $tokens, $return ) ][0] || fail( 'registration_args() must end with the argument array' ); + $out = " array(", "\t'name' => '" . $abname . "',", - "\t'class' => '" . $dir . '\\' . $pkgClass . "',", + "\t'class' => '" . $packageClass . "',", "\t'category' => '" . $prof['category'] . "',", "\t'tier' => 'core',", "\t'min_wp_version' => '6.9',", - "\t'core_since' => '7.2',", + "\t'core_since' => '" . ( $prof['core_since'] ?? '7.2' ) . "',", "\t'requires' => " . ( array() === $requires ? 'array()' : 'array( ' . implode( ', ', $requires ) . ' )' ) . ',', "\t'renamed_from' => null,", "\t'proposal' => '" . $prof['proposal'] . "',", "),", '', - 'Note: tier=core, core_since=7.2 per settled design; category, requires and', + 'Note: the Core version, category, requirements and', 'proposal come from the coreify profile and must match src/Manifest.php.', ); return implode( "\n", $lines ); @@ -1576,6 +1670,9 @@ function negate_condition( string $cond ): string { */ function transform_reverse( string $coreText ): string { $tokens = tokenize_source( $coreText ); + if ( is_procedural_registration( $tokens ) ) { + return reverse_procedural_registration( $tokens ); + } $cls = parse_class( $tokens ); $members = parse_members( $tokens, $cls['open'], $cls['close'] ); $by = index_by_name( $members ); @@ -1884,6 +1981,10 @@ function write_out( string $path, string $text ): void { /** Default tools/out paths of an ability: [reverse (package form), forward (core form)]. */ function out_paths( string $root, string $abname ): array { + $prof = profile( $abname ); + if ( ! empty( $prof['procedural'] ) ) { + return array( $root . '/tools/out/' . substr( $prof['package'], 4 ), $root . '/tools/out/' . basename( $prof['fixture'] ) ); + } list( $dir, $pkgClass, $coreClass ) = slug_parts( $abname ); return array( $root . '/tools/out/' . $dir . '/' . $pkgClass . '.php', @@ -1968,6 +2069,9 @@ function run( array $o ): int { /** Ability name alone (for default out paths). */ function reverse_ability_name( string $coreText ): string { $tokens = tokenize_source( $coreText ); + if ( is_procedural_registration( $tokens ) ) { + return procedural_registration( $tokens )[0]; + } $cls = parse_class( $tokens ); $members = parse_members( $tokens, $cls['open'], $cls['close'] ); list( , $stmts, $idx ) = core_registration_method( $tokens, $members );