From 0aef486ab1c643abbda467509566b76d62829d11 Mon Sep 17 00:00:00 2001 From: Copons Date: Wed, 1 May 2019 11:58:19 +0100 Subject: [PATCH 1/3] Only enqueue blocks scripts when editing wp_template posts --- .../blocks/content-slot/index.js | 34 +++++++--------- .../blocks/template-part/index.js | 40 ++++++++----------- .../full-site-editing-plugin.php | 10 +++++ apps/full-site-editing/package.json | 1 - 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/apps/full-site-editing/full-site-editing-plugin/blocks/content-slot/index.js b/apps/full-site-editing/full-site-editing-plugin/blocks/content-slot/index.js index 366051428f3ae..4c4e50cacb37f 100644 --- a/apps/full-site-editing/full-site-editing-plugin/blocks/content-slot/index.js +++ b/apps/full-site-editing/full-site-editing-plugin/blocks/content-slot/index.js @@ -2,8 +2,6 @@ * WordPress dependencies */ import { registerBlockType } from '@wordpress/blocks'; -import { select } from '@wordpress/data'; -import domReady from '@wordpress/dom-ready'; import { __ } from '@wordpress/i18n'; /** @@ -12,22 +10,18 @@ import { __ } from '@wordpress/i18n'; import edit from './edit'; import './style.scss'; -domReady( () => { - if ( 'wp_template' === select( 'core/editor' ).getCurrentPostType() ) { - registerBlockType( 'a8c/content-slot', { - title: __( 'Content Slot' ), - description: __( 'Placeholder for a post or a page.' ), - icon: 'layout', - category: 'layout', - supports: { - align: [ 'wide', 'full' ], - anchor: true, - html: false, - multiple: false, - reusable: false, - }, - edit, - save: () => null, - } ); - } +registerBlockType( 'a8c/content-slot', { + title: __( 'Content Slot' ), + description: __( 'Placeholder for a post or a page.' ), + icon: 'layout', + category: 'layout', + supports: { + align: [ 'wide', 'full' ], + anchor: true, + html: false, + multiple: false, + reusable: false, + }, + edit, + save: () => null, } ); diff --git a/apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.js b/apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.js index 0ebbef25b7963..488aea091c671 100644 --- a/apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.js +++ b/apps/full-site-editing/full-site-editing-plugin/blocks/template-part/index.js @@ -2,8 +2,6 @@ * WordPress dependencies */ import { registerBlockType } from '@wordpress/blocks'; -import { select } from '@wordpress/data'; -import domReady from '@wordpress/dom-ready'; import { __ } from '@wordpress/i18n'; /** @@ -12,25 +10,21 @@ import { __ } from '@wordpress/i18n'; import edit from './edit'; import './style.scss'; -domReady( () => { - if ( 'wp_template' === select( 'core/editor' ).getCurrentPostType() ) { - registerBlockType( 'a8c/template-part', { - title: __( 'Template Part' ), - description: __( 'Display a template part.' ), - icon: 'layout', - category: 'layout', - attributes: { - selectedPostId: { type: 'number' }, - selectedPostType: { type: 'string' }, - }, - supports: { - align: [ 'wide', 'full' ], - anchor: true, - html: false, - reusable: false, - }, - edit, - save: () => null, - } ); - } +registerBlockType( 'a8c/template-part', { + title: __( 'Template Part' ), + description: __( 'Display a template part.' ), + icon: 'layout', + category: 'layout', + attributes: { + selectedPostId: { type: 'number' }, + selectedPostType: { type: 'string' }, + }, + supports: { + align: [ 'wide', 'full' ], + anchor: true, + html: false, + reusable: false, + }, + edit, + save: () => null, } ); diff --git a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php index a1568691a5a88..534c8b64dc6d2 100644 --- a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php +++ b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php @@ -27,6 +27,10 @@ function register_wp_template() { } function register_script_and_style() { + if ( ! $this->is_editor_wp_template() ) { + return; + } + $script_dependencies = json_decode( file_get_contents( plugin_dir_path( __FILE__ ) . 'dist/full-site-editing-plugin.deps.json' ), true ); @@ -76,6 +80,12 @@ function allow_searching_for_templates() { // setting this to `public` will allow it to be found in the search endpoint $post_type->public = true; } + + function is_editor_wp_template() { + return + ( isset( $_GET['post_type'] ) && 'wp_template' === $_GET['post_type'] ) || + ( isset( $_GET['post'] ) && 'wp_template' === get_post_type( $_GET['post'] ) ); + } } new A8C_Full_Site_Editing(); diff --git a/apps/full-site-editing/package.json b/apps/full-site-editing/package.json index 513970231cae3..ece6720093d31 100644 --- a/apps/full-site-editing/package.json +++ b/apps/full-site-editing/package.json @@ -31,7 +31,6 @@ "@wordpress/components": "^7.3.0", "@wordpress/compose": "^3.2.0", "@wordpress/data": "^4.4.0", - "@wordpress/dom-ready": "^2.2.0", "@wordpress/editor": "^9.2.4", "@wordpress/element": "^2.3.0", "@wordpress/i18n": "^3.3.0", From 23200546d641e629740c14a68ebfc0f61fa9409c Mon Sep 17 00:00:00 2001 From: Copons Date: Wed, 1 May 2019 18:20:21 +0100 Subject: [PATCH 2/3] Manually enqueue FSE blocks assets --- .../full-site-editing-plugin.php | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php index 534c8b64dc6d2..d9162e53502f5 100644 --- a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php +++ b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php @@ -15,10 +15,10 @@ function __construct() { } self::$initialized = true; - add_action( 'init', array( $this, 'register_script_and_style' ), 100 ); add_action( 'init', array( $this, 'register_blocks' ), 100 ); add_action( 'init', array( $this, 'register_wp_template' ) ); add_action( 'rest_api_init', array( $this, 'allow_searching_for_templates' ) ); + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_script_and_style' ), 100 ); } function register_wp_template() { @@ -26,7 +26,7 @@ function register_wp_template() { fse_register_wp_template(); } - function register_script_and_style() { + function enqueue_script_and_style() { if ( ! $this->is_editor_wp_template() ) { return; } @@ -34,17 +34,18 @@ function register_script_and_style() { $script_dependencies = json_decode( file_get_contents( plugin_dir_path( __FILE__ ) . 'dist/full-site-editing-plugin.deps.json' ), true ); - wp_register_script( + wp_enqueue_script( 'a8c-full-site-editing-script', plugins_url( 'dist/full-site-editing-plugin.js', __FILE__ ), is_array( $script_dependencies ) ? $script_dependencies : array(), - filemtime( plugin_dir_path( __FILE__ ) . 'dist/full-site-editing-plugin.js' ) + filemtime( plugin_dir_path( __FILE__ ) . 'dist/full-site-editing-plugin.js' ), + true ); $style_file = is_rtl() ? 'full-site-editing-plugin.rtl.css' : 'full-site-editing-plugin.css'; - wp_register_style( + wp_enqueue_style( 'a8c-full-site-editing-style', plugins_url( 'dist/' . $style_file, __FILE__ ), array(), @@ -52,12 +53,8 @@ function register_script_and_style() { ); } - // We only need to declare script and style as dependencies once - // Because they'll be then enqueued for every block. function register_blocks() { register_block_type( 'a8c/content-slot', array( - 'editor_script' => 'a8c-full-site-editing-script', - 'editor_style' => 'a8c-full-site-editing-style', 'render_callback' => 'render_content_slot_block', ) ); @@ -82,9 +79,8 @@ function allow_searching_for_templates() { } function is_editor_wp_template() { - return - ( isset( $_GET['post_type'] ) && 'wp_template' === $_GET['post_type'] ) || - ( isset( $_GET['post'] ) && 'wp_template' === get_post_type( $_GET['post'] ) ); + $current_screen = get_current_screen(); + return 'wp_template' === $current_screen->post_type; } } From b4f6e85ba444f30234ff3fbd3c903fba71419669 Mon Sep 17 00:00:00 2001 From: Copons Date: Fri, 3 May 2019 12:36:40 +0100 Subject: [PATCH 3/3] Simplify CPT check --- .../full-site-editing-plugin/full-site-editing-plugin.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php index d9162e53502f5..9b1c7cf674e7b 100644 --- a/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php +++ b/apps/full-site-editing/full-site-editing-plugin/full-site-editing-plugin.php @@ -27,7 +27,7 @@ function register_wp_template() { } function enqueue_script_and_style() { - if ( ! $this->is_editor_wp_template() ) { + if ( 'wp_template' !== get_current_screen()->post_type ) { return; } @@ -77,11 +77,6 @@ function allow_searching_for_templates() { // setting this to `public` will allow it to be found in the search endpoint $post_type->public = true; } - - function is_editor_wp_template() { - $current_screen = get_current_screen(); - return 'wp_template' === $current_screen->post_type; - } } new A8C_Full_Site_Editing();