@@ -58,16 +58,24 @@ public function setup() {
58
58
* Set up necessary hooks.
59
59
*/
60
60
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
+
61
68
add_action ( 'admin_enqueue_scripts ' , [ $ this , 'enqueue_admin_assets ' ] );
62
69
add_action ( 'enqueue_block_editor_assets ' , [ $ this , 'enqueue_editor_assets ' ] );
63
70
add_action ( 'classifai_after_feature_settings_form ' , [ $ this , 'render_previewer ' ] );
64
71
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.
65
75
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 ' ] );
67
77
add_action ( 'admin_post_classifai_classify_post ' , array ( $ this , 'classifai_classify_post ' ) );
68
78
add_action ( 'admin_notices ' , [ $ this , 'show_error_if ' ] );
69
-
70
- add_filter ( 'default_post_metadata ' , [ $ this , 'default_post_metadata ' ], 10 , 3 );
71
79
add_filter ( 'removable_query_args ' , [ $ this , 'removable_query_args ' ] );
72
80
}
73
81
@@ -187,6 +195,10 @@ public function rest_endpoint_callback( WP_REST_Request $request ) {
187
195
]
188
196
);
189
197
198
+ if ( ! empty ( $ results ) && ! is_wp_error ( $ results ) ) {
199
+ $ this ->save ( $ request ->get_param ( 'id ' ), $ results );
200
+ }
201
+
190
202
return rest_ensure_response (
191
203
[
192
204
'terms ' => $ results ,
@@ -217,6 +229,47 @@ public function save( int $post_id, array $results ) {
217
229
}
218
230
}
219
231
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
+
220
273
/**
221
274
* Enqueue the admin scripts.
222
275
*/
@@ -264,8 +317,6 @@ public function enqueue_editor_assets() {
264
317
true
265
318
);
266
319
267
- // TODO: check JS var classifaiEmbeddingData
268
-
269
320
wp_add_inline_script (
270
321
'classifai-gutenberg-plugin ' ,
271
322
sprintf (
@@ -367,7 +418,7 @@ public function render_meta_box( \WP_Post $post ) {
367
418
*
368
419
* @param int $post_id Current post ID.
369
420
*/
370
- public function save_metabox ( int $ post_id ) {
421
+ public function save_meta_box ( int $ post_id ) {
371
422
if (
372
423
wp_is_post_autosave ( $ post_id ) ||
373
424
wp_is_post_revision ( $ post_id ) ||
@@ -392,7 +443,10 @@ public function save_metabox( int $post_id ) {
392
443
393
444
$ results = $ this ->run ( $ post_id , 'classify ' );
394
445
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 ) ) {
396
450
update_post_meta (
397
451
$ post_id ,
398
452
'_classifai_error ' ,
@@ -403,9 +457,6 @@ public function save_metabox( int $post_id ) {
403
457
]
404
458
)
405
459
);
406
- } else {
407
- $ this ->save ( $ post_id , $ results );
408
- delete_post_meta ( $ post_id , '_classifai_error ' );
409
460
}
410
461
}
411
462
}
@@ -446,11 +497,11 @@ public function classifai_classify_post() {
446
497
447
498
$ classified = array ();
448
499
449
- if ( ! is_wp_error ( $ results ) ) {
500
+ if ( ! empty ( $ results ) && ! is_wp_error ( $ results ) ) {
450
501
$ this ->save ( $ post_id , $ results );
451
502
$ classified = array ( 'classifai_classify ' => 1 );
452
503
delete_post_meta ( $ post_id , '_classifai_error ' );
453
- } else {
504
+ } elseif ( is_wp_error ( $ results ) ) {
454
505
update_post_meta (
455
506
$ post_id ,
456
507
'_classifai_error ' ,
@@ -780,41 +831,6 @@ public function sanitize_default_feature_settings( array $new_settings ): array
780
831
return $ new_settings ;
781
832
}
782
833
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
-
818
834
/**
819
835
* Get all feature taxonomies.
820
836
*
0 commit comments