Make WordPress Core

Ticket #50328: 50328-skip-assets.diff

File 50328-skip-assets.diff, 6.3 KB (added by gziolo, 5 years ago)

Test with the logic to skip assets when not on the front

  • src/wp-includes/blocks/index.php

     
    5656                ABSPATH . WPINC . '/blocks/' . $block_folder
    5757        );
    5858}
     59
     60// TODO: Move this code to unit tests.
     61function register_block_test_block() {
     62        wp_register_script( 'my-block-plugin-block-js', null );
     63        wp_add_inline_script(
     64                'my-block-plugin-block-js',
     65                'console.log( "my-block-plugin-block-js" );'
     66        );
     67        wp_register_script( 'my-block-plugin-block-editor-js', null, array( 'wp-blocks' ) );
     68        wp_add_inline_script(
     69                'my-block-plugin-block-editor-js',
     70                'console.log( "my-block-plugin-block-editor-js" );' .
     71                'wp.blocks.registerBlockType( "my-block-plugin/block", { edit() { return "edit my-block-plugin/block"; }, save() { return "content"; } } );'
     72        );
     73        wp_register_style( 'my-block-plugin-block-css', null );
     74        wp_add_inline_style(
     75                'my-block-plugin-block-css',
     76                '.my-block-plugin-block.css { color: red; }'
     77        );
     78        wp_register_style( 'my-block-plugin-block-editor-css', null );
     79        wp_add_inline_style(
     80                'my-block-plugin-block-editor-css',
     81                '.my-block-plugin-block.editor-css { color: red; }'
     82        );
     83        register_block_type( 'my-block-plugin/block', [
     84                'title'           => 'My Block',
     85                'script'          => 'my-block-plugin-block-js',
     86                'editor_script'   => 'my-block-plugin-block-editor-js',
     87                'style'           => 'my-block-plugin-block-css',
     88                'editor_style'    => 'my-block-plugin-block-editor-css',
     89        ] );
     90}
     91add_action( 'init', 'register_block_test_block' );
  • src/wp-includes/class-wp-block.php

     
    193193         */
    194194        public function render( $options = array() ) {
    195195                global $post;
    196                 $options = wp_parse_args(
     196                $options       = wp_parse_args(
    197197                        $options,
    198198                        array(
    199199                                'dynamic' => true,
    200200                        )
    201201                );
     202                $block_content = '';
    202203
    203204                $is_dynamic    = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
    204                 $block_content = '';
    205205
    206206                if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
    207207                        $index = 0;
     
    218218                        $post          = $global_post;
    219219                }
    220220
    221                 if ( ! empty( $this->block_type->script ) ) {
    222                         wp_enqueue_script( $this->block_type->script );
    223                 }
    224 
    225                 if ( ! empty( $this->block_type->style ) ) {
    226                         wp_enqueue_style( $this->block_type->style );
    227                 }
    228 
    229221                /**
    230222                 * Filters the content of a single block.
    231223                 *
     
    234226                 * @param string $block_content The block content about to be appended.
    235227                 * @param array  $block         The full block, including name and attributes.
    236228                 */
    237                 return apply_filters( 'render_block', $block_content, $this->parsed_block );
     229                $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
     230
     231                $skip_assets = is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST );
     232
     233                // Print script when it is set. Note that the scripts is not being
     234                // enqueued at the wp_enqueue_script action because this could result
     235                // in the script being printed when it would never be used. When
     236                // a script is printed in the body it has the additional benefit of
     237                // not being render-blocking. When a script is printed the first time,
     238                // subsequent calls to wp_print_script() will no-op.
     239                if ( ! $skip_assets && ! empty( $this->block_type->script ) ) {
     240                        ob_start();
     241                        wp_print_scripts( $this->block_type->script );
     242                        $block_content = trim( ob_get_clean() ) . $block_content;
     243                }
     244
     245                // Print stylesheet when it is set. Note that the stylesheet is not being
     246                // enqueued at the wp_enqueue_style action because this could result
     247                // in the stylesheet being printed when it would never be used. When
     248                // a stylesheet is printed in the body it has the additional benefit of
     249                // not being render-blocking. When a stylesheet is printed the first time,
     250                // subsequent calls to wp_print_styles() will no-op.
     251                if ( ! $skip_assets && ! empty( $this->block_type->style ) ) {
     252                        ob_start();
     253                        wp_print_styles( $this->block_type->style );
     254                        $block_content = trim( ob_get_clean() ) . $block_content;
     255                }
     256
     257                return $block_content;
    238258        }
    239259
    240260}
  • src/wp-includes/default-filters.php

     
    507507add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
    508508add_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
    509509add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
    510 add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
    511510add_action( 'enqueue_block_editor_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
    512511add_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' );
    513512add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
  • src/wp-includes/script-loader.php

     
    23992399        global $current_screen;
    24002400
    24012401        $is_editor = ( ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor() );
     2402        if ( ! $is_editor ) {
     2403                return;
     2404        }
    24022405
    24032406        $block_registry = WP_Block_Type_Registry::get_instance();
    24042407        foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
    2405                 // Front-end styles.
     2408                // Front-end and editor styles.
    24062409                if ( ! empty( $block_type->style ) ) {
    24072410                        wp_enqueue_style( $block_type->style );
    24082411                }
    24092412
    2410                 // Front-end script.
     2413                // Front-end and editor script.
    24112414                if ( ! empty( $block_type->script ) ) {
    24122415                        wp_enqueue_script( $block_type->script );
    24132416                }
    24142417
    24152418                // Editor styles.
    2416                 if ( $is_editor && ! empty( $block_type->editor_style ) ) {
     2419                if ( ! empty( $block_type->editor_style ) ) {
    24172420                        wp_enqueue_style( $block_type->editor_style );
    24182421                }
    24192422
    24202423                // Editor script.
    2421                 if ( $is_editor && ! empty( $block_type->editor_script ) ) {
     2424                if ( ! empty( $block_type->editor_script ) ) {
    24222425                        wp_enqueue_script( $block_type->editor_script );
    24232426                }
    24242427        }