Skip to content

Commit

Permalink
Merge pull request #682 from 10up/fix/676
Browse files Browse the repository at this point in the history
fix/676: ensure all WP_CLI commands work
  • Loading branch information
dkotter authored Feb 5, 2024
2 parents ed7f9ac + 85bcd9d commit 3b7262a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
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 @@ -901,6 +901,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.
*
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 @@ -175,7 +175,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

0 comments on commit 3b7262a

Please sign in to comment.