WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.51k stars 4.2k forks source link

Core hooks fired in incorrect order, breaks CMB library #4929

Closed johnbillion closed 6 years ago

johnbillion commented 6 years ago

Issue Overview

The way in which Gutenberg loads meta boxes causes it to trigger core actions in the incorrect order on the post editing screen. One problem that this causes is to break meta boxes which are registered with the CMB library because the admin_enqueue_scripts action fires before the fields are registered, causing JavaScript files to not get enqueued.

Steps to Reproduce (for bugs)

  1. Install the Query Monitor plugin, or any debugging plugin which displays hooks in the order in which they are fired.
  2. Load the post editing screen without Gutenberg enabled and note that the dbx_post_advanced action fires before the admin_enqueue_scripts hook.
  3. Load the post editing screen with Gutenberg enabled and note that the order in which these two hooks fire is reversed.

Expected Behavior

The dbx_post_advanced hook fires before the admin_enqueue_scripts hook.

Current Behavior

The admin_enqueue_scripts hook fires before the dbx_post_advanced hook.

/cc @mikeselander @mattheu

johnbillion commented 6 years ago

The underlying cause is that the gutenberg_collect_meta_box_data() function (which fires the dbx_post_advanced hook, among several others) fires on the admin_head action, which is too late.

bobbingwide commented 6 years ago

Do you have a workaround, such as hooking into gutenberg_can_edit_post_type ? See also https://github.com/WordPress/gutenberg/issues/1316

johnbillion commented 6 years ago

I don't have a workaround yet but I'm looking into it.

johnbillion commented 6 years ago

I believe that the following is a workaround for this particular issue, but it's not been extensively tested. If anyone else has the same issue, feedback would be welcome.

add_action( 'admin_enqueue_scripts', function() {
    do_action( 'dbx_post_advanced', $GLOBALS['post'] );
    remove_all_actions( 'dbx_post_advanced' );
}, -999 );
BE-Webdesign commented 6 years ago

The underlying cause is that the gutenberg_collect_meta_box_data() function (which fires the dbx_post_advanced hook, among several others) fires on the admin_head action, which is too late.

If it doesn't fire on admin_head it is too early for other meta box plugins, like ACF. For whatever reason a significant amount of ACF registrations happen on admin_head. I can look into this at some point, and fix it up for CMB. Don't have any bandwidth currently.

danielbachhuber commented 6 years ago

From https://github.com/WordPress/gutenberg/issues/7000#issuecomment-398067899

NB: According to this comment, it looks like metaboxes are added "as soon as possible". I don't know why it cannot be done as soon as in the classic editor but worry that changing the order of two of the most widely used actions (See hooks usage statistics) could introduce other various backward compatibility issues in the ecosystem.

I think we may need to revert back to the existing order and have ACF include some form of compatibility shim.

johnbillion commented 6 years ago

@jsternberg Hi! Hope you don't mind the ping. Are you aware if this issue affects CMB2?

jsternberg commented 6 years ago

Hi, I think you might have pinged the wrong person. I do not know what the acronym CMB2 stands for so I'm unlikely to know if this issue affects it.

johnbillion commented 6 years ago

Sorry! @jtsternberg is who I was after :-)

jtsternberg commented 6 years ago

👋Looks like I'm too late as #10660 has resolved it. Sorry I missed the ping.