From 64ab237e7488de4ca2656951bc66314a05351a9a Mon Sep 17 00:00:00 2001 From: Karen Attfield Date: Wed, 18 Dec 2024 15:28:33 +0000 Subject: [PATCH] Photon: Ensure VIP site are correctly identified within isVIP. (#39941) Co-authored-by: Sergey Mitroshin --- .../jetpack/changelog/fix-photon-on-vip | 4 ++++ .../jetpack/class.jetpack-gutenberg.php | 4 ++++ .../blocks/tiled-gallery/tiled-gallery.php | 3 ++- .../blocks/tiled-gallery/utils/index.js | 21 +++++++++++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/fix-photon-on-vip diff --git a/projects/plugins/jetpack/changelog/fix-photon-on-vip b/projects/plugins/jetpack/changelog/fix-photon-on-vip new file mode 100644 index 0000000000000..5d205154e23f6 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-photon-on-vip @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Photon: Ensure VIP sites are correctly identified, to prevent unnecessary early Photonization of URLs. diff --git a/projects/plugins/jetpack/class.jetpack-gutenberg.php b/projects/plugins/jetpack/class.jetpack-gutenberg.php index ee8d7ddb4e3cc..aede14da9b1e1 100644 --- a/projects/plugins/jetpack/class.jetpack-gutenberg.php +++ b/projects/plugins/jetpack/class.jetpack-gutenberg.php @@ -715,6 +715,7 @@ public static function enqueue_block_editor_assets() { $modules = $module_list_endpoint->get_modules(); } + $jetpack_plan = Jetpack_Plan::get(); $initial_state = array( 'available_blocks' => self::get_availability(), 'blocks_variation' => $blocks_variation, @@ -731,6 +732,9 @@ public static function enqueue_block_editor_assets() { // this is the equivalent of JP initial state siteData.showMyJetpack (class-jetpack-redux-state-helper) // used to determine if we can link to My Jetpack from the block editor 'is_my_jetpack_available' => My_Jetpack_Initializer::should_initialize(), + 'jetpack_plan' => array( + 'data' => $jetpack_plan['product_slug'], + ), /** * Enable the RePublicize UI in the block editor context. * diff --git a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/tiled-gallery.php b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/tiled-gallery.php index 3be4604368cef..33b925fc2f0f5 100644 --- a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/tiled-gallery.php +++ b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/tiled-gallery.php @@ -58,7 +58,8 @@ public static function render( $attr, $content ) { Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); $is_squareish_layout = self::is_squareish_layout( $attr ); - + // For backward compatibility (ensuring Tiled Galleries using now deprecated versions of the block are not affected). + // See isVIP() in utils/index.js. $jetpack_plan = Jetpack_Plan::get(); wp_localize_script( 'jetpack-gallery-settings', 'jetpack_plan', array( 'data' => $jetpack_plan['product_slug'] ) ); 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 9a4e4f2ac3c6e..3de6ec8bdd0df 100644 --- a/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js +++ b/projects/plugins/jetpack/extensions/blocks/tiled-gallery/utils/index.js @@ -4,11 +4,23 @@ import { isSimpleSite, } from '@automattic/jetpack-shared-extension-utils'; import { isBlobURL } from '@wordpress/blob'; +import { select } from '@wordpress/data'; import { range } from 'lodash'; import photon from 'photon'; import isOfflineMode from '../../../shared/is-offline-mode'; import { PHOTON_MAX_RESIZE } from '../constants'; +let jetpackPlanFromState; + +window.addEventListener( 'load', function () { + const hasImageCompare = select( 'core/block-editor' ) + .getBlocks() + .some( block => block.name === 'jetpack/image-compare' ); + if ( hasImageCompare && ! jetpackPlanFromState ) { + jetpackPlanFromState = window?.Jetpack_Editor_Initial_State?.jetpack?.jetpack_plan; + } +} ); + export function isSquareishLayout( layout ) { return [ 'circle', 'square' ].includes( layout ); } @@ -112,9 +124,14 @@ export function photonizedImgProps( img, galleryAtts = {} ) { } function isVIP() { /*global jetpack_plan*/ - if ( typeof jetpack_plan !== 'undefined' && jetpack_plan.data === 'vip' ) { - return true; + // Use `jetpackPlanFromState` if available, otherwise fall back to `jetpack_plan` defined within the render function in tiled-gallery.php. + let jetpackPlan; + if ( typeof jetpackPlanFromState !== 'undefined' ) { + jetpackPlan = jetpackPlanFromState; + } else if ( typeof jetpack_plan !== 'undefined' ) { + jetpackPlan = jetpack_plan; } + return jetpackPlan && jetpackPlan?.data === 'vip'; } /**