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

Registration: Add server script, style block type property #4039

Merged
merged 3 commits into from Jan 5, 2018

Conversation

aduth
Copy link
Member

@aduth aduth commented Dec 15, 2017

Closes #2756
Related: #2751

This pull request seeks to implement and document an alternative approach to enqueueing block assets, integrating new settings into the register_block_type server-side API to help manage context-specific script and style enqueues.

The register_block_type API now supports the following properties:

  • editor_script
  • editor_style
  • script
  • style

Assigning one or more of these to registered script or style handles will cause them to be enqueued in the corresponding render contexts (editor-specific or always).

This is intended as part of an effort to coalesce initial block implementations toward the server-side block registration API, as proposed in #2751 to be the default for new blocks.

Testing instructions:

Since this simply builds on existing APIs and no core blocks have been ported to using it (unclear whether this is a direction we'd want to go), there should be no expected impact of these changes.

You may try including a third-party block code and changing to implement with the register_block_type API.

Example:

https://gist.github.com/pento/19b35d621709042fc899e394a9387a54

diff --git a/stars-block.php b/stars-block.php
index da5598c..9ea8cfd 100755
--- a/stars-block.php
+++ b/stars-block.php
@@ -9,15 +9,6 @@
  * Description: Everyone loves stars! Let's add stars (now with more meta)!
  */
 
-function stars_block_enqueue_block_editor_assets() {
-       wp_enqueue_script(
-               'stars-block',
-               plugins_url( 'stars-block.js', __FILE__ ),
-               array( 'wp-blocks' )
-       );
-}
-add_action( 'enqueue_block_editor_assets', 'stars_block_enqueue_block_editor_assets' );
-
 function stars_block_init() {
        // Register the postmeta key that we'll store our data in
        register_meta( 'post', 'stars', array(
@@ -25,5 +16,15 @@ function stars_block_init() {
                'single'       => true,
                'type'         => 'number',
        ) );
+
+       wp_register_script(
+               'stars-block',
+               plugins_url( 'stars-block.js', __FILE__ ),
+               array( 'wp-blocks' )
+       );
+
+       register_block_type( 'stars/stars-block', array(
+               'editor_script' => 'stars-block',
+       ) );
 }
 add_action( 'init', 'stars_block_init' );

@aduth aduth added the [Feature] Block API API that allows to express the block paradigm. label Dec 15, 2017
'gutenberg-boilerplate-es5-step02',
function gutenberg_boilerplate_block() {
wp_register_script(
'gutenberg-boilerplate-es5-step02-editor-script',
Copy link
Member Author

@aduth aduth Dec 15, 2017

Choose a reason for hiding this comment

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

A seemingly basic question I wasn't sure of: Are handles required to be unique only between script and style registrations, or for both? i.e. Is it valid to:

wp_register_script( 'foo', /* ... */ );
wp_register_style( 'foo', /* ... */ );

If above is valid, can probably drop the suffix here to remove redundancy.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

They are free to be the same.

Thanks! Updated in 5972c9b.

@gziolo
Copy link
Member

gziolo commented Dec 15, 2017

We will have to update WP-CLI to promote those changes. Please ping me when it is ready to merge so I could open a follow-up PR.

@aduth
Copy link
Member Author

aduth commented Jan 4, 2018

@gziolo I expect this is ready to merge as-is. Do you have any work you'd like to have prepared before I do so?

@gziolo
Copy link
Member

gziolo commented Jan 4, 2018

It looks like non-breaking change so you can merge anytime. I will update WP-CLI next week to use this simplified API 👍

@gziolo
Copy link
Member

gziolo commented Jan 10, 2018

@aduth I created wp-cli/scaffold-command#111 for wp scaffold block to include your changes. The output looks great 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants