Skip to content

Commit 5425c45

Browse files
committed
Ensure that manually classifying works in the Block Editor for Watson
1 parent 19919fd commit 5425c45

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

includes/Classifai/Features/Classification.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
195195
]
196196
);
197197

198+
// Save results or return the results that need saved.
198199
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
199-
$this->save( $request->get_param( 'id' ), $results );
200+
$results = $this->save( $request->get_param( 'id' ), $results, $request->get_param( 'linkTerms' ) ?? true );
200201
}
201202

202203
return rest_ensure_response(
@@ -214,19 +215,23 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
214215
* Save the classification results.
215216
*
216217
* @param int $post_id The post ID.
217-
* @param array $results Term results.
218+
* @param array $results Term results
219+
* @param bool $link Whether to link the terms or not.
220+
* @return array|WP_Error
218221
*/
219-
public function save( int $post_id, array $results ) {
222+
public function save( int $post_id, array $results, bool $link = true ) {
220223
$provider_instance = $this->get_feature_provider_instance();
221224

222225
switch ( $provider_instance::ID ) {
223226
case NLU::ID:
224-
$results = $provider_instance->link( $post_id, $results );
227+
$results = $provider_instance->link( $post_id, $results, $link );
225228
break;
226229
case Embeddings::ID:
227230
$results = $provider_instance->set_terms( $post_id, $results );
228231
break;
229232
}
233+
234+
return $results;
230235
}
231236

232237
/**
@@ -844,7 +849,7 @@ public function get_all_feature_taxonomies(): array {
844849
return $feature_taxonomies;
845850
}
846851

847-
foreach ( array_keys( $this->get_supported_taxonomies() ) as $feature_name ) {
852+
foreach ( array_keys( $provider_instance->nlu_features ) as $feature_name ) {
848853
if ( ! get_classification_feature_enabled( $feature_name ) ) {
849854
continue;
850855
}

includes/Classifai/Providers/Watson/NLU.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -379,26 +379,29 @@ public function classify( int $post_id ) {
379379

380380
$classifier = new PostClassifier();
381381

382-
$output = $classifier->classify( $post_id );
382+
return get_post_meta( $post_id, 'classifai_watson_nlu_results', true ); // TODO
383+
// $output = $classifier->classify( $post_id );
383384

384-
return $output;
385+
// update_post_meta( $post_id, 'classifai_watson_nlu_results', $output );
386+
// return $output;
385387
}
386388

387389
/**
388390
* Links the Watson NLU response output to taxonomy terms.
389391
*
390392
* @param int $post_id The post ID.
391393
* @param array $terms The classification results from Watson NLU.
394+
* @param bool $link Whether to link the terms or not.
392395
* @return array|WP_Error
393396
*/
394-
public function link( int $post_id, array $terms ) {
397+
public function link( int $post_id, array $terms, bool $link = true ) {
395398
if ( empty( $terms ) ) {
396399
return new WP_Error( 'invalid', esc_html__( 'No terms to link.', 'classifai' ) );
397400
}
398401

399402
$classifier = new PostClassifier();
400403

401-
$output = $classifier->link( $post_id, $terms );
404+
$output = $classifier->link( $post_id, $terms, [], $link );
402405

403406
return $output;
404407
}

src/js/gutenberg-plugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ const ClassificationButton = () => {
288288
<div id="classify-post-component">
289289
{ isOpen && (
290290
<Modal
291-
title={ __( 'Confirm Post Classification', 'classifai' ) }
291+
title={ __( 'Confirm Classification', 'classifai' ) }
292292
onRequestClose={ closeModal }
293293
isFullScreen={ false }
294294
className="classify-modal"

0 commit comments

Comments
 (0)