From 67f86fd4d4b511f47a2d71827ffae30ec3b17a6e Mon Sep 17 00:00:00 2001
From: James Morrison
+ Learn more about manual excerpts.' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ __( 'https://wordpress.org/documentation/article/what-is-an-excerpt-classic-editor/' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ );
+ ?>
+
+
+
+ get_settings();
+ $user_roles = wp_get_current_user()->roles ?? [];
+ $excerpt_roles = $settings['roles'] ?? [];
+
+ if (
+ ( ! empty( $excerpt_roles ) && empty( array_diff( $user_roles, $excerpt_roles ) ) )
+ && ( isset( $settings['enable_excerpt'] ) && 1 === (int) $settings['enable_excerpt'] )
+ ) {
+
+ $_post_id = isset( $_GET['post'] ) ? filter_var( $_GET['post'], FILTER_VALIDATE_INT ) : false;
+
+ if ( $_post_id ) {
+
+ wp_enqueue_script(
+ 'classifai-post-excerpt-classic-editor',
+ CLASSIFAI_PLUGIN_URL . 'dist/post-excerpt-classic-editor.js',
+ [],
+ CLASSIFAI_PLUGIN_VERSION,
+ true
+ );
+ wp_localize_script(
+ 'classifai-post-excerpt-classic-editor',
+ 'classifai_generate_excerpt',
+ array(
+ 'endpoint_url' => esc_url( get_rest_url( null, "/classifai/v1/openai/generate-excerpt/{$_post_id}" ) ),
+ 'script_debug' => defined( 'SCRIPT_DEBUG' ) ? SCRIPT_DEBUG : false,
+ 'generate_excerpt_text' => __( 'Generate excerpt', 'classifai' ),
+ 'regenerate_excerpt_text' => __( 'Re-generate excerpt', 'classifai' ),
+ 'nonce' => wp_create_nonce( 'wp_rest' ),
+ )
+ );
+ }
+ }
+
wp_enqueue_style(
'classifai-language-processing-style',
CLASSIFAI_PLUGIN_URL . 'dist/language-processing.css',
diff --git a/src/js/post-excerpt-classic-editor/index.js b/src/js/post-excerpt-classic-editor/index.js
new file mode 100644
index 000000000..59a5dd175
--- /dev/null
+++ b/src/js/post-excerpt-classic-editor/index.js
@@ -0,0 +1,62 @@
+
+/**
+ * This is the main webpack entry point for compiling STAT Comments plugin JS.
+ */
+
+console.log('Classifai Generate Excerpt Debug: Script Loaded');
+
+/* Variables */
+const classifaiGenerateExcerptButtonID = 'classifai-generate-excerpt';
+const classifaiGenerateExcerptButtonElement = document.getElementById(classifaiGenerateExcerptButtonID);
+const classifaiGenerateExcerptTextareaID = 'excerpt';
+const classifaiGenerateExcerptTextareaElement = document.getElementById(classifaiGenerateExcerptTextareaID);
+let classifarGenerateExcerptDebug = false;
+let classifaiGenerateExcerptText;
+
+if (classifai_generate_excerpt && classifai_generate_excerpt.script_debug) {
+ classifarGenerateExcerptDebug = classifai_generate_excerpt.script_debug;
+}
+
+/* Generate excerpt when the button is clicked */
+if (null !== classifaiGenerateExcerptButtonElement) {
+
+ classifaiGenerateExcerptButtonElement.onclick = function () {
+ classifarGenerateExcerptDebug && console.log('Classifai Generate Excerpt Debug: Button clicked, excerpt is being generated..');
+ classifaiExcerptGenerate();
+ };
+}
+
+/* Generate excerpt from API endpoint */
+function classifaiExcerptGenerate() {
+
+ // Confirm the endpoint URL is available; excerpt generation cannot function without this.
+ if (!classifai_generate_excerpt || false === classifai_generate_excerpt.endpoint_url) {
+ classifarGenerateExcerptDebug && console.log('Classifai Generate Excerpt Debug: Endpoint URL not set!');
+ return;
+ }
+
+ classifaiGenerateExcerptButtonElement.disabled = true;
+
+ fetch(classifai_generate_excerpt.endpoint_url, {
+ headers: {
+ 'X-WP-Nonce': classifai_generate_excerpt.nonce
+ }
+ })
+ .then((response) => response.json())
+ .then((result) => {
+ classifaiGenerateExcerptText = result;
+
+ classifarGenerateExcerptDebug && console.log('Classifai Generate Excerpt Debug: Generated Text', classifaiGenerateExcerptText)
+
+ if (classifaiGenerateExcerptText) {
+ classifaiGenerateExcerptTextareaElement.textContent = classifaiGenerateExcerptText
+ classifaiGenerateExcerptButtonElement.value = classifai_generate_excerpt.regenerate_excerpt_text;
+ }
+ })
+ .catch((error) => {
+ })
+ .finally(() => {
+ classifaiGenerateExcerptButtonElement.disabled = false
+ });
+
+}
diff --git a/webpack.config.js b/webpack.config.js
index 04ab30874..85543ee28 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -1,20 +1,20 @@
-const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
-const path = require( 'path' );
+const defaultConfig = require('@wordpress/scripts/config/webpack.config');
+const path = require('path');
module.exports = {
...defaultConfig,
output: {
...defaultConfig.output,
- path: path.resolve( __dirname, 'dist' ),
+ path: path.resolve(__dirname, 'dist'),
},
entry: {
- editor: [ './src/js/editor.js' ],
- 'editor-ocr': [ './src/js/editor-ocr.js' ],
- media: [ './src/js/media.js' ],
- admin: [ './src/js/admin.js' ],
- 'language-processing': [ './src/js/language-processing.js' ],
- 'gutenberg-plugin': [ './src/js/gutenberg-plugin.js' ],
- 'post-audio-controls': [ './src/js/post-audio-controls.js' ],
+ editor: ['./src/js/editor.js'],
+ 'editor-ocr': ['./src/js/editor-ocr.js'],
+ media: ['./src/js/media.js'],
+ admin: ['./src/js/admin.js'],
+ 'language-processing': ['./src/js/language-processing.js'],
+ 'gutenberg-plugin': ['./src/js/gutenberg-plugin.js'],
+ 'post-audio-controls': ['./src/js/post-audio-controls.js'],
'post-status-info': [
'./src/js/gutenberg-plugins/post-status-info.js',
],
@@ -24,8 +24,9 @@ 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' ],
+ 'post-excerpt': ['./src/js/post-excerpt/index.js'],
+ 'post-excerpt-classic-editor': ['./src/js/post-excerpt-classic-editor/index.js'],
+ 'media-modal': ['./src/js/media-modal/index.js'],
},
module: {
rules: [
From b82f9aa00377cefd1b23c2d6b9030eb624f418a2 Mon Sep 17 00:00:00 2001
From: James Morrison
- + esc_url( get_rest_url( null, "/classifai/v1/openai/generate-excerpt/{$_post_id}" ) ), - 'script_debug' => defined( 'SCRIPT_DEBUG' ) ? SCRIPT_DEBUG : false, - 'generate_excerpt_text' => __( 'Generate excerpt', 'classifai' ), - 'regenerate_excerpt_text' => __( 'Re-generate excerpt', 'classifai' ), - 'nonce' => wp_create_nonce( 'wp_rest' ), + 'endpointUrl' => esc_url( get_rest_url( null, "/classifai/v1/openai/generate-excerpt/{$_post_id}" ) ), + 'scriptDebug' => defined( 'SCRIPT_DEBUG' ) ? SCRIPT_DEBUG : false, + 'generateExcerptText' => __( 'Generate excerpt', 'classifai' ), + 'regenerateExcerptText' => __( 'Re-generate excerpt', 'classifai' ), + 'nonce' => wp_create_nonce( 'wp_rest' ), ) ); } diff --git a/package-lock.json b/package-lock.json index 9d341f746..aec3a295a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18054,20 +18054,6 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, - "node_modules/typescript": { - "version": "5.1.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", - "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==", - "dev": true, - "peer": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, "node_modules/uc.micro": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", diff --git a/src/js/post-excerpt-classic-editor/index.js b/src/js/post-excerpt-classic-editor/index.js index 59a5dd175..0dc2bf21f 100644 --- a/src/js/post-excerpt-classic-editor/index.js +++ b/src/js/post-excerpt-classic-editor/index.js @@ -1,10 +1,4 @@ -/** - * This is the main webpack entry point for compiling STAT Comments plugin JS. - */ - -console.log('Classifai Generate Excerpt Debug: Script Loaded'); - /* Variables */ const classifaiGenerateExcerptButtonID = 'classifai-generate-excerpt'; const classifaiGenerateExcerptButtonElement = document.getElementById(classifaiGenerateExcerptButtonID); @@ -13,8 +7,10 @@ const classifaiGenerateExcerptTextareaElement = document.getElementById(classifa let classifarGenerateExcerptDebug = false; let classifaiGenerateExcerptText; -if (classifai_generate_excerpt && classifai_generate_excerpt.script_debug) { - classifarGenerateExcerptDebug = classifai_generate_excerpt.script_debug; +// Note: classifarGenerateExcerptDebug is set by the SCRIPT_DEBUG constant +// Errors are only logged to console when SCRIPT_DEBUG is defined with value: TRUE +if (classifaiGenerateExcerpt && classifaiGenerateExcerpt.scriptDebug) { + classifarGenerateExcerptDebug = classifaiGenerateExcerpt.scriptDebug; } /* Generate excerpt when the button is clicked */ @@ -30,16 +26,16 @@ if (null !== classifaiGenerateExcerptButtonElement) { function classifaiExcerptGenerate() { // Confirm the endpoint URL is available; excerpt generation cannot function without this. - if (!classifai_generate_excerpt || false === classifai_generate_excerpt.endpoint_url) { + if (!classifaiGenerateExcerpt || false === classifaiGenerateExcerpt.endpointUrl) { classifarGenerateExcerptDebug && console.log('Classifai Generate Excerpt Debug: Endpoint URL not set!'); return; } classifaiGenerateExcerptButtonElement.disabled = true; - fetch(classifai_generate_excerpt.endpoint_url, { + fetch(classifaiGenerateExcerpt.endpointUrl, { headers: { - 'X-WP-Nonce': classifai_generate_excerpt.nonce + 'X-WP-Nonce': classifaiGenerateExcerpt.nonce } }) .then((response) => response.json()) @@ -50,7 +46,7 @@ function classifaiExcerptGenerate() { if (classifaiGenerateExcerptText) { classifaiGenerateExcerptTextareaElement.textContent = classifaiGenerateExcerptText - classifaiGenerateExcerptButtonElement.value = classifai_generate_excerpt.regenerate_excerpt_text; + classifaiGenerateExcerptButtonElement.value = classifaiGenerateExcerpt.regenerateExcerptText; } }) .catch((error) => { diff --git a/src/scss/admin.scss b/src/scss/admin.scss index 267232cac..911c6ff4b 100644 --- a/src/scss/admin.scss +++ b/src/scss/admin.scss @@ -1,55 +1,55 @@ -:root{ - --classifai-admin-theme-color:#007cba; - --classifai-admin-theme-color--rgb:0, 124, 186; - --classifai-admin-theme-color-darker-10:#006ba1; +:root { + --classifai-admin-theme-color: #007cba; + --classifai-admin-theme-color--rgb: 0, 124, 186; + --classifai-admin-theme-color-darker-10: #006ba1; } -body.admin-color-light{ - --classifai-admin-theme-color:#0085ba; - --classifai-admin-theme-color--rgb:0, 133, 186; - --classifai-admin-theme-color-darker-10:#0073a1; +body.admin-color-light { + --classifai-admin-theme-color: #0085ba; + --classifai-admin-theme-color--rgb: 0, 133, 186; + --classifai-admin-theme-color-darker-10: #0073a1; } -body.admin-color-modern{ - --classifai-admin-theme-color:#3858e9; - --classifai-admin-theme-color--rgb:56, 88, 233; - --classifai-admin-theme-color-darker-10:#2145e6; +body.admin-color-modern { + --classifai-admin-theme-color: #3858e9; + --classifai-admin-theme-color--rgb: 56, 88, 233; + --classifai-admin-theme-color-darker-10: #2145e6; } -body.admin-color-blue{ - --classifai-admin-theme-color:#096484; - --classifai-admin-theme-color--rgb:9, 100, 132; - --classifai-admin-theme-color-darker-10:#07526c; +body.admin-color-blue { + --classifai-admin-theme-color: #096484; + --classifai-admin-theme-color--rgb: 9, 100, 132; + --classifai-admin-theme-color-darker-10: #07526c; } -body.admin-color-coffee{ - --classifai-admin-theme-color:#46403c; - --classifai-admin-theme-color--rgb:70, 64, 60; - --classifai-admin-theme-color-darker-10:#383330; +body.admin-color-coffee { + --classifai-admin-theme-color: #46403c; + --classifai-admin-theme-color--rgb: 70, 64, 60; + --classifai-admin-theme-color-darker-10: #383330; } -body.admin-color-ectoplasm{ - --classifai-admin-theme-color:#523f6d; - --classifai-admin-theme-color--rgb:82, 63, 109; - --classifai-admin-theme-color-darker-10:#46365d; +body.admin-color-ectoplasm { + --classifai-admin-theme-color: #523f6d; + --classifai-admin-theme-color--rgb: 82, 63, 109; + --classifai-admin-theme-color-darker-10: #46365d; } -body.admin-color-midnight{ - --classifai-admin-theme-color:#e14d43; - --classifai-admin-theme-color--rgb:225, 77, 67; - --classifai-admin-theme-color-darker-10:#dd382d; +body.admin-color-midnight { + --classifai-admin-theme-color: #e14d43; + --classifai-admin-theme-color--rgb: 225, 77, 67; + --classifai-admin-theme-color-darker-10: #dd382d; } -body.admin-color-ocean{ - --classifai-admin-theme-color:#627c83; - --classifai-admin-theme-color--rgb:98, 124, 131; - --classifai-admin-theme-color-darker-10:#576e74; +body.admin-color-ocean { + --classifai-admin-theme-color: #627c83; + --classifai-admin-theme-color--rgb: 98, 124, 131; + --classifai-admin-theme-color-darker-10: #576e74; } -body.admin-color-sunrise{ - --classifai-admin-theme-color:#dd823b; - --classifai-admin-theme-color--rgb:221, 130, 59; - --classifai-admin-theme-color-darker-10:#d97426; +body.admin-color-sunrise { + --classifai-admin-theme-color: #dd823b; + --classifai-admin-theme-color--rgb: 221, 130, 59; + --classifai-admin-theme-color-darker-10: #d97426; } #classifai-activation-notice { @@ -57,6 +57,7 @@ body.admin-color-sunrise{ .classifai-logo { line-height: 0; + img { width: 180px; } @@ -74,7 +75,7 @@ a.classifai-button, input.classifai-button { font-size: 14px; padding: 8px 16px; - background: var( --classifai-admin-theme-color ); + background: var(--classifai-admin-theme-color); color: #fff; font-style: normal; font-weight: 400; @@ -85,7 +86,7 @@ input.classifai-button { text-decoration: none; text-shadow: none; text-transform: uppercase; - border-color: var( --classifai-admin-theme-color ); + border-color: var(--classifai-admin-theme-color); border-radius: 4px; border-width: 1px; border-style: solid; @@ -94,12 +95,12 @@ input.classifai-button { &:focus, &:active { color: #fff; - background: var( --classifai-admin-theme-color-darker-10 ); - border-color: var( --classifai-admin-theme-color-darker-10 ); + background: var(--classifai-admin-theme-color-darker-10); + border-color: var(--classifai-admin-theme-color-darker-10); } &:focus { - box-shadow: 0 0 0 1px #fff, 0 0 0 3px var( --classifai-admin-theme-color-darker-10 ); + box-shadow: 0 0 0 1px #fff, 0 0 0 3px var(--classifai-admin-theme-color-darker-10); } &:active { @@ -145,6 +146,7 @@ input.classifai-button { .classifai-setup-page & { position: relative; + body.admin-bar & { @media (min-width: 601px) { top: 0px; @@ -165,6 +167,7 @@ input.classifai-button { #classifai-logo { line-height: 0; + img { max-width: 180px; } @@ -192,11 +195,11 @@ input.classifai-button { text-align: center; &[aria-expanded="true"] { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); } &:hover { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); } span.control-item-text { @@ -205,7 +208,7 @@ input.classifai-button { } } - .classifai-help-menu{ + .classifai-help-menu { display: block; width: 200px; @@ -216,12 +219,12 @@ input.classifai-button { padding: 8px 12px; &:hover { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); } } } - .admin_page_classifai_setup &{ + .admin_page_classifai_setup & { box-shadow: none; } } @@ -269,16 +272,17 @@ input.classifai-button { left: 0px; right: 0px; border-radius: 3px 3px 0px 0px; - background: var( --classifai-admin-theme-color ); - box-shadow: 0px 4px 10px 3px rgba( var(--classifai-admin-theme-color--rgb), .15); + background: var(--classifai-admin-theme-color); + box-shadow: 0px 4px 10px 3px rgba(var(--classifai-admin-theme-color--rgb), .15); opacity: 0; transform: scale(0, 1); } &.nav-tab-active, &:hover { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); font-weight: 600; + &:after { opacity: 1; transform: scale(1, 1); @@ -341,10 +345,13 @@ input.classifai-button { justify-content: center; } - a, a:hover, a:active, a:focus { + a, + a:hover, + a:active, + a:focus { display: inherit; text-decoration: none; - color: #3c434a; + color: #3c434a; } @media screen and (max-width: 480px) { @@ -354,7 +361,7 @@ input.classifai-button { &.is-complete { span.step-count { - background: var( --classifai-admin-theme-color ); + background: var(--classifai-admin-theme-color); color: #fff; } } @@ -365,7 +372,7 @@ input.classifai-button { } span.step-count { - background: var( --classifai-admin-theme-color ); + background: var(--classifai-admin-theme-color); color: #fff; } } @@ -450,7 +457,7 @@ input.classifai-button { margin-top: 20px; } - .classifai-step2-content{ + .classifai-step2-content { max-width: 480px; margin: 0 auto; } @@ -494,7 +501,7 @@ input.classifai-button { font-size: 14px; } - span.classifai-feature-text{ + span.classifai-feature-text { float: left; } @@ -513,6 +520,7 @@ input.classifai-button { } @media screen and (max-width: 767px) { + .classifai-step1-content, .classifai-step4-content { max-width: 100%; @@ -531,7 +539,7 @@ input.classifai-button { max-width: 480px; } - .classifai-setup-form-field{ + .classifai-setup-form-field { margin-top: 40px; } @@ -550,7 +558,7 @@ input.classifai-button { margin-bottom: 4px; } - .classifai-setup-footer{ + .classifai-setup-footer { margin-top: 40px; } } @@ -576,14 +584,17 @@ input.classifai-button { .classifai-tabs { display: block; + &.tabs-center { margin: auto; margin-bottom: 24px; } + &.tabs-justify { width: 100%; table-layout: fixed; } + a.tab { text-decoration: none; position: relative; @@ -595,12 +606,15 @@ input.classifai-button { white-space: nowrap; cursor: pointer; font-size: 14px; + &:focus { box-shadow: none; } + &:hover { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); } + &:after { transition: all .3s cubic-bezier(1, 0, 0, 1); will-change: transform, box-shadow, opacity; @@ -611,14 +625,16 @@ input.classifai-button { left: 0px; right: 0px; border-radius: 3px 3px 0px 0px; - background: var( --classifai-admin-theme-color ); - box-shadow: 0px 4px 10px 3px rgba( var(--classifai-admin-theme-color--rgb), .15); + background: var(--classifai-admin-theme-color); + box-shadow: 0px 4px 10px 3px rgba(var(--classifai-admin-theme-color--rgb), .15); opacity: 0; transform: scale(0, 1); } + &.active { - color: var( --classifai-admin-theme-color ); + color: var(--classifai-admin-theme-color); font-weight: 600; + &:after { opacity: 1; transform: scale(1, 1); @@ -644,6 +660,7 @@ input.classifai-button { width: 100%; text-align: right; } + .classifai-toggle-switch { display: inline-block; background: transparent; @@ -655,10 +672,12 @@ input.classifai-button { vertical-align: middle; transition: background 0.25s; box-sizing: border-box; + &:before, &:after { content: ""; } + &:before { display: block; background: #1e1e1e; @@ -672,16 +691,24 @@ input.classifai-button { transition: left 0.25s; } - .classifai-toggle-checkbox:checked + & { + .classifai-toggle-checkbox:checked+& { background: var(--classifai-admin-theme-color); border-color: var(--classifai-admin-theme-color); + &:before { background-color: #fff; left: 20px; } } } + .classifai-toggle-checkbox { position: absolute; visibility: hidden; } + +#classifai-generate-excerpt { + position: absolute; + right: 12px; + top: 70px; +} From 57aec1e21ef3fad3b2913f9f238f663d23cfb882 Mon Sep 17 00:00:00 2001 From: Ravinder Kumar
Learn more about manual excerpts.' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + __( 'Excerpts are optional hand-crafted summaries of your content that can be used in your theme. Learn more about manual excerpts.' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped __( 'https://wordpress.org/documentation/article/what-is-an-excerpt-classic-editor/' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); ?>
- - -- - + + +
+ + + +
+ + Date: Thu, 10 Aug 2023 21:54:37 +0530 Subject: [PATCH 06/16] refactor: register classic editor excerpt related assets --- .../Classifai/Providers/OpenAI/ChatGPT.php | 64 ++++++++----------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 2a692cb55..dc699c79a 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -157,6 +157,31 @@ public function replace_classic_editor_excerpt(): void { 'core', array( '__back_compat_meta_box' => true ) ); + + // Register assets. + wp_enqueue_script( + 'classifai-post-excerpt-classic-editor', + CLASSIFAI_PLUGIN_URL . 'dist/post-excerpt-classic-editor.js', + [], + CLASSIFAI_PLUGIN_VERSION, + true + ); + wp_localize_script( + 'classifai-post-excerpt-classic-editor', + 'classifaiGenerateExcerpt', + [ + 'endpointUrl' => esc_url( + get_rest_url( + null, + "/classifai/v1/openai/generate-excerpt/{$post->ID}" + ) + ), + 'scriptDebug' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG, + 'generateExcerptText' => __( 'Generate excerpt', 'classifai' ), + 'regenerateExcerptText' => __( 'Re-generate excerpt', 'classifai' ), + 'nonce' => wp_create_nonce( 'wp_rest' ), + ] + ); } /** @@ -290,53 +315,16 @@ public function enqueue_editor_assets() { * * @param string $hook_suffix The current admin page. */ - public function enqueue_admin_assets( $hook_suffix = '' ) { + public function enqueue_admin_assets( string $hook_suffix = '' ): void { if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) { return; } - $settings = $this->get_settings(); - $excerpt_roles = $settings['roles'] ?? []; $screen = get_current_screen(); $settings = $this->get_settings(); $user_roles = wp_get_current_user()->roles ?? []; $title_roles = $settings['title_roles'] ?? []; - if ( - ( ! empty( $excerpt_roles ) && empty( array_diff( $user_roles, $excerpt_roles ) ) ) - && ( isset( $settings['enable_excerpt'] ) && 1 === (int) $settings['enable_excerpt'] ) - ) { - $_post_id = isset( $_GET['post'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended - ? filter_var( $_GET['post'], FILTER_VALIDATE_INT ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended - : false; - - if ( $_post_id ) { - wp_enqueue_script( - 'classifai-post-excerpt-classic-editor', - CLASSIFAI_PLUGIN_URL . 'dist/post-excerpt-classic-editor.js', - [], - CLASSIFAI_PLUGIN_VERSION, - true - ); - wp_localize_script( - 'classifai-post-excerpt-classic-editor', - 'classifaiGenerateExcerpt', - [ - 'endpointUrl' => esc_url( - get_rest_url( - null, - "/classifai/v1/openai/generate-excerpt/{$_post_id}" - ) - ), - 'scriptDebug' => defined( 'SCRIPT_DEBUG' ) ? SCRIPT_DEBUG : false, - 'generateExcerptText' => __( 'Generate excerpt', 'classifai' ), - 'regenerateExcerptText' => __( 'Re-generate excerpt', 'classifai' ), - 'nonce' => wp_create_nonce( 'wp_rest' ), - ] - ); - } - } - // Load the assets for the classic editor. if ( $screen && ! $screen->is_block_editor() From ec7e355ae20aed72fd78250a4f8a4cb5f24aadfd Mon Sep 17 00:00:00 2001 From: Ravinder Kumar- Learn more about manual excerpts.' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - __( 'https://wordpress.org/documentation/article/what-is-an-excerpt-classic-editor/' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - ); - ?> -
- - - -- - - -
- - - 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( diff --git a/src/js/openai/classic-editor-excerpt-generator.js b/src/js/openai/classic-editor-excerpt-generator.js index 8235fa18e..43aa29391 100644 --- a/src/js/openai/classic-editor-excerpt-generator.js +++ b/src/js/openai/classic-editor-excerpt-generator.js @@ -1,79 +1,84 @@ -/* globals classifaiGenerateExcerpt */ -const classifaiGenerateExcerptButtonID = 'classifai-generate-excerpt'; -const classifaiGenerateExcerptButtonElement = document.getElementById( - classifaiGenerateExcerptButtonID -); -const classifaiGenerateExcerptTextareaID = 'excerpt'; -const classifaiGenerateExcerptTextareaElement = document.getElementById( - classifaiGenerateExcerptTextareaID -); -let classifarGenerateExcerptDebug = false; -let classifaiGenerateExcerptText; +import apiFetch from '@wordpress/api-fetch'; +import '../../scss/openai/classic-editor-title-generator.scss'; -// Note: classifarGenerateExcerptDebug is set by the SCRIPT_DEBUG constant -// Errors are only logged to console when SCRIPT_DEBUG is defined with value: TRUE -if ( classifaiGenerateExcerpt && classifaiGenerateExcerpt.scriptDebug ) { - classifarGenerateExcerptDebug = classifaiGenerateExcerpt.scriptDebug; -} +const classifaiExcerptData = window.classifaiGenerateExcerpt || {}; -/* Generate excerpt when the button is clicked */ -if ( null !== classifaiGenerateExcerptButtonElement ) { - classifaiGenerateExcerptButtonElement.onclick = function () { - if ( classifarGenerateExcerptDebug ) { - // eslint-disable-next-line no-console - console.log( - 'Classifai Generate Excerpt Debug: Button clicked, excerpt is being generated..' - ); +( 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' ); - classifaiExcerptGenerate(); - }; -} + // Boolean indicating whether generation is in progress. + let isProcessing = false; + + // Creates and appends the "Generate excerpt" button. + $( '', { + text: excerptContainer.value + ? classifaiExcerptData?.regenerateText ?? '' + : classifaiExcerptData?.buttonText ?? '', + class: 'classifai-openai__excerpt-generate-btn--text', + } ) + .wrap( + '' + ) + .parent() + .append( + $( '', { + 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; + } -/* Generate excerpt from API endpoint */ -function classifaiExcerptGenerate() { - // Confirm the endpoint URL is available; excerpt generation cannot function without this. - if ( - ! classifaiGenerateExcerpt || - false === classifaiGenerateExcerpt.endpointUrl - ) { - if ( classifarGenerateExcerptDebug ) { - // eslint-disable-next-line no-console - console.log( - 'Classifai Generate Excerpt Debug: Endpoint URL not set!' + const generateTextEl = $( + '.classifai-openai__excerpt-generate-btn--text' + ); + const spinnerEl = $( + '.classifai-openai__excerpt-generate-btn--spinner' ); - } - return; - } + generateTextEl.css( 'opacity', '0' ); + spinnerEl.show(); + isProcessing = true; - classifaiGenerateExcerptButtonElement.disabled = true; + const path = classifaiExcerptData?.path + postId; - fetch( classifaiGenerateExcerpt.endpointUrl, { - headers: { - 'X-WP-Nonce': classifaiGenerateExcerpt.nonce, - }, - } ) - .then( ( response ) => response.json() ) - .then( ( result ) => { - classifaiGenerateExcerptText = result; + apiFetch( { + path, + } ).then( ( result ) => { + generateTextEl.css( 'opacity', '1' ); + spinnerEl.hide(); + isProcessing = false; - if ( classifarGenerateExcerptDebug ) { - // eslint-disable-next-line no-console - console.log( - 'Classifai Generate Excerpt Debug: Generated Text', - classifaiGenerateExcerptText + $( excerptContainer ).val( result ).trigger( 'input' ); + generateTextEl.text( + classifaiExcerptData?.regenerateText ?? '' ); - } + } ); + }; - if ( classifaiGenerateExcerptText ) { - classifaiGenerateExcerptTextareaElement.textContent = - classifaiGenerateExcerptText; - classifaiGenerateExcerptButtonElement.value = - classifaiGenerateExcerpt.regenerateExcerptText; - } - } ) - .finally( () => { - classifaiGenerateExcerptButtonElement.disabled = false; - } ); -} + // Event handler registration to generate the excerpt. + $( document ).on( + 'click', + '#classifai-openai__excerpt-generate-btn', + generateExcerpt + ); + } +} )( jQuery ); diff --git a/src/scss/openai/classic-editor-title-generator.scss b/src/scss/openai/classic-editor-title-generator.scss index 0fab2c8ba..173e6a3e8 100644 --- a/src/scss/openai/classic-editor-title-generator.scss +++ b/src/scss/openai/classic-editor-title-generator.scss @@ -18,6 +18,26 @@ } } +#postexcerpt { + #classifai-openai__excerpt-generate-btn { + display: block; + width: 160px; + max-width: 100%; + margin: 1em 0 0 auto; + text-align: center; + position: relative; + + & > .classifai-openai__excerpt-generate-btn--spinner { + display: none; + position: absolute; + top: 50%; + left: 50%; + transform: translate( -50%, -50% ); + line-height: 29px; + } + } +} + #classifai-openai { &__overlay { position: fixed; @@ -108,7 +128,8 @@ } } - &__title-generate-btn--spinner { + &__title-generate-btn--spinner, + &__excerpt-generate-btn--spinner { &:before { display: block; animation: rotation 2s infinite linear; From d1f4d0cc68a45839dae1117c24e8c4ef3059619f Mon Sep 17 00:00:00 2001 From: Darin Kotter