diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php index a2bcd3745..6d740dcd7 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php @@ -48,6 +48,7 @@ class Sync implements Setup, Assets { 'transformation' => '_transformations', 'sync_error' => '_sync_error', 'cloudinary' => '_cloudinary_v2', + 'attempts' => '_sync_attempts', ); /** diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php index f4568904f..7621769eb 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php @@ -55,6 +55,20 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) { */ if ( ! empty( $meta['cloudinary'] ) && empty( $public_id ) ) { $cloudinary_id = $this->convert_cloudinary_version( $attachment_id ); + } elseif ( ! empty( $meta['cloudinary'] ) ) { + // Has public ID, but still has cloudinary, check pending status. + $is_pending = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); + $attempts = (int) $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['attempts'], true ); + if ( ( empty( $is_pending ) || $is_pending < time() - 5 * 60 ) && 10 > $attempts ) { + // Timeout. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['attempts'], $attempts + 1 ); + + // return proposed ID to allow front render. + return $this->convert_cloudinary_version( $attachment_id ); + } + $cloudinary_id = $public_id; + } else { + $cloudinary_id = $public_id; } } @@ -105,6 +119,8 @@ function ( $val ) use ( $media ) { $public_id = strstr( $public_id, '.' . $path['extension'], true ); // Save public ID. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + // Set download started data. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['pending'], time() ); // Setup a call for a background sync. $params = array( @@ -125,7 +141,7 @@ public function setup_hooks() { add_filter( 'cloudinary_id', array( $this, 'check_cloudinary_version' ), 9, 2 ); // Priority 9, to take preference over prep_on_demand_upload. // Add a redirection to the new plugin settings, from the old plugin. - if( is_admin() ) { + if ( is_admin() ) { add_action( 'admin_menu', function () { global $plugin_page; if ( ! empty( $plugin_page ) && false !== strpos( $plugin_page, 'cloudinary-image-management-and-manipulation-in-the-cloud-cdn' ) ) { diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index cda52be93..92de295ba 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -7,6 +7,8 @@ namespace Cloudinary\Sync; +use Cloudinary\Sync; + /** * Class Download_Sync. * @@ -88,9 +90,15 @@ public function rest_can_upload_files( \WP_REST_Request $request ) { */ public function handle_failed_download( $attachment_id, $error ) { // @todo: Place a handler to catch the error for logging. - // Delete attachment temp. - wp_delete_attachment( $attachment_id, true ); + $is_pending = $this->plugin->components['media']->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true ); + if ( ! empty( $is_pending ) ) { + // Dont delete if it's a downsync. + $this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], $error ); + } else { + // Delete attachment temp. + wp_delete_attachment( $attachment_id, true ); + } // Send error. wp_send_json_error( $error ); } @@ -181,6 +189,10 @@ public function rest_download_asset( \WP_REST_Request $request ) { 'data' => $attachment, ); + // Remove pending. + delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); + delete_post_meta( $attachment_id, Sync::META_KEYS['sync_error'] ); + return rest_ensure_response( $response ); } }