WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/11/2021 02:47:10 AM (3 weeks ago)
Author:
westonruter
Message:

Embeds: Conditionally enqueue wp-embed only if needed and send ready message in case script loads after post embed windows.

  • Prevent loading wp-embed script unconditionally on every page in favor of conditionally enqueueing when a post embed is detected. The wp-embed script is also explicitly marked as being in the footer group. Sites which currently disable post embed scripts from being enqueued via remove_action( 'wp_head', 'wp_oembed_add_host_js' ) will continue to do so.
  • Send a ready message from the host page to each post embed window in case the iframe loads before the wp-embed script does. When the ready message is received by the post embed window, it sends the same height message as it sends when it loads.
  • Eliminate use of grunt-include to inject emoji script and the post embed script. Instead obtain the script contents via file_get_contents() (as is done elsewhere in core) and utilize wp_print_inline_script_tag()/wp_get_inline_script_tag() to construct out the script. This simplifies the logic and allows the running of src without SCRIPT_DEBUG enabled.
  • For the embed code that users are provided to copy for embedding outside of WP, add the secret on the blockquote and iframe. This ensures the blockquote will be hidden when the iframe loads. The embed code in question is accessed here via get_post_embed_html().

Props westonruter, swissspidy, pento, flixos90, ocean90.
Fixes #44632, #44306.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r52035 r52132  
    57575757    );
    57585758
    5759     $version   = 'ver=' . get_bloginfo( 'version' );
    5760     $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/javascript"';
     5759    $version = 'ver=' . get_bloginfo( 'version' );
    57615760
    57625761    if ( SCRIPT_DEBUG ) {
     
    57675766            'twemoji' => apply_filters( 'script_loader_src', includes_url( "js/twemoji.js?$version" ), 'twemoji' ),
    57685767        );
    5769 
    5770         ?>
    5771         <script<?php echo $type_attr; ?>>
    5772             window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
    5773             <?php readfile( ABSPATH . WPINC . '/js/wp-emoji-loader.js' ); ?>
    5774         </script>
    5775         <?php
    57765768    } else {
    57775769        $settings['source'] = array(
     
    57795771            'concatemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji-release.min.js?$version" ), 'concatemoji' ),
    57805772        );
    5781 
    5782         /*
    5783          * If you're looking at a src version of this file, you'll see an "include"
    5784          * statement below. This is used by the `npm run build` process to directly
    5785          * include a minified version of wp-emoji-loader.js, instead of using the
    5786          * readfile() method from above.
    5787          *
    5788          * If you're looking at a build version of this file, you'll see a string of
    5789          * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
    5790          * and edit wp-emoji-loader.js directly.
    5791          */
    5792         ?>
    5793         <script<?php echo $type_attr; ?>>
    5794             window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
    5795             include "js/wp-emoji-loader.min.js"
    5796         </script>
    5797         <?php
    5798     }
     5773    }
     5774
     5775    wp_print_inline_script_tag(
     5776        sprintf( 'window._wpemojiSettings = %s;', wp_json_encode( $settings ) ) .
     5777            file_get_contents( sprintf( ABSPATH . WPINC . '/js/wp-emoji-loader' . wp_scripts_get_suffix() . '.js' ) )
     5778    );
    57995779}
    58005780
Note: See TracChangeset for help on using the changeset viewer.