Skip to content

Commit 043b82c

Browse files
committed
add folder_sync to maintain sync structure and cleanup code
1 parent 10c11ff commit 043b82c

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
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+
'folder_sync' => '_folder_sync',
5152
);
5253

5354
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ function ( $val ) use ( $media ) {
157157
$public_id = strstr( $public_id, '.' . $path['extension'], true );
158158
// Save public ID.
159159
$media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
160+
// Check if the asset is in the same folder as the defined Cloudinary folder.
161+
if ( false !== strpos( $public_id, '/' ) ) {
162+
$path = pathinfo( $public_id );
163+
$asset_folder = trailingslashit( $path['dirname'] );
164+
$cloudinary_folder = trailingslashit( $this->plugin->config['settings']['sync_media']['cloudinary_folder'] );
165+
if ( $asset_folder === $cloudinary_folder ) {
166+
// The asset folder matches the defined cloudinary folder, flag it as being in a folder sync.
167+
$media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true );
168+
}
169+
}
160170

161171
return $this->download_asset( $attachment_id, $file, basename( $file ), $media->get_transformations_from_string( $file ) );
162172
}

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ public function prepare_upload( $post, $down_sync = false ) {
351351
return new \WP_Error( 'attachment_post_expected', __( 'An attachment post was expected.', 'cloudinary' ) );
352352
}
353353

354+
// Get the media component.
355+
$media = $this->plugin->components['media'];
356+
354357
// First check if this has a file and it can be uploaded.
355358
$file = get_attached_file( $post->ID );
356359
$file_size = 0;
@@ -359,7 +362,7 @@ public function prepare_upload( $post, $down_sync = false ) {
359362
} elseif ( ! file_exists( $file ) ) {
360363
// May be an old upload type.
361364
$src = get_post_meta( $post->ID, '_wp_attached_file', true );
362-
if ( $this->plugin->components['media']->is_cloudinary_url( $src ) ) {
365+
if ( $media->is_cloudinary_url( $src ) ) {
363366
// Download first maybe.
364367
if ( true === $down_sync ) {
365368
$download = $this->plugin->components['sync']->managers['download']->down_sync( $post->ID );
@@ -384,7 +387,7 @@ public function prepare_upload( $post, $down_sync = false ) {
384387

385388
// translators: variable is file size.
386389
$error = sprintf( __( 'File size exceeds the maximum of %s. This media asset will be served from WordPress.', 'cloudinary' ), $max_size_hr );
387-
$this->plugin->components['media']->delete_post_meta( $post->ID, Sync::META_KEYS['pending'] ); // Remove Flag.
390+
$media->delete_post_meta( $post->ID, Sync::META_KEYS['pending'] ); // Remove Flag.
388391

389392
return new \WP_Error( 'upload_error', $error );
390393
}
@@ -409,7 +412,14 @@ public function prepare_upload( $post, $down_sync = false ) {
409412
$public_id_folder = trailingslashit( $public_id_info['dirname'] );
410413
$public_id_file = $public_id_info['filename'];
411414
}
412-
415+
// Check if this asset is a folder sync.
416+
$folder_sync = $media->get_post_meta( $post->ID, Sync::META_KEYS['folder_sync'], true );
417+
if ( ! empty( $folder_sync ) ) {
418+
$public_id_folder = $cld_folder; // Ensure the public ID folder is constant.
419+
} else {
420+
// Not folder synced, so set the folder to the folder that the asset originally came from.
421+
$cld_folder = $public_id_folder;
422+
}
413423
// Prepare upload options.
414424
$options = array(
415425
'unique_filename' => false,
@@ -436,7 +446,7 @@ public function prepare_upload( $post, $down_sync = false ) {
436446
$imagesize = getimagesize( $file );
437447
$meta['width'] = $imagesize[0];
438448
}
439-
$max_width = $this->plugin->components['media']->get_max_width();
449+
$max_width = $media->get_max_width();
440450
// Add breakpoints request options.
441451
if ( ! empty( $settings['global_transformations']['enable_breakpoints'] ) ) {
442452
$options['responsive_breakpoints'] = array(
@@ -446,7 +456,7 @@ public function prepare_upload( $post, $down_sync = false ) {
446456
'max_width' => $meta['width'] < $max_width ? $meta['width'] : $max_width,
447457
'min_width' => $settings['global_transformations']['min_width'],
448458
);
449-
$transformations = $this->plugin->components['media']->get_transformation_from_meta( $post->ID );
459+
$transformations = $media->get_transformation_from_meta( $post->ID );
450460
if ( ! empty( $transformations ) ) {
451461
$options['responsive_breakpoints']['transformation'] = Api::generate_transformation_string( $transformations );
452462
}
@@ -526,6 +536,8 @@ public function push_attachments( $attachments ) {
526536
'total' => count( $attachments ),
527537
'processed' => 0,
528538
);
539+
// Get media component.
540+
$media = $this->plugin->components['media'];
529541

530542
// Go over each attachment.
531543
foreach ( $attachments as $attachment ) {
@@ -580,7 +592,7 @@ public function push_attachments( $attachments ) {
580592
} elseif ( 'rename' === $sync_type ) {
581593
// Rename an asset.
582594
$args = array(
583-
'from_public_id' => $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ),
595+
'from_public_id' => $media->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ),
584596
'to_public_id' => $upload['public_id'],
585597
);
586598
$result = $this->plugin->components['connect']->api->{$upload['options']['resource_type']}( 'rename', 'POST', $args );
@@ -597,7 +609,7 @@ public function push_attachments( $attachments ) {
597609
if ( is_wp_error( $result ) ) {
598610
$error = $result->get_error_message();
599611
$stats['fail'][] = $error;
600-
$this->plugin->components['media']->update_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], $error );
612+
$media->update_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], $error );
601613
continue;
602614
}
603615

@@ -611,8 +623,8 @@ public function push_attachments( $attachments ) {
611623
if ( ! empty( $result['version'] ) ) {
612624
$meta_data[ Sync::META_KEYS['version'] ] = $result['version'];
613625
}
614-
$this->plugin->components['media']->delete_post_meta( $attachment->ID, Sync::META_KEYS['pending'] );
615-
$this->plugin->components['media']->delete_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], false );
626+
$media->delete_post_meta( $attachment->ID, Sync::META_KEYS['pending'] );
627+
$media->delete_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], false );
616628
if ( ! empty( $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] ) ) {
617629
if ( ! empty( $result['responsive_breakpoints'] ) ) { // Images only.
618630
$meta_data[ Sync::META_KEYS['breakpoints'] ] = $result['responsive_breakpoints'][0]['breakpoints'];
@@ -633,7 +645,7 @@ public function push_attachments( $attachments ) {
633645
$meta = wp_get_attachment_metadata( $attachment->ID, true );
634646
$meta[ Sync::META_KEYS['cloudinary'] ] = $meta_data;
635647
wp_update_attachment_metadata( $attachment->ID, $meta );
636-
$this->plugin->components['media']->update_post_meta( $attachment->ID, Sync::META_KEYS['public_id'], $upload['options']['public_id'] );
648+
$media->update_post_meta( $attachment->ID, Sync::META_KEYS['public_id'], $upload['options']['public_id'] );
637649
// Search and update link references in content.
638650
$content_search = new \WP_Query( array( 's' => 'wp-image-' . $attachment->ID, 'fields' => 'ids', 'posts_per_page' => 1000 ) );
639651
if ( ! empty( $content_search->found_posts ) ) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public function add_to_sync( $attachment_id ) {
261261
if ( ! in_array( $attachment_id, $this->to_sync, true ) ) {
262262
// Flag image as pending to prevent duplicate upload.
263263
update_post_meta( $attachment_id, Sync::META_KEYS['pending'], time() );
264+
$this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true );
264265
$this->to_sync[] = $attachment_id;
265266
}
266267
}

0 commit comments

Comments
 (0)