Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#65021 new defect (bug)

Fatal error in _wp_get_iframed_editor_assets(): wp_print_media_templates not found

Reported by: manuelurak's profile manuelurak Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Customize Keywords: reporter-feedback
Focuses: Cc:

Description

When opening the Customizer, a fatal error occurs:

Fatal error: call_user_func_array(): Argument #1 ($callback) must be
a valid callback, function "wp_print_media_templates" not found

The function _wp_get_iframed_editor_assets() in block-editor.php
creates an isolated script context and calls wp_print_footer_scripts(),
which fires the print_media_templates action. However, media-template.php
is not loaded in this isolated context, so wp_print_media_templates()
is undefined.

Fix: Add a require_once for media-template.php before wp_print_footer_scripts()
inside _wp_get_iframed_editor_assets().

Here is my temporary fix for the function:

<?php
function _wp_get_iframed_editor_assets() {
        global $wp_styles, $wp_scripts;

        // Keep track of the styles and scripts instance to restore later.
        $current_wp_styles  = $wp_styles;
        $current_wp_scripts = $wp_scripts;

        // Create new instances to collect the assets.
        $wp_styles  = new WP_Styles();
        $wp_scripts = new WP_Scripts();

        /*
         * Register all currently registered styles and scripts. The actions that
         * follow enqueue assets, but don't necessarily register them.
         */
        $wp_styles->registered  = $current_wp_styles->registered;
        $wp_scripts->registered = $current_wp_scripts->registered;

        /*
         * We generally do not need reset styles for the iframed editor.
         * However, if it's a classic theme, margins will be added to every block,
         * which is reset specifically for list items, so classic themes rely on
         * these reset styles.
         */
        $wp_styles->done =
                wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array();

        wp_enqueue_script( 'wp-polyfill' );
        // Enqueue the `editorStyle` handles for all core block, and dependencies.
        wp_enqueue_style( 'wp-edit-blocks' );

        if ( current_theme_supports( 'wp-block-styles' ) ) {
                wp_enqueue_style( 'wp-block-library-theme' );
        }

        /*
         * We don't want to load EDITOR scripts in the iframe, only enqueue
         * front-end assets for the content.
         */
        add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
        do_action( 'enqueue_block_assets' );
        remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );

        $block_registry = WP_Block_Type_Registry::get_instance();

        /*
         * Additionally, do enqueue `editorStyle` assets for all blocks, which
         * contains editor-only styling for blocks (editor content).
         */
        foreach ( $block_registry->get_all_registered() as $block_type ) {
                if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) {
                        foreach ( $block_type->editor_style_handles as $style_handle ) {
                                wp_enqueue_style( $style_handle );
                        }
                }
        }

        /**
         * Remove the deprecated `print_emoji_styles` handler.
         * It avoids breaking style generation with a deprecation message.
         */
        $has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' );
        if ( $has_emoji_styles ) {
                remove_action( 'wp_print_styles', 'print_emoji_styles' );
        }

        ob_start();
        wp_print_styles();
        wp_print_font_faces();
        wp_print_font_faces_from_style_variations();
        $styles = ob_get_clean();

        if ( $has_emoji_styles ) {
                add_action( 'wp_print_styles', 'print_emoji_styles' );
        }

        ob_start();
        wp_print_head_scripts();
        if ( ! function_exists( 'wp_print_media_templates' ) ) {
                require_once ABSPATH . WPINC . '/media-template.php';
        }
        wp_print_footer_scripts();
        $scripts = ob_get_clean();

        // Restore the original instances.
        $wp_styles  = $current_wp_styles;
        $wp_scripts = $current_wp_scripts;

        return array(
                'styles'  => $styles,
                'scripts' => $scripts,
        );
}

Change History (1)

#1 @westonruter
6 weeks ago

  • Keywords reporter-feedback added
  • Version 6.9.4 deleted

I'm not able to reproduce a fatal error in the Customizer.

I just tested in both the Twenty Twenty-Five and Twenty Twenty core themes.

Please provide the minimal steps to reproduce this issue from a fresh installation of WordPress.

Note: See TracTickets for help on using tickets.