diff --git a/php/class-media.php b/php/class-media.php index 0b9296254..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,7 +861,7 @@ 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() ); + $key = $this->get_cache_key( func_get_args() ); if ( isset( $cache[ $key ] ) ) { return $cache[ $key ]; } @@ -977,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. * @@ -997,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-lazy-load.php b/php/delivery/class-lazy-load.php index 9238ed52a..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 ); + $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 ); 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 );