diff --git a/.github/workflows/cypress.yml b/.github/workflows/cypress.yml
index 9da966642..8093f5e21 100644
--- a/.github/workflows/cypress.yml
+++ b/.github/workflows/cypress.yml
@@ -17,7 +17,6 @@ jobs:
core:
- {name: 'WP latest', version: 'latest'}
- {name: 'WP minimum', version: 'WordPress/WordPress#5.7'}
- - {name: 'WP trunk', version: 'WordPress/WordPress#master'}
steps:
- name: Checkout
uses: actions/checkout@v3
diff --git a/.wp-env.json b/.wp-env.json
index 7881ddaa8..d3be216b9 100644
--- a/.wp-env.json
+++ b/.wp-env.json
@@ -1,10 +1,13 @@
{
- "plugins": [".", "./tests/test-plugin"],
+ "plugins": [".", "./tests/test-plugin", "https://downloads.wordpress.org/plugin/classic-editor.zip"],
"env": {
"tests": {
"mappings": {
"wp-cli.yml": "./tests/bin/wp-cli.yml"
}
}
+ },
+ "lifecycleScripts": {
+ "afterStart": "bash ./tests/bin/initialize.sh"
}
}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 642cdfd6b..241318801 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file, per [the Ke
## [Unreleased] - TBD
+## [2.2.2] - 2023-06-28
+### Added
+- Support for generating post titles in the Classic Editor using OpenAI's ChatGPT API (props [@Sidsector9](https://github.com/Sidsector9), [@dkotter](https://github.com/dkotter) via [#506](https://github.com/10up/classifai/pull/506)).
+- New utility method to retrieve all post statuses (props [@Sidsector9](https://github.com/Sidsector9), [@dkotter](https://github.com/dkotter) via [#507](https://github.com/10up/classifai/pull/507)).
+
+### Changed
+- Optimized calls to the Microsoft Azure Text to Speech API (props [@joshuaabenazer](https://github.com/joshuaabenazer), [@ravinderk](https://github.com/ravinderk), [@dkotter](https://github.com/dkotter) via [#487](https://github.com/10up/classifai/pull/487)).
+- When the Text to Speech option is toggled off, hide the Text to Speech audio button on the single post level (props [@joshuaabenazer](https://github.com/joshuaabenazer), [@ravinderk](https://github.com/ravinderk), [@dkotter](https://github.com/dkotter) via [#494](https://github.com/10up/classifai/pull/494)).
+
+### Fixed
+- Ensure any edits made to generated titles persist when that title is inserted (props [@dkotter](https://github.com/dkotter), [@Sidsector9](https://github.com/Sidsector9) via [#509](https://github.com/10up/classifai/pull/509)).
+- Ensure we show all post statuses in our settings instead of just the core post ones (props [@Sidsector9](https://github.com/Sidsector9), [@dkotter](https://github.com/dkotter) via [#507](https://github.com/10up/classifai/pull/507)).
+
## [2.2.1] - 2023-06-08
### Added
- Ability to generate images in the Classic Editor (props [@dkotter](https://github.com/dkotter), [@ravinderk](https://github.com/ravinderk) via [#471](https://github.com/10up/classifai/pull/471)).
@@ -356,6 +369,7 @@ All notable changes to this project will be documented in this file, per [the Ke
- Initial closed source release
[Unreleased]: https://github.com/10up/classifai/compare/trunk...develop
+[2.2.2]: https://github.com/10up/classifai/compare/2.2.1...2.2.2
[2.2.1]: https://github.com/10up/classifai/compare/2.2.0...2.2.1
[2.2.0]: https://github.com/10up/classifai/compare/2.1.0...2.2.0
[2.1.0]: https://github.com/10up/classifai/compare/2.0.0...2.1.0
diff --git a/CREDITS.md b/CREDITS.md
index cb901fba0..aa37c9d57 100644
--- a/CREDITS.md
+++ b/CREDITS.md
@@ -10,7 +10,7 @@ The following individuals are responsible for curating the list of issues, respo
Thank you to all the people who have already contributed to this repository via bug reports, code, design, ideas, project management, translation, testing, etc.
-[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Ryan Welcher (@ryanwelcher)](https://github.com/ryanwelcher), [Adam Silverstein (@adamsilverstein)](https://github.com/adamsilverstein), [Jeffrey Paul (@jeffpaul)](https://github.com/jeffpaul), [Helen Hou-Sandí (@helen)](https://github.com/helen), [Christian Chung (@christianc1)](https://github.com/christianc1), [Dependabot (@dependabot)](https://github.com/dependabot), [Dillon McCallum (@McCallumDillon)](https://github.com/McCallumDillon), [Dustin Rue (@dustinrue)](https://github.com/dustinrue), [Jay Wood (@JayWood)](https://github.com/JayWood), [John Watkins (@johnwatkins0)](https://github.com/johnwatkins0), [Matthew McAchran (@mmcachran)](https://github.com/mmcachran), [Tung Du (@dinhtungdu)](https://github.com/dinhtungdu), [Darío Hereñú (@kant)](https://github.com/kant), [Aaron Jorbin (@aaronjorbin)](https://github.com/aaronjorbin), [Eduard Florea (@eflorea)](https://github.com/eflorea), [Jackie Kjome (@JackieKjome)](https://github.com/JackieKjome), [Ricky Lee Whittemore (@rickalee)](https://github.com/rickalee), [Ritesh Patel (@Ritesh-patel)](https://github.com/Ritesh-patel), [Dana Ross (@dana-ross)](https://github.com/dana-ross), [Darin Kotter (@dkotter)](https://github.com/dkotter), [Pete Nelson (@petenelson)](https://github.com/petenelson), [Connor Smyth (@ActuallyConnor)](https://github.com/ActuallyConnor), [Oscar Sanchez S. (@oscarssanchez)](https://github.com/oscarssanchez), [Shah Qureshi (@ShahAaron)](https://github.com/ShahAaron), [Barry Ceelen (@barryceelen)](https://github.com/barryceelen), [Jeffrey Carandang (@phpbits)](https://github.com/phpbits), [Thrijith Thankachan (@thrijith)](https://github.com/thrijith), [Junaid Bhura (@junaidbhura)](https://github.com/junaidbhura), [Blanca Hong [@blancahong]](https://profiles.wordpress.org/blancahong/), [Barney Jeffries (@barneyjeffries)](https://github.com/barneyjeffries), [Siddharth Thevaril (@Sidsector9)](https://github.com/Sidsector9), [@myBCN](https://github.com/myBCN), [James Morrison (@jamesmorrison)](https://github.com/jamesmorrison), [Rahul Prajapati (@rahulsprajapati)](https://github.com/rahulsprajapati), [Sérgio Santos (@s3rgiosan)](https://github.com/s3rgiosan), [Dharmesh Patel (@iamdharmesh)](https://github.com/iamdharmesh), [Delfina Hoxha](https://www.linkedin.com/in/delfina-hoxha/), [Ajay Maurya (@ajmaurya99)](https://github.com/ajmaurya99), [Faisal Alvi (@faisal-alvi)](https://github.com/faisal-alvi), [Mustafa Uysal (@mustafauysal)](https://github.com/mustafauysal), [Max Lyuchin (@cadic)](https://github.com/cadic), [Peter Wilson (@peterwilsoncc)](https://github.com/peterwilsoncc), [Quamruz Zaman (@zamanq)](https://github.com/zamanq), [Ravinder Kumar (@ravinderk)](https://github.com/ravinderk), [Joe McGill (@joemcgill)](https://github.com/joemcgill), [Tyler Bailey (@TylerB24890)](https://github.com/TylerB24890), [Viktor Szépe (@szepeviktor)](https://github.com/szepeviktor), [Ivan Ivanić (@Spoygg)](https://github.com/Spoygg), [Jayedul Kabir (@jayedul)](https://github.com/jayedul), [Garth Gutenberg (@ggutenberg)](https://github.com/ggutenberg), [Mehidi Hassan (@mehidi258)](https://github.com/mehidi258), [Julian Weiland (@derweili)](https://github.com/derweili), [Anton Vanyukov (@av3nger)](https://github.com/av3nger), [Ben Keith (@benlk)](https://github.com/benlk), [David Hechler (@pixeldevsio)](https://github.com/pixeldevsio).
+[Darshan Sawardekar (@dsawardekar)](https://github.com/dsawardekar), [Ryan Welcher (@ryanwelcher)](https://github.com/ryanwelcher), [Adam Silverstein (@adamsilverstein)](https://github.com/adamsilverstein), [Jeffrey Paul (@jeffpaul)](https://github.com/jeffpaul), [Helen Hou-Sandí (@helen)](https://github.com/helen), [Christian Chung (@christianc1)](https://github.com/christianc1), [Dependabot (@dependabot)](https://github.com/dependabot), [Dillon McCallum (@McCallumDillon)](https://github.com/McCallumDillon), [Dustin Rue (@dustinrue)](https://github.com/dustinrue), [Jay Wood (@JayWood)](https://github.com/JayWood), [John Watkins (@johnwatkins0)](https://github.com/johnwatkins0), [Matthew McAchran (@mmcachran)](https://github.com/mmcachran), [Tung Du (@dinhtungdu)](https://github.com/dinhtungdu), [Darío Hereñú (@kant)](https://github.com/kant), [Aaron Jorbin (@aaronjorbin)](https://github.com/aaronjorbin), [Eduard Florea (@eflorea)](https://github.com/eflorea), [Jackie Kjome (@JackieKjome)](https://github.com/JackieKjome), [Ricky Lee Whittemore (@rickalee)](https://github.com/rickalee), [Ritesh Patel (@Ritesh-patel)](https://github.com/Ritesh-patel), [Dana Ross (@dana-ross)](https://github.com/dana-ross), [Darin Kotter (@dkotter)](https://github.com/dkotter), [Pete Nelson (@petenelson)](https://github.com/petenelson), [Connor Smyth (@ActuallyConnor)](https://github.com/ActuallyConnor), [Oscar Sanchez S. (@oscarssanchez)](https://github.com/oscarssanchez), [Shah Qureshi (@ShahAaron)](https://github.com/ShahAaron), [Barry Ceelen (@barryceelen)](https://github.com/barryceelen), [Jeffrey Carandang (@phpbits)](https://github.com/phpbits), [Thrijith Thankachan (@thrijith)](https://github.com/thrijith), [Junaid Bhura (@junaidbhura)](https://github.com/junaidbhura), [Blanca Hong [@blancahong]](https://profiles.wordpress.org/blancahong/), [Barney Jeffries (@barneyjeffries)](https://github.com/barneyjeffries), [Siddharth Thevaril (@Sidsector9)](https://github.com/Sidsector9), [@myBCN](https://github.com/myBCN), [James Morrison (@jamesmorrison)](https://github.com/jamesmorrison), [Rahul Prajapati (@rahulsprajapati)](https://github.com/rahulsprajapati), [Sérgio Santos (@s3rgiosan)](https://github.com/s3rgiosan), [Dharmesh Patel (@iamdharmesh)](https://github.com/iamdharmesh), [Delfina Hoxha](https://www.linkedin.com/in/delfina-hoxha/), [Ajay Maurya (@ajmaurya99)](https://github.com/ajmaurya99), [Faisal Alvi (@faisal-alvi)](https://github.com/faisal-alvi), [Mustafa Uysal (@mustafauysal)](https://github.com/mustafauysal), [Max Lyuchin (@cadic)](https://github.com/cadic), [Peter Wilson (@peterwilsoncc)](https://github.com/peterwilsoncc), [Quamruz Zaman (@zamanq)](https://github.com/zamanq), [Ravinder Kumar (@ravinderk)](https://github.com/ravinderk), [Joe McGill (@joemcgill)](https://github.com/joemcgill), [Tyler Bailey (@TylerB24890)](https://github.com/TylerB24890), [Viktor Szépe (@szepeviktor)](https://github.com/szepeviktor), [Ivan Ivanić (@Spoygg)](https://github.com/Spoygg), [Jayedul Kabir (@jayedul)](https://github.com/jayedul), [Garth Gutenberg (@ggutenberg)](https://github.com/ggutenberg), [Mehidi Hassan (@mehidi258)](https://github.com/mehidi258), [Julian Weiland (@derweili)](https://github.com/derweili), [Anton Vanyukov (@av3nger)](https://github.com/av3nger), [Ben Keith (@benlk)](https://github.com/benlk), [David Hechler (@pixeldevsio)](https://github.com/pixeldevsio), [Joshua Abenazer (@joshuaabenazer)](https://github.com/joshuaabenazer).
## Libraries
diff --git a/README.md b/README.md
index eccb40fa3..e3b184908 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@
* [Set Up OpenAI ChatGPT Language Processing](#set-up-language-processing-via-openai-chatgpt)
* [Set Up OpenAI Embeddings Language Processing](#set-up-language-processing-via-openai-embeddings)
* [Set Up OpenAI Whisper Language Processing](#set-up-language-processing-via-openai-whisper)
+* [Set Up Azure AI Language Processing](#set-up-language-processing-via-microsoft-azure)
* [Set Up Azure AI Computer Vision Image Processing](#set-up-image-processing-via-microsoft-azure)
* [Set Up OpenAI DALL·E Image Processing](#set-up-image-processing-via-openai)
* [Set Up Azure Personalizer Recommended Content](#set-up-recommended-content-via-microsoft-azure-personalizer)
@@ -226,7 +227,7 @@ IBM Watson's [Categories](https://cloud.ibm.com/docs/natural-language-understand
* To test excerpt generation, edit (or create) an item that supports excerpts. Note: only the block editor is supported.
* Ensure this item has content saved.
* Open the Excerpt panel in the sidebar and click on `Generate Excerpt`.
-* To test title generation, edit (or create) an item that supports titles. Note: only the block editor is supported.
+* To test title generation, edit (or create) an item that supports titles.
* Ensure this item has content saved.
* Open the Summary panel in the sidebar and click on `Generate titles`.
@@ -288,14 +289,14 @@ Note that [OpenAI](https://platform.openai.com/docs/guides/speech-to-text) can c
### 1. Sign up for Azure services
* [Register for a Microsoft Azure account](https://azure.microsoft.com/en-us/free/) or sign into your existing one.
-* Log into your account and create a new [*Speech Service*](https://portal.azure.com/#view/Microsoft_Azure_ProjectOxford/CognitiveServicesHub/~/overview) Service if you do not already have one. It may take a minute for your account to fully populate with the default resource group to use.
-* Click `Keys and Endpoint` in the left hand Resource Management menu to view the `Endpoint` URL for this resource.
+* Log into your account and create a new [*Speech Service*](https://portal.azure.com/#view/Microsoft_Azure_ProjectOxford/CognitiveServicesHub/~/overview) if you do not already have one. It may take a minute for your account to fully populate with the default resource group to use.
+* Click `Keys and Endpoint` in the left hand Resource Management menu to view the `Location/Region` for this resource.
* Click the copy icon next to `KEY 1` to copy the API Key credential for this resource.
### 2. Configure Microsoft Azure API and Key under Tools > ClassifAI > Language Processing > Microsoft Azure
-* In the `Endpoint URL` field, enter your `API endpoint`.
-* In the `API Key` field, enter your `KEY 1`.
+* In the `Endpoint URL` field, enter the following URL, replacing `LOCATION` with the `Location/Region` you found above: `https://LOCATION.tts.speech.microsoft.com/`.
+* In the `API Key` field, enter your `KEY 1` copied from above.
* Click **Save Changes** (the page will reload).
* If connected successfully, a new dropdown with the label "Voices" will be displayed.
* Select a voice as per your choice.
diff --git a/classifai.php b/classifai.php
index 83637ffa5..735e0bce6 100644
--- a/classifai.php
+++ b/classifai.php
@@ -4,7 +4,7 @@
* Plugin URI: https://github.com/10up/classifai
* Update URI: https://classifaiplugin.com
* Description: Enhance your WordPress content with Artificial Intelligence and Machine Learning services.
- * Version: 2.2.1
+ * Version: 2.2.2
* Requires at least: 5.7
* Requires PHP: 7.4
* Author: 10up
diff --git a/config.php b/config.php
index 31759e718..e4e20d990 100644
--- a/config.php
+++ b/config.php
@@ -4,7 +4,7 @@
* declared here instead of a Class.
*/
-$plugin_version = '2.2.1';
+$plugin_version = '2.2.2';
if ( file_exists( __DIR__ . '/.commit' ) ) {
$plugin_version .= '-' . file_get_contents( __DIR__ . '/.commit' );
diff --git a/includes/Classifai/Admin/SavePostHandler.php b/includes/Classifai/Admin/SavePostHandler.php
index b0f7484e3..0cf6ddb68 100644
--- a/includes/Classifai/Admin/SavePostHandler.php
+++ b/includes/Classifai/Admin/SavePostHandler.php
@@ -192,10 +192,24 @@ public function synthesize_speech( $post_id ) {
);
}
- $normalizer = new Normalizer();
- $settings = \Classifai\get_plugin_settings( 'language_processing', TextToSpeech::FEATURE_NAME );
- $post = get_post( $post_id );
- $post_content = $normalizer->normalize_content( $post->post_content, $post->post_title, $post_id );
+ $normalizer = new Normalizer();
+ $settings = \Classifai\get_plugin_settings( 'language_processing', TextToSpeech::FEATURE_NAME );
+ $post = get_post( $post_id );
+ $post_content = $normalizer->normalize_content( $post->post_content, $post->post_title, $post_id );
+ $content_hash = get_post_meta( $post_id, TextToSpeech::AUDIO_HASH_KEY, true );
+ $saved_attachment_id = (int) get_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, true );
+
+ // Don't regenerate the audio file it it already exists and the content hasn't changed.
+ if ( $saved_attachment_id ) {
+
+ // Check if the audio file exists.
+ $audio_attachment_url = wp_get_attachment_url( $saved_attachment_id );
+
+ if ( $audio_attachment_url && ! empty( $content_hash ) && ( md5( $post_content ) === $content_hash ) ) {
+ return $saved_attachment_id;
+ }
+ }
+
$voice = $settings['voice'] ?? '';
$voice_data = explode( '|', $voice );
$voice_name = '';
@@ -255,8 +269,6 @@ public function synthesize_speech( $post_id ) {
);
}
- $saved_attachment_id = (int) get_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, true );
-
// If audio already exists for this post, delete it.
if ( $saved_attachment_id ) {
wp_delete_attachment( $saved_attachment_id, true );
@@ -291,7 +303,8 @@ public function synthesize_speech( $post_id ) {
'post_title' => $audio_file_name,
'post_mime_type' => $file_data['type'],
),
- $file_data['file']
+ $file_data['file'],
+ $post_id
);
// Return error if creation of attachment fails.
@@ -304,6 +317,7 @@ public function synthesize_speech( $post_id ) {
update_post_meta( $post_id, TextToSpeech::AUDIO_ID_KEY, absint( $attachment_id ) );
update_post_meta( $post_id, TextToSpeech::AUDIO_TIMESTAMP_KEY, time() );
+ update_post_meta( $post_id, TextToSpeech::AUDIO_HASH_KEY, md5( $post_content ) );
return $attachment_id;
}
diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php
index 36664dcca..734bf1db6 100644
--- a/includes/Classifai/Helpers.php
+++ b/includes/Classifai/Helpers.php
@@ -225,7 +225,7 @@ function get_post_types_for_language_settings() {
* @return array
*/
function get_post_statuses_for_language_settings() {
- $post_statuses = get_post_statuses();
+ $post_statuses = get_all_post_statuses();
/**
* Filter post statuses shown in language processing settings.
@@ -693,3 +693,59 @@ function find_provider_class( array $provider_classes = [], string $service_name
return $provider;
}
+
+/**
+ * Get core and custom post statuses.
+ *
+ * @return array
+ */
+function get_all_post_statuses() {
+ $all_statuses = wp_list_pluck(
+ get_post_stati(
+ [],
+ 'objects'
+ ),
+ 'label'
+ );
+
+ /*
+ * We unset the following because we want to limit the post
+ * statuses to the ones returned by `get_post_statuses()` and
+ * any custom post statuses registered using `register_post_status()`
+ */
+ unset(
+ $all_statuses['future'],
+ $all_statuses['trash'],
+ $all_statuses['auto-draft'],
+ $all_statuses['inherit'],
+ $all_statuses['request-pending'],
+ $all_statuses['request-confirmed'],
+ $all_statuses['request-failed'],
+ $all_statuses['request-completed']
+ );
+
+ /*
+ * There is a minor difference in the label for 'pending' status between
+ * `get_post_statuses()` and `get_post_stati()`.
+ *
+ * `get_post_stati()` has the label as `Pending` whereas
+ * `get_post_statuses()` has the label as `Pending Review`.
+ *
+ * So we normalise it here.
+ */
+ if ( isset( $all_statuses['pending'] ) ) {
+ $all_statuses['pending'] = esc_html__( 'Pending Review', 'classifai' );
+ }
+
+ /**
+ * Hook to filter post statuses.
+ *
+ * @since 2.2.2
+ * @hook classifai_all_post_statuses
+ *
+ * @param {array} $all_statuses Array of post statuses.
+ *
+ * @return {array} Array of post statuses.
+ */
+ return apply_filters( 'classifai_all_post_statuses', $all_statuses );
+}
diff --git a/includes/Classifai/Providers/Azure/TextToSpeech.php b/includes/Classifai/Providers/Azure/TextToSpeech.php
index e119341ce..8f7aa915e 100644
--- a/includes/Classifai/Providers/Azure/TextToSpeech.php
+++ b/includes/Classifai/Providers/Azure/TextToSpeech.php
@@ -51,6 +51,14 @@ class TextToSpeech extends Provider {
*/
const AUDIO_TIMESTAMP_KEY = '_classifai_post_audio_timestamp';
+ /**
+ * Meta key to get/set the audio hash that helps to indicate if there is any need
+ * for the audio file to be regenerated or not.
+ *
+ * @var string
+ */
+ const AUDIO_HASH_KEY = '_classifai_post_audio_hash';
+
/**
* Azure Text to Speech constructor.
*
@@ -151,7 +159,7 @@ public function setup_fields_sections() {
'label_for' => 'url',
'input_type' => 'text',
'default_value' => $default_settings['credentials']['url'],
- 'description' => __( 'Supported protocol and hostname endpoints, e.g., https://REGION.api.cognitive.microsoft.com
or https://EXAMPLE.cognitiveservices.azure.com
. This can look different based on your setting choices in Azure.', 'classifai' ),
+ 'description' => __( 'Text to Speech region endpoint, e.g., https://LOCATION.tts.speech.microsoft.com/
. Replace REGION
with the region you selected for the resource in Azure.', 'classifai' ),
]
);
@@ -501,6 +509,11 @@ public function render_post_audio_controls( $content ) {
return $content;
}
+ $is_audio_enabled = get_post_meta( $post->ID, self::SYNTHESIZE_SPEECH_KEY, true );
+ if ( 'no' === $is_audio_enabled ) {
+ return $content;
+ }
+
/**
* Filter to disable the rendering of the Text to Speech block.
*
@@ -522,6 +535,12 @@ public function render_post_audio_controls( $content ) {
return $content;
}
+ $audio_attachment_url = wp_get_attachment_url( $audio_attachment_id );
+
+ if ( ! $audio_attachment_url ) {
+ return $content;
+ }
+
wp_enqueue_script(
'classifai-post-audio-player-js',
CLASSIFAI_PLUGIN_URL . 'dist/post-audio-controls.js',
@@ -538,12 +557,11 @@ public function render_post_audio_controls( $content ) {
'all'
);
- $audio_timestamp = (int) get_post_meta( $post->ID, self::AUDIO_TIMESTAMP_KEY, true );
- $audio_attachment_url = sprintf(
- '%1$s?ver=%2$s',
- wp_get_attachment_url( $audio_attachment_id ),
- filter_var( $audio_timestamp, FILTER_SANITIZE_NUMBER_INT )
- );
+ $audio_timestamp = (int) get_post_meta( $post->ID, self::AUDIO_TIMESTAMP_KEY, true );
+
+ if ( $audio_timestamp ) {
+ $audio_attachment_url = add_query_arg( 'ver', filter_var( $audio_timestamp, FILTER_SANITIZE_NUMBER_INT ), $audio_attachment_url );
+ }
ob_start();
diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php
index 835ab4f4d..3e22488f4 100644
--- a/includes/Classifai/Providers/OpenAI/ChatGPT.php
+++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php
@@ -69,6 +69,27 @@ 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, 'register_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' ),
+ 'selectBtnText' => __( 'Select', 'classifai' ),
+ ],
+ ],
+ 'noPermissions' => ! is_user_logged_in() || ! current_user_can( 'edit_post', $post->ID ),
+ ];
}
/**
@@ -117,19 +138,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 +155,43 @@ public function enqueue_admin_assets( $hook_suffix = '' ) {
return;
}
+ $screen = get_current_screen();
+ $settings = $this->get_settings();
+ $user_roles = wp_get_current_user()->roles ?? [];
+ $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
+ );
+
+ 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 +201,23 @@ public function enqueue_admin_assets( $hook_suffix = '' ) {
);
}
+ /**
+ * HTML template for title generation result popup.
+ */
+ public function register_generated_titles_template() {
+ ?>
+