From ecc05bcc4df54173cc31d86d9ac4352d27653bb4 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Thu, 1 Feb 2024 14:59:18 -0700 Subject: [PATCH 1/2] Ensure we don't run both NLU and Embeddings functionality when doing a preview, which leads to JS errors. Remove code that isn't needed. Fix a deprecation warning --- src/js/language-processing.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/js/language-processing.js b/src/js/language-processing.js index d5a18b393..5be79de71 100644 --- a/src/js/language-processing.js +++ b/src/js/language-processing.js @@ -10,8 +10,11 @@ import '../scss/language-processing.scss'; return; } + const providerSelect = document.getElementById( 'provider' ); + const provider = providerSelect.value; + const previewWatson = () => { - if ( ! nonceEl ) { + if ( 'ibm_watson_nlu' !== provider ) { return; } @@ -199,7 +202,7 @@ import '../scss/language-processing.scss'; previewWatson(); const previewEmbeddings = () => { - if ( ! nonceEl ) { + if ( 'openai_embeddings' !== provider ) { return; } @@ -341,6 +344,7 @@ import '../scss/language-processing.scss'; ); const selectElChoices = new Choices( selectEl, { noResultsText: '', + allowHTML: true, } ); /** @@ -349,10 +353,6 @@ import '../scss/language-processing.scss'; * @param {Object} event Choices.js's 'search' event object. */ function searchPosts( event ) { - if ( ! nonceEl ) { - return; - } - /** Previewer nonce. */ const previewerNonce = nonceEl.value; From 620c2b78a17bd7715fd841dea8f51b3029d70f5d Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Thu, 1 Feb 2024 15:35:37 -0700 Subject: [PATCH 2/2] Render the classification previewer separately for NLU and Embeddings instead of relying on a single method, as they have different things to output --- .../Classifai/Providers/OpenAI/Embeddings.php | 54 +++++++++++++++++++ .../Classifai/Providers/Watson/Helpers.php | 2 - includes/Classifai/Providers/Watson/NLU.php | 10 +++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Providers/OpenAI/Embeddings.php b/includes/Classifai/Providers/OpenAI/Embeddings.php index 13ebdbea6..29e563da2 100644 --- a/includes/Classifai/Providers/OpenAI/Embeddings.php +++ b/includes/Classifai/Providers/OpenAI/Embeddings.php @@ -14,6 +14,8 @@ use WP_Error; use function Classifai\get_asset_info; +use function Classifai\Providers\Watson\get_supported_post_statuses; +use function Classifai\Providers\Watson\get_supported_post_types; class Embeddings extends Provider { @@ -127,6 +129,58 @@ public function render_provider_fields() { ); do_action( 'classifai_' . static::ID . '_render_provider_fields', $this ); + add_action( 'classifai_after_feature_settings_form', [ $this, 'render_previewer' ] ); + } + + /** + * Renders the previewer window for the feature. + * + * @param string $active_feature The active feature. + */ + public function render_previewer( string $active_feature ) { + $feature = new Classification(); + $provider = $feature->get_feature_provider_instance(); + + if ( + self::ID !== $provider::ID || + $feature::ID !== $active_feature || + ! $feature->is_feature_enabled() + ) { + return; + } + ?> + +
+ $supported_post_types, + 'post_status' => $supported_post_statuses, + 'posts_per_page' => 10, + ) + ); + ?> + +

+
+ + + +
+
+
+
+ + get_feature_provider_instance(); - if ( $feature::ID !== $active_feature || ! $feature->is_feature_enabled() ) { + if ( + self::ID !== $provider::ID || + $feature::ID !== $active_feature || + ! $feature->is_feature_enabled() + ) { return; } ?>