Skip to content

Commit

Permalink
Merge pull request #677 from 10up/fix/623
Browse files Browse the repository at this point in the history
Ensure we don't run updates more than once
  • Loading branch information
dkotter authored Feb 1, 2024
2 parents 36aac2d + e6073d5 commit 517af08
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions includes/Classifai/Features/DescriptiveTextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ public function maybe_rescan_image( int $attachment_id ) {
$result = $this->run( $attachment_id, 'descriptive_text' );

if ( $result && ! is_wp_error( $result ) ) {
// Ensure we don't re-run this when the attachment is updated.
remove_action( 'edit_attachment', [ $this, 'maybe_rescan_image' ] );
$this->save( $result, $attachment_id );
}
}
Expand Down
4 changes: 1 addition & 3 deletions includes/Classifai/Features/ImageCropping.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,8 @@ public function attachment_data_meta_box_content() {
* @param int $attachment_id Attachment ID.
*/
public function maybe_crop_image( int $attachment_id ) {
$metadata = wp_get_attachment_metadata( $attachment_id );

if ( clean_input( 'rescan-smart-crop' ) ) {
$result = $this->run( $attachment_id, 'crop', $metadata );
$result = $this->run( $attachment_id, 'crop' );

if ( ! empty( $result ) && ! is_wp_error( $result ) ) {
$meta = $this->save( $result, $attachment_id );
Expand Down
2 changes: 2 additions & 0 deletions includes/Classifai/Features/ImageTextExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ public function maybe_rescan_image( int $attachment_id ) {
$result = $this->run( $attachment_id, 'ocr' );

if ( $result && ! is_wp_error( $result ) ) {
// Ensure we don't re-run this when the attachment is updated.
remove_action( 'edit_attachment', [ $this, 'maybe_rescan_image' ] );
$this->save( $result, $attachment_id );
}
}
Expand Down
3 changes: 3 additions & 0 deletions includes/Classifai/Features/PDFTextExtraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ public function maybe_rescan_pdf( int $attachment_id ) {
* @param int $attachment_id The attachment ID.
*/
public function save( string $result, int $attachment_id ) {
// Ensure we don't re-run this when the attachment is updated.
remove_action( 'edit_attachment', [ $this, 'maybe_rescan_pdf' ] );

return wp_update_post(
[
'ID' => $attachment_id,
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public function rest_endpoint_callback( $attachment_id, string $route_to_call =

$metadata = wp_get_attachment_metadata( $attachment_id );

if ( ! $metadata ) {
if ( ! $metadata || ! is_array( $metadata ) ) {
return new WP_Error( 'invalid', esc_html__( 'No valid metadata found.', 'classifai' ) );
}

Expand Down

0 comments on commit 517af08

Please sign in to comment.