diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index b560fd2451..7b98ae1206 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -34,6 +34,496 @@ require( ABSPATH . WPINC . '/class.wp-styles.php' ); /** WordPress Styles Functions */ require( ABSPATH . WPINC . '/functions.wp-styles.php' ); +/** + * Registers TinyMCE scripts. + * + * @since 5.0.0 + * + * @param WP_Scripts $scripts WP_Scripts object. + */ +function wp_register_tinymce_scripts( &$scripts ) { + global $tinymce_version, $concatenate_scripts, $compress_scripts; + $suffix = SCRIPT_DEBUG ? '' : '.min'; + $compressed = $compress_scripts && $concatenate_scripts && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) + && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ); + // Load tinymce.js when running from /src, otherwise load wp-tinymce.js.gz (in production) or + // tinymce.min.js (when SCRIPT_DEBUG is true). + $mce_suffix = false !== strpos( get_bloginfo( 'version' ), '-src' ) ? '' : '.min'; + if ( $compressed ) { + $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . 'wp-tinymce.php', array(), $tinymce_version ); + } else { + $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce{$mce_suffix}.js", array(), $tinymce_version ); + $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin{$suffix}.js", array( 'wp-tinymce-root' ), $tinymce_version ); + } + + $scripts->add( 'wp-tinymce-lists', includes_url( 'js/tinymce/plugins/lists/index' . $suffix . '.js', array( 'wp-tinymce' ), $tinymce_version ) ); +} + +/** + * Registers all the WordPress vendor scripts that are in the standardized + * `js/dist/vendor/` location. + * + * For the order of `$scripts->add` see `wp_default_scripts`. + * + * @since 5.0.0 + * + * @param WP_Scripts $scripts WP_Scripts object. + */ +function wp_default_packages_vendor( &$scripts, $dev_suffix ) { + wp_register_tinymce_scripts( $scripts ); + + $vendor_scripts = array( + 'react', + 'react-dom' => array( 'react' ), + 'moment', + 'lodash', + 'wp-polyfill-fetch', + 'wp-polyfill-formdata', + 'wp-polyfill-node-contains', + 'wp-polyfill-element-closest', + 'wp-polyfill-ecmascript', + ); + + foreach ( $vendor_scripts as $handle => $dependencies ) { + if ( is_string( $dependencies ) ) { + $handle = $dependencies; + $dependencies = array(); + } + + $path = '/js/dist/vendor/' . $handle . $dev_suffix . '.js'; + + $scripts->add( $handle, $path, $dependencies, false, 1 ); + } + + $scripts->add( 'wp-polyfill', null, array( 'wp-polyfill-ecmascript' ) ); + did_action( 'init' ) && $scripts->add_inline_script( + 'wp-polyfill', + 'data', + wp_get_script_polyfill( + $scripts, + array( + '\'fetch\' in window' => 'wp-polyfill-fetch', + 'document.contains' => 'wp-polyfill-node-contains', + 'window.FormData && window.FormData.prototype.keys' => 'wp-polyfill-formdata', + 'Element.prototype.matches && Element.prototype.closest' => 'wp-polyfill-element-closest', + ) + ) + ); + + did_action( 'init' ) && $scripts->add_inline_script( 'lodash', 'window.lodash = _.noConflict();' ); +} + +/** + * Returns contents of an inline script used in appending polyfill scripts for + * browsers which fail the provided tests. The provided array is a mapping from + * a condition to verify feature support to its polyfill script handle. + * + * @since 5.0.0 + * + * @param WP_Scripts $scripts WP_Scripts object. + * @param array $tests Features to detect. + * @return string Conditional polyfill inline script. + */ +function wp_get_script_polyfill( $scripts, $tests ) { + $polyfill = ''; + foreach ( $tests as $test => $handle ) { + if ( ! array_key_exists( $handle, $scripts->registered ) ) { + continue; + } + + $polyfill .= ( + // Test presence of feature... + '( ' . $test . ' ) || ' . + // ...appending polyfill on any failures. Cautious viewers may balk + // at the `document.write`. Its caveat of synchronous mid-stream + // blocking write is exactly the behavior we need though. + 'document.write( \'