Skip to content

Commit

Permalink
Photon: do not use Photon on private simple sites (#36876)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jeherve authored Apr 16, 2024
1 parent 7948dc9 commit efc8613
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

WordPress.com: do not Photonize images on private WordPress.com sites.
2 changes: 1 addition & 1 deletion projects/packages/image-cdn/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
14 changes: 13 additions & 1 deletion projects/packages/image-cdn/src/class-image-cdn-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/image-cdn/src/class-image-cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
final class Image_CDN {

const PACKAGE_VERSION = '0.3.4';
const PACKAGE_VERSION = '0.3.5-alpha';

/**
* Singleton.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Tiled Gallery Block: do not use image CDN on private WordPress.com Simple sites.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit efc8613

Please sign in to comment.