Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP.com Block Editor: Enqueue missing common styles on the front-end. #14070

Merged
merged 6 commits into from
Nov 25, 2019
Merged
58 changes: 48 additions & 10 deletions modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private function __construct() {

add_action( 'login_init', array( $this, 'allow_block_editor_login' ), 1 );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ), 9 );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) );
add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) );
}

Expand Down Expand Up @@ -297,16 +298,6 @@ public function enqueue_scripts() {
)
);

$src_styles = $debug
? '//widgets.wp.com/wpcom-block-editor/common.css?minify=false'
: '//widgets.wp.com/wpcom-block-editor/common.min.css';
wp_enqueue_style(
'wpcom-block-editor-styles',
$src_styles,
array(),
$version
);

if ( $this->is_iframed_block_editor() ) {
$src_calypso_iframe_bridge = $debug
? '//widgets.wp.com/wpcom-block-editor/calypso-iframe-bridge-server.js?minify=false'
Expand All @@ -333,6 +324,53 @@ public function enqueue_scripts() {
}
}

/**
* Enqueue WP.com block editor common styles.
*/
public function enqueue_styles() {
// Enqueue only for the block editor in WP Admin.
global $pagenow;
if ( is_admin() && ! in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) {
return;
}

// Enqueue on the front-end only if justified blocks are present.
if ( ! is_admin() && ! $this->has_justified_block() ) {
return;
}

$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$version = gmdate( 'Ymd' );

$src_styles = $debug
? '//widgets.wp.com/wpcom-block-editor/common.css?minify=false'
: '//widgets.wp.com/wpcom-block-editor/common.min.css';
wp_enqueue_style(
'wpcom-block-editor-styles',
$src_styles,
array(),
$version
);
}

/**
* Determines if the current $post contains a justified paragraph block.
*
* @return boolean true if justified paragraph is found, false otherwise.
*/
public function has_justified_block() {
global $post;
if ( ! $post instanceof WP_Post ) {
return false;
};

if ( ! has_blocks( $post ) ) {
return false;
}

return false !== strpos( $post->post_content, '<!-- wp:paragraph {"align":"justify"' );
}

/**
* Register the Tiny MCE plugins for the WordPress.com block editor integration.
*
Expand Down