From f4d837e20222546c6eb5ef1b76ae594b14daf921 Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Mon, 18 Nov 2019 12:39:02 -0800 Subject: [PATCH 1/6] WP.com Block Editor: Enqueue missing common styles on the front-end. --- .../class-jetpack-wpcom-block-editor.php | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 893eee713a4ea..2cbaa53c585ef 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -42,6 +42,8 @@ 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_editor_assets', array( $this, 'enqueue_styles' ), 9 ); + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) ); } @@ -297,16 +299,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' @@ -333,6 +325,24 @@ public function enqueue_scripts() { } } + /** + * Enqueue WP.com block editor common styles. + */ + public function enqueue_styles() { + $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 + ); + } + /** * Register the Tiny MCE plugins for the WordPress.com block editor integration. * From 56c6b845c97a9280c98e536969b2d4e3c327b1ab Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Mon, 18 Nov 2019 12:51:07 -0800 Subject: [PATCH 2/6] Use `enqueue_block_assets` instead of two separate hooks. --- .../wpcom-block-editor/class-jetpack-wpcom-block-editor.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 2cbaa53c585ef..66501ba7e7021 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -42,8 +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_editor_assets', array( $this, 'enqueue_styles' ), 9 ); - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); + add_action( 'enqueue_block_assets', array( $this, 'enqueue_styles' ) ); add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_plugins' ) ); } From 89f540d1507378b05baac79d33730f202b75532c Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Tue, 19 Nov 2019 16:51:41 -0800 Subject: [PATCH 3/6] Only enqueue styles in the editor, and on the front-end when the `$post` object has paragraph blocks (the only blocks that have the Justify button). --- .../wpcom-block-editor/class-jetpack-wpcom-block-editor.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 66501ba7e7021..3013271343c59 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -328,6 +328,10 @@ public function enqueue_scripts() { * Enqueue WP.com block editor common styles. */ public function enqueue_styles() { + if ( ! WP_Screen::is_block_editor() && ! has_block( 'core/paragraph' ) ) { + return; + } + $debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; $version = gmdate( 'Ymd' ); From 5c6e710b01988c52292fcb7fc0ef58a01247c0d4 Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Tue, 19 Nov 2019 17:28:20 -0800 Subject: [PATCH 4/6] Correct paragraph block identifier. --- modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 3013271343c59..8b65970acd46a 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -328,7 +328,7 @@ public function enqueue_scripts() { * Enqueue WP.com block editor common styles. */ public function enqueue_styles() { - if ( ! WP_Screen::is_block_editor() && ! has_block( 'core/paragraph' ) ) { + if ( ! WP_Screen::is_block_editor() && ! has_block( 'paragraph' ) ) { return; } From d3526d793b0784a075bf05bed77f1c841278793a Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Tue, 19 Nov 2019 17:34:15 -0800 Subject: [PATCH 5/6] Avoid fatal errors for unfound `WP_Screen`. --- .../wpcom-block-editor/class-jetpack-wpcom-block-editor.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 8b65970acd46a..84d86e545ee1c 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -328,7 +328,8 @@ public function enqueue_scripts() { * Enqueue WP.com block editor common styles. */ public function enqueue_styles() { - if ( ! WP_Screen::is_block_editor() && ! has_block( 'paragraph' ) ) { + $screen = get_current_screen(); + if ( ! $screen->is_block_editor() && ! has_block( 'paragraph' ) ) { return; } From f25842e70eb6918955b3608e3f98ba694c396867 Mon Sep 17 00:00:00 2001 From: Kirk Wight Date: Thu, 21 Nov 2019 13:44:04 -0800 Subject: [PATCH 6/6] Enqueue only if in the block editor, or on the front-end with justified paragraphs in the content. --- .../class-jetpack-wpcom-block-editor.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php index 84d86e545ee1c..b40dd8376d3d7 100644 --- a/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php +++ b/modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php @@ -328,8 +328,14 @@ public function enqueue_scripts() { * Enqueue WP.com block editor common styles. */ public function enqueue_styles() { - $screen = get_current_screen(); - if ( ! $screen->is_block_editor() && ! has_block( 'paragraph' ) ) { + // 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; } @@ -347,6 +353,24 @@ public function enqueue_styles() { ); } + /** + * 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, '