From 366e5cb88ff1d87536aac715d2e2d7d12dbd4507 Mon Sep 17 00:00:00 2001 From: Jeffrey Carandang Date: Thu, 6 Jul 2023 21:37:17 +0800 Subject: [PATCH 1/5] Create POST endpoint for generating the post title --- includes/Classifai/Providers/OpenAI/ChatGPT.php | 16 +++++++++++----- .../Classifai/Services/LanguageProcessing.php | 16 ++++++++++++++-- src/js/gutenberg-plugins/post-status-info.js | 6 +++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 97cea4360..0d062b2e7 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -83,6 +83,7 @@ public function get_localised_vars() { 0 => [ 'feature' => 'title', 'path' => '/classifai/v1/openai/generate-title/', + 'path_post' => '/classifai/v1/openai/generate-post-title/', 'buttonText' => __( 'Generate titles', 'classifai' ), 'modalTitle' => __( 'Select a title', 'classifai' ), 'selectBtnText' => __( 'Select', 'classifai' ), @@ -573,7 +574,8 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { $args = wp_parse_args( array_filter( $args ), [ - 'num' => $settings['number_titles'] ?? 1, + 'num' => $settings['number_titles'] ?? 1, + 'post_content' => '', ] ); @@ -617,7 +619,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { 'messages' => [ [ 'role' => 'user', - 'content' => esc_html( $prompt ) . ': ' . $this->get_content( $post_id, absint( $args['num'] ) * 15, false ) . '', + 'content' => esc_html( $prompt ) . ': ' . $this->get_content( $post_id, absint( $args['num'] ) * 15, false, $args['post_content'] ) . '', ], ], 'temperature' => 0.9, @@ -662,9 +664,10 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { * @param int $post_id Post ID to get content from. * @param int $return_length Word length of returned content. * @param bool $use_title Whether to use the title or not. + * @param bool $post_content The post content. * @return string */ - public function get_content( int $post_id = 0, int $return_length = 0, bool $use_title = true ) { + public function get_content( int $post_id = 0, int $return_length = 0, bool $use_title = true, string $post_content = '' ) { $tokenizer = new Tokenizer( $this->max_tokens ); $normalizer = new Normalizer(); @@ -690,8 +693,11 @@ public function get_content( int $post_id = 0, int $return_length = 0, bool $use (int) $max_content_tokens ); } else { - $post = get_post( $post_id ); - $post_content = apply_filters( 'the_content', $post->post_content ); + if ( empty( $post_content ) ) { + $post = get_post( $post_id ); + $post_content = apply_filters( 'the_content', $post->post_content ); + } + $post_content = preg_replace( '#\[.+\](.+)\[/.+\]#', '$1', $post_content ); $content = $tokenizer->trim_content( diff --git a/includes/Classifai/Services/LanguageProcessing.php b/includes/Classifai/Services/LanguageProcessing.php index 462815535..575e9b7f3 100644 --- a/includes/Classifai/Services/LanguageProcessing.php +++ b/includes/Classifai/Services/LanguageProcessing.php @@ -142,6 +142,16 @@ public function register_endpoints() { 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], ] ); + + register_rest_route( + 'classifai/v1/openai', + 'generate-post-title/', + [ + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => [ $this, 'generate_post_title' ], + 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], + ] + ); } /** @@ -436,7 +446,8 @@ public function generate_audio_transcript_permissions_check( WP_REST_Request $re * @return \WP_REST_Response|WP_Error */ public function generate_post_title( WP_REST_Request $request ) { - $post_id = $request->get_param( 'id' ); + $post_id = $request->get_param( 'id' ); + $post_content = $request->get_param( 'post_content' ); $provider = ''; // Find the right provider class. @@ -456,7 +467,8 @@ public function generate_post_title( WP_REST_Request $request ) { $post_id, 'title', [ - 'num' => $request->get_param( 'n' ), + 'num' => $request->get_param( 'n' ), + 'post_content' => $post_content, ] ) ); diff --git a/src/js/gutenberg-plugins/post-status-info.js b/src/js/gutenberg-plugins/post-status-info.js index f72d3a99e..5f552e81b 100644 --- a/src/js/gutenberg-plugins/post-status-info.js +++ b/src/js/gutenberg-plugins/post-status-info.js @@ -36,6 +36,8 @@ const PostStatusInfo = () => { } const postId = select( 'core/editor' ).getCurrentPostId(); + const postContent = + select( 'core/editor' ).getEditedPostAttribute( 'content' ); const openModal = () => setOpen( true ); const closeModal = () => setOpen( false ) && setData( [] ) && setError( false ); @@ -45,6 +47,8 @@ const PostStatusInfo = () => { openModal(); apiFetch( { path, + method: 'POST', + data: { id: postId, post_content: postContent }, } ).then( ( res ) => { setData( res ); @@ -111,7 +115,7 @@ const PostStatusInfo = () => { ) } { classifaiChatGPTData.enabledFeatures.map( ( feature ) => { - const path = feature?.path + postId; + const path = feature?.path_post; return ( Date: Thu, 6 Jul 2023 23:38:41 +0800 Subject: [PATCH 2/5] Add POST endpoint for excerpt creation --- .../Classifai/Providers/OpenAI/ChatGPT.php | 29 +++++--- .../Classifai/Services/LanguageProcessing.php | 15 +++- includes/Classifai/Watson/Normalizer.php | 5 +- src/js/post-excerpt/panel.js | 74 ++++++++++++------- 4 files changed, 83 insertions(+), 40 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 0d062b2e7..83a5f0f36 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -462,7 +462,7 @@ public function rest_endpoint_callback( $post_id = 0, $route_to_call = '', $args // Handle all of our routes. switch ( $route_to_call ) { case 'excerpt': - $return = $this->generate_excerpt( $post_id ); + $return = $this->generate_excerpt( $post_id, $args ); break; case 'title': $return = $this->generate_titles( $post_id, $args ); @@ -476,14 +476,21 @@ public function rest_endpoint_callback( $post_id = 0, $route_to_call = '', $args * Generate an excerpt using ChatGPT. * * @param int $post_id The Post ID we're processing + * @param array $args Arguments passed in. * @return string|WP_Error */ - public function generate_excerpt( int $post_id = 0 ) { + public function generate_excerpt( int $post_id = 0, array $args = [] ) { if ( ! $post_id || ! get_post( $post_id ) ) { return new WP_Error( 'post_id_required', esc_html__( 'A valid post ID is required to generate an excerpt.', 'classifai' ) ); } $settings = $this->get_settings(); + $args = wp_parse_args( + array_filter( $args ), + [ + 'post_content' => '', + ] + ); // These checks (and the one above) happen in the REST permission_callback, // but we run them again here in case this method is called directly. @@ -527,7 +534,7 @@ public function generate_excerpt( int $post_id = 0 ) { 'messages' => [ [ 'role' => 'user', - 'content' => $prompt . ': ' . $this->get_content( $post_id, $excerpt_length ) . '', + 'content' => $prompt . ': ' . $this->get_content( $post_id, $excerpt_length, true, $args['post_content'] ) . '', ], ], 'temperature' => 0, @@ -686,20 +693,20 @@ public function get_content( int $post_id = 0, int $return_length = 0, bool $use */ $max_content_tokens = $this->max_tokens - $return_tokens - 13; + if ( empty( $post_content ) ) { + $post = get_post( $post_id ); + $post_content = apply_filters( 'the_content', $post->post_content ); + } + + $post_content = preg_replace( '#\[.+\](.+)\[/.+\]#', '$1', $post_content ); + // Then trim our content, if needed, to stay under the max. if ( $use_title ) { $content = $tokenizer->trim_content( - $normalizer->normalize( $post_id ), + $normalizer->normalize( $post_id, $post_content ), (int) $max_content_tokens ); } else { - if ( empty( $post_content ) ) { - $post = get_post( $post_id ); - $post_content = apply_filters( 'the_content', $post->post_content ); - } - - $post_content = preg_replace( '#\[.+\](.+)\[/.+\]#', '$1', $post_content ); - $content = $tokenizer->trim_content( $normalizer->normalize_content( $post_content, '', $post_id ), (int) $max_content_tokens diff --git a/includes/Classifai/Services/LanguageProcessing.php b/includes/Classifai/Services/LanguageProcessing.php index 575e9b7f3..bc6485423 100644 --- a/includes/Classifai/Services/LanguageProcessing.php +++ b/includes/Classifai/Services/LanguageProcessing.php @@ -152,6 +152,16 @@ public function register_endpoints() { 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], ] ); + + register_rest_route( + 'classifai/v1/openai', + 'generate-post-excerpt/', + [ + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => [ $this, 'generate_post_excerpt' ], + 'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ], + ] + ); } /** @@ -251,7 +261,8 @@ public function generate_post_tags_permissions_check( WP_REST_Request $request ) * @return \WP_REST_Response|WP_Error */ public function generate_post_excerpt( WP_REST_Request $request ) { - $post_id = $request->get_param( 'id' ); + $post_id = $request->get_param( 'id' ); + $post_content = $request->get_param( 'post_content' ); // Find the right provider class. $provider = find_provider_class( $this->provider_classes ?? [], 'ChatGPT' ); @@ -261,7 +272,7 @@ public function generate_post_excerpt( WP_REST_Request $request ) { return $provider; } - return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt' ) ); + return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt', [ 'post_content' => $post_content ] ) ); } /** diff --git a/includes/Classifai/Watson/Normalizer.php b/includes/Classifai/Watson/Normalizer.php index a3470deac..8696ee6ca 100644 --- a/includes/Classifai/Watson/Normalizer.php +++ b/includes/Classifai/Watson/Normalizer.php @@ -18,11 +18,12 @@ class Normalizer { * accuracy. * * @param int $post_id The post to normalize + * @param string $post_content The post content to normalize * @return string */ - public function normalize( $post_id ) { + public function normalize( $post_id, $post_content = '' ) { $post = get_post( $post_id ); - $post_content = apply_filters( 'the_content', $post->post_content ); + $post_content = empty( $post_content ) ? apply_filters( 'the_content', $post->post_content ) : $post_content; $post_title = apply_filters( 'the_title', $post->post_title ); /* Strip shortcodes but keep internal caption text */ diff --git a/src/js/post-excerpt/panel.js b/src/js/post-excerpt/panel.js index 6a17ca582..24e7f63cd 100644 --- a/src/js/post-excerpt/panel.js +++ b/src/js/post-excerpt/panel.js @@ -5,11 +5,8 @@ import { __ } from '@wordpress/i18n'; import { Button, ExternalLink, TextareaControl } from '@wordpress/components'; import { withSelect, withDispatch } from '@wordpress/data'; import { compose } from '@wordpress/compose'; - -/** - * Internal dependencies - */ -import { handleClick } from '../helpers'; +import { useState } from '@wordpress/element'; +import apiFetch from '@wordpress/api-fetch'; /** * PostExcerpt component. @@ -23,8 +20,14 @@ import { handleClick } from '../helpers'; * @param {Function} props.onUpdateExcerpt Callback to update the post excerpt. */ function PostExcerpt( { excerpt, onUpdateExcerpt } ) { + const [ isLoading, setIsLoading ] = useState( false ); + const [ error, setError ] = useState( false ); + const [ data, setData ] = useState( excerpt ); + const { select } = wp.data; const postId = select( 'core/editor' ).getCurrentPostId(); + const postContent = + select( 'core/editor' ).getEditedPostAttribute( 'content' ); const buttonText = '' === excerpt ? __( 'Generate excerpt', 'classifai' ) @@ -32,6 +35,26 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) { const isPublishPanelOpen = select( 'core/edit-post' ).isPublishSidebarOpened(); + const buttonClick = async ( path ) => { + setIsLoading( true ); + apiFetch( { + path, + method: 'POST', + data: { id: postId, post_content: postContent }, + } ).then( + ( res ) => { + setData( res ); + setError( false ); + setIsLoading( false ); + }, + ( err ) => { + setError( err?.message ); + setData( [] ); + setIsLoading( false ); + } + ); + }; + return (
onUpdateExcerpt( value ) } - value={ excerpt } + value={ data } /> { ! isPublishPanelOpen && ( - handleClick( { - button: e.target, - endpoint: '/classifai/v1/openai/generate-excerpt/', - callback: onUpdateExcerpt, - buttonText, - } ) + onClick={ () => + buttonClick( '/classifai/v1/openai/generate-post-excerpt/' ) } > { buttonText } - - + { isLoading && ( + + ) } + { error && ( + + { error } + + ) }
); } From 4212d0436f2b98195115e0416626a4785376dcd8 Mon Sep 17 00:00:00 2001 From: Jeffrey Carandang Date: Thu, 6 Jul 2023 23:44:13 +0800 Subject: [PATCH 3/5] Formatting fixes --- includes/Classifai/Providers/OpenAI/ChatGPT.php | 6 +++--- includes/Classifai/Services/LanguageProcessing.php | 2 +- includes/Classifai/Watson/Normalizer.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 83a5f0f36..657febbdd 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -475,7 +475,7 @@ public function rest_endpoint_callback( $post_id = 0, $route_to_call = '', $args /** * Generate an excerpt using ChatGPT. * - * @param int $post_id The Post ID we're processing + * @param int $post_id The Post ID we're processing * @param array $args Arguments passed in. * @return string|WP_Error */ @@ -671,7 +671,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { * @param int $post_id Post ID to get content from. * @param int $return_length Word length of returned content. * @param bool $use_title Whether to use the title or not. - * @param bool $post_content The post content. + * @param string $post_content The post content. * @return string */ public function get_content( int $post_id = 0, int $return_length = 0, bool $use_title = true, string $post_content = '' ) { @@ -694,7 +694,7 @@ public function get_content( int $post_id = 0, int $return_length = 0, bool $use $max_content_tokens = $this->max_tokens - $return_tokens - 13; if ( empty( $post_content ) ) { - $post = get_post( $post_id ); + $post = get_post( $post_id ); $post_content = apply_filters( 'the_content', $post->post_content ); } diff --git a/includes/Classifai/Services/LanguageProcessing.php b/includes/Classifai/Services/LanguageProcessing.php index bc6485423..3afba12a4 100644 --- a/includes/Classifai/Services/LanguageProcessing.php +++ b/includes/Classifai/Services/LanguageProcessing.php @@ -459,7 +459,7 @@ public function generate_audio_transcript_permissions_check( WP_REST_Request $re public function generate_post_title( WP_REST_Request $request ) { $post_id = $request->get_param( 'id' ); $post_content = $request->get_param( 'post_content' ); - $provider = ''; + $provider = ''; // Find the right provider class. foreach ( $this->provider_classes as $provider_class ) { diff --git a/includes/Classifai/Watson/Normalizer.php b/includes/Classifai/Watson/Normalizer.php index 8696ee6ca..b24d84d4a 100644 --- a/includes/Classifai/Watson/Normalizer.php +++ b/includes/Classifai/Watson/Normalizer.php @@ -17,7 +17,7 @@ class Normalizer { * The post title is also included in the content to improve * accuracy. * - * @param int $post_id The post to normalize + * @param int $post_id The post to normalize * @param string $post_content The post content to normalize * @return string */ From 644895f0df10f03cc1c9a39114c2c77b9ccdbb87 Mon Sep 17 00:00:00 2001 From: Jeffrey Carandang Date: Thu, 6 Jul 2023 23:47:33 +0800 Subject: [PATCH 4/5] Formatting fixes --- includes/Classifai/Providers/OpenAI/ChatGPT.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 657febbdd..a288ff1ab 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -668,9 +668,9 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { /** * Get our content, trimming if needed. * - * @param int $post_id Post ID to get content from. - * @param int $return_length Word length of returned content. - * @param bool $use_title Whether to use the title or not. + * @param int $post_id Post ID to get content from. + * @param int $return_length Word length of returned content. + * @param bool $use_title Whether to use the title or not. * @param string $post_content The post content. * @return string */ From 07e57f931140cf2c82434702ef47fe0ddb1d941b Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Mon, 17 Jul 2023 15:00:01 -0600 Subject: [PATCH 5/5] Combine our GET and POST endpoints together instead of having those be separate. Rename from post_content to just contnet. Fix an issue where excerpts couldn't be overwritten --- .../Classifai/Providers/OpenAI/ChatGPT.php | 11 +- .../Classifai/Services/LanguageProcessing.php | 121 ++++++++++-------- src/js/gutenberg-plugins/post-status-info.js | 4 +- src/js/post-excerpt/panel.js | 13 +- 4 files changed, 79 insertions(+), 70 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index a288ff1ab..75578dd25 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -83,7 +83,6 @@ public function get_localised_vars() { 0 => [ 'feature' => 'title', 'path' => '/classifai/v1/openai/generate-title/', - 'path_post' => '/classifai/v1/openai/generate-post-title/', 'buttonText' => __( 'Generate titles', 'classifai' ), 'modalTitle' => __( 'Select a title', 'classifai' ), 'selectBtnText' => __( 'Select', 'classifai' ), @@ -488,7 +487,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) { $args = wp_parse_args( array_filter( $args ), [ - 'post_content' => '', + 'content' => '', ] ); @@ -534,7 +533,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) { 'messages' => [ [ 'role' => 'user', - 'content' => $prompt . ': ' . $this->get_content( $post_id, $excerpt_length, true, $args['post_content'] ) . '', + 'content' => $prompt . ': ' . $this->get_content( $post_id, $excerpt_length, true, $args['content'] ) . '', ], ], 'temperature' => 0, @@ -581,8 +580,8 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { $args = wp_parse_args( array_filter( $args ), [ - 'num' => $settings['number_titles'] ?? 1, - 'post_content' => '', + 'num' => $settings['number_titles'] ?? 1, + 'content' => '', ] ); @@ -626,7 +625,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { 'messages' => [ [ 'role' => 'user', - 'content' => esc_html( $prompt ) . ': ' . $this->get_content( $post_id, absint( $args['num'] ) * 15, false, $args['post_content'] ) . '', + 'content' => esc_html( $prompt ) . ': ' . $this->get_content( $post_id, absint( $args['num'] ) * 15, false, $args['content'] ) . '', ], ], 'temperature' => 0.9, diff --git a/includes/Classifai/Services/LanguageProcessing.php b/includes/Classifai/Services/LanguageProcessing.php index 3afba12a4..91eb31f39 100644 --- a/includes/Classifai/Services/LanguageProcessing.php +++ b/includes/Classifai/Services/LanguageProcessing.php @@ -65,19 +65,35 @@ public function register_endpoints() { register_rest_route( 'classifai/v1/openai', - 'generate-excerpt/(?P\d+)', + 'generate-excerpt(?:/(?P\d+))?', [ - 'methods' => WP_REST_Server::READABLE, - 'callback' => [ $this, 'generate_post_excerpt' ], - 'args' => [ - 'id' => [ - 'required' => true, - 'type' => 'integer', - 'sanitize_callback' => 'absint', - 'description' => esc_html__( 'Post ID to generate excerpt for.', 'classifai' ), + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'generate_post_excerpt' ], + 'args' => [ + 'id' => [ + 'required' => true, + 'type' => 'integer', + 'sanitize_callback' => 'absint', + 'description' => esc_html__( 'Post ID to generate excerpt for.', 'classifai' ), + ], ], + 'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ], + ], + [ + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => [ $this, 'generate_post_excerpt' ], + 'args' => [ + 'content' => [ + 'required' => true, + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + 'description' => esc_html__( 'Content to generate a title for', 'classifai' ), + ], + ], + 'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ], ], - 'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ], ] ); @@ -119,47 +135,43 @@ public function register_endpoints() { register_rest_route( 'classifai/v1/openai', - 'generate-title/(?P\d+)', + 'generate-title(?:/(?P\d+))?', [ - 'methods' => WP_REST_Server::READABLE, - 'callback' => [ $this, 'generate_post_title' ], - 'args' => [ - 'id' => [ - 'required' => true, - 'type' => 'integer', - 'sanitize_callback' => 'absint', - 'description' => esc_html__( 'Post ID to generate title for.', 'classifai' ), + [ + 'methods' => WP_REST_Server::READABLE, + 'callback' => [ $this, 'generate_post_title' ], + 'args' => [ + 'id' => [ + 'required' => true, + 'type' => 'integer', + 'sanitize_callback' => 'absint', + 'description' => esc_html__( 'Post ID to generate title for.', 'classifai' ), + ], + 'n' => [ + 'type' => 'integer', + 'minimum' => 1, + 'maximum' => 10, + 'sanitize_callback' => 'absint', + 'validate_callback' => 'rest_validate_request_arg', + 'description' => esc_html__( 'Number of titles to generate', 'classifai' ), + ], ], - 'n' => [ - 'type' => 'integer', - 'minimum' => 1, - 'maximum' => 10, - 'sanitize_callback' => 'absint', - 'validate_callback' => 'rest_validate_request_arg', - 'description' => esc_html__( 'Number of titles to generate', 'classifai' ), + 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], + ], + [ + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => [ $this, 'generate_post_title' ], + 'args' => [ + 'content' => [ + 'required' => true, + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + 'validate_callback' => 'rest_validate_request_arg', + 'description' => esc_html__( 'Content to generate a title for', 'classifai' ), + ], ], + 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], ], - 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], - ] - ); - - register_rest_route( - 'classifai/v1/openai', - 'generate-post-title/', - [ - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => [ $this, 'generate_post_title' ], - 'permission_callback' => [ $this, 'generate_post_title_permissions_check' ], - ] - ); - - register_rest_route( - 'classifai/v1/openai', - 'generate-post-excerpt/', - [ - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => [ $this, 'generate_post_excerpt' ], - 'permission_callback' => [ $this, 'generate_post_excerpt_permissions_check' ], ] ); } @@ -261,8 +273,8 @@ public function generate_post_tags_permissions_check( WP_REST_Request $request ) * @return \WP_REST_Response|WP_Error */ public function generate_post_excerpt( WP_REST_Request $request ) { - $post_id = $request->get_param( 'id' ); - $post_content = $request->get_param( 'post_content' ); + $post_id = $request->get_param( 'id' ); + $content = $request->get_param( 'content' ); // Find the right provider class. $provider = find_provider_class( $this->provider_classes ?? [], 'ChatGPT' ); @@ -272,7 +284,7 @@ public function generate_post_excerpt( WP_REST_Request $request ) { return $provider; } - return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt', [ 'post_content' => $post_content ] ) ); + return rest_ensure_response( $provider->rest_endpoint_callback( $post_id, 'excerpt', [ 'content' => $content ] ) ); } /** @@ -457,9 +469,8 @@ public function generate_audio_transcript_permissions_check( WP_REST_Request $re * @return \WP_REST_Response|WP_Error */ public function generate_post_title( WP_REST_Request $request ) { - $post_id = $request->get_param( 'id' ); - $post_content = $request->get_param( 'post_content' ); - $provider = ''; + $post_id = $request->get_param( 'id' ); + $provider = ''; // Find the right provider class. foreach ( $this->provider_classes as $provider_class ) { @@ -478,8 +489,8 @@ public function generate_post_title( WP_REST_Request $request ) { $post_id, 'title', [ - 'num' => $request->get_param( 'n' ), - 'post_content' => $post_content, + 'num' => $request->get_param( 'n' ), + 'content' => $request->get_param( 'content' ), ] ) ); diff --git a/src/js/gutenberg-plugins/post-status-info.js b/src/js/gutenberg-plugins/post-status-info.js index 5f552e81b..57b23e254 100644 --- a/src/js/gutenberg-plugins/post-status-info.js +++ b/src/js/gutenberg-plugins/post-status-info.js @@ -48,7 +48,7 @@ const PostStatusInfo = () => { apiFetch( { path, method: 'POST', - data: { id: postId, post_content: postContent }, + data: { id: postId, content: postContent }, } ).then( ( res ) => { setData( res ); @@ -115,7 +115,7 @@ const PostStatusInfo = () => { ) } { classifaiChatGPTData.enabledFeatures.map( ( feature ) => { - const path = feature?.path_post; + const path = feature?.path; return ( { - setData( res ); + onUpdateExcerpt( res ); setError( false ); setIsLoading( false ); }, ( err ) => { setError( err?.message ); - setData( [] ); setIsLoading( false ); } ); @@ -66,7 +64,7 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) { } className="editor-post-excerpt__textarea" onChange={ ( value ) => onUpdateExcerpt( value ) } - value={ data } + value={ excerpt } /> { ! isPublishPanelOpen && ( - buttonClick( '/classifai/v1/openai/generate-post-excerpt/' ) + buttonClick( '/classifai/v1/openai/generate-excerpt/' ) } > { buttonText } @@ -93,11 +91,12 @@ function PostExcerpt( { excerpt, onUpdateExcerpt } ) { style={ { float: 'none' } } > ) } - { error && ( + { error && ! isLoading && (