From 72e25875339e2229afde5989f9b4dd7a42d36579 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 00:45:52 +0100 Subject: [PATCH 1/6] Cleanup --- .../php/class-media.php | 2 +- .../php/sync/class-upload-sync.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 4c8fac6b6..fd43bf17b 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -762,7 +762,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation // Make a copy as not to destroy the options in \Cloudinary::cloudinary_url(). $args = $pre_args; - $url = $this->plugin->components['connect']->api->cloudinary_url( $cloudinary_id, $args, $size, $clean ); + $url = $this->plugin->components['connect']->api->cloudinary_url( $cloudinary_id, $args, $size ); // Check if this type is a preview only type. i.e PDF. if ( ! empty( $size ) && $this->is_preview_only( $attachment_id ) ) { 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 fbacfc049..c630ac133 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 @@ -233,7 +233,6 @@ public function upload_asset( $attachment_id ) { } } - // Set folder Synced. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], $this->media->is_folder_synced( $attachment_id ) ); // Set public_id. From 93b9687c63be958ac918adaffc8081b9113059ce Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 00:46:24 +0100 Subject: [PATCH 2/6] Make sure that key is set before using it --- .../php/class-media.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index fd43bf17b..33c7a7ffd 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -1051,6 +1051,8 @@ public function image_srcset( $sources, $size_array, $image_src, $image_meta, $a $transformations = $this->get_post_meta( $attachment_id, Sync::META_KEYS['transformation'], true ); // Use Cloudinary breakpoints for same ratio. + $image_meta['overwrite_transformations'] = ! empty( $image_meta['overwrite_transformations'] ) ? $image_meta['overwrite_transformations'] : false; + if ( 'on' === $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] && wp_image_matches_ratio( $image_meta['width'], $image_meta['height'], $size_array[0], $size_array[1] ) ) { $meta = $this->get_post_meta( $attachment_id, Sync::META_KEYS['breakpoints'], true ); if ( ! empty( $meta ) ) { From 8cc60286ea0e17967dc5a00373033909601419f6 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 00:46:46 +0100 Subject: [PATCH 3/6] Initialize variable function wide --- .../php/connect/class-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php index bd6166060..e69bc362b 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php @@ -410,6 +410,7 @@ public function upload( $attachment_id, $args, $headers = array() ) { $disable_https_fetch = get_transient( '_cld_disable_http_upload' ); $file_url = wp_get_original_image_url( $attachment_id ); $media = get_plugin_instance()->get_component( 'media' ); + $tempfile = false; if ( $media && $media->is_cloudinary_url( $file_url ) ) { // If this is a Cloudinary URL, then we can use it to fetch from that location. $disable_https_fetch = false; @@ -429,7 +430,6 @@ public function upload( $attachment_id, $args, $headers = array() ) { return $this->upload_large( $attachment_id, $args ); } } - $tempfile = false; if ( false !== strpos( $args['file'], 'vip://' ) ) { $args['file'] = $this->create_local_copy( $args['file'] ); if ( is_wp_error( $args['file'] ) ) { From 037b58a5a59f52e5d25ab03d7fb86815c923957c Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 00:49:58 +0100 Subject: [PATCH 4/6] Do not override the attachament URL on upload --- .../php/class-media.php | 2 +- .../php/sync/class-upload-sync.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 33c7a7ffd..2bf1da190 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -633,7 +633,7 @@ public function attachment_url( $url, $attachment_id ) { if ( false !== $previous_url ) { return substr( $url, $previous_url ); } - if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize ) { + if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize && ! apply_filters( 'cloudinary_doing_upload', '__return_false' ) ) { if ( $this->cloudinary_id( $attachment_id ) ) { $url = $this->cloudinary_url( $attachment_id ); } 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 c630ac133..de5ecc197 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 @@ -217,6 +217,8 @@ public function upload_asset( $attachment_id ) { // Run the upload Call. $result = $this->connect->api->upload( $attachment_id, $options ); + remove_filter( 'cloudinary_doing_upload', '__return_true' ); + if ( ! is_wp_error( $result ) ) { // Check that this wasn't an existing. From c0db1b94a5e9e16d033f81d171a78bf9429f1311 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 00:50:58 +0100 Subject: [PATCH 5/6] When forcing a file upload override the is folder synced flag --- .../php/class-media.php | 16 +++++++++++++--- .../php/sync/class-upload-sync.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 2bf1da190..25f33d15f 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -1678,12 +1678,22 @@ public function get_context_options( $attachment_id ) { */ public function is_folder_synced( $attachment_id ) { - $return = true; // By default all assets in WordPress will be synced. + $is_folder_synced = true; // By default all assets in WordPress will be synced. if ( $this->sync->been_synced( $attachment_id ) ) { - $return = ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ) ); + $is_folder_synced = ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ) ); } - return $return; + /** + * Filter is folder synced flag. + * + * @param bool $is_folder_synced Flag value for is folder sync. + * @param int $attachment_id The attachment ID. + * + * @return bool + */ + $is_folder_synced = apply_filters( 'cloudinary_is_folder_synced', $is_folder_synced, $attachment_id ); + + return (bool) $is_folder_synced; } /** 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 de5ecc197..ebf8def2d 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 @@ -201,6 +201,21 @@ public function setup() { */ public function upload_asset( $attachment_id ) { + add_filter( 'cloudinary_doing_upload', '__return_true' ); + + add_filter( + 'cloudinary_is_folder_synced', + function( $is_synced, $post_id ) use ( $attachment_id ) { + if ( $post_id === $attachment_id ) { + return true; + } + + return $is_synced; + }, + 10, + 2 + ); + $type = $this->sync->get_sync_type( $attachment_id ); $options = $this->media->get_upload_options( $attachment_id ); $public_id = $options['public_id']; From 4010e37d5b9dbc17e27152a8c1f782fb9beb8c0b Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 25 Sep 2020 18:45:53 +0100 Subject: [PATCH 6/6] Add docblock and fix the default value --- .../php/class-media.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index 25f33d15f..b85ae2af0 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -633,7 +633,19 @@ public function attachment_url( $url, $attachment_id ) { if ( false !== $previous_url ) { return substr( $url, $previous_url ); } - if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize && ! apply_filters( 'cloudinary_doing_upload', '__return_false' ) ) { + if ( + ! doing_action( 'wp_insert_post_data' ) + && false === $this->in_downsize + /** + * Filter doing upload. + * If so, return the default attachment URL. + * + * @param bool Default false. + * + * @return bool + */ + && ! apply_filters( 'cloudinary_doing_upload', false ) + ) { if ( $this->cloudinary_id( $attachment_id ) ) { $url = $this->cloudinary_url( $attachment_id ); }