From 40f464eba583e4c31a78459100b885ffb2e7c127 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Thu, 19 Sep 2024 23:05:46 +0530 Subject: [PATCH] move exerpt generation out as a separate plugin --- .../Classifai/Features/ExcerptGeneration.php | 8 +- src/js/gutenberg-plugins/post-status-info.js | 156 ------------------ .../excerpt-generation}/index.js | 0 .../maybe-excerpt-panel.js | 0 .../excerpt-generation}/panel.js | 2 +- webpack.config.js | 2 +- 6 files changed, 6 insertions(+), 162 deletions(-) delete mode 100644 src/js/gutenberg-plugins/post-status-info.js rename src/js/{post-excerpt => plugins/excerpt-generation}/index.js (100%) rename src/js/{post-excerpt => plugins/excerpt-generation}/maybe-excerpt-panel.js (100%) rename src/js/{post-excerpt => plugins/excerpt-generation}/panel.js (98%) diff --git a/includes/Classifai/Features/ExcerptGeneration.php b/includes/Classifai/Features/ExcerptGeneration.php index 431ec22cb..2af8fd0af 100644 --- a/includes/Classifai/Features/ExcerptGeneration.php +++ b/includes/Classifai/Features/ExcerptGeneration.php @@ -198,10 +198,10 @@ public function enqueue_editor_assets() { // This script removes the core excerpt panel and replaces it with our own. wp_enqueue_script( - 'classifai-post-excerpt', - CLASSIFAI_PLUGIN_URL . 'dist/post-excerpt.js', - array_merge( get_asset_info( 'post-excerpt', 'dependencies' ), [ 'lodash' ] ), - get_asset_info( 'post-excerpt', 'version' ), + 'classifai-plugin-excerpt-generation-js', + CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-excerpt-generation.js', + array_merge( get_asset_info( 'classifai-plugin-excerpt-generation', 'dependencies' ), [ 'lodash' ] ), + get_asset_info( 'classifai-plugin-excerpt-generation', 'version' ), true ); } diff --git a/src/js/gutenberg-plugins/post-status-info.js b/src/js/gutenberg-plugins/post-status-info.js deleted file mode 100644 index f970b0e83..000000000 --- a/src/js/gutenberg-plugins/post-status-info.js +++ /dev/null @@ -1,156 +0,0 @@ -import { dispatch, select } from '@wordpress/data'; -import { PluginPostStatusInfo } from '@wordpress/edit-post'; -import { PostTypeSupportCheck } from '@wordpress/editor'; -import { Button, Modal, Spinner } from '@wordpress/components'; -import { __ } from '@wordpress/i18n'; -import { registerPlugin } from '@wordpress/plugins'; -import { useState } from '@wordpress/element'; -import apiFetch from '@wordpress/api-fetch'; -import { DisableFeatureButton } from '../components'; - -const { classifaiChatGPTData } = window; - -const RenderError = ( { error } ) => { - if ( ! error ) { - return null; - } - - return
{ error }
; -}; - -const TextGenerationPlugin = () => { - const [ isLoading, setIsLoading ] = useState( false ); - const [ isOpen, setOpen ] = useState( false ); - const [ error, setError ] = useState( false ); - const [ data, setData ] = useState( [] ); - - if ( ! classifaiChatGPTData || ! classifaiChatGPTData.enabledFeatures ) { - return null; - } - - // Ensure the user has proper permissions - if ( - classifaiChatGPTData.noPermissions && - 1 === parseInt( classifaiChatGPTData.noPermissions ) - ) { - return null; - } - - const postId = select( 'core/editor' ).getCurrentPostId(); - const postType = select( 'core/editor' ).getCurrentPostType(); - const postContent = - select( 'core/editor' ).getEditedPostAttribute( 'content' ); - const openModal = () => setOpen( true ); - const closeModal = () => - setOpen( false ) && setData( [] ) && setError( false ); - - const buttonClick = async ( path ) => { - setIsLoading( true ); - openModal(); - apiFetch( { - path, - method: 'POST', - data: { id: postId, content: postContent }, - } ).then( - ( res ) => { - setData( res ); - setError( false ); - setIsLoading( false ); - }, - ( err ) => { - setError( err?.message ); - setData( [] ); - setIsLoading( false ); - } - ); - }; - - const RenderData = ( { data: dataToRender } ) => { - if ( ! dataToRender ) { - return null; - } - - return ( - <> - { dataToRender.map( ( item, i ) => { - return ( -
- - -
- ); - } ) } - - ); - }; - - return ( - - { isOpen && ( - - { isLoading && } - { ! isLoading && data && } - { ! isLoading && error && } - { ! isLoading && ( - - ) } - - ) } - { classifaiChatGPTData.enabledFeatures.map( ( feature ) => { - const path = feature?.path; - return ( - - - - ); - } ) } - - ); -}; - -registerPlugin( 'classifai-plugin-text-generation', { render: TextGenerationPlugin } ); diff --git a/src/js/post-excerpt/index.js b/src/js/plugins/excerpt-generation/index.js similarity index 100% rename from src/js/post-excerpt/index.js rename to src/js/plugins/excerpt-generation/index.js diff --git a/src/js/post-excerpt/maybe-excerpt-panel.js b/src/js/plugins/excerpt-generation/maybe-excerpt-panel.js similarity index 100% rename from src/js/post-excerpt/maybe-excerpt-panel.js rename to src/js/plugins/excerpt-generation/maybe-excerpt-panel.js diff --git a/src/js/post-excerpt/panel.js b/src/js/plugins/excerpt-generation/panel.js similarity index 98% rename from src/js/post-excerpt/panel.js rename to src/js/plugins/excerpt-generation/panel.js index e4ba898c3..88cafb089 100644 --- a/src/js/post-excerpt/panel.js +++ b/src/js/plugins/excerpt-generation/panel.js @@ -7,7 +7,7 @@ import { withSelect, withDispatch } from '@wordpress/data'; import { compose } from '@wordpress/compose'; import { useState } from '@wordpress/element'; import apiFetch from '@wordpress/api-fetch'; -import { DisableFeatureButton } from '../components'; +import { DisableFeatureButton } from '../../components'; /** * PostExcerpt component. diff --git a/webpack.config.js b/webpack.config.js index 90012a04d..ea2631512 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -20,7 +20,6 @@ module.exports = { 'recommended-content-block-frontend': [ './includes/Classifai/Blocks/recommended-content-block/frontend.js', ], - 'post-excerpt': [ './src/js/post-excerpt/index.js' ], 'media-modal': [ './src/js/media-modal/index.js' ], 'inserter-media-category': [ './src/js/gutenberg-plugins/inserter-media-category.js', @@ -42,6 +41,7 @@ module.exports = { 'classifai-plugin-text-to-speech': './src/js/plugins/text-to-speech/index.js', 'classifai-plugin-content-resizing': './src/js/plugins/content-resizing/index.js', 'classifai-plugin-title-generation': './src/js/plugins/title-generation/index.js', + 'classifai-plugin-excerpt-generation': './src/js/plugins/excerpt-generation/index.js', }, module: { rules: [