From 70596d233d4e311ff67b416ff5595f226d396646 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 23 Jun 2020 11:44:02 +0200 Subject: [PATCH 01/13] add rename sync type --- .../php/sync/class-push-sync.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 892e86a50..66d7d2874 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -58,9 +58,10 @@ public function __construct( \Cloudinary\Plugin $plugin ) { // Define the sync types and their option keys. $sync_types = array( 'file' => 'upload', + 'public_id' => 'rename', 'breakpoints' => 'explicit', 'options' => 'context', - 'folder' => 'upload', + 'folder' => 'rename', 'cloud_name' => 'upload', ); $this->sync_types = apply_filters( 'cloudinary_sync_types', $sync_types ); @@ -570,6 +571,13 @@ public function push_attachments( $attachments ) { $args['context'] = $upload['options']['context']; } $result = $this->plugin->components['connect']->api->explicit( $args ); + } elseif ( 'rename' === $sync_type ) { + // Rename an asset. + $args = array( + 'from_public_id' => $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ), + 'to_public_id' => $upload['public_id'], + ); + $result = $this->plugin->components['connect']->api->{$upload['options']['resource_type']}( 'rename', 'POST', $args ); } else { // dynamic sync type.. $result = $this->plugin->components['connect']->api->{$sync_type}( $upload['file'], $upload['options'] ); From f5f4e6a544c39fda1d1eb3889ef842bff3eac76e Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 23 Jun 2020 11:45:19 +0200 Subject: [PATCH 02/13] add public_id sync state --- .../php/sync/class-push-sync.php | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 66d7d2874..3d8a75d82 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -390,25 +390,17 @@ public function prepare_upload( $post, $down_sync = false ) { } // If it's got a public ID, then this is an explicit update. + $settings = $this->plugin->config['settings']; $public_id = $post->{Sync::META_KEYS['public_id']}; // use the __get method on the \WP_Post to get post_meta. - $dirs = wp_get_upload_dir(); - $cld_folder = false; - $folder = trailingslashit( $dirs['cloudinary_folder'] ); - if ( '/' === $dirs['cloudinary_folder'] ) { - $folder = ''; - } + $cld_folder = trailingslashit( $settings['sync_media']['cloudinary_folder'] ); if ( empty( $public_id ) ) { $file_info = pathinfo( $file ); - $public_id = $folder . $file_info['filename']; - } - - // Check if cloudinary folder is in public_id. - $parts = explode( '/', $public_id ); - if ( untrailingslashit( $dirs['cloudinary_folder'] ) === $parts[0] ) { - $cld_folder = $dirs['cloudinary_folder']; + $public_id = $file_info['filename']; + } else { + $public_id_parts = pathinfo( $public_id ); + $public_id = $public_id_parts['basename']; } - // Prepare upload options. $options = array( 'unique_filename' => false, @@ -475,14 +467,19 @@ public function prepare_upload( $post, $down_sync = false ) { $breakpoints['context'] = http_build_query( $breakpoints['context'], null, '|' ); } + // Stage folder to public_id. + $public_id = $cld_folder . $options['public_id']; $return = array( 'file' => $file, 'folder' => $cld_folder, + 'public_id' => $public_id, 'breakpoints' => array(), 'options' => $options, ); + $return['options']['public_id'] = $public_id; if ( ! empty( $breakpoints ) ) { $return['breakpoints'] = $breakpoints; + $return['breakpoints']['public_id'] = $public_id; // Stage public ID to folder for breakpoints. } $this->upload_options[ $post->ID ] = $return; @@ -561,7 +558,7 @@ public function push_attachments( $attachments ) { if ( 'explicit' === $sync_type ) { // Explicit update. $args = array( - 'public_id' => $upload['options']['public_id'], + 'public_id' => $upload['public_id'], 'type' => 'upload', ); if ( ! empty( $upload['options']['responsive_breakpoints'] ) ) { From 82a0b49ad0fc2e4dae8df43bc1388b50e73b20dc Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 23 Jun 2020 11:49:03 +0200 Subject: [PATCH 03/13] fix upgrade check to return if v2 meta exists. --- .../php/media/class-upgrade.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 55517a0db..53a165d5e 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 @@ -46,7 +46,10 @@ public function __construct( \Cloudinary\Media $media ) { public function check_cloudinary_version( $cloudinary_id, $attachment_id ) { if ( false === $cloudinary_id ) { // Backwards compat. - $meta = wp_get_attachment_metadata( $attachment_id ); + $meta = wp_get_attachment_metadata( $attachment_id ); + if ( ! empty( $meta[ Sync::META_KEYS['cloudinary'] ] ) ) { + return $cloudinary_id; // Current version. + } $public_id = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); /* From d245cef999cc02c97ef1ebcc59c297ac2a3a1522 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 23 Jun 2020 11:49:27 +0200 Subject: [PATCH 04/13] cleanup meta checking --- .../php/sync/class-push-sync.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 3d8a75d82..1939e8846 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -304,7 +304,7 @@ private function get_sync_type( $attachment ) { $type = 'upload'; // Check for explicit (has public_id, but no breakpoints). - $attachment_signature = $attachment->{Sync::META_KEYS['signature']}; + $attachment_signature = $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['signature'] ); if ( empty( $attachment_signature ) ) { if ( ! empty( $attachment->{Sync::META_KEYS['public_id']} ) ) { // Has a public id but no signature, explicit update to complete download. From 43355d3abdf2e64120ef3cc2c22470cfe1341ea7 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 23 Jun 2020 11:49:43 +0200 Subject: [PATCH 05/13] cleanup settings use. --- .../php/sync/class-push-sync.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 1939e8846..2ab91531c 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -429,13 +429,13 @@ public function prepare_upload( $post, $down_sync = false ) { } $max_width = $this->plugin->components['media']->get_max_width(); // Add breakpoints request options. - if ( ! empty( $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] ) ) { + if ( ! empty( $settings['global_transformations']['enable_breakpoints'] ) ) { $options['responsive_breakpoints'] = array( 'create_derived' => true, - 'bytes_step' => $this->plugin->config['settings']['global_transformations']['bytes_step'], - 'max_images' => $this->plugin->config['settings']['global_transformations']['breakpoints'], + 'bytes_step' => $settings['global_transformations']['bytes_step'], + 'max_images' => $settings['global_transformations']['breakpoints'], 'max_width' => $meta['width'] < $max_width ? $meta['width'] : $max_width, - 'min_width' => $this->plugin->config['settings']['global_transformations']['min_width'], + 'min_width' => $settings['global_transformations']['min_width'], ); $transformations = $this->plugin->components['media']->get_transformation_from_meta( $post->ID ); if ( ! empty( $transformations ) ) { From 9a9ab05ed7ac7cbec338c3a419f99bd6881bc8a2 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 25 Jun 2020 13:13:45 +0200 Subject: [PATCH 06/13] use the generated signature as a default for stored. --- .../php/sync/class-push-sync.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 2ab91531c..4929640b4 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -312,8 +312,10 @@ private function get_sync_type( $attachment ) { } // fallback to upload. } else { - // Has signature. Compare and find if different. + // Has signature find differences. $required_signature = $this->plugin->components['sync']->generate_signature( $attachment->ID ); + // Apply the generated asset signature as a default. To allow for signature extensions that may not exist. + $attachment_signature = wp_parse_args( $attachment_signature, $required_signature ); foreach ( $required_signature as $key => $signature ) { if ( ( ! isset( $attachment_signature[ $key ] ) || $attachment_signature[ $key ] !== $signature ) && isset( $this->sync_types[ $key ] ) ) { return $this->sync_types[ $key ]; From 3d720c6cc067b91018be803aa6e223c8a8f73989 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 25 Jun 2020 13:23:10 +0200 Subject: [PATCH 07/13] move to get_signature method. --- .../php/class-sync.php | 20 ++++++++++++++++++- .../php/sync/class-push-sync.php | 10 +++++----- 2 files changed, 24 insertions(+), 6 deletions(-) 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..4e31188f2 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 @@ -100,7 +100,7 @@ public function is_active() { */ public function is_synced( $post_id ) { $return = false; - $signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true ); + $signature = $this->get_signature( $post_id ); if ( ! empty( $signature ) && $this->generate_signature( $post_id ) === $signature ) { $return = $signature; } @@ -133,6 +133,24 @@ function ( $item ) { return $return; } + /** + * Get the current sync signature of an asset. + * + * @param int $post_id The post ID. + * + * @return array|bool + */ + public function get_signature( $post_id ) { + $return = false; + $signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true ); + if ( ! empty( $signature ) ) { + $base_signatures = $this->generate_signature( $post_id ); + $return = wp_parse_args( $signature, $base_signatures ); + } + + return $return; + } + /** * Additional component setup. */ diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 4929640b4..24712c3dd 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -304,7 +304,7 @@ private function get_sync_type( $attachment ) { $type = 'upload'; // Check for explicit (has public_id, but no breakpoints). - $attachment_signature = $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['signature'] ); + $attachment_signature = $this->plugin->components['sync']->generate_signature( $attachment->ID ); if ( empty( $attachment_signature ) ) { if ( ! empty( $attachment->{Sync::META_KEYS['public_id']} ) ) { // Has a public id but no signature, explicit update to complete download. @@ -400,7 +400,7 @@ public function prepare_upload( $post, $down_sync = false ) { $public_id = $file_info['filename']; } else { $public_id_parts = pathinfo( $public_id ); - $public_id = $public_id_parts['basename']; + $public_id = $public_id_parts['basename']; } // Prepare upload options. @@ -470,8 +470,8 @@ public function prepare_upload( $post, $down_sync = false ) { } // Stage folder to public_id. - $public_id = $cld_folder . $options['public_id']; - $return = array( + $public_id = $cld_folder . $options['public_id']; + $return = array( 'file' => $file, 'folder' => $cld_folder, 'public_id' => $public_id, @@ -480,7 +480,7 @@ public function prepare_upload( $post, $down_sync = false ) { ); $return['options']['public_id'] = $public_id; if ( ! empty( $breakpoints ) ) { - $return['breakpoints'] = $breakpoints; + $return['breakpoints'] = $breakpoints; $return['breakpoints']['public_id'] = $public_id; // Stage public ID to folder for breakpoints. } $this->upload_options[ $post->ID ] = $return; From c71b2e00170f875fe9469ae59390043f13262292 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 25 Jun 2020 13:29:39 +0200 Subject: [PATCH 08/13] cache signatures and cleanup. --- .../php/class-sync.php | 17 ++++++++++++----- .../php/sync/class-push-sync.php | 6 ++---- 2 files changed, 14 insertions(+), 9 deletions(-) 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 4e31188f2..56f7dc5fd 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 @@ -141,11 +141,18 @@ function ( $item ) { * @return array|bool */ public function get_signature( $post_id ) { - $return = false; - $signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true ); - if ( ! empty( $signature ) ) { - $base_signatures = $this->generate_signature( $post_id ); - $return = wp_parse_args( $signature, $base_signatures ); + static $signatures = array(); // Cache signatures already fetched. + + $return = false; + if ( ! empty( $signatures[ $post_id ] ) ) { + $return = $signatures[ $post_id ]; + } else { + $signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true ); + if ( ! empty( $signature ) ) { + $base_signatures = $this->generate_signature( $post_id ); + $signatures[ $post_id ] = wp_parse_args( $signature, $base_signatures ); + $return = $signatures[ $post_id ]; + } } return $return; diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 24712c3dd..a2ac8fd55 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -304,7 +304,7 @@ private function get_sync_type( $attachment ) { $type = 'upload'; // Check for explicit (has public_id, but no breakpoints). - $attachment_signature = $this->plugin->components['sync']->generate_signature( $attachment->ID ); + $attachment_signature = $this->plugin->components['sync']->get_signature( $attachment->ID ); if ( empty( $attachment_signature ) ) { if ( ! empty( $attachment->{Sync::META_KEYS['public_id']} ) ) { // Has a public id but no signature, explicit update to complete download. @@ -312,10 +312,8 @@ private function get_sync_type( $attachment ) { } // fallback to upload. } else { - // Has signature find differences. + // Has signature find differences and use specific sync method. $required_signature = $this->plugin->components['sync']->generate_signature( $attachment->ID ); - // Apply the generated asset signature as a default. To allow for signature extensions that may not exist. - $attachment_signature = wp_parse_args( $attachment_signature, $required_signature ); foreach ( $required_signature as $key => $signature ) { if ( ( ! isset( $attachment_signature[ $key ] ) || $attachment_signature[ $key ] !== $signature ) && isset( $this->sync_types[ $key ] ) ) { return $this->sync_types[ $key ]; From 502337192f07fa39b9f3e93622f67eed813e45a0 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Mon, 29 Jun 2020 14:04:13 +0200 Subject: [PATCH 09/13] use the public id's synced path rather than folder set. --- .../php/class-sync.php | 3 ++- .../php/sync/class-push-sync.php | 22 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) 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 56f7dc5fd..f64230e4d 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 @@ -101,7 +101,8 @@ public function is_active() { public function is_synced( $post_id ) { $return = false; $signature = $this->get_signature( $post_id ); - if ( ! empty( $signature ) && $this->generate_signature( $post_id ) === $signature ) { + $expecting = $this->generate_signature( $post_id ); + if ( ! empty( $signature ) && $expecting === $signature ) { $return = $signature; } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index a2ac8fd55..1f2045c1c 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -61,7 +61,7 @@ public function __construct( \Cloudinary\Plugin $plugin ) { 'public_id' => 'rename', 'breakpoints' => 'explicit', 'options' => 'context', - 'folder' => 'rename', + 'folder' => 'upload', 'cloud_name' => 'upload', ); $this->sync_types = apply_filters( 'cloudinary_sync_types', $sync_types ); @@ -395,17 +395,25 @@ public function prepare_upload( $post, $down_sync = false ) { $cld_folder = trailingslashit( $settings['sync_media']['cloudinary_folder'] ); if ( empty( $public_id ) ) { $file_info = pathinfo( $file ); - $public_id = $file_info['filename']; + $public_id = $cld_folder . $file_info['filename']; + } + + if ( false !== strpos( $public_id, '/' ) ) { + // Split the public_id into path and filename to allow filtering just the ID and not giving access to the path. + $public_id_info = pathinfo( $public_id ); + $public_id_folder = trailingslashit( $public_id_info['dirname'] ); + $public_id_file = $public_id_info['filename']; } else { - $public_id_parts = pathinfo( $public_id ); - $public_id = $public_id_parts['basename']; + // File is in the root of cloudinary. + $public_id_folder = ''; + $public_id_file = $public_id; } // Prepare upload options. $options = array( 'unique_filename' => false, 'resource_type' => $resource_type, - 'public_id' => $public_id, + 'public_id' => $public_id_file, 'context' => array( 'caption' => esc_attr( $post->post_title ), 'alt' => $post->_wp_attachment_image_alt, @@ -467,8 +475,8 @@ public function prepare_upload( $post, $down_sync = false ) { $breakpoints['context'] = http_build_query( $breakpoints['context'], null, '|' ); } - // Stage folder to public_id. - $public_id = $cld_folder . $options['public_id']; + // Restructure the path to the filename to allow correct placement in Cloudinary. + $public_id = $public_id_folder . $options['public_id']; $return = array( 'file' => $file, 'folder' => $cld_folder, From 562affe48d149fd55b4771dbe91898262a1921d8 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 30 Jun 2020 13:47:49 +0200 Subject: [PATCH 10/13] reorganise sync types in logicial order --- .../php/sync/class-push-sync.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 1f2045c1c..7376d973a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -57,12 +57,12 @@ public function __construct( \Cloudinary\Plugin $plugin ) { // Define the sync types and their option keys. $sync_types = array( + 'cloud_name' => 'upload', + 'folder' => 'upload', 'file' => 'upload', 'public_id' => 'rename', 'breakpoints' => 'explicit', 'options' => 'context', - 'folder' => 'upload', - 'cloud_name' => 'upload', ); $this->sync_types = apply_filters( 'cloudinary_sync_types', $sync_types ); From 10c11fff5c30507fa7351eaff6d9ec1a4d916807 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 30 Jun 2020 13:53:55 +0200 Subject: [PATCH 11/13] simplify public_id / folder split --- .../php/sync/class-push-sync.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 7376d973a..0e765c519 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -398,15 +398,16 @@ public function prepare_upload( $post, $down_sync = false ) { $public_id = $cld_folder . $file_info['filename']; } + // Assume that the public_id is a root item. + $public_id_folder = ''; + $public_id_file = $public_id; + + // Check if in a lower level. if ( false !== strpos( $public_id, '/' ) ) { // Split the public_id into path and filename to allow filtering just the ID and not giving access to the path. $public_id_info = pathinfo( $public_id ); $public_id_folder = trailingslashit( $public_id_info['dirname'] ); $public_id_file = $public_id_info['filename']; - } else { - // File is in the root of cloudinary. - $public_id_folder = ''; - $public_id_file = $public_id; } // Prepare upload options. From 043b82c8bf1177876355507014edcf61a80be839 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 30 Jun 2020 15:52:24 +0200 Subject: [PATCH 12/13] add folder_sync to maintain sync structure and cleanup code --- .../php/class-sync.php | 1 + .../php/sync/class-download-sync.php | 10 ++++++ .../php/sync/class-push-sync.php | 32 +++++++++++++------ .../php/sync/class-upload-sync.php | 1 + 4 files changed, 34 insertions(+), 10 deletions(-) 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 f64230e4d..44968e555 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', + 'folder_sync' => '_folder_sync', ); /** 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 cb4c9e6c6..cf7c65a99 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 @@ -157,6 +157,16 @@ function ( $val ) use ( $media ) { $public_id = strstr( $public_id, '.' . $path['extension'], true ); // Save public ID. $media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + // Check if the asset is in the same folder as the defined Cloudinary folder. + if ( false !== strpos( $public_id, '/' ) ) { + $path = pathinfo( $public_id ); + $asset_folder = trailingslashit( $path['dirname'] ); + $cloudinary_folder = trailingslashit( $this->plugin->config['settings']['sync_media']['cloudinary_folder'] ); + if ( $asset_folder === $cloudinary_folder ) { + // The asset folder matches the defined cloudinary folder, flag it as being in a folder sync. + $media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ); + } + } return $this->download_asset( $attachment_id, $file, basename( $file ), $media->get_transformations_from_string( $file ) ); } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php index 0e765c519..da9238bae 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-push-sync.php @@ -351,6 +351,9 @@ public function prepare_upload( $post, $down_sync = false ) { return new \WP_Error( 'attachment_post_expected', __( 'An attachment post was expected.', 'cloudinary' ) ); } + // Get the media component. + $media = $this->plugin->components['media']; + // First check if this has a file and it can be uploaded. $file = get_attached_file( $post->ID ); $file_size = 0; @@ -359,7 +362,7 @@ public function prepare_upload( $post, $down_sync = false ) { } elseif ( ! file_exists( $file ) ) { // May be an old upload type. $src = get_post_meta( $post->ID, '_wp_attached_file', true ); - if ( $this->plugin->components['media']->is_cloudinary_url( $src ) ) { + if ( $media->is_cloudinary_url( $src ) ) { // Download first maybe. if ( true === $down_sync ) { $download = $this->plugin->components['sync']->managers['download']->down_sync( $post->ID ); @@ -384,7 +387,7 @@ public function prepare_upload( $post, $down_sync = false ) { // translators: variable is file size. $error = sprintf( __( 'File size exceeds the maximum of %s. This media asset will be served from WordPress.', 'cloudinary' ), $max_size_hr ); - $this->plugin->components['media']->delete_post_meta( $post->ID, Sync::META_KEYS['pending'] ); // Remove Flag. + $media->delete_post_meta( $post->ID, Sync::META_KEYS['pending'] ); // Remove Flag. return new \WP_Error( 'upload_error', $error ); } @@ -409,7 +412,14 @@ public function prepare_upload( $post, $down_sync = false ) { $public_id_folder = trailingslashit( $public_id_info['dirname'] ); $public_id_file = $public_id_info['filename']; } - + // Check if this asset is a folder sync. + $folder_sync = $media->get_post_meta( $post->ID, Sync::META_KEYS['folder_sync'], true ); + if ( ! empty( $folder_sync ) ) { + $public_id_folder = $cld_folder; // Ensure the public ID folder is constant. + } else { + // Not folder synced, so set the folder to the folder that the asset originally came from. + $cld_folder = $public_id_folder; + } // Prepare upload options. $options = array( 'unique_filename' => false, @@ -436,7 +446,7 @@ public function prepare_upload( $post, $down_sync = false ) { $imagesize = getimagesize( $file ); $meta['width'] = $imagesize[0]; } - $max_width = $this->plugin->components['media']->get_max_width(); + $max_width = $media->get_max_width(); // Add breakpoints request options. if ( ! empty( $settings['global_transformations']['enable_breakpoints'] ) ) { $options['responsive_breakpoints'] = array( @@ -446,7 +456,7 @@ public function prepare_upload( $post, $down_sync = false ) { 'max_width' => $meta['width'] < $max_width ? $meta['width'] : $max_width, 'min_width' => $settings['global_transformations']['min_width'], ); - $transformations = $this->plugin->components['media']->get_transformation_from_meta( $post->ID ); + $transformations = $media->get_transformation_from_meta( $post->ID ); if ( ! empty( $transformations ) ) { $options['responsive_breakpoints']['transformation'] = Api::generate_transformation_string( $transformations ); } @@ -526,6 +536,8 @@ public function push_attachments( $attachments ) { 'total' => count( $attachments ), 'processed' => 0, ); + // Get media component. + $media = $this->plugin->components['media']; // Go over each attachment. foreach ( $attachments as $attachment ) { @@ -580,7 +592,7 @@ public function push_attachments( $attachments ) { } elseif ( 'rename' === $sync_type ) { // Rename an asset. $args = array( - 'from_public_id' => $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ), + 'from_public_id' => $media->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ), 'to_public_id' => $upload['public_id'], ); $result = $this->plugin->components['connect']->api->{$upload['options']['resource_type']}( 'rename', 'POST', $args ); @@ -597,7 +609,7 @@ public function push_attachments( $attachments ) { if ( is_wp_error( $result ) ) { $error = $result->get_error_message(); $stats['fail'][] = $error; - $this->plugin->components['media']->update_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], $error ); + $media->update_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], $error ); continue; } @@ -611,8 +623,8 @@ public function push_attachments( $attachments ) { if ( ! empty( $result['version'] ) ) { $meta_data[ Sync::META_KEYS['version'] ] = $result['version']; } - $this->plugin->components['media']->delete_post_meta( $attachment->ID, Sync::META_KEYS['pending'] ); - $this->plugin->components['media']->delete_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], false ); + $media->delete_post_meta( $attachment->ID, Sync::META_KEYS['pending'] ); + $media->delete_post_meta( $attachment->ID, Sync::META_KEYS['sync_error'], false ); if ( ! empty( $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] ) ) { if ( ! empty( $result['responsive_breakpoints'] ) ) { // Images only. $meta_data[ Sync::META_KEYS['breakpoints'] ] = $result['responsive_breakpoints'][0]['breakpoints']; @@ -633,7 +645,7 @@ public function push_attachments( $attachments ) { $meta = wp_get_attachment_metadata( $attachment->ID, true ); $meta[ Sync::META_KEYS['cloudinary'] ] = $meta_data; wp_update_attachment_metadata( $attachment->ID, $meta ); - $this->plugin->components['media']->update_post_meta( $attachment->ID, Sync::META_KEYS['public_id'], $upload['options']['public_id'] ); + $media->update_post_meta( $attachment->ID, Sync::META_KEYS['public_id'], $upload['options']['public_id'] ); // Search and update link references in content. $content_search = new \WP_Query( array( 's' => 'wp-image-' . $attachment->ID, 'fields' => 'ids', 'posts_per_page' => 1000 ) ); if ( ! empty( $content_search->found_posts ) ) { diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php index cd9272c2d..f3a8bfcce 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php @@ -261,6 +261,7 @@ public function add_to_sync( $attachment_id ) { if ( ! in_array( $attachment_id, $this->to_sync, true ) ) { // Flag image as pending to prevent duplicate upload. update_post_meta( $attachment_id, Sync::META_KEYS['pending'], time() ); + $this->plugin->components['media']->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ); $this->to_sync[] = $attachment_id; } } From e124f2d9c87a049596205fc22d82d022f6ac1d27 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 30 Jun 2020 16:56:10 +0200 Subject: [PATCH 13/13] add folder_sync upgrade script for 1.2.1 and below. --- .../php/media/class-upgrade.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 53a165d5e..1051bc668 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 @@ -62,6 +62,24 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) { // Has public ID, but not fully down synced. $cloudinary_id = $public_id; } + } else { + // Backwards compat. + $folder_sync = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ); + if ( 0 === strlen( $folder_sync ) ) { + // Does not exist, add it to be compatible with v1.2.2. + $public_id = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + // Set the folder sync to 0 to flag it by default as not synced. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], '0' ); + if ( false !== strpos( $public_id, '/' ) ) { + $path = pathinfo( $public_id ); + $asset_folder = trailingslashit( $path['dirname'] ); + $cloudinary_folder = trailingslashit( $this->media->plugin->config['settings']['sync_media']['cloudinary_folder'] ); + if ( $asset_folder === $cloudinary_folder ) { + // The asset folder matches the defined cloudinary folder, flag it as being in a folder sync. + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], '1' ); + } + } + } } return $cloudinary_id;