From a19b48a4f8835da9ba6fd62ff07aed1b6dec2aad Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Thu, 23 Apr 2020 17:51:55 +0100 Subject: [PATCH 1/4] Add breakpoint flag to convert_url() Description This will allow apply the default transformations when calling the_post_thumbnail() --- .../php/class-media.php | 7 ++++--- 1 file changed, 4 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 f91414f2a..587c3df60 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 @@ -735,14 +735,15 @@ public function filter_downsize( $image, $attachment_id, $size ) { * @param string $url Url to convert. * @param int $attachment_id Attachment ID. * @param array $transformations Optional transformations. + * @param bool $breakpoint Flag url is a breakpoint URL to stop re-applying default transformations. * * @return string Converted URL. */ - public function convert_url( $url, $attachment_id, $transformations = array() ) { + public function convert_url( $url, $attachment_id, $transformations = array(), $breakpoint = true ) { $size = $this->get_crop( $url, $attachment_id ); - return $this->cloudinary_url( $attachment_id, $size, $transformations, null, true ); + return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $breakpoint ); } /** @@ -820,7 +821,7 @@ function ( $item ) use ( $crop ) { // Use current sources, but convert the URLS. foreach ( $sources as &$source ) { if ( ! $this->is_cloudinary_url( $source['url'] ) ) { - $source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations ); + $source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, false ); } } From 2d3bb2778bef117f7424aaf71abb7736ec2a559d Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 24 Apr 2020 09:46:28 +0200 Subject: [PATCH 2/4] Allow initial/primary URL to be changed on request. --- .../php/class-media.php | 2 +- 1 file changed, 1 insertion(+), 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 587c3df60..d87ef8527 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 @@ -463,7 +463,7 @@ public function get_transformations_from_string( $str, $type = 'image' ) { * @return string Cloudinary URL. */ public function attachment_url( $url, $attachment_id ) { - if ( ! doing_action( 'wp_insert_post_data' ) && wp_attachment_is( 'video', $attachment_id ) ) { + if ( ! doing_action( 'wp_insert_post_data' ) ) { $cloudinary_id = $this->cloudinary_id( $attachment_id ); if ( false !== $cloudinary_id ) { $url = $this->cloudinary_url( $attachment_id, $cloudinary_id ); From 2fc172db7cc73f99e88462f9fb86c8dca0a6b17b Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 24 Apr 2020 09:48:31 +0200 Subject: [PATCH 3/4] correctly name to --- .../php/class-media.php | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 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 d87ef8527..5d5d1366b 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 @@ -542,16 +542,16 @@ public function apply_default_transformations( array $transformations, $type = ' /** * Generate a Cloudinary URL based on attachment ID and required size. * - * @param int $attachment_id The id of the attachment. - * @param array|string $size The wp size to set for the URL. - * @param array $transformations Set of transformations to apply to this url. - * @param string $cloudinary_id Optional forced cloudinary ID. - * @param bool $breakpoint Flag url is a breakpoint URL to stop re-applying default transformations. - * @param bool $clean Flag to present a clean url (With out a WP size variable. + * @param int $attachment_id The id of the attachment. + * @param array|string $size The wp size to set for the URL. + * @param array $transformations Set of transformations to apply to this url. + * @param string $cloudinary_id Optional forced cloudinary ID. + * @param bool $overwrite_transformations Flag url is a breakpoint URL to stop re-applying default transformations. + * @param bool $clean Flag to present a clean url (With out a WP size variable. * * @return string The converted URL. */ - public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $breakpoint = false, $clean = false ) { + public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false, $clean = false ) { if ( empty( $cloudinary_id ) ) { $cloudinary_id = $this->cloudinary_id( $attachment_id ); @@ -587,7 +587,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation */ $pre_args['transformation'] = apply_filters( 'cloudinary_transformations', $transformations, $attachment_id ); // Defaults are only to be added on front, main images ( not breakpoints, since these are adapted down), and videos. - if ( ( ! defined( 'REST_REQUEST' ) || false === REST_REQUEST ) && ! is_admin() && false === $breakpoint ) { + if ( ( ! defined( 'REST_REQUEST' ) || false === REST_REQUEST ) && ! is_admin() && false === $overwrite_transformations ) { $pre_args['transformation'] = $this->apply_default_transformations( $pre_args['transformation'], $resource_type ); } @@ -732,18 +732,21 @@ public function filter_downsize( $image, $attachment_id, $size ) { /** * Convert an attachment URL to a Cloudinary one. * - * @param string $url Url to convert. - * @param int $attachment_id Attachment ID. - * @param array $transformations Optional transformations. - * @param bool $breakpoint Flag url is a breakpoint URL to stop re-applying default transformations. + * @param string $url Url to convert. + * @param int $attachment_id Attachment ID. + * @param array $transformations Optional transformations. + * @param bool $overwrite_transformations Flag url as having an overwrite transformation. * * @return string Converted URL. */ - public function convert_url( $url, $attachment_id, $transformations = array(), $breakpoint = true ) { + public function convert_url( $url, $attachment_id, $transformations = array(), $overwrite_transformations = true ) { + if ( $this->is_cloudinary_url( $url ) ) { + return $url; // Already is a cloudinary URL, just return. + } $size = $this->get_crop( $url, $attachment_id ); - return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $breakpoint ); + return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $overwrite_transformations, true ); } /** @@ -821,7 +824,7 @@ function ( $item ) use ( $crop ) { // Use current sources, but convert the URLS. foreach ( $sources as &$source ) { if ( ! $this->is_cloudinary_url( $source['url'] ) ) { - $source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, false ); + $source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, true ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL. } } From 3d5fc9f03938fbc1413f050bc25f62f35b8d637f Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 24 Apr 2020 12:26:41 +0100 Subject: [PATCH 4/4] Bypass updating the attachment URL on admin --- .../php/class-media.php | 2 +- 1 file changed, 1 insertion(+), 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 5d5d1366b..9aa9299ae 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 @@ -463,7 +463,7 @@ public function get_transformations_from_string( $str, $type = 'image' ) { * @return string Cloudinary URL. */ public function attachment_url( $url, $attachment_id ) { - if ( ! doing_action( 'wp_insert_post_data' ) ) { + if ( ! doing_action( 'wp_insert_post_data' ) && ! is_admin() ) { $cloudinary_id = $this->cloudinary_id( $attachment_id ); if ( false !== $cloudinary_id ) { $url = $this->cloudinary_url( $attachment_id, $cloudinary_id );