From efc8613faab862957743ec127b72991528735e7f Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Tue, 16 Apr 2024 13:08:45 +0200 Subject: [PATCH] Photon: do not use Photon on private simple sites (#36876) * Tiled Gallery Block: do not use Photon on private simple sites Follow-up to #19201 We should not use Photon when creating Tiled Gallery blocks on private WordPress.com Simple sites (such sites can now use a mapped domain, and not just the ignored *.files.wordpress.com subdomain. * Support private WordPress.com sites in all Photon requests * Simplify check * Revert comment change --- .../fix-tiled-gallery-block-private-simple-sites | 4 ++++ projects/packages/image-cdn/package.json | 2 +- .../image-cdn/src/class-image-cdn-core.php | 14 +++++++++++++- .../packages/image-cdn/src/class-image-cdn.php | 2 +- .../fix-tiled-gallery-block-private-simple-sites | 4 ++++ .../extensions/blocks/tiled-gallery/utils/index.js | 14 +++++++------- 6 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 projects/packages/image-cdn/changelog/fix-tiled-gallery-block-private-simple-sites create mode 100644 projects/plugins/jetpack/changelog/fix-tiled-gallery-block-private-simple-sites diff --git a/projects/packages/image-cdn/changelog/fix-tiled-gallery-block-private-simple-sites b/projects/packages/image-cdn/changelog/fix-tiled-gallery-block-private-simple-sites new file mode 100644 index 0000000000000..19a0ac7dc4b88 --- /dev/null +++ b/projects/packages/image-cdn/changelog/fix-tiled-gallery-block-private-simple-sites @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +WordPress.com: do not Photonize images on private WordPress.com sites. diff --git a/projects/packages/image-cdn/package.json b/projects/packages/image-cdn/package.json index efabdde55fa98..49e2f2a5c48fa 100644 --- a/projects/packages/image-cdn/package.json +++ b/projects/packages/image-cdn/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@automattic/jetpack-image-cdn", - "version": "0.3.4", + "version": "0.3.5-alpha", "description": "Serve images through Jetpack's powerful CDN", "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/image-cdn/#readme", "bugs": { diff --git a/projects/packages/image-cdn/src/class-image-cdn-core.php b/projects/packages/image-cdn/src/class-image-cdn-core.php index 7fe0c7f5c7315..1f1b2a4980ca3 100644 --- a/projects/packages/image-cdn/src/class-image-cdn-core.php +++ b/projects/packages/image-cdn/src/class-image-cdn-core.php @@ -10,6 +10,7 @@ namespace Automattic\Jetpack\Image_CDN; use Automattic\Jetpack\Status; +use Automattic\Jetpack\Status\Host; /** * A static class that provides core Image CDN functionality. @@ -146,17 +147,28 @@ public static function cdn_url( $image_url, $args = array(), $scheme = null ) { } } + $is_wpcom_private_site = false; + if ( ( new Host() )->is_wpcom_platform() && ( new Status() )->is_private_site() ) { + $is_wpcom_private_site = true; + if ( isset( $args['ssl'] ) ) { + // Do not send the ssl argument to prevent caching issues. + unset( $args['ssl'] ); + } + } + /** This filter is documented below. */ $custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $image_url ); $custom_photon_url = esc_url( $custom_photon_url ); // You can't run a Photon URL through Photon again because query strings are stripped. // So if the image is already a Photon URL, append the new arguments to the existing URL. - // Alternately, if it's a *.files.wordpress.com url, then keep the domain as is. + // Alternately, if it's a *.files.wordpress.com url or an image on a private WordPress.com Simple site, + // then keep the domain as is. if ( in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ), true ) || wp_parse_url( $custom_photon_url, PHP_URL_HOST ) === $image_url_parts['host'] || $is_wpcom_image + || $is_wpcom_private_site ) { $photon_url = add_query_arg( $args, $image_url ); return self::cdn_url_scheme( $photon_url, $scheme ); diff --git a/projects/packages/image-cdn/src/class-image-cdn.php b/projects/packages/image-cdn/src/class-image-cdn.php index 4f908364f9a3f..64e028d3f9d0a 100644 --- a/projects/packages/image-cdn/src/class-image-cdn.php +++ b/projects/packages/image-cdn/src/class-image-cdn.php @@ -14,7 +14,7 @@ */ final class Image_CDN { - const PACKAGE_VERSION = '0.3.4'; + const PACKAGE_VERSION = '0.3.5-alpha'; /** * Singleton. diff --git a/projects/plugins/jetpack/changelog/fix-tiled-gallery-block-private-simple-sites b/projects/plugins/jetpack/changelog/fix-tiled-gallery-block-private-simple-sites new file mode 100644 index 0000000000000..dd81082807825 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-tiled-gallery-block-private-simple-sites @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Tiled Gallery Block: do not use image CDN on private WordPress.com Simple sites. diff --git a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js index 821c10f0a82cf..b8cc8eae8c1a6 100644 --- a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js +++ b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js @@ -1,4 +1,8 @@ -import { isAtomicSite, isPrivateSite } from '@automattic/jetpack-shared-extension-utils'; +import { + isAtomicSite, + isPrivateSite, + isSimpleSite, +} from '@automattic/jetpack-shared-extension-utils'; import { isBlobURL } from '@wordpress/blob'; import { range } from 'lodash'; import photon from 'photon'; @@ -47,8 +51,7 @@ export function photonizedImgProps( img, galleryAtts = {} ) { const { height, width } = img; const { layoutStyle } = galleryAtts; - const photonImplementation = - isWpcomFilesUrl( url ) || true === isVIP() ? photonWpcomImage : photon; + const photonImplementation = true === isVIP() || isSimpleSite() ? photonWpcomImage : photon; /** * Build the `src` @@ -113,13 +116,10 @@ function isVIP() { return true; } } -function isWpcomFilesUrl( url ) { - const { host } = new URL( url, window.location.href ); - return /\.files\.wordpress\.com$/.test( host ); -} /** * Apply photon arguments to *.files.wordpress.com images + * or images on mapped domains on private simple sites. * * This function largely duplicates the functionality of the photon.js lib. * This is necessary because we want to serve images from *.files.wordpress.com so that private