Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Sync implements Setup, Assets {
'transformation' => '_transformations',
'sync_error' => '_sync_error',
'cloudinary' => '_cloudinary_v2',
'attempts' => '_sync_attempts',
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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(
Expand All @@ -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' ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Cloudinary\Sync;

use Cloudinary\Sync;

/**
* Class Download_Sync.
*
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
}