-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
f725afa
commit f5b9906
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/mu-wpcom-override-preview-button-url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
MU WPCOM: Port override-preview-button-url feature from ETK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
.../jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
57 changes: 57 additions & 0 deletions
57
...jetpack-mu-wpcom/src/features/override-preview-button-url/override-preview-button-url.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Apply the new font-smoothing styles. | ||
* | ||
* @package automattic/jetpack-mu-wpcom | ||
*/ | ||
|
||
use Automattic\Jetpack\Jetpack_Mu_Wpcom; | ||
|
||
// Turn off the feature on ETK plugin. | ||
define( 'MU_WPCOM_OVERRIDE_PREVIEW_BUTTON_URL', true ); | ||
|
||
/** | ||
* Check if the feature should be loaded. | ||
* | ||
* @return bool | ||
*/ | ||
function should_load_override_preview_button_url() { | ||
if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) { | ||
return false; | ||
} | ||
|
||
$host = new Automattic\Jetpack\Status\Host(); | ||
if ( ! $host->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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters