Skip to content

Commit 19919fd

Browse files
committed
Ensure saving in the block editor triggers classification for both providers
1 parent 6ac32ee commit 19919fd

File tree

6 files changed

+96
-287
lines changed

6 files changed

+96
-287
lines changed

includes/Classifai/Features/Classification.php

+63-47
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,24 @@ public function setup() {
5858
* Set up necessary hooks.
5959
*/
6060
public function feature_setup() {
61+
$post_types = $this->get_supported_post_types();
62+
if ( ! empty( $post_types ) ) {
63+
foreach ( $post_types as $post_type ) {
64+
add_action( 'rest_after_insert_' . $post_type, [ $this, 'rest_after_insert' ] );
65+
}
66+
}
67+
6168
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
6269
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
6370
add_action( 'classifai_after_feature_settings_form', [ $this, 'render_previewer' ] );
6471
add_action( 'rest_api_init', [ $this, 'add_process_content_meta_to_rest_api' ] );
72+
add_filter( 'default_post_metadata', [ $this, 'default_post_metadata' ], 10, 3 );
73+
74+
// Support the Classic Editor.
6575
add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ], 10, 2 );
66-
add_action( 'save_post', [ $this, 'save_metabox' ] );
76+
add_action( 'save_post', [ $this, 'save_meta_box' ] );
6777
add_action( 'admin_post_classifai_classify_post', array( $this, 'classifai_classify_post' ) );
6878
add_action( 'admin_notices', [ $this, 'show_error_if' ] );
69-
70-
add_filter( 'default_post_metadata', [ $this, 'default_post_metadata' ], 10, 3 );
7179
add_filter( 'removable_query_args', [ $this, 'removable_query_args' ] );
7280
}
7381

@@ -187,6 +195,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
187195
]
188196
);
189197

198+
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
199+
$this->save( $request->get_param( 'id' ), $results );
200+
}
201+
190202
return rest_ensure_response(
191203
[
192204
'terms' => $results,
@@ -217,6 +229,47 @@ public function save( int $post_id, array $results ) {
217229
}
218230
}
219231

232+
/**
233+
* Run classification after an item has been inserted via REST.
234+
*
235+
* @param \WP_Post $post Post object.
236+
*/
237+
public function rest_after_insert( \WP_Post $post ) {
238+
$supported_post_types = $this->get_supported_post_types();
239+
$post_statuses = $this->get_supported_post_statuses();
240+
241+
// Ensure the post type and status is allowed.
242+
if (
243+
! in_array( $post->post_type, $supported_post_types, true ) ||
244+
! in_array( $post->post_status, $post_statuses, true )
245+
) {
246+
return;
247+
}
248+
249+
// Check if processing on save is disabled.
250+
if ( 'no' === get_post_meta( $post->ID, '_classifai_process_content', true ) ) {
251+
return;
252+
}
253+
254+
$results = $this->run( $post->ID, 'classify' );
255+
256+
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
257+
$this->save( $post->ID, $results );
258+
delete_post_meta( $post->ID, '_classifai_error' );
259+
} elseif ( is_wp_error( $results ) ) {
260+
update_post_meta(
261+
$post->ID,
262+
'_classifai_error',
263+
wp_json_encode(
264+
[
265+
'code' => $results->get_error_code(),
266+
'message' => $results->get_error_message(),
267+
]
268+
)
269+
);
270+
}
271+
}
272+
220273
/**
221274
* Enqueue the admin scripts.
222275
*/
@@ -264,8 +317,6 @@ public function enqueue_editor_assets() {
264317
true
265318
);
266319

267-
// TODO: check JS var classifaiEmbeddingData
268-
269320
wp_add_inline_script(
270321
'classifai-gutenberg-plugin',
271322
sprintf(
@@ -367,7 +418,7 @@ public function render_meta_box( \WP_Post $post ) {
367418
*
368419
* @param int $post_id Current post ID.
369420
*/
370-
public function save_metabox( int $post_id ) {
421+
public function save_meta_box( int $post_id ) {
371422
if (
372423
wp_is_post_autosave( $post_id ) ||
373424
wp_is_post_revision( $post_id ) ||
@@ -392,7 +443,10 @@ public function save_metabox( int $post_id ) {
392443

393444
$results = $this->run( $post_id, 'classify' );
394445

395-
if ( is_wp_error( $results ) ) {
446+
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
447+
$this->save( $post_id, $results );
448+
delete_post_meta( $post_id, '_classifai_error' );
449+
} elseif ( is_wp_error( $results ) ) {
396450
update_post_meta(
397451
$post_id,
398452
'_classifai_error',
@@ -403,9 +457,6 @@ public function save_metabox( int $post_id ) {
403457
]
404458
)
405459
);
406-
} else {
407-
$this->save( $post_id, $results );
408-
delete_post_meta( $post_id, '_classifai_error' );
409460
}
410461
}
411462
}
@@ -446,11 +497,11 @@ public function classifai_classify_post() {
446497

447498
$classified = array();
448499

449-
if ( ! is_wp_error( $results ) ) {
500+
if ( ! empty( $results ) && ! is_wp_error( $results ) ) {
450501
$this->save( $post_id, $results );
451502
$classified = array( 'classifai_classify' => 1 );
452503
delete_post_meta( $post_id, '_classifai_error' );
453-
} else {
504+
} elseif ( is_wp_error( $results ) ) {
454505
update_post_meta(
455506
$post_id,
456507
'_classifai_error',
@@ -780,41 +831,6 @@ public function sanitize_default_feature_settings( array $new_settings ): array
780831
return $new_settings;
781832
}
782833

783-
/**
784-
* Runs the feature.
785-
*
786-
* @param mixed ...$args Arguments required by the feature depending on the provider selected.
787-
* @return mixed
788-
*/
789-
public function runs( ...$args ) {
790-
$settings = $this->get_settings();
791-
$provider_id = $settings['provider'] ?? NLU::ID;
792-
$provider_instance = $this->get_feature_provider_instance( $provider_id );
793-
$result = '';
794-
795-
if ( NLU::ID === $provider_instance::ID ) {
796-
/** @var NLU $provider_instance */
797-
$result = call_user_func_array(
798-
[ $provider_instance, 'classify' ],
799-
[ ...$args ]
800-
);
801-
} elseif ( Embeddings::ID === $provider_instance::ID ) {
802-
/** @var Embeddings $provider_instance */
803-
$result = call_user_func_array(
804-
[ $provider_instance, 'generate_embeddings_for_post' ],
805-
[ ...$args ]
806-
);
807-
}
808-
809-
return apply_filters(
810-
'classifai_' . static::ID . '_run',
811-
$result,
812-
$provider_instance,
813-
$args,
814-
$this
815-
);
816-
}
817-
818834
/**
819835
* Get all feature taxonomies.
820836
*

includes/Classifai/Features/Feature.php

+1
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ public function rest_endpoint_callback( WP_REST_Request $request ) { // phpcs:ig
12441244
* @return mixed
12451245
*/
12461246
public function run( ...$args ) {
1247+
error_log( 'running' ); // TODO: remove
12471248
$settings = $this->get_settings();
12481249
$provider_id = $settings['provider'];
12491250
$provider_instance = $this->get_feature_provider_instance( $provider_id );

includes/Classifai/Providers/OpenAI/Embeddings.php

+19
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ public function generate_embeddings_for_post( int $post_id ) {
268268
return new WP_Error( 'invalid', esc_html__( 'Classification is disabled for this item.', 'classifai' ) );
269269
}
270270

271+
/**
272+
* Filter whether ClassifAI should classify a post.
273+
*
274+
* Default is true, return false to skip classifying a post.
275+
*
276+
* @since 1.2.0
277+
* @hook classifai_should_classify_post
278+
*
279+
* @param {bool} $should_classify Whether the post should be classified. Default `true`, return `false` to skip
280+
* classification for this post.
281+
* @param {int} $post_id The ID of the post to be considered for classification.
282+
*
283+
* @return {bool} Whether the post should be classified.
284+
*/
285+
$should_classify = apply_filters( 'classifai_should_classify_post', true, $post_id );
286+
if ( ! $should_classify ) {
287+
return new WP_Error( 'invalid', esc_html__( 'Classification is disabled for this item.', 'classifai' ) );
288+
}
289+
271290
// TODO: $embeddings = $this->generate_embeddings( $post_id, 'post' );
272291
$embeddings = get_post_meta( $post_id, 'classifai_openai_embeddings', true );
273292

includes/Classifai/Providers/Watson/NLU.php

+2-48
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class NLU extends Provider {
2121
*/
2222
public $taxonomy_factory;
2323

24-
/**
25-
* @var $save_post_handler SavePostHandler Triggers a classification with Watson
26-
*/
27-
public $save_post_handler;
28-
2924
/**
3025
* NLU features that are supported by this provider
3126
*
@@ -233,9 +228,6 @@ public function register() {
233228
$this->taxonomy_factory = new TaxonomyFactory();
234229
$this->taxonomy_factory->build_all();
235230

236-
// $this->save_post_handler = new SavePostHandler();
237-
// $this->save_post_handler->register();
238-
239231
// new PreviewClassifierData();
240232
}
241233
}
@@ -357,48 +349,10 @@ public function rest_endpoint_callback( $post_id = 0, string $route_to_call = ''
357349
return $return;
358350
}
359351

360-
/**
361-
* Handle request to generate tags for given post ID.
362-
*
363-
* @param int $post_id The Post Id we're processing.
364-
* @return mixed
365-
*/
366-
public function classify_post( int $post_id ) {
367-
try {
368-
if ( empty( $post_id ) ) {
369-
return new WP_Error( 'post_id_required', esc_html__( 'Post ID is required to classify post.', 'classifai' ) );
370-
}
371-
372-
$taxonomy_terms = [];
373-
$features = [ 'category', 'keyword', 'concept', 'entity' ];
374-
375-
// Process post content.
376-
$result = $this->classify( $post_id );
377-
378-
if ( is_wp_error( $result ) ) {
379-
return $result;
380-
}
381-
382-
foreach ( $features as $feature ) {
383-
$taxonomy = get_feature_taxonomy( $feature );
384-
$terms = wp_get_object_terms( $post_id, $taxonomy );
385-
if ( ! is_wp_error( $terms ) ) {
386-
foreach ( $terms as $term ) {
387-
$taxonomy_terms[ $taxonomy ][] = $term->term_id;
388-
}
389-
}
390-
}
391-
392-
// Return taxonomy terms.
393-
return rest_ensure_response( [ 'terms' => $taxonomy_terms ] );
394-
} catch ( \Exception $e ) {
395-
return new WP_Error( 'request_failed', $e->getMessage() );
396-
}
397-
}
398-
399352
/**
400353
* Classifies the post specified with the PostClassifier object.
401-
* Existing terms relationships are removed before classification.
354+
*
355+
* Existing terms relationships are removed during classification.
402356
*
403357
* @param int $post_id the post to classify & link
404358
* @return array|WP_Error

0 commit comments

Comments
 (0)