Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#61335 new defect (bug)

block_editor_rest_api_preload function is restoring $wp_scripts and styles enqueued by wp_enqueue_media are lost

Reported by: oglekler's profile oglekler Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Widgets Keywords:
Focuses: administration Cc:

Description (last modified by oglekler)

Widgets screen > block_editor_rest_api_preload()
ACF Pro is calling wp_enqueue_media():

Array
(
    [0] => wp_enqueue_media
    [1] => ACF_Assets->enqueue_uploader
    [2] => acf_enqueue_uploader
    [3] => acf_field_image->render_field
    [4] => WP_Hook->apply_filters
    [5] => WP_Hook->do_action
    [6] => do_action_ref_array('acf/render_field/type=image')
    [7] => _acf_apply_hook_variations
    [8] => WP_Hook->apply_filters
    [9] => WP_Hook->do_action
    [10] => do_action('acf/render_field')
    [11] => acf_render_field
    [12] => acf_render_field_wrap
    [13] => ACF_Repeater_Table->row
    [14] => ACF_Repeater_Table->rows
    [15] => ACF_Repeater_Table->render
    [16] => acf_field_repeater->render_field
    [17] => WP_Hook->apply_filters
    [18] => WP_Hook->do_action
    [19] => do_action_ref_array('acf/render_field/type=repeater')
    [20] => _acf_apply_hook_variations
    [21] => WP_Hook->apply_filters
    [22] => WP_Hook->do_action
    [23] => do_action('acf/render_field')
    [24] => acf_render_field
    [25] => acf_render_field_wrap
    [26] => acf_render_fields
    [27] => acf_form_widget->edit_widget
    [28] => WP_Hook->apply_filters
    [29] => WP_Hook->do_action
    [30] => do_action_ref_array('in_widget_form')
    [31] => WP_Widget->form_callback
    [32] => wp_render_widget_control
    [33] => WP_REST_Widgets_Controller->prepare_item_for_response
    [34] => WP_REST_Widgets_Controller->get_items
    [35] => WP_REST_Server->respond_to_request
    [36] => WP_REST_Server->dispatch
    [37] => rest_do_request
    [38] => rest_preload_api_request
    [39] => array_reduce
    [40] => block_editor_rest_api_preload
    [41] => require('wp-admin/widgets-form-blocks.php')
)

But next, what block_editor_rest_api_preload() is doing is restoring everything into previous state:

	$post       = $backup_global_post;
	$wp_scripts = $backup_wp_scripts;
	$wp_styles  = $backup_wp_styles;

It could have been fine, but wp_enqueue_media() has a check that it has already been called (due to #45913, it looks necessary):

function wp_enqueue_media( $args = array() ) {
	// Enqueue me just once per page, please.
	if ( did_action( 'wp_enqueue_media' ) ) {
		return;
	}
....

And as a result, I am getting a broken media uploader on the Widget's page, and calling wp_enqueue_media() is doing no good in this situation, and there is no right way to fix it. I can change $wp_actions global before calling wp_enqueue_media() second time or add

	wp_enqueue_style( 'media-views' );
	wp_enqueue_style( 'imgareaselect' );

hoping that I don't need any additional logic from the function. I believe that both solutions are not the correct way to do things, and something needs to be done in the core to fix the root of the problem (not the ACF plugin because from one point of view it can have its reason to this call and another is that other plugins can have similar behaviour).

I decided to add action on 'current_screen' and call wp_enqueue_media() on widgets screen preventively. But this is also a workaround and cannot be counted as a proper solution.

Change History (1)

#1 @oglekler
2 months ago

  • Description modified (diff)
Note: See TracTickets for help on using tickets.