From f5b9906bac665c8bc7a24603e44153bb0401af3c Mon Sep 17 00:00:00 2001 From: arthur791004 Date: Fri, 5 Jul 2024 15:00:02 +0900 Subject: [PATCH] MU WPCOM: Port override-preview-button-url feature from ETK (#38196) * MU WPCOM: Port override-preview-button-url feature from ETK * changelog * Load feature only on simple sites --- .../mu-wpcom-override-preview-button-url | 4 ++ .../src/class-jetpack-mu-wpcom.php | 1 + .../override-preview-button-url.js | 33 +++++++++++ .../override-preview-button-url.php | 57 +++++++++++++++++++ .../jetpack-mu-wpcom/webpack.config.js | 2 + 5 files changed, 97 insertions(+) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/mu-wpcom-override-preview-button-url create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.js create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.php diff --git a/projects/packages/jetpack-mu-wpcom/changelog/mu-wpcom-override-preview-button-url b/projects/packages/jetpack-mu-wpcom/changelog/mu-wpcom-override-preview-button-url new file mode 100644 index 0000000000000..ec1c5fef15ce4 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/mu-wpcom-override-preview-button-url @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +MU WPCOM: Port override-preview-button-url feature from ETK diff --git a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php index 744337b089260..3d2c1206df8b7 100644 --- a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php +++ b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php @@ -99,6 +99,7 @@ public static function load_features() { require_once __DIR__ . '/features/import-customizations/import-customizations.php'; require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php'; require_once __DIR__ . '/features/media/heif-support.php'; + require_once __DIR__ . '/features/override-preview-button-url/override-preview-button-url.php'; require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php'; require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php'; require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php'; diff --git a/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.js b/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.js new file mode 100644 index 0000000000000..f8bd8b1aa7849 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.js @@ -0,0 +1,33 @@ +import { use } from '@wordpress/data'; +import { addQueryArgs } from '@wordpress/url'; + +/** + * The gutenberg block editor preview button opens a new window to a simple site's mapped + * domain. + * Adds logmein query param to editor draft post preview url to add WordPress cookies in + * a first party context ( allowing us to avoid third party cookie issues ) + */ +async function overridePreviewButtonUrl() { + use( registry => { + return { + dispatch: store => { + const namespace = store.name ?? store; + const actions = { ...registry.dispatch( namespace ) }; + + if ( namespace === 'core/editor' && actions.__unstableSaveForPreview ) { + const { __unstableSaveForPreview } = actions; + actions.__unstableSaveForPreview = async ( ...args ) => { + const link = await __unstableSaveForPreview( ...args ); + return link.startsWith( window.location.origin ) + ? link + : addQueryArgs( link, { logmein: 'direct' } ); + }; + } + + return actions; + }, + }; + } ); +} + +overridePreviewButtonUrl(); diff --git a/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.php b/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.php new file mode 100644 index 0000000000000..ae3ae7c905f80 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.php @@ -0,0 +1,57 @@ +is_wpcom_simple() ) { + return false; + } + + global $pagenow; + $allowed_pages = array( + 'post.php', + 'post-new.php', + ); + return isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ); +} + +/** + * Enqueue assets + */ +function wpcom_enqueue_override_preview_button_url_assets() { + if ( ! should_load_override_preview_button_url() ) { + return; + } + + $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/override-preview-button-url/override-preview-button-url.asset.php'; + $script_dependencies = $asset_file['dependencies'] ?? array(); + $script_version = $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/override-preview-button-url/override-preview-button-url.js' ); + $script_id = 'wpcom-override-preview-button-url-script'; + + wp_enqueue_script( + $script_id, + plugins_url( 'build/override-preview-button-url/override-preview-button-url.js', Jetpack_Mu_Wpcom::BASE_FILE ), + $script_dependencies, + $script_version, + true + ); +} +add_action( 'admin_enqueue_scripts', 'wpcom_enqueue_override_preview_button_url_assets' ); diff --git a/projects/packages/jetpack-mu-wpcom/webpack.config.js b/projects/packages/jetpack-mu-wpcom/webpack.config.js index 5a57563896f06..90a04cb02d177 100644 --- a/projects/packages/jetpack-mu-wpcom/webpack.config.js +++ b/projects/packages/jetpack-mu-wpcom/webpack.config.js @@ -16,6 +16,8 @@ module.exports = [ 'core-customizer-css-preview': './src/features/custom-css/custom-css/js/core-customizer-css-preview.js', 'customizer-control': './src/features/custom-css/custom-css/css/customizer-control.css', + 'override-preview-button-url': + './src/features/override-preview-button-url/override-preview-button-url.js', }, mode: jetpackConfig.mode, devtool: jetpackConfig.devtool,