[wp-trac] [WordPress Trac] #57421: Editor: add new parameter to hook when enqueuing block assets

WordPress Trac noreply at wordpress.org
Tue Jan 10 18:16:16 UTC 2023


#57421: Editor: add new parameter to hook when enqueuing block assets
-------------------------+------------------------------
 Reporter:  jeherve      |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Editor       |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:  javascript
-------------------------+------------------------------
Changes (by ironprogrammer):

 * version:  5.8 =>
 * type:  defect (bug) => enhancement


Old description:

> When enqueuing assets for a block registered in a plugin, one can rely on
> `block.json`, or hook into the `enqueue_block_editor_assets` hook to
> enqueue necessary scripts and styles in the block editor.
>
> This works well, but offers some challenges with some specific blocks.
> For instance, your block may rely on the `@wordpress/editor` dependency
> to handle some of its functionality. That dependency is available in the
> post editor, but cannot be used in the widget editor or the site editor.
> See #53569 and [https://github.com/WordPress/gutenberg/issues/33203
> 33203-gutenberg] for more information on this topic.
>
> In this scenario, one may want to enqueue the block's scripts (and its
> `wp-editor` dependency) **only in the post editor**.
>
> The `enqueue_block_editor_assets` hook doesn't currently offer this
> flexibility. As a result, folks wanting to do that must add a check
> before they enqueue. Something like this for someone not wanting to load
> in the widget editor for example.
>
> {{{#!php
> <?php
> function enqueue_block_assets() {
>     global $pagenow;
>
>     // Return early when viewing the customizer or widgets screen.
>     if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
>         return;
>     }
>
>     wp_enqueue_script( 'here' );
> }
> }}}
>

> I was consequently wondering if it were possible to extend the
> `enqueue_block_editor_assets` hook a bit, so it could be used just like
> the `admin_enqueue_scripts` hook today, so we could do something like
> this:
>
> {{{#!php
> <?php
> function enqueue_block_assets( $hook ) {
>     // We only need our script in the post editor
>     // not in the widget editor or the site editor.
>     if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) )
> {
>         return;
>     }
>
>     wp_enqueue_script( 'here' );
> }
> }}}

New description:

 This trac ticket is part 1 of a larger discussion spanning multiple
 issues, about the **potential issues plugin authors may face when
 developing blocks that rely on the @wordpress/editor dependency**.

 - This trac ticket is **part 1**: it suggests an option to make
 conditional asset enqueuing easier in the different block editors.
 - In **part 2**, I would like to suggest an option to declare support for
 specific editors at block registration. Discussion about this is happening
 in [https://github.com/WordPress/gutenberg/issues/46900 this Gutenberg
 issue].

 ----

 When enqueuing assets for a block registered in a plugin, one can rely on
 `block.json`, or hook into the `enqueue_block_editor_assets` hook to
 enqueue necessary scripts and styles in the block editor.

 This works well, but offers some challenges with some specific blocks. For
 instance, your block may rely on the `@wordpress/editor` dependency to
 handle some of its functionality. That dependency is available in the post
 editor, but cannot be used in the widget editor or the site editor. See
 #53569 and [https://github.com/WordPress/gutenberg/issues/33203
 33203-gutenberg] for more information on this topic.

 In this scenario, one may want to enqueue the block's scripts (and its
 `wp-editor` dependency) **only in the post editor**.

 The `enqueue_block_editor_assets` hook doesn't currently offer this
 flexibility. As a result, folks wanting to do that must add a check before
 they enqueue. Something like this for someone not wanting to load in the
 widget editor for example.

 {{{#!php
 <?php
 function enqueue_block_assets() {
     global $pagenow;

     // Return early when viewing the customizer or widgets screen.
     if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
         return;
     }

     wp_enqueue_script( 'here' );
 }
 }}}


 I was consequently wondering if it were possible to extend the
 `enqueue_block_editor_assets` hook a bit, so it could be used just like
 the `admin_enqueue_scripts` hook today, so we could do something like
 this:

 {{{#!php
 <?php
 function enqueue_block_assets( $hook ) {
     // We only need our script in the post editor
     // not in the widget editor or the site editor.
     if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) )
 {
         return;
     }

     wp_enqueue_script( 'here' );
 }
 }}}

--

Comment:

 Thanks for the report, @jeherve! I've added the requested text to the top
 of the ticket description, and marked this as an enhancement for clarity.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57421#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list