From d108b8ccb66cdd3d260baa4cc77dc36300eb2cd1 Mon Sep 17 00:00:00 2001 From: Dukagjin Surdulli Date: Sat, 30 May 2020 16:57:56 -0400 Subject: [PATCH 1/2] Fixes bug where fetched images don't load --- .../php/class-media.php | 2 +- .../php/connect/class-api.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 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 3a697c70f..543e91caa 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 @@ -1021,7 +1021,7 @@ public function down_sync_asset() { if ( $format !== $file_info['extension'] ) { // Format transformation. $this->set_transformation( $transformations, 'fetch_format', $file_info['extension'] ); - $url = $file_info['dirname'] . '/' . $file_info['filename'] . '.' . $format; + $url = $file_info['dirname'] . '/' . $file_info['filename'] . '.' . $file_info['extension']; } // Try to find the Attachment ID in context meta data. $attachment_id = $this->get_id_from_sync_key( $sync_key ); 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 75c0bbb27..b8b1a04a4 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 @@ -240,9 +240,11 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c 'https:/', $this->url( $args['resource_type'], 'upload' ), ); + if ( ! empty( $args['transformation'] ) ) { $url_parts[] = self::generate_transformation_string( $args['transformation'] ); } + // Add size. if ( ! empty( $size ) && is_array( $size ) ) { if ( true === $clean ) { @@ -258,7 +260,15 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c // Clear out empty parts. $url_parts = array_filter( $url_parts ); - return implode( '/', $url_parts ); + $final_url = implode( '/', $url_parts ); + + // Determine if we're dealing with a fetched + // ...or uploaded image and update the URL accordingly. + if ( 1 < substr_count( $final_url, 'http' ) ) { + $final_url = str_replace( '/upload/', '/fetch/', $final_url ); + } + + return $final_url; } /** From 731758c3b9247d5c724fc21485dc8dade269c493 Mon Sep 17 00:00:00 2001 From: Dukagjin Surdulli Date: Sat, 30 May 2020 19:27:58 -0400 Subject: [PATCH 2/2] Implement a cleaner fix --- .../php/connect/class-api.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 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 b8b1a04a4..203df4f00 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 @@ -236,9 +236,13 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c $args['version'] = 'v1'; } + // Determine if we're dealing with a fetched + // ...or uploaded image and update the URL accordingly. + $asset_endpoint = filter_var( $public_id, FILTER_VALIDATE_URL ) ? 'fetch' : 'upload'; + $url_parts = array( 'https:/', - $this->url( $args['resource_type'], 'upload' ), + $this->url( $args['resource_type'], $asset_endpoint ), ); if ( ! empty( $args['transformation'] ) ) { @@ -254,21 +258,13 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c } $url_parts[] = $args['version']; - + $url_parts[] = $public_id; // Clear out empty parts. $url_parts = array_filter( $url_parts ); - $final_url = implode( '/', $url_parts ); - - // Determine if we're dealing with a fetched - // ...or uploaded image and update the URL accordingly. - if ( 1 < substr_count( $final_url, 'http' ) ) { - $final_url = str_replace( '/upload/', '/fetch/', $final_url ); - } - - return $final_url; + return implode( '/', $url_parts ); } /**