From 4760e32996e3f3762419aaaae396cbdae58b05c2 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 6 Aug 2021 11:12:42 +0200 Subject: [PATCH 1/3] add is taxonomy overwrite key and use overwrite from tag element in url. --- php/class-media.php | 5 ++++- php/delivery/class-lazy-load.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 0b9296254..65d4c1959 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -861,7 +861,10 @@ public function attachment_url( $url, $attachment_id ) { public function apply_default_transformations( array $transformations, $attachment_id ) { static $cache = array(), $freeform = array(); - $key = wp_json_encode( func_get_args() ); + $taxonomy_overwrite = $this->global_transformations->is_taxonomy_overwrite(); + $args = func_get_args(); + $args[] = $taxonomy_overwrite; + $key = wp_json_encode( $args ); if ( isset( $cache[ $key ] ) ) { return $cache[ $key ]; } diff --git a/php/delivery/class-lazy-load.php b/php/delivery/class-lazy-load.php index 9238ed52a..ccd262495 100644 --- a/php/delivery/class-lazy-load.php +++ b/php/delivery/class-lazy-load.php @@ -125,7 +125,7 @@ public function add_features( $tag_element, $attachment_id, $original_tag ) { } $src = $tag_element['atts']['src']; if ( ! $this->media->is_cloudinary_url( $src ) ) { - $src = $this->media->cloudinary_url( $attachment_id ); + $src = $this->media->cloudinary_url( $attachment_id, array(), array(), array(), null ); } $tag_element['atts']['data-src'] = $src; $transformations = $this->media->get_transformations_from_string( $src ); From 0eb238b50a238c7701b803d826186c6bc8af0136 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 6 Aug 2021 11:16:50 +0200 Subject: [PATCH 2/3] add flag --- php/delivery/class-lazy-load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/delivery/class-lazy-load.php b/php/delivery/class-lazy-load.php index ccd262495..2b058339c 100644 --- a/php/delivery/class-lazy-load.php +++ b/php/delivery/class-lazy-load.php @@ -125,7 +125,7 @@ public function add_features( $tag_element, $attachment_id, $original_tag ) { } $src = $tag_element['atts']['src']; if ( ! $this->media->is_cloudinary_url( $src ) ) { - $src = $this->media->cloudinary_url( $attachment_id, array(), array(), array(), null ); + $src = $this->media->cloudinary_url( $attachment_id, array(), array(), array(), $tag_element['cld-overwrite'] ); } $tag_element['atts']['data-src'] = $src; $transformations = $this->media->get_transformations_from_string( $src ); From c815aa4b6d71af2bb0191cdb4ef6f96b7c51e16e Mon Sep 17 00:00:00 2001 From: David Cramer Date: Fri, 6 Aug 2021 12:09:53 +0200 Subject: [PATCH 3/3] create a tag key method to keep consistant --- php/class-media.php | 22 ++++++++++++++----- php/delivery/class-responsive-breakpoints.php | 2 +- php/media/class-global-transformations.php | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 65d4c1959..2126e91b0 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -710,7 +710,7 @@ public function get_transformation( $transformations, $type ) { public function get_transformations( $attachment_id, $transformations = array(), $overwrite_transformations = false ) { static $cache = array(); - $key = wp_json_encode( func_get_args() ); + $key = $this->get_cache_key( func_get_args() ); if ( isset( $cache[ $key ] ) ) { return $cache[ $key ]; } @@ -861,10 +861,7 @@ public function attachment_url( $url, $attachment_id ) { public function apply_default_transformations( array $transformations, $attachment_id ) { static $cache = array(), $freeform = array(); - $taxonomy_overwrite = $this->global_transformations->is_taxonomy_overwrite(); - $args = func_get_args(); - $args[] = $taxonomy_overwrite; - $key = wp_json_encode( $args ); + $key = $this->get_cache_key( func_get_args() ); if ( isset( $cache[ $key ] ) ) { return $cache[ $key ]; } @@ -980,6 +977,19 @@ public function default_image_freeform_transformations( $default ) { return $default; } + /** + * Get a cache key for static caching. + * + * @param array $args The arguments array to generate a key with. + * + * @return string + */ + protected function get_cache_key( $args ) { + $args[] = $this->global_transformations->get_current_post(); + + return md5( wp_json_encode( $args ) ); + } + /** * Generate a Cloudinary URL based on attachment ID and required size. * @@ -1000,7 +1010,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation return null; } } - $key = wp_json_encode( func_get_args() ); + $key = $this->get_cache_key( func_get_args() ); if ( isset( $cache[ $key ] ) ) { return $cache[ $key ]; } diff --git a/php/delivery/class-responsive-breakpoints.php b/php/delivery/class-responsive-breakpoints.php index 698d05ac2..582cc1cea 100644 --- a/php/delivery/class-responsive-breakpoints.php +++ b/php/delivery/class-responsive-breakpoints.php @@ -57,7 +57,7 @@ protected function setup_hooks() { public function add_features( $tag_element, $attachment_id, $original_tag ) { if ( ! $this->media->is_cloudinary_url( $tag_element['atts']['src'] ) ) { - $tag_element['atts']['src'] = $this->media->cloudinary_url( $attachment_id ); + $tag_element['atts']['src'] = $this->media->cloudinary_url( $attachment_id, array(), array(), array(), $tag_element['cld-overwrite'] ); } $transformations = $this->media->get_transformations_from_string( $tag_element['atts']['src'] ); $original_string = Api::generate_transformation_string( $transformations ); diff --git a/php/media/class-global-transformations.php b/php/media/class-global-transformations.php index 1fb5c395e..233150259 100644 --- a/php/media/class-global-transformations.php +++ b/php/media/class-global-transformations.php @@ -575,14 +575,14 @@ public function save_overwrite_transformations_featured_image( $post_id ) { * * @return WP_Post|null */ - protected function get_current_post() { + public function get_current_post() { /** * Filter the post ID. * * @hook cloudinary_current_post_id * @default null * - * @return {WP_Post|null} + * @return {WP_Post|null} */ $post_id = apply_filters( 'cloudinary_current_post_id', null );