From 7feaff3397b87922fbea083a6f411bbad93e914e Mon Sep 17 00:00:00 2001 From: Nichita Date: Mon, 9 Dec 2024 14:18:33 +0200 Subject: [PATCH 1/2] Replaces the CDN domain in the image URL with the server's domain --- editor/crop-cache-media.php | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/editor/crop-cache-media.php b/editor/crop-cache-media.php index e4bc9454a..3c6dadb5c 100644 --- a/editor/crop-cache-media.php +++ b/editor/crop-cache-media.php @@ -235,8 +235,33 @@ private function getImgUrlByWpSize( $uid, $size ) { $imgUrl = $this->getImgUrlByWpSize( $uid, 'full' ); } - return $imgUrl; - } + return $this->replaceCdnUrl($imgUrl); + } + + /** + * Replaces the CDN domain in the image URL with the server's domain. + * + * @param string $imgUrl The original image URL. + * @return string The updated image URL with the server domain and without query parameters. + */ + private function replaceCdnUrl( + string $imgUrl + ): string { + $server_domain = site_url(); + + $parsed_url = parse_url($imgUrl); + $img_domain = $parsed_url['scheme'].'://'.$parsed_url['host']; + + $path = $parsed_url['path'] ?? ''; + + if ($img_domain !== $server_domain) { + $imgUrl = str_replace($img_domain, $server_domain, $img_domain.$path); + } else { + $imgUrl = $img_domain.$path; + } + + return $imgUrl; + } /** * @throws Exception @@ -259,4 +284,4 @@ public function basename( $originalPath ) { public function buildPath( ...$parts ) { return implode( '/', $parts ); } -} \ No newline at end of file +} From 662b4d2435a47f90c01221c88c9a8c9ab7d1bbc5 Mon Sep 17 00:00:00 2001 From: Nichita Date: Mon, 9 Dec 2024 15:11:02 +0200 Subject: [PATCH 2/2] Refactor `replaceCdnUrl` for PHP 5.6 compatibility --- editor/crop-cache-media.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/editor/crop-cache-media.php b/editor/crop-cache-media.php index 3c6dadb5c..ae920721a 100644 --- a/editor/crop-cache-media.php +++ b/editor/crop-cache-media.php @@ -244,15 +244,13 @@ private function getImgUrlByWpSize( $uid, $size ) { * @param string $imgUrl The original image URL. * @return string The updated image URL with the server domain and without query parameters. */ - private function replaceCdnUrl( - string $imgUrl - ): string { + private function replaceCdnUrl($imgUrl) { $server_domain = site_url(); $parsed_url = parse_url($imgUrl); $img_domain = $parsed_url['scheme'].'://'.$parsed_url['host']; - $path = $parsed_url['path'] ?? ''; + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; if ($img_domain !== $server_domain) { $imgUrl = str_replace($img_domain, $server_domain, $img_domain.$path);