From 28381c86d675c88efa5028f1532eb1d3f01d33d8 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Wed, 31 Jan 2024 13:29:07 -0700 Subject: [PATCH 1/2] Ensure we avoid running our update functionality multiple times --- includes/Classifai/Features/DescriptiveTextGenerator.php | 2 ++ includes/Classifai/Features/ImageTextExtraction.php | 2 ++ includes/Classifai/Features/PDFTextExtraction.php | 3 +++ 3 files changed, 7 insertions(+) diff --git a/includes/Classifai/Features/DescriptiveTextGenerator.php b/includes/Classifai/Features/DescriptiveTextGenerator.php index dd88dd633..b21e04597 100644 --- a/includes/Classifai/Features/DescriptiveTextGenerator.php +++ b/includes/Classifai/Features/DescriptiveTextGenerator.php @@ -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 ); } } diff --git a/includes/Classifai/Features/ImageTextExtraction.php b/includes/Classifai/Features/ImageTextExtraction.php index 90000ebc8..d8dafd3cd 100644 --- a/includes/Classifai/Features/ImageTextExtraction.php +++ b/includes/Classifai/Features/ImageTextExtraction.php @@ -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 ); } } diff --git a/includes/Classifai/Features/PDFTextExtraction.php b/includes/Classifai/Features/PDFTextExtraction.php index 3baa6bccb..066b974f2 100644 --- a/includes/Classifai/Features/PDFTextExtraction.php +++ b/includes/Classifai/Features/PDFTextExtraction.php @@ -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, From e6073d532349bfc7c42e6fe3b0877e459a35abb6 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Thu, 1 Feb 2024 07:42:29 -0700 Subject: [PATCH 2/2] Ensure metadata is an array before proceeding. Remove code that isn't needed --- includes/Classifai/Features/ImageCropping.php | 4 +--- includes/Classifai/Providers/Azure/ComputerVision.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/includes/Classifai/Features/ImageCropping.php b/includes/Classifai/Features/ImageCropping.php index 777e4d0cd..c50c3965e 100644 --- a/includes/Classifai/Features/ImageCropping.php +++ b/includes/Classifai/Features/ImageCropping.php @@ -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 ); diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index 8ae0c33ef..0c0775a16 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -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' ) ); }