Skip to content

Commit 93dfbbd

Browse files
authored
Merge pull request #116 from cloudinary/fix/upgrade-broken-images
add upgrade handling
2 parents 864a4f0 + a220462 commit 93dfbbd

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,22 @@ public function cloudinary_id( $attachment_id ) {
685685
}
686686

687687
/**
688-
* Filter the Cloudinary ID to allow extending it's availability.
688+
* Filter to validate the Cloudinary ID to allow extending it's availability.
689689
*
690690
* @param string|bool $cloudinary_id The public ID from Cloudinary, or false if not found.
691691
* @param int $attachment_id The id of the asset.
692692
*
693693
* @return string|bool
694694
*/
695-
$cloudinary_id = apply_filters( 'cloudinary_id', $cloudinary_id, $attachment_id );
695+
$cloudinary_id = apply_filters( 'validate_cloudinary_id', $cloudinary_id, $attachment_id );
696+
697+
/**
698+
* Action the Cloudinary ID to allow extending it's availability.
699+
*
700+
* @param string|bool $cloudinary_id The public ID from Cloudinary, or false if not found.
701+
* @param int $attachment_id The id of the asset.
702+
*/
703+
do_action( 'cloudinary_id', $cloudinary_id, $attachment_id );
696704
// Cache ID to prevent multiple lookups.
697705
if ( false !== $cloudinary_id ) {
698706
$this->cloudinary_ids[ $attachment_id ] = $cloudinary_id;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ class Upgrade {
2525
*/
2626
private $media;
2727

28+
/**
29+
* Holds the Sync instance.
30+
*
31+
* @since 0.1
32+
*
33+
* @var \Cloudinary\Sync Instance of the plugin.
34+
*/
35+
private $sync;
36+
2837
/**
2938
* Filter constructor.
3039
*
3140
* @param \Cloudinary\Media $media The plugin.
3241
*/
3342
public function __construct( \Cloudinary\Media $media ) {
3443
$this->media = $media;
44+
$this->sync = $media->plugin->components['sync'];
3545
$this->setup_hooks();
3646
}
3747

@@ -44,6 +54,7 @@ public function __construct( \Cloudinary\Media $media ) {
4454
* @return string|bool
4555
*/
4656
public function check_cloudinary_version( $cloudinary_id, $attachment_id ) {
57+
4758
if ( false === $cloudinary_id ) {
4859
// Backwards compat.
4960
$meta = wp_get_attachment_metadata( $attachment_id );
@@ -128,6 +139,9 @@ function ( $val ) use ( $media ) {
128139
$path = pathinfo( $public_id );
129140
$public_id = strstr( $public_id, '.' . $path['extension'], true );
130141
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
142+
if ( ! defined( 'DOING_BULK_SYNC' ) ) {
143+
$this->sync->managers['upload']->add_to_sync( $attachment_id ); // Auto sync if upgrading outside of bulk sync.
144+
}
131145

132146
return $public_id;
133147
}
@@ -136,7 +150,7 @@ function ( $val ) use ( $media ) {
136150
* Setup hooks for the filters.
137151
*/
138152
public function setup_hooks() {
139-
add_filter( 'cloudinary_id', array( $this, 'check_cloudinary_version' ), 10, 2 ); // Priority 10, to allow prep_on_demand_upload.
153+
add_filter( 'validate_cloudinary_id', array( $this, 'check_cloudinary_version' ), 10, 2 ); // Priority 10, to allow prep_on_demand_upload.
140154

141155
// Add a redirection to the new plugin settings, from the old plugin.
142156
if ( is_admin() ) {

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ public function rest_push_attachments( \WP_REST_Request $request ) {
231231
public function resume_queue() {
232232
// Check if there is a Cloudinary ID in case this was synced on-demand before being processed by the queue.
233233
add_filter( 'cloudinary_on_demand_sync_enabled', '__return_false' ); // Disable the on-demand sync since we want the status.
234-
add_filter( 'cloudinary_id', '__return_false' ); // Disable the on-demand sync since we want the status.
234+
define( 'DOING_BULK_SYNC', true ); // Define bulk sync in action.
235235

236-
if ( false === $this->plugin->components['media']->cloudinary_id( $this->post_id ) ) {
236+
if ( ! $this->plugin->components['sync']->is_synced( $this->post_id ) ) {
237237
$stat = $this->push_attachments( array( $this->post_id ) );
238238
if ( ! empty( $stat['processed'] ) ) {
239239
$result = 'done';
@@ -357,6 +357,7 @@ public function prepare_upload( $post, $down_sync = false ) {
357357
// First check if this has a file and it can be uploaded.
358358
$file = get_attached_file( $post->ID );
359359
$file_size = 0;
360+
$downsync = false;
360361
if ( empty( $file ) ) {
361362
return new \WP_Error( 'attachment_no_file', __( 'Attachment did not have a file.', 'cloudinary' ) );
362363
} elseif ( ! file_exists( $file ) ) {
@@ -373,6 +374,7 @@ public function prepare_upload( $post, $down_sync = false ) {
373374
}
374375
$file = get_attached_file( $post->ID );
375376
$file_size = filesize( $file );
377+
$downsync = true;
376378
}
377379
}
378380
} else {
@@ -414,7 +416,7 @@ public function prepare_upload( $post, $down_sync = false ) {
414416
}
415417
// Check if this asset is a folder sync.
416418
$folder_sync = $media->get_post_meta( $post->ID, Sync::META_KEYS['folder_sync'], true );
417-
if ( ! empty( $folder_sync ) ) {
419+
if ( ! empty( $folder_sync ) && false === $downsync ) {
418420
$public_id_folder = $cld_folder; // Ensure the public ID folder is constant.
419421
} else {
420422
// Not folder synced, so set the folder to the folder that the asset originally came from.
@@ -490,7 +492,7 @@ public function prepare_upload( $post, $down_sync = false ) {
490492
$public_id = ltrim( $public_id_folder . $options['public_id'], '/' );
491493
$return = array(
492494
'file' => $file,
493-
'folder' => $cld_folder,
495+
'folder' => ltrim( $cld_folder, '/' ),
494496
'public_id' => $public_id,
495497
'breakpoints' => array(),
496498
'options' => $options,
@@ -542,7 +544,10 @@ public function push_attachments( $attachments ) {
542544
// Go over each attachment.
543545
foreach ( $attachments as $attachment ) {
544546
$attachment = get_post( $attachment );
545-
$upload = $this->prepare_upload( $attachment->ID, true );
547+
// Clear upload option cache for this item to allow down sync.
548+
$this->upload_options[ $attachment->ID ] = false;
549+
550+
$upload = $this->prepare_upload( $attachment->ID, true );
546551

547552
// Filter out any attachments with problematic options.
548553
if ( is_wp_error( $upload ) ) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public function __construct( \Cloudinary\Plugin $plugin, $enabled = false, $push
6565
private function register_hooks() {
6666
// Add action to upload.
6767
add_action( 'add_attachment', array( $this, 'push_on_upload' ), 10 );
68-
// Filter id for on-demand upload sync.
69-
add_filter( 'cloudinary_id', array( $this, 'prep_on_demand_upload' ), 9, 2 );
68+
// Action Cloudinary id for on-demand upload sync.
69+
add_action( 'cloudinary_id', array( $this, 'prep_on_demand_upload' ), 9, 2 );
7070
// Show sync status.
7171
add_filter( 'cloudinary_media_status', array( $this, 'filter_status' ), 10, 2 );
7272
// Hook for on demand upload push.

0 commit comments

Comments
 (0)