Skip to content

Commit 0180001

Browse files
authored
Merge pull request #679 from 10up/fix/663
Ensure Classification preview works as expected
2 parents 34b65a3 + 620c2b7 commit 0180001

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

includes/Classifai/Providers/OpenAI/Embeddings.php

+54
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use WP_Error;
1515

1616
use function Classifai\get_asset_info;
17+
use function Classifai\Providers\Watson\get_supported_post_statuses;
18+
use function Classifai\Providers\Watson\get_supported_post_types;
1719

1820
class Embeddings extends Provider {
1921

@@ -127,6 +129,58 @@ public function render_provider_fields() {
127129
);
128130

129131
do_action( 'classifai_' . static::ID . '_render_provider_fields', $this );
132+
add_action( 'classifai_after_feature_settings_form', [ $this, 'render_previewer' ] );
133+
}
134+
135+
/**
136+
* Renders the previewer window for the feature.
137+
*
138+
* @param string $active_feature The active feature.
139+
*/
140+
public function render_previewer( string $active_feature ) {
141+
$feature = new Classification();
142+
$provider = $feature->get_feature_provider_instance();
143+
144+
if (
145+
self::ID !== $provider::ID ||
146+
$feature::ID !== $active_feature ||
147+
! $feature->is_feature_enabled()
148+
) {
149+
return;
150+
}
151+
?>
152+
153+
<div id="classifai-post-preview-app">
154+
<?php
155+
$supported_post_statuses = get_supported_post_statuses();
156+
$supported_post_types = get_supported_post_types();
157+
158+
$posts_to_preview = get_posts(
159+
array(
160+
'post_type' => $supported_post_types,
161+
'post_status' => $supported_post_statuses,
162+
'posts_per_page' => 10,
163+
)
164+
);
165+
?>
166+
167+
<h2><?php esc_html_e( 'Preview Language Processing', 'classifai' ); ?></h2>
168+
<div id="classifai-post-preview-controls">
169+
<select id="classifai-preview-post-selector">
170+
<?php foreach ( $posts_to_preview as $post ) : ?>
171+
<option value="<?php echo esc_attr( $post->ID ); ?>"><?php echo esc_html( $post->post_title ); ?></option>
172+
<?php endforeach; ?>
173+
</select>
174+
<?php wp_nonce_field( 'classifai-previewer-action', 'classifai-previewer-nonce' ); ?>
175+
<button type="button" class="button" id="get-classifier-preview-data-btn">
176+
<span><?php esc_html_e( 'Preview', 'classifai' ); ?></span>
177+
</button>
178+
</div>
179+
<div id="classifai-post-preview-wrapper">
180+
</div>
181+
</div>
182+
183+
<?php
130184
}
131185

132186
/**

includes/Classifai/Providers/Watson/Helpers.php

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
use Classifai\Features\Classification;
99

10-
use function Classifai\get_plugin_settings;
11-
1210
/**
1311
* Returns the currently configured Watson API URL. Lookup order is,
1412
*

includes/Classifai/Providers/Watson/NLU.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ function ( $args = [] ) {
224224
);
225225
}
226226

227+
do_action( 'classifai_' . static::ID . '_render_provider_fields', $this );
227228
add_action( 'classifai_after_feature_settings_form', [ $this, 'render_previewer' ] );
228229
}
229230

@@ -233,9 +234,14 @@ function ( $args = [] ) {
233234
* @param string $active_feature The active feature.
234235
*/
235236
public function render_previewer( string $active_feature ) {
236-
$feature = new Classification();
237+
$feature = new Classification();
238+
$provider = $feature->get_feature_provider_instance();
237239

238-
if ( $feature::ID !== $active_feature || ! $feature->is_feature_enabled() ) {
240+
if (
241+
self::ID !== $provider::ID ||
242+
$feature::ID !== $active_feature ||
243+
! $feature->is_feature_enabled()
244+
) {
239245
return;
240246
}
241247
?>

src/js/language-processing.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import '../scss/language-processing.scss';
1010
return;
1111
}
1212

13+
const providerSelect = document.getElementById( 'provider' );
14+
const provider = providerSelect.value;
15+
1316
const previewWatson = () => {
14-
if ( ! nonceEl ) {
17+
if ( 'ibm_watson_nlu' !== provider ) {
1518
return;
1619
}
1720

@@ -199,7 +202,7 @@ import '../scss/language-processing.scss';
199202
previewWatson();
200203

201204
const previewEmbeddings = () => {
202-
if ( ! nonceEl ) {
205+
if ( 'openai_embeddings' !== provider ) {
203206
return;
204207
}
205208

@@ -341,6 +344,7 @@ import '../scss/language-processing.scss';
341344
);
342345
const selectElChoices = new Choices( selectEl, {
343346
noResultsText: '',
347+
allowHTML: true,
344348
} );
345349

346350
/**
@@ -349,10 +353,6 @@ import '../scss/language-processing.scss';
349353
* @param {Object} event Choices.js's 'search' event object.
350354
*/
351355
function searchPosts( event ) {
352-
if ( ! nonceEl ) {
353-
return;
354-
}
355-
356356
/** Previewer nonce. */
357357
const previewerNonce = nonceEl.value;
358358

0 commit comments

Comments
 (0)