-
Notifications
You must be signed in to change notification settings - Fork 64
WIP: Start work on integrating the new WordPress Abilities API #1003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
de84150
20116f6
1e7b926
cfb9227
75df803
2ac28f4
e770861
7d9bd24
762e0d5
3d69326
7291769
9fc21ec
7dbc05d
631fa7c
3aa5fcc
a32dc8b
1b55ed8
c677596
42c01d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,58 @@ | |
| add_action( 'edit_form_before_permalink', [ $this, 'register_generated_titles_template' ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Register the ability for the Feature. | ||
| */ | ||
| public function register_ability() { | ||
| wp_register_ability( | ||
| 'classifai/generate-title', | ||
| [ | ||
| 'label' => esc_html__( 'Generate title suggestions', 'classifai' ), | ||
| 'description' => esc_html__( 'Use AI to generate title suggestions based on content. This can be content that is passed in or content that is pulled from a post via the post ID. Will return an array of title suggestions.', 'classifai' ), | ||
| 'input_schema' => [ | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'content' => [ | ||
| 'type' => 'string', | ||
| 'sanitize_callback' => 'sanitize_text_field', | ||
| 'description' => esc_html__( 'Content to generate a title for.', 'classifai' ), | ||
| ], | ||
| 'id' => [ | ||
| 'type' => 'integer', | ||
| 'sanitize_callback' => 'absint', | ||
| 'description' => esc_html__( 'Post ID to generate a title for.', 'classifai' ), | ||
| ], | ||
| 'n' => [ | ||
| 'type' => 'integer', | ||
| 'minimum' => 1, | ||
| 'maximum' => 10, | ||
| 'sanitize_callback' => 'absint', | ||
| 'description' => esc_html__( 'Number of titles to generate', 'classifai' ), | ||
| ], | ||
| ], | ||
| ], | ||
| 'output_schema' => [ | ||
| 'type' => 'object', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my testing, seems we need to return an object in order to support any |
||
| 'properties' => [ | ||
| 'titles' => [ | ||
| 'type' => 'array', | ||
| 'items' => [ | ||
| 'type' => 'string', | ||
| 'description' => esc_html__( 'Title suggestion', 'classifai' ), | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| 'execute_callback' => [ $this, 'abilities_api_callback' ], | ||
| 'permission_callback' => [ $this, 'abilities_api_permissions_check' ], | ||
| 'meta' => [ | ||
| 'type' => 'tool', | ||
| ], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Register any needed endpoints. | ||
| */ | ||
|
|
@@ -202,6 +254,66 @@ | |
| return parent::rest_endpoint_callback( $request ); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a given request has access to generate a title. | ||
| * | ||
| * TODO: If we want to keep our custom REST endpoint, | ||
| * we can look to merge these permission callbacks together | ||
| * to avoid duplicating code. | ||
| * | ||
| * @param array $input The input array. | ||
| * @return bool|WP_Error | ||
| */ | ||
| public function abilities_api_permissions_check( array $input ) { | ||
| $post_id = $input['id'] ?? null; | ||
|
|
||
| // Ensure we have a logged in user that can edit the item. | ||
| if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $post_type = get_post_type( $post_id ); | ||
| $post_type_obj = get_post_type_object( $post_type ); | ||
|
|
||
| // Ensure the post type is allowed in REST endpoints. | ||
| if ( ! $post_type || empty( $post_type_obj ) || empty( $post_type_obj->show_in_rest ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| // Ensure the feature is enabled. Also runs a user check. | ||
| if ( ! $this->is_feature_enabled() ) { | ||
| return new WP_Error( 'not_enabled', esc_html__( 'Title generation not currently enabled.', 'classifai' ) ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Request handler for the abilities API. | ||
| * | ||
| * @param array $input The input array. | ||
| * @return \WP_REST_Response | ||
| */ | ||
| public function abilities_api_callback( array $input ) { | ||
| $args = wp_parse_args( | ||
| $input, | ||
| [ | ||
| 'content' => null, | ||
| 'id' => null, | ||
| 'n' => null, | ||
| ] | ||
| ); | ||
|
|
||
| return $this->run( | ||
| $args['id'], | ||
| 'title', | ||
| [ | ||
| 'num' => $args['n'], | ||
| 'content' => $args['content'], | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue the editor scripts. | ||
| */ | ||
|
|
@@ -299,7 +411,7 @@ | |
| 'enabledFeatures' => [ | ||
| 0 => [ | ||
| 'feature' => 'title', | ||
| 'path' => '/classifai/v1/generate-title/', | ||
| 'path' => '/wp/v2/abilities/classifai/generate-title/run/', | ||
| 'buttonText' => __( 'Generate titles', 'classifai' ), | ||
| 'modalTitle' => __( 'Select a title', 'classifai' ), | ||
| 'selectBtnText' => __( 'Select', 'classifai' ), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've set this up to match what we already do in this class but wondering if it would be better to have a
method_existscheck here and remove the empty method below?