From 33ef6a2c9071dc5218c9def8d6de34c494e52669 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 2 Feb 2024 17:58:44 +0530 Subject: [PATCH 1/2] fix WP_CLI Commands --- includes/Classifai/Command/ClassifaiCommand.php | 13 +++++++++---- includes/Classifai/Features/Feature.php | 4 ++++ includes/Classifai/Providers/OpenAI/Whisper.php | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index 487bbf302..c393fa746 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -4,6 +4,7 @@ use Classifai\Features\AudioTranscriptsGeneration; use Classifai\Features\Classification; +use Classifai\Features\DescriptiveTextGenerator; use Classifai\Features\ExcerptGeneration; use Classifai\Features\ImageCropping; use Classifai\Features\TextToSpeech; @@ -788,8 +789,7 @@ public function image( $args = [], $opts = [] ) { $attachment_ids = $this->get_attachment_to_classify( $opts ); } - $total = count( $attachment_ids ); - $classifier = new ComputerVision( false ); + $total = count( $attachment_ids ); if ( empty( $total ) ) { return \WP_CLI::log( 'No images to classify.' ); @@ -812,8 +812,13 @@ public function image( $args = [], $opts = [] ) { $current_meta = wp_get_attachment_metadata( $attachment_id ); \WP_CLI::line( 'Processing ' . $attachment_id ); - $classifier->generate_image_alt_tags( $current_meta, $attachment_id ); - $classifier->smart_crop_image( $current_meta, $attachment_id ); + $feature = new DescriptiveTextGenerator(); + $result = $feature->run( $attachment_id, 'descriptive_text' ); + $feature->save( $result, $attachment_id ); + + $feature = new ImageCropping(); + $result = $feature->run( $attachment_id, 'crop' ); + $feature->save( $result, $attachment_id ); } $progress_bar->finish(); diff --git a/includes/Classifai/Features/Feature.php b/includes/Classifai/Features/Feature.php index 7f11af94e..ed9b0d6dd 100644 --- a/includes/Classifai/Features/Feature.php +++ b/includes/Classifai/Features/Feature.php @@ -956,6 +956,10 @@ public function has_access(): bool { $access = ( ! in_array( static::ID, $opted_out_features, true ) ); } + if ( defined( 'WP_CLI' ) && WP_CLI ) { + $access = true; + } + /** * Filter to override user access to a ClassifAI feature. * diff --git a/includes/Classifai/Providers/OpenAI/Whisper.php b/includes/Classifai/Providers/OpenAI/Whisper.php index 3e513bbba..5d644c63d 100644 --- a/includes/Classifai/Providers/OpenAI/Whisper.php +++ b/includes/Classifai/Providers/OpenAI/Whisper.php @@ -187,7 +187,7 @@ public function rest_endpoint_callback( $post_id = 0, string $route_to_call = '' * @return WP_Error|bool */ public function transcribe_audio( int $attachment_id = 0, array $args = [] ) { - if ( $attachment_id && ! current_user_can( 'edit_post', $attachment_id ) ) { + if ( $attachment_id && ! current_user_can( 'edit_post', $attachment_id ) && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) { return new \WP_Error( 'no_permission', esc_html__( 'User does not have permission to edit this attachment.', 'classifai' ) ); } From 85bcd9df3b5d4a2ddb881ae5e2062b6f877e22ed Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Mon, 5 Feb 2024 11:45:20 -0700 Subject: [PATCH 2/2] Fix the smart cropping and descriptive text CLI commands --- .../Classifai/Command/ClassifaiCommand.php | 49 +++++++------------ 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/includes/Classifai/Command/ClassifaiCommand.php b/includes/Classifai/Command/ClassifaiCommand.php index c393fa746..20ec489f6 100644 --- a/includes/Classifai/Command/ClassifaiCommand.php +++ b/includes/Classifai/Command/ClassifaiCommand.php @@ -12,8 +12,6 @@ use Classifai\Providers\Watson\Classifier; use Classifai\Normalizer; use Classifai\Providers\Watson\PostClassifier; -use Classifai\Providers\Azure\ComputerVision; -use Classifai\Providers\Azure\SmartCropping; use Classifai\Providers\OpenAI\Embeddings; use function Classifai\Providers\Watson\get_username; @@ -804,21 +802,26 @@ public function image( $args = [], $opts = [] ) { $message = "Classifying $limit_total images ..."; $progress_bar = \WP_CLI\Utils\make_progress_bar( $message, $limit_total ); + $text_feature = new DescriptiveTextGenerator(); + $crop_feature = new ImageCropping(); for ( $index = 0; $index < $limit_total; $index++ ) { $attachment_id = $attachment_ids[ $index ]; $progress_bar->tick(); - $current_meta = wp_get_attachment_metadata( $attachment_id ); \WP_CLI::line( 'Processing ' . $attachment_id ); - $feature = new DescriptiveTextGenerator(); - $result = $feature->run( $attachment_id, 'descriptive_text' ); - $feature->save( $result, $attachment_id ); - $feature = new ImageCropping(); - $result = $feature->run( $attachment_id, 'crop' ); - $feature->save( $result, $attachment_id ); + $text_result = $text_feature->run( $attachment_id, 'descriptive_text' ); + if ( $text_result && ! is_wp_error( $text_result ) ) { + $text_feature->save( $text_result, $attachment_id ); + } + + $crop_result = $crop_feature->run( $attachment_id, 'crop' ); + if ( ! empty( $crop_result ) && ! is_wp_error( $crop_result ) ) { + $meta = $crop_feature->save( $crop_result, $attachment_id ); + wp_update_attachment_metadata( $attachment_id, $meta ); + } } $progress_bar->finish(); @@ -888,28 +891,12 @@ public function crop( $args = [], $opts = [] ) { $progress_bar->tick(); - $current_meta = wp_get_attachment_metadata( $attachment_id ); - - foreach ( $current_meta['sizes'] as $size => $size_data ) { - switch ( $provider_class::ID ) { - case ComputerVision::ID: - $smart_cropping = new SmartCropping( $settings ); - - if ( ! $smart_cropping->should_crop( $size ) ) { - break; - } - - $data = [ - 'width' => $size_data['width'], - 'height' => $size_data['height'], - ]; - - $smart_thumbnail = $smart_cropping->get_cropped_thumbnail( $attachment_id, $data ); - if ( is_wp_error( $smart_thumbnail ) ) { - $errors[ $attachment_id . ':' . $size_data['width'] . 'x' . $size_data['height'] ] = $smart_thumbnail; - } - break; - } + $crop_result = $image_cropping->run( $attachment_id, 'crop' ); + if ( ! empty( $crop_result ) && ! is_wp_error( $crop_result ) ) { + $meta = $image_cropping->save( $crop_result, $attachment_id ); + wp_update_attachment_metadata( $attachment_id, $meta ); + } else { + $errors[ $attachment_id ] = $crop_result; } }