Skip to content

Commit 78f736c

Browse files
committed
Merge branch 'master' of github.com:cloudinary/cloudinary_wordpress
2 parents 31bc873 + 9e5d3d0 commit 78f736c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-sync.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Sync implements Setup, Assets {
4848
'transformation' => '_transformations',
4949
'sync_error' => '_sync_error',
5050
'cloudinary' => '_cloudinary_v2',
51+
'attempts' => '_sync_attempts',
5152
);
5253

5354
/**

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-upgrade.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) {
5555
*/
5656
if ( ! empty( $meta['cloudinary'] ) && empty( $public_id ) ) {
5757
$cloudinary_id = $this->convert_cloudinary_version( $attachment_id );
58+
} elseif ( ! empty( $meta['cloudinary'] ) ) {
59+
// Has public ID, but still has cloudinary, check pending status.
60+
$is_pending = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true );
61+
$attempts = (int) $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['attempts'], true );
62+
if ( ( empty( $is_pending ) || $is_pending < time() - 5 * 60 ) && 10 > $attempts ) {
63+
// Timeout.
64+
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['attempts'], $attempts + 1 );
65+
66+
// return proposed ID to allow front render.
67+
return $this->convert_cloudinary_version( $attachment_id );
68+
}
69+
$cloudinary_id = $public_id;
70+
} else {
71+
$cloudinary_id = $public_id;
5872
}
5973
}
6074

@@ -105,6 +119,8 @@ function ( $val ) use ( $media ) {
105119
$public_id = strstr( $public_id, '.' . $path['extension'], true );
106120
// Save public ID.
107121
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
122+
// Set download started data.
123+
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['pending'], time() );
108124

109125
// Setup a call for a background sync.
110126
$params = array(
@@ -125,7 +141,7 @@ public function setup_hooks() {
125141
add_filter( 'cloudinary_id', array( $this, 'check_cloudinary_version' ), 9, 2 ); // Priority 9, to take preference over prep_on_demand_upload.
126142

127143
// Add a redirection to the new plugin settings, from the old plugin.
128-
if( is_admin() ) {
144+
if ( is_admin() ) {
129145
add_action( 'admin_menu', function () {
130146
global $plugin_page;
131147
if ( ! empty( $plugin_page ) && false !== strpos( $plugin_page, 'cloudinary-image-management-and-manipulation-in-the-cloud-cdn' ) ) {

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Cloudinary\Sync;
99

10+
use Cloudinary\Sync;
11+
1012
/**
1113
* Class Download_Sync.
1214
*
@@ -88,9 +90,15 @@ public function rest_can_upload_files( \WP_REST_Request $request ) {
8890
*/
8991
public function handle_failed_download( $attachment_id, $error ) {
9092
// @todo: Place a handler to catch the error for logging.
91-
// Delete attachment temp.
92-
wp_delete_attachment( $attachment_id, true );
9393

94+
$is_pending = $this->plugin->components['media']->get_post_meta( $attachment_id, Sync::META_KEYS['pending'], true );
95+
if ( ! empty( $is_pending ) ) {
96+
// Dont delete if it's a downsync.
97+
$this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['sync_error'], $error );
98+
} else {
99+
// Delete attachment temp.
100+
wp_delete_attachment( $attachment_id, true );
101+
}
94102
// Send error.
95103
wp_send_json_error( $error );
96104
}
@@ -181,6 +189,10 @@ public function rest_download_asset( \WP_REST_Request $request ) {
181189
'data' => $attachment,
182190
);
183191

192+
// Remove pending.
193+
delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] );
194+
delete_post_meta( $attachment_id, Sync::META_KEYS['sync_error'] );
195+
184196
return rest_ensure_response( $response );
185197
}
186198
}

0 commit comments

Comments
 (0)