From 09ccb19a66042a424fda325fee6bb4eaad251e16 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 23 Jun 2023 00:35:10 +0530 Subject: [PATCH 1/7] add title generation popup for classic editor --- .../Classifai/Providers/OpenAI/ChatGPT.php | 71 +++++++++-- .../openai/classic-editor-title-generator.js | 86 ++++++++++++++ src/scss/admin.scss | 2 + .../classic-editor-title-generator.scss | 112 ++++++++++++++++++ webpack.config.js | 1 + 5 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 src/js/openai/classic-editor-title-generator.js create mode 100644 src/scss/openai/classic-editor-title-generator.scss diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 835ab4f4d..9db544398 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -69,6 +69,26 @@ public function __construct( $service ) { public function register() { add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] ); + add_action( 'edit_form_before_permalink', [ $this, 'regster_generated_titles_template' ] ); + } + + /** + * Returns localised data for title generation. + */ + public function get_localised_vars() { + global $post; + + return [ + 'enabledFeatures' => [ + 0 => [ + 'feature' => 'title', + 'path' => '/classifai/v1/openai/generate-title/', + 'buttonText' => __( 'Generate titles', 'classifai' ), + 'modalTitle' => __( 'Select a title', 'classifai' ), + ], + ], + 'noPermissions' => ! is_user_logged_in() || ! current_user_can( 'edit_post', $post->ID ), + ]; } /** @@ -117,19 +137,7 @@ public function enqueue_editor_assets() { 'classifai-post-status-info', sprintf( 'var classifaiChatGPTData = %s;', - wp_json_encode( - [ - 'enabledFeatures' => [ - 0 => [ - 'feature' => 'title', - 'path' => '/classifai/v1/openai/generate-title/', - 'buttonText' => __( 'Generate titles', 'classifai' ), - 'modalTitle' => __( 'Select a title', 'classifai' ), - ], - ], - 'noPermissions' => ! is_user_logged_in() || ! current_user_can( 'edit_post', $post->ID ), - ] - ) + wp_json_encode( $this->get_localised_vars() ) ), 'before' ); @@ -146,6 +154,27 @@ public function enqueue_admin_assets( $hook_suffix = '' ) { return; } + $screen = get_current_screen(); + + if ( $screen && ! $screen->is_block_editor() ) { + wp_enqueue_script( + 'classifai-generate-title-classic-js', + CLASSIFAI_PLUGIN_URL . 'dist/generate-title-classic.js', + array_merge( get_asset_info( 'generate-title-classic', 'dependencies' ), array( 'wp-api' ) ), + get_asset_info( 'generate-title-classic', 'version' ), + true + ); + + wp_add_inline_script( + 'classifai-generate-title-classic-js', + sprintf( + 'var classifaiChatGPTData = %s;', + wp_json_encode( $this->get_localised_vars() ) + ), + 'before' + ); + } + wp_enqueue_style( 'classifai-language-processing-style', CLASSIFAI_PLUGIN_URL . 'dist/language-processing.css', @@ -155,6 +184,22 @@ public function enqueue_admin_assets( $hook_suffix = '' ) { ); } + /** + * HTML template for title generation result popup. + */ + public function regster_generated_titles_template() { + ?> + + ( { [ cur.feature ] : cur } ), {} ); + +console.log(scriptData) + +( function( $ ) { + $( document ).ready( () => { + if ( scriptData?.title ) { + generateTitleInit(); + } + } ); + + function generateTitleInit() { + // Adds button to the UI. + // $( '', { + // href: '#', + // id: 'classifai-openai__title-generate-btn', + // text: scriptData?.title?.buttonText ?? '', + // class: 'button', + // } ).appendTo( '#titlewrap' ); + + $( '', { + text: scriptData?.title?.buttonText ?? '', + 'class': 'classifai-openai__title-generate-btn--text', + } ) + .wrap( '
' ) + .parent() + .append( $( '', { + 'class': 'classifai-openai__title-generate-btn--spinner', + } ) ) + .appendTo( '#titlewrap' ) + + const postId = $( '#post_ID' ).val(); + + const hidePopup = () => { + $( '#classifai-openai__results' ).removeClass( 'classifai-openai--fade-in' ).delay(300).fadeOut(0); + } + + const generateTitle = ( e ) => { + e.preventDefault(); + + $( '#classifai-openai__results-content' ).html( '' ); + const generateTextEl = $( '.classifai-openai__title-generate-btn--text' ); + const spinnerEl = $( '.classifai-openai__title-generate-btn--spinner' ); + + generateTextEl.css( 'opacity', '0' ); + spinnerEl.show(); + + const path = scriptData.title?.path + postId; + + apiFetch( { + path, + } ) + .then( ( result ) => { + generateTextEl.css( 'opacity', '1' ); + spinnerEl.hide(); + + result.forEach( ( title ) => { + $( '