Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions includes/Abilities/Title_Generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just as easily take an $id, $label, $description (or nothing and inverse the relationship) was just demonstrating how DI could look if there was a reason for an Ability and a Feature to be directly coupled.

$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<string, mixed> 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(
Expand Down Expand Up @@ -63,11 +98,7 @@ protected function input_schema(): array {
}

/**
* Returns the output schema of the ability.
*
* @since 0.1.0
*
* @return array<string, mixed> The output schema of the ability.
* {@inheritDoc}
*/
protected function output_schema(): array {
return array(
Expand All @@ -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.
Expand Down Expand Up @@ -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'] ),
Expand Down Expand Up @@ -193,11 +219,7 @@ protected function permission_callback( $args ) {
}

/**
* Returns the meta of the ability.
*
* @since 0.1.0
*
* @return array<string, mixed> The meta of the ability.
* {@inheritDoc}
*/
protected function meta(): array {
return array(
Expand Down
57 changes: 46 additions & 11 deletions includes/Abstracts/Abstract_Ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed> $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(),
Expand All @@ -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.
*
Expand Down
18 changes: 1 addition & 17 deletions includes/Features/Title_Generation/Title_Generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}