From 85d6392c9b2d107a90d78dcf57b168cb395d45ac Mon Sep 17 00:00:00 2001 From: David Cramer Date: Wed, 23 Sep 2020 14:19:10 +0200 Subject: [PATCH 1/2] set header to file if cloud_name sync type. --- .../php/sync/class-upload-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-upload-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php index fbacfc049..69f885be4 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,9 +201,10 @@ public function setup() { */ public function upload_asset( $attachment_id ) { - $type = $this->sync->get_sync_type( $attachment_id ); - $options = $this->media->get_upload_options( $attachment_id ); - $public_id = $options['public_id']; + $type = $this->sync->get_sync_type( $attachment_id ); + $options = $this->media->get_upload_options( $attachment_id ); + $public_id = $options['public_id']; + $try_remote = 'cloud_name' === $type ? 'file' : false; // Add the suffix before uploading. if ( $this->media->get_public_id( $attachment_id ) === $public_id ) { @@ -215,7 +216,7 @@ public function upload_asset( $attachment_id ) { } // Run the upload Call. - $result = $this->connect->api->upload( $attachment_id, $options ); + $result = $this->connect->api->upload( $attachment_id, $options, $try_remote ); if ( ! is_wp_error( $result ) ) { From 731a0300d4abaa94bfa4c905180ddb915f206f3f Mon Sep 17 00:00:00 2001 From: David Cramer Date: Mon, 28 Sep 2020 13:46:54 +0200 Subject: [PATCH 2/2] add try_remote param --- .../php/connect/class-api.php | 15 ++++++++------- .../php/sync/class-upload-sync.php | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) 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 2bee4bed6..98bb49417 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 @@ -141,7 +141,7 @@ class Api { * @param string The plugin version. */ public function __construct( $connect, $version ) { - $this->credentials = $connect->get_credentials(); + $this->credentials = $connect->get_credentials(); $this->plugin_version = $version; // Use CNAME. if ( ! empty( $this->credentials['cname'] ) ) { @@ -231,9 +231,9 @@ function ( $item ) use ( $transformation_index ) { * Generate a Cloudinary URL. * * @param string|null $public_id The Public ID to get a url for. - * @param array $args Additional args. - * @param array $size The WP Size array. - * @param bool $clean Flag to produce a non variable size url. + * @param array $args Additional args. + * @param array $size The WP Size array. + * @param bool $clean Flag to produce a non variable size url. * * @return string */ @@ -408,10 +408,11 @@ public function upload_large( $attachment_id, $args ) { * @param int $attachment_id Attachment ID to upload. * @param array $args Array of upload options. * @param array $headers Additional headers to use in upload. + * @param bool $try_remote Flag to try_remote upload. * * @return array|\WP_Error */ - public function upload( $attachment_id, $args, $headers = array() ) { + public function upload( $attachment_id, $args, $headers = array(), $try_remote = true ) { $resource = ! empty( $args['resource_type'] ) ? $args['resource_type'] : 'image'; $resource = $this->convert_resource_type( $resource ); @@ -425,7 +426,7 @@ public function upload( $attachment_id, $args, $headers = array() ) { $disable_https_fetch = false; } // Check if we can try http file upload. - if ( empty( $headers ) && empty( $disable_https_fetch ) ) { + if ( empty( $headers ) && empty( $disable_https_fetch ) && true === $try_remote ) { $args['file'] = $file_url; } else { // We should have the file in args at this point, but if the transient was set, it will be defaulting here. @@ -466,7 +467,7 @@ public function upload( $attachment_id, $args, $headers = array() ) { // Hook in flag to allow for non accessible URLS. if ( is_wp_error( $result ) ) { $error = $result->get_error_message(); - $code = $result->get_error_code(); + $code = $result->get_error_code(); /** * If there's an error and the file is a URL in the error message, * it's likely due to CURL or the location does not support URL file attachments. 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 69f885be4..5c6b2ee47 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 @@ -204,7 +204,7 @@ public function upload_asset( $attachment_id ) { $type = $this->sync->get_sync_type( $attachment_id ); $options = $this->media->get_upload_options( $attachment_id ); $public_id = $options['public_id']; - $try_remote = 'cloud_name' === $type ? 'file' : false; + $try_remote = 'cloud_name' === $type ? false : true; // Add the suffix before uploading. if ( $this->media->get_public_id( $attachment_id ) === $public_id ) { @@ -216,7 +216,7 @@ public function upload_asset( $attachment_id ) { } // Run the upload Call. - $result = $this->connect->api->upload( $attachment_id, $options, $try_remote ); + $result = $this->connect->api->upload( $attachment_id, $options, array(), $try_remote ); if ( ! is_wp_error( $result ) ) {