Skip to content

Block: Update PHP template to use the server script and style api#111

Merged
schlessera merged 3 commits intowp-cli:masterfrom
gziolo:update/block-block-php
Jan 10, 2018
Merged

Block: Update PHP template to use the server script and style api#111
schlessera merged 3 commits intowp-cli:masterfrom
gziolo:update/block-block-php

Conversation

@gziolo
Copy link
Contributor

@gziolo gziolo commented Jan 10, 2018

This PR updates PHP template generated with wp scaffold block command. It adds changes introduced by @aduth in WordPress/gutenberg#4039. Both ways are still supported, but the new one is the preferred one.

Generated output after changes introduced:

<?php
/**
 * Functions to register client-side assets (scripts and stylesheets) for the Gutenberg block.
 *
 * @package books
 */

/**
 * Enqueues all block assets to apply both on the front of your site and in the editor.
 *
 * @see https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type/#enqueuing-block-scripts
 */
function my_book_block_init() {
	$dir = dirname( __FILE__ );

	$block_js = 'my-book/block.js';
	wp_register_script(
		'my-book-block-editor',
		plugins_url( $block_js, __FILE__ ),
		array(
			'wp-blocks',
			'wp-i18n',
			'wp-element',
		),
		filemtime( "$dir/$block_js" )
	);

	$editor_css = 'my-book/editor.css';
	wp_register_style(
		'my-book-block-editor',
		plugins_url( $editor_css, __FILE__ ),
		array(
			'wp-blocks',
		),
		filemtime( "$dir/$editor_css" )
	);

	$style_css = 'my-book/style.css';
	wp_register_style(
		'my-book-block',
		plugins_url( $style_css, __FILE__ ),
		array(
			'wp-blocks',
		),
		filemtime( "$dir/$style_css" )
	);

	register_block_type( 'books/my-book', array(
		'editor_script' => 'my-book-block-editor',
		'editor_style'  => 'my-book-block-editor',
		'style'         => 'my-book-block',
	) );
}
add_action( 'init', 'my_book_block_init' );

@gziolo
Copy link
Contributor Author

gziolo commented Jan 10, 2018

@schlessera can you review this one? :)
This seems to be the last change we have planned for the upcoming release. Unless we have someone that wants to work on providing more JS block templates as described in docs:


/**
* Enqueues block assets to apply inside the editor only.
* Enqueues all block assets to apply both on the front of your site and in the editor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only registers the assets now, it doesn't enqueue them anymore.

Copy link
Contributor Author

@gziolo gziolo Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that is done only indirectly (just like WordPress enqueues everything, at one point). Wouldn't it be more accurate to say something like:

Registers all block assets so that they can be enqueued through Gutenberg in the corresponding context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, updating.

plugins_url( $style_css, __FILE__ ),
array(
'wp-blocks',
§'wp-blocks',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have introduced an unintended change here: §

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that myself, too. It's already removed.

@gziolo
Copy link
Contributor Author

gziolo commented Jan 10, 2018

Updated again, should be good now :)

@schlessera schlessera merged commit 10b38c5 into wp-cli:master Jan 10, 2018
@schlessera schlessera added the command:scaffold-block Related to 'scaffold block' command label Jan 10, 2018
@schlessera schlessera added this to the 1.1.2 milestone Jan 10, 2018
@gziolo gziolo deleted the update/block-block-php branch January 10, 2018 19:21
danielbachhuber pushed a commit that referenced this pull request Nov 18, 2022
Block: Update PHP template to use the server script and style api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:scaffold-block Related to 'scaffold block' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants