Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/676: ensure all WP_CLI commands work #682

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions includes/Classifai/Command/ClassifaiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use Classifai\Features\AudioTranscriptsGeneration;
use Classifai\Features\Classification;
use Classifai\Features\DescriptiveTextGenerator;
use Classifai\Features\ExcerptGeneration;
use Classifai\Features\ImageCropping;
use Classifai\Features\TextToSpeech;
use Classifai\Providers\Watson\APIRequest;
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;
Expand Down Expand Up @@ -788,8 +787,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.' );
Expand All @@ -804,16 +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 );
$classifier->generate_image_alt_tags( $current_meta, $attachment_id );
$classifier->smart_crop_image( $current_meta, $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();
Expand Down Expand Up @@ -883,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;
}
}

Expand Down
4 changes: 4 additions & 0 deletions includes/Classifai/Features/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkotter can you verify this. I've added it so that a feature can run via WP_CLI.
cc: @iamdharmesh

}

/**
* Filter to override user access to a ClassifAI feature.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/OpenAI/Whisper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

Expand Down
Loading