Skip to content

Commit 620c2b7

Browse files
committed
Render the classification previewer separately for NLU and Embeddings instead of relying on a single method, as they have different things to output
1 parent ecc05bc commit 620c2b7

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
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
?>

0 commit comments

Comments
 (0)