diff --git a/includes/Abilities/Title_Generation.php b/includes/Abilities/Title_Generation.php index cda1af41..10694ca2 100644 --- a/includes/Abilities/Title_Generation.php +++ b/includes/Abilities/Title_Generation.php @@ -20,22 +20,57 @@ class Title_Generation extends Abstract_Ability { /** - * Returns the category of the ability. + * The Feature that the ability belongs to. * * @since 0.1.0 + * @var \WordPress\AI\Features\Title_Generation\Title_Generation + */ + protected $feature; + + /** + * Constructor * - * @return string The category of the ability. + * @since 0.1.0 + * + * @param \WordPress\AI\Features\Title_Generation\Title_Generation $feature The Feature this ability is is registered to. */ - protected function category(): string { - return 'ai-experiments'; // TODO: add a reusable way to get the category slug? + public function __construct( \WordPress\AI\Features\Title_Generation\Title_Generation $feature ) { + $this->feature = $feature; + } + + /** + * {@inheritDoc} + */ + protected function name(): string { + return $this->feature->get_id(); + } + + /** + * {@inheritDoc} + */ + protected function description(): string { + return $this->feature->get_description(); + } + + /** + * {@inheritDoc} + */ + protected function label(): string { + return $this->feature->get_label(); } /** - * Returns the input schema of the ability. * * @since 0.1.0 * - * @return array The input schema of the ability. + * @return string The category of the ability. + */ + protected function category(): string { + return 'ai-experiments'; // TODO: add a reusable way to get the category slug? + } + + /** + * {@inheritDoc} */ protected function input_schema(): array { return array( @@ -63,11 +98,7 @@ protected function input_schema(): array { } /** - * Returns the output schema of the ability. - * - * @since 0.1.0 - * - * @return array The output schema of the ability. + * {@inheritDoc} */ protected function output_schema(): array { return array( @@ -85,12 +116,7 @@ protected function output_schema(): array { } /** - * Executes the ability with the given input arguments. - * - * @since 0.1.0 - * - * @param mixed $input The input arguments to the ability. - * @return mixed|\WP_Error The result of the ability execution, or a WP_Error on failure. + * {@inheritDoc} */ protected function execute_callback( $input ) { // Default arguments. @@ -129,9 +155,9 @@ protected function execute_callback( $input ) { // TODO: Implement the title generation logic. return array( - 'name' => $this->get_name(), - 'label' => $this->get_label(), - 'description' => $this->get_description(), + 'name' => $this->name(), + 'label' => $this->label(), + 'description' => $this->description(), 'content' => wp_kses_post( $args['content'] ), 'post_id' => $args['post_id'] ? absint( $args['post_id'] ) : esc_html__( 'Not provided', 'ai' ), 'n' => absint( $args['n'] ), @@ -193,11 +219,7 @@ protected function permission_callback( $args ) { } /** - * Returns the meta of the ability. - * - * @since 0.1.0 - * - * @return array The meta of the ability. + * {@inheritDoc} */ protected function meta(): array { return array( diff --git a/includes/Abstracts/Abstract_Ability.php b/includes/Abstracts/Abstract_Ability.php index 8413c254..897f6954 100644 --- a/includes/Abstracts/Abstract_Ability.php +++ b/includes/Abstracts/Abstract_Ability.php @@ -9,29 +9,37 @@ namespace WordPress\AI\Abstracts; -use WP_Ability; - /** * Base implementation for a WordPress Ability. * * @since 0.1.0 */ -abstract class Abstract_Ability extends WP_Ability { +abstract class Abstract_Ability { + /** + * The ability prefix for the plugin + */ + protected static string $ability_prefix = 'ai/'; /** - * Constructor. + * Register any needed hooks. * * @since 0.1.0 + */ + public function register(): void { + add_action( 'wp_abilities_api_init', array( $this, 'register_ability' ) ); + } + + /** + * Registers any needed abilities. * - * @param string $name The name of the ability. - * @param array $properties The properties of the ability. Must include `label`. + * @since 0.1.0 */ - public function __construct( string $name, array $properties = array() ) { - parent::__construct( - $name, + protected function register_ability(): void { + wp_register_ability( + self::$ability_prefix . $this->name(), array( - 'label' => $properties['label'] ?? '', - 'description' => $properties['description'] ?? '', + 'label' => $this->label(), + 'description' => $this->description(), 'category' => $this->category(), 'input_schema' => $this->input_schema(), 'output_schema' => $this->output_schema(), @@ -42,6 +50,33 @@ public function __construct( string $name, array $properties = array() ) { ); } + /** + * Returns the name of the ability - without the "plugin prefix. + * + * @since 0.1.0 + * + * @return string The name of the ability. + */ + abstract protected function name(): string; + + /** + * Returns the label of the ability. + * + * @since 0.1.0 + * + * @return string The label of the ability. + */ + abstract protected function label(): string; + + /** + * Returns the description of the ability. + * + * @since 0.1.0 + * + * @return string The description of the ability. + */ + abstract protected function description(): string; + /** * Returns the category of the ability. * diff --git a/includes/Features/Title_Generation/Title_Generation.php b/includes/Features/Title_Generation/Title_Generation.php index efeb9a62..be133255 100644 --- a/includes/Features/Title_Generation/Title_Generation.php +++ b/includes/Features/Title_Generation/Title_Generation.php @@ -40,22 +40,6 @@ protected function load_feature_metadata(): array { * @since 0.1.0 */ public function register(): void { - add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); - } - - /** - * Registers any needed abilities. - * - * @since 0.1.0 - */ - public function register_abilities(): void { - wp_register_ability( - 'ai/' . $this->get_id(), - array( - 'label' => $this->get_label(), - 'description' => $this->get_description(), - 'ability_class' => Title_Generation_Ability::class, - ), - ); + ( new Title_Generation_Ability( $this ) )->register(); } }