Skip to content

Commit

Permalink
Merge pull request #491 from 10up/feature/417-add-excerpt-generation-…
Browse files Browse the repository at this point in the history
…to-classic-editor

Added Generate Excerpt for the Classic Editor.
  • Loading branch information
dkotter authored Aug 17, 2023
2 parents d1be4e8 + 70caae8 commit bd9ea48
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 97 deletions.
97 changes: 66 additions & 31 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
namespace Classifai\Providers\OpenAI;

use Classifai\Providers\Provider;
use Classifai\Providers\OpenAI\APIRequest;
use Classifai\Providers\OpenAI\Tokenizer;
use Classifai\Watson\Normalizer;
use function Classifai\get_asset_info;
use WP_Error;
use function Classifai\get_asset_info;

class ChatGPT extends Provider {

Expand Down Expand Up @@ -192,35 +190,72 @@ public function enqueue_admin_assets( $hook_suffix = '' ) {
$title_roles = $settings['title_roles'] ?? [];

// Load the assets for the classic editor.
if (
$screen && ! $screen->is_block_editor()
&& ( ! empty( $title_roles ) && empty( array_diff( $user_roles, $title_roles ) ) )
&& ( isset( $settings['enable_titles'] ) && 1 === (int) $settings['enable_titles'] )
) {
wp_enqueue_style(
'classifai-generate-title-classic-css',
CLASSIFAI_PLUGIN_URL . 'dist/generate-title-classic.css',
[],
CLASSIFAI_PLUGIN_VERSION,
'all'
);

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
);
if ( $screen && ! $screen->is_block_editor() ) {
if (
post_type_supports( $screen->post_type, 'title' ) &&
$this->is_feature_enabled( 'enable_titles' )
) {
wp_enqueue_style(
'classifai-generate-title-classic-css',
CLASSIFAI_PLUGIN_URL . 'dist/generate-title-classic.css',
[],
CLASSIFAI_PLUGIN_VERSION,
'all'
);

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_add_inline_script(
'classifai-generate-title-classic-js',
sprintf(
'var classifaiChatGPTData = %s;',
wp_json_encode( $this->get_localised_vars() )
),
'before'
);
if (
post_type_supports( $screen->post_type, 'excerpt' ) &&
$this->is_feature_enabled( 'enable_excerpt' )
) {
wp_enqueue_style(
'classifai-generate-title-classic-css',
CLASSIFAI_PLUGIN_URL . 'dist/generate-title-classic.css',
[],
CLASSIFAI_PLUGIN_VERSION,
'all'
);

wp_enqueue_script(
'classifai-generate-excerpt-classic-js',
CLASSIFAI_PLUGIN_URL . 'dist/generate-excerpt-classic.js',
array_merge( get_asset_info( 'generate-excerpt-classic', 'dependencies' ), array( 'wp-api' ) ),
get_asset_info( 'generate-excerpt-classic', 'version' ),
true
);

wp_add_inline_script(
'classifai-generate-excerpt-classic-js',
sprintf(
'var classifaiGenerateExcerpt = %s;',
wp_json_encode(
[
'path' => '/classifai/v1/openai/generate-excerpt/',
'buttonText' => __( 'Generate excerpt', 'classifai' ),
'regenerateText' => __( 'Re-generate excerpt', 'classifai' ),
]
)
),
'before'
);
}
}

wp_enqueue_style(
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions src/js/openai/classic-editor-excerpt-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import apiFetch from '@wordpress/api-fetch';
import '../../scss/openai/classic-editor-title-generator.scss';

const classifaiExcerptData = window.classifaiGenerateExcerpt || {};

( function ( $ ) {
$( document ).ready( () => {
if ( document.getElementById( 'postexcerpt' ) ) {
generateExcerptInit();
}
} );

/**
* This function is solely responsibe for rendering, generating
* and applying the generated excerpt in the classic editor.
*/
function generateExcerptInit() {
const excerptContainer = document.getElementById( 'excerpt' );

// Boolean indicating whether generation is in progress.
let isProcessing = false;

// Creates and appends the "Generate excerpt" button.
$( '<span />', {
text: excerptContainer.value
? classifaiExcerptData?.regenerateText ?? ''
: classifaiExcerptData?.buttonText ?? '',
class: 'classifai-openai__excerpt-generate-btn--text',
} )
.wrap(
'<div class="button" id="classifai-openai__excerpt-generate-btn" />'
)
.parent()
.append(
$( '<span />', {
class: 'classifai-openai__excerpt-generate-btn--spinner',
} )
)
.insertAfter( excerptContainer );

// The current post ID.
const postId = $( '#post_ID' ).val();

// Callback to generate the excerpt.
const generateExcerpt = () => {
if ( isProcessing ) {
return;
}

const generateTextEl = $(
'.classifai-openai__excerpt-generate-btn--text'
);
const spinnerEl = $(
'.classifai-openai__excerpt-generate-btn--spinner'
);

generateTextEl.css( 'opacity', '0' );
spinnerEl.show();
isProcessing = true;

const path = classifaiExcerptData?.path + postId;

apiFetch( {
path,
} ).then( ( result ) => {
generateTextEl.css( 'opacity', '1' );
spinnerEl.hide();
isProcessing = false;

$( excerptContainer ).val( result ).trigger( 'input' );
generateTextEl.text(
classifaiExcerptData?.regenerateText ?? ''
);
} );
};

// Event handler registration to generate the excerpt.
$( document ).on(
'click',
'#classifai-openai__excerpt-generate-btn',
generateExcerpt
);
}
} )( jQuery );
Loading

0 comments on commit bd9ea48

Please sign in to comment.