@@ -495,6 +495,7 @@ public function get_crop( $url, $attachment_id ) {
495495 // Make the WP Size array.
496496 $ wp_size = array (
497497 'wpsize ' => $ size_name ,
498+ 'file ' => $ size ['file ' ],
498499 'width ' => $ size ['width ' ],
499500 'height ' => $ size ['height ' ],
500501 'crop ' => $ cropped ? 'fill ' : 'scale ' ,
@@ -604,9 +605,6 @@ public function get_transformations_from_string( $str, $type = 'image' ) {
604605 $ transformation_chains = explode ( '/ ' , $ str );
605606 $ transformations = array ();
606607 foreach ( $ transformation_chains as $ index => $ chain ) {
607- if ( false !== strpos ( $ chain , 'wpsize ' ) ) {
608- continue ; // A wpsize is not a transformation.
609- }
610608 $ items = explode ( ', ' , $ chain );
611609 foreach ( $ items as $ item ) {
612610 $ item = trim ( $ item );
@@ -724,7 +722,7 @@ public function apply_default_transformations( array $transformations, $type = '
724722 *
725723 * @return string The converted URL.
726724 */
727- public function cloudinary_url ( $ attachment_id , $ size = array (), $ transformations = array (), $ cloudinary_id = null , $ overwrite_transformations = false , $ clean = false ) {
725+ public function cloudinary_url ( $ attachment_id , $ size = array (), $ transformations = array (), $ cloudinary_id = null , $ overwrite_transformations = false ) {
728726
729727 if ( ! ( $ cloudinary_id ) ) {
730728 $ cloudinary_id = $ this ->cloudinary_id ( $ attachment_id );
@@ -744,13 +742,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
744742 'resource_type ' => $ resource_type ,
745743 );
746744
747- // Check size and correct if string or size.
748- if ( is_string ( $ size ) || ( is_array ( $ size ) && 3 === count ( $ size ) ) ) {
749- $ intermediate = image_get_intermediate_size ( $ attachment_id , $ size );
750- if ( is_array ( $ intermediate ) ) {
751- $ size = $ this ->get_crop ( $ intermediate ['url ' ], $ attachment_id );
752- }
753- }
745+ $ size = $ this ->prepare_size ( $ attachment_id , $ size );
754746 if ( false === $ overwrite_transformations ) {
755747 $ overwrite_transformations = $ this ->maybe_overwrite_featured_image ( $ attachment_id );
756748 }
@@ -789,6 +781,44 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
789781 return apply_filters ( 'cloudinary_converted_url ' , $ url , $ attachment_id , $ pre_args );
790782 }
791783
784+ /**
785+ * Prepare the Size array for the Cloudinary URL API.
786+ *
787+ * @param int $attachment_id The attachment ID.
788+ * @param array|string $size The size array or slug.
789+ *
790+ * @return array|string
791+ */
792+ public function prepare_size ( $ attachment_id , $ size ) {
793+ // Check size and correct if string or size.
794+ if ( empty ( $ size ) || 'full ' === $ size ) {
795+ // Maybe get full size if scaled.
796+ $ meta = wp_get_attachment_metadata ( $ attachment_id , true );
797+ if ( ! empty ( $ meta ['original_image ' ] ) ) {
798+ $ size = array (
799+ 'width ' => $ meta ['width ' ],
800+ 'height ' => $ meta ['height ' ],
801+ 'full ' => true ,
802+ );
803+ }
804+ } elseif ( is_string ( $ size ) || ( is_array ( $ size ) && 3 === count ( $ size ) ) ) {
805+ $ intermediate = image_get_intermediate_size ( $ attachment_id , $ size );
806+ if ( is_array ( $ intermediate ) ) {
807+ $ size = $ this ->get_crop ( $ intermediate ['url ' ], $ attachment_id );
808+ }
809+ } elseif ( array_keys ( $ size ) === array ( 0 , 1 ) ) {
810+ $ size = array (
811+ 'width ' => $ size [0 ],
812+ 'height ' => $ size [1 ],
813+ );
814+ if ( $ size ['width ' ] === $ size ['height ' ] ) {
815+ $ size ['crop ' ] = 'fill ' ;
816+ }
817+ }
818+
819+ return $ size ;
820+ }
821+
792822 /**
793823 * Add domain to subdir.
794824 *
@@ -998,7 +1028,7 @@ public function convert_url( $url, $attachment_id, $transformations = array(), $
9981028 }
9991029 $ size = $ this ->get_crop ( $ url , $ attachment_id );
10001030
1001- return $ this ->cloudinary_url ( $ attachment_id , $ size , $ transformations , null , $ overwrite_transformations, true );
1031+ return $ this ->cloudinary_url ( $ attachment_id , $ size , $ transformations , null , $ overwrite_transformations );
10021032 }
10031033
10041034 /**
@@ -1017,8 +1047,8 @@ public function image_srcset( $sources, $size_array, $image_src, $image_meta, $a
10171047 if ( ! $ cloudinary_id ) {
10181048 return $ sources ; // Return WordPress default sources.
10191049 }
1020- // Get transformations from URL .
1021- $ transformations = $ this ->get_transformations_from_string ( $ image_src );
1050+ // Get transformations if any .
1051+ $ transformations = $ this ->get_post_meta ( $ attachment_id , Sync:: META_KEYS [ ' transformation ' ], true );
10221052 // Use Cloudinary breakpoints for same ratio.
10231053
10241054 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 ] ) ) {
@@ -1050,7 +1080,7 @@ function ( $item ) use ( $crop ) {
10501080 'width ' => $ breakpoint ['width ' ],
10511081 );
10521082 $ sources [ $ breakpoint ['width ' ] ] = array (
1053- 'url ' => $ this ->cloudinary_url ( $ attachment_id , $ size , $ transformations , $ cloudinary_id , true ),
1083+ 'url ' => $ this ->cloudinary_url ( $ attachment_id , $ size , $ transformations , $ cloudinary_id , $ image_meta [ ' overwrite_transformations ' ] ),
10541084 'descriptor ' => 'w ' ,
10551085 'value ' => $ breakpoint ['width ' ],
10561086 );
@@ -1076,35 +1106,13 @@ function ( $item ) use ( $crop ) {
10761106 // Use current sources, but convert the URLS.
10771107 foreach ( $ sources as &$ source ) {
10781108 if ( ! $ this ->is_cloudinary_url ( $ source ['url ' ] ) ) {
1079- $ source ['url ' ] = $ this ->convert_url ( $ source ['url ' ], $ attachment_id , $ transformations , true ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
1109+ $ source ['url ' ] = $ this ->convert_url ( $ source ['url ' ], $ attachment_id , $ transformations , $ image_meta [ ' overwrite_transformations ' ] ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
10801110 }
10811111 }
10821112
10831113 return $ sources ;
10841114 }
10851115
1086- /**
1087- * Alter the image sizes metadata to match the Cloudinary ID so that WordPress can detect a matched source for responsive breakpoints.
1088- *
1089- * @param array $image_meta The image metadata array.
1090- * @param array $size_array The size array.
1091- * @param string $image_src The image src.
1092- * @param int $attachment_id The attachment ID.
1093- *
1094- * @return array
1095- */
1096- public function match_responsive_sources ( $ image_meta , $ size_array , $ image_src , $ attachment_id ) {
1097- if ( wp_attachment_is_image ( $ attachment_id ) && ! empty ( $ image_meta ['sizes ' ] ) ) {
1098- $ cloudinary_id = $ this ->cloudinary_id ( $ attachment_id );
1099- if ( $ cloudinary_id ) {
1100- // Set the file to the Cloudinary ID so that it will be matched.
1101- $ image_meta ['file ' ] = $ cloudinary_id ;
1102- }
1103- }
1104-
1105- return $ image_meta ;
1106- }
1107-
11081116 /**
11091117 * Check if a url is a cloudinary url or not.
11101118 *
@@ -1789,8 +1797,6 @@ public function setup() {
17891797
17901798 // Filter live URLS. (functions that return a URL).
17911799 add_filter ( 'wp_calculate_image_srcset ' , array ( $ this , 'image_srcset ' ), 10 , 5 );
1792- add_filter ( 'wp_calculate_image_srcset_meta ' , array ( $ this , 'match_responsive_sources ' ), 10 , 4 );
1793- add_filter ( 'wp_get_attachment_metadata ' , array ( $ this , 'match_file_name_with_cloudinary_source ' ), 10 , 2 );
17941800 add_filter ( 'wp_get_attachment_url ' , array ( $ this , 'attachment_url ' ), 10 , 2 );
17951801 add_filter ( 'image_downsize ' , array ( $ this , 'filter_downsize ' ), 10 , 3 );
17961802
@@ -1802,23 +1808,4 @@ public function setup() {
18021808 add_action ( 'begin_fetch_post_thumbnail_html ' , array ( $ this , 'set_doing_featured ' ), 10 , 2 );
18031809 }
18041810 }
1805-
1806- /**
1807- * Ensure the file in image meta is the same as the Cloudinary ID.
1808- *
1809- * @param array $image_meta Meta information of the attachment.
1810- * @param int $attachment_id The attachment ID.
1811- *
1812- * @return array
1813- */
1814- public function match_file_name_with_cloudinary_source ( $ image_meta , $ attachment_id ) {
1815- if ( $ this ->has_public_id ( $ attachment_id ) ) {
1816- $ cld_file = 'v ' . $ this ->get_cloudinary_version ( $ attachment_id ) . '/ ' . $ this ->get_cloudinary_id ( $ attachment_id );
1817- if ( false === strpos ( $ image_meta ['file ' ], $ cld_file ) ) {
1818- $ image_meta ['file ' ] = $ cld_file ;
1819- }
1820- }
1821-
1822- return $ image_meta ;
1823- }
18241811}
0 commit comments