Skip to content

Commit

Permalink
Merge pull request #5164 from ampproject/fix/5147-missing-underscore-…
Browse files Browse the repository at this point in the history
…find-function

Improve handling of polyfilled assets (and improve handling of Reader themes data)
  • Loading branch information
westonruter authored Aug 6, 2020
2 parents 7c4ecfb + 44c8408 commit 2ffc660
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 113 deletions.
16 changes: 13 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ module.exports = function( grunt ) {
// Clean up the build.
clean: {
compiled: {
src: [ 'assets/js/*.js', '!assets/js/amp-service-worker-runtime-precaching.js', 'assets/js/*.asset.php' ],
src: [
'assets/js/**/*.js',
'assets/js/**/*.js.map',
'!assets/js/amp-service-worker-runtime-precaching.js',
'assets/js/**/*.asset.php',
'assets/css/*.css',
],
},
build: {
src: [ 'build' ],
Expand Down Expand Up @@ -155,11 +161,15 @@ module.exports = function( grunt ) {

paths.push( 'composer.*' ); // Copy in order to be able to do run composer_install.
paths.push( 'lib/**' );
paths.push( 'assets/js/*.js' ); // @todo Also include *.map files?
paths.push( 'assets/js/*.asset.php' );
paths.push( 'assets/js/**/*.js' );
paths.push( 'assets/js/**/*.asset.php' );
paths.push( 'assets/css/*.css' );
paths.push( 'patches/*.patch' );

if ( 'development' === process.env.NODE_ENV ) {
paths.push( 'assets/js/**/*.js.map' );
}

grunt.config.set( 'copy', {
build: {
src: paths,
Expand Down
9 changes: 9 additions & 0 deletions includes/admin/class-amp-template-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ public function add_customizer_scripts() {
$dependencies = $asset['dependencies'];
$version = $asset['version'];

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

wp_enqueue_script(
$handle,
amp_get_asset_url( 'js/amp-customize-controls.js' ),
Expand Down Expand Up @@ -558,6 +561,9 @@ public function add_legacy_customizer_scripts() {
$dependencies = $asset['dependencies'];
$version = $asset['version'];

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

wp_enqueue_script(
'amp-customize-controls', // Note: This is not 'amp-customize-controls-legacy' to not break existing scripts that have this dependency.
amp_get_asset_url( 'js/amp-customize-controls-legacy.js' ),
Expand Down Expand Up @@ -615,6 +621,9 @@ public function enqueue_legacy_preview_scripts() {
return;
}

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

$asset_file = AMP__DIR__ . '/assets/js/amp-customize-preview-legacy.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
Expand Down
35 changes: 0 additions & 35 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,41 +891,6 @@ function amp_add_generator_metadata() {
* @param WP_Scripts $wp_scripts Scripts.
*/
function amp_register_default_scripts( $wp_scripts ) {
/*
* Polyfill dependencies that are registered in Gutenberg and WordPress 5.0.
* Note that Gutenberg will override these at wp_enqueue_scripts if it is active.
*/
$handles = [ 'wp-i18n', 'wp-dom-ready', 'wp-polyfill', 'wp-url' ];
foreach ( $handles as $handle ) {
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
$asset_file = AMP__DIR__ . '/assets/js/' . $handle . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];

$wp_scripts->add(
$handle,
amp_get_asset_url( sprintf( 'js/%s.js', $handle ) ),
$dependencies,
$version
);
}
}

$vendor_scripts = [
'lodash' => [
'dependencies' => [],
'version' => '4.17.15',
],
];
foreach ( $vendor_scripts as $handle => $handle_data ) {
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
$path = amp_get_asset_url( sprintf( 'js/vendor/%s.js', $handle ) );

$wp_scripts->add( $handle, $path, $handle_data['dependencies'], $handle_data['version'], 1 );
}
}

// AMP Runtime.
$handle = 'amp-runtime';
$wp_scripts->add(
Expand Down
10 changes: 10 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,13 @@ public static function setup_paired_browsing_client() {
return;
}

/**
* Fires before registering plugin assets that may require core asset polyfills.
*
* @internal
*/
do_action( 'amp_register_polyfills' );

$asset_file = AMP__DIR__ . '/assets/js/amp-paired-browsing-client.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
Expand Down Expand Up @@ -2387,6 +2394,9 @@ public static function serve_paired_browsing_experience( $template ) {
);
}

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

wp_enqueue_style(
'amp-paired-browsing-app',
amp_get_asset_url( '/css/amp-paired-browsing-app.css' ),
Expand Down
3 changes: 3 additions & 0 deletions src/Admin/OnboardingWizardSubmenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ public function enqueue_assets( $hook_suffix ) {
return;
}

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

/** @var AmpSlugCustomizationWatcher $amp_slug_customization_watcher */
$amp_slug_customization_watcher = Services::get( 'amp_slug_customization_watcher' );

Expand Down
62 changes: 3 additions & 59 deletions src/Admin/OptionsMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ public function register() {

$plugin_file = preg_replace( '#.+/(?=.+?/.+?)#', '', AMP__FILE__ );
add_filter( "plugin_action_links_{$plugin_file}", [ $this, 'add_plugin_action_links' ] );

add_action( 'admin_enqueue_scripts', [ $this, 'register_shimmed_assets' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
}

Expand Down Expand Up @@ -154,63 +152,6 @@ public function screen_handle() {
return sprintf( 'toplevel_page_%s', AMP_Options_Manager::OPTION_NAME );
}

/**
* Registers shimmed assets not guaranteed to be available in core.
*/
public function register_shimmed_assets() {
if ( ! wp_script_is( 'wp-api-fetch', 'registered' ) ) {
$asset_handle = 'wp-api-fetch';
$asset_file = AMP__DIR__ . '/assets/js/' . $asset_handle . '.asset.php';
$asset = require $asset_file;
$version = $asset['version'];

wp_register_script(
$asset_handle,
amp_get_asset_url( 'js/' . $asset_handle . '.js' ),
[],
$version,
true
);

wp_add_inline_script(
$asset_handle,
sprintf(
'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',
esc_url_raw( get_rest_url() )
),
'after'
);
wp_add_inline_script(
$asset_handle,
implode(
"\n",
[
sprintf(
'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
),
'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
sprintf(
'wp.apiFetch.nonceEndpoint = "%s";',
admin_url( 'admin-ajax.php?action=rest-nonce' )
),
]
),
'after'
);
}

if ( ! wp_style_is( 'wp-components', 'registered' ) ) {
wp_register_style(
'wp-components',
amp_get_asset_url( 'css/wp-components.css' ),
[],
AMP__VERSION
);
}
}

/**
* Enqueues settings page assets.
*
Expand All @@ -223,6 +164,9 @@ public function enqueue_assets( $hook_suffix ) {
return;
}

/** This action is documented in includes/class-amp-theme-support.php */
do_action( 'amp_register_polyfills' );

$asset_file = AMP__DIR__ . '/assets/js/' . self::ASSET_HANDLE . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
Expand Down
140 changes: 140 additions & 0 deletions src/Admin/Polyfills.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Handles backward compatibility of assets for older versions of WP.
*
* @since 2.0
*
* @package AmpProject\AmpWP
*/

namespace AmpProject\AmpWP\Admin;

use AmpProject\AmpWP\Infrastructure\Delayed;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use WP_Scripts;
use WP_Styles;

/**
* Registers assets that may not be available in the current site's version of core.
*
* @since 2.0
*/
final class Polyfills implements Delayed, Service, Registerable {

/**
* Get the action to use for registering the service.
*
* @return string Registration action to use.
*/
public static function get_registration_action() {
return 'amp_register_polyfills';
}

/**
* Runs on instantiation.
*/
public function register() {
$this->register_shimmed_scripts( wp_scripts() );
$this->register_shimmed_styles( wp_styles() );
}

/**
* Registers scripts not guaranteed to be available in core.
*
* @param WP_Scripts $wp_scripts The WP_Scripts instance for the current page.
*/
public function register_shimmed_scripts( $wp_scripts ) {
if ( ! isset( $wp_scripts->registered['lodash'] ) ) {
$wp_scripts->add(
'lodash',
amp_get_asset_url( 'js/vendor/lodash.js' ),
[],
'4.17.19',
true
);

$wp_scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' );
}

/*
* Polyfill dependencies that are registered in Gutenberg and WordPress 5.0.
* Note that Gutenberg will override these at wp_enqueue_scripts if it is active.
*/
$handles = [ 'wp-i18n', 'wp-dom-ready', 'wp-polyfill', 'wp-url' ];
foreach ( $handles as $handle ) {
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
$asset_file = AMP__DIR__ . "/assets/js/{$handle}.asset.php";
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];

$wp_scripts->add(
$handle,
amp_get_asset_url( "js/{$handle}.js" ),
$dependencies,
$version
);
}
}

if ( ! isset( $wp_scripts->registered['wp-api-fetch'] ) ) {
$asset_handle = 'wp-api-fetch';
$asset_file = AMP__DIR__ . "/assets/js/{$asset_handle}.asset.php";
$asset = require $asset_file;
$version = $asset['version'];

$wp_scripts->add(
$asset_handle,
amp_get_asset_url( "js/{$asset_handle}.js" ),
[],
$version,
true
);

$wp_scripts->add_inline_script(
$asset_handle,
sprintf(
'wp.apiFetch.use( wp.apiFetch.createRootURLMiddleware( "%s" ) );',
esc_url_raw( get_rest_url() )
),
'after'
);
$wp_scripts->add_inline_script(
$asset_handle,
implode(
"\n",
[
sprintf(
'wp.apiFetch.nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
),
'wp.apiFetch.use( wp.apiFetch.nonceMiddleware );',
'wp.apiFetch.use( wp.apiFetch.mediaUploadMiddleware );',
sprintf(
'wp.apiFetch.nonceEndpoint = "%s";',
admin_url( 'admin-ajax.php?action=rest-nonce' )
),
]
),
'after'
);
}
}

/**
* Registers shimmed assets not guaranteed to be available in core.
*
* @param WP_Styles $wp_styles The WP_Styles instance for the current page.
*/
public function register_shimmed_styles( $wp_styles ) {
if ( ! isset( $wp_styles->registered['wp-components'] ) ) {
$wp_styles->add(
'wp-components',
amp_get_asset_url( 'css/wp-components.css' ),
[],
'10.0.2'
);
}
}
}
4 changes: 4 additions & 0 deletions src/Admin/RESTPreloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function add_preloaded_path( $path ) {
* Preloads data using apiFetch preloading middleware.
*/
public function preload_data() {
if ( ! function_exists( 'rest_preload_api_request' ) ) { // Not available pre-5.0.
return;
}

$preload_data = array_reduce( $this->paths, 'rest_preload_api_request', [] );

wp_add_inline_script(
Expand Down
Loading

0 comments on commit 2ffc660

Please sign in to comment.