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

Full Site Editing: Conditionally enqueue blocks scripts when editing wp_template posts #32717

Merged
merged 3 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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,
} );
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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,
} );
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,46 @@ 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() {
require_once plugin_dir_path( __FILE__ ) . 'wp-template.php';
fse_register_wp_template();
}

function register_script_and_style() {
function enqueue_script_and_style() {
if ( 'wp_template' !== get_current_screen()->post_type ) {
return;
}

$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(),
filemtime( plugin_dir_path( __FILE__ ) . 'dist/' . $style_file )
);
}

// 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',
) );

Expand Down
1 change: 0 additions & 1 deletion apps/full-site-editing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down