diff -r c7c34916027a -r 177826044cd9 wp/wp-includes/script-loader.php --- a/wp/wp-includes/script-loader.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-includes/script-loader.php Mon Oct 14 18:28:13 2019 +0200 @@ -35,6 +35,802 @@ 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, $force_uncompressed = false ) { + global $tinymce_version, $concatenate_scripts, $compress_scripts; + $suffix = wp_scripts_get_suffix(); + $dev_suffix = wp_scripts_get_suffix( 'dev' ); + + script_concat_settings(); + + $compressed = $compress_scripts && $concatenate_scripts && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) + && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && ! $force_uncompressed; + + // Load tinymce.js when running from /src, otherwise load wp-tinymce.js.gz (in production) or + // tinymce.min.js (when SCRIPT_DEBUG is true). + if ( $compressed ) { + $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . 'wp-tinymce.js', array(), $tinymce_version ); + } else { + $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce$dev_suffix.js", array(), $tinymce_version ); + $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin$dev_suffix.js", array( 'wp-tinymce-root' ), $tinymce_version ); + } + + $scripts->add( 'wp-tinymce-lists', includes_url( "js/tinymce/plugins/lists/plugin$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 ) { + global $wp_locale; + + $suffix = wp_scripts_get_suffix(); + + $vendor_scripts = array( + 'react' => array( 'wp-polyfill' ), + 'react-dom' => array( 'react' ), + 'moment', + 'lodash', + 'wp-polyfill-fetch', + 'wp-polyfill-formdata', + 'wp-polyfill-node-contains', + 'wp-polyfill-element-closest', + 'wp-polyfill', + ); + + $vendor_scripts_versions = array( + 'react' => '16.8.4', + 'react-dom' => '16.8.4', + 'moment' => '2.22.2', + 'lodash' => '4.17.11', + 'wp-polyfill-fetch' => '3.0.0', + 'wp-polyfill-formdata' => '3.0.12', + 'wp-polyfill-node-contains' => '3.26.0-0', + 'wp-polyfill-element-closest' => '2.0.2', + 'wp-polyfill' => '7.0.0', + ); + + foreach ( $vendor_scripts as $handle => $dependencies ) { + if ( is_string( $dependencies ) ) { + $handle = $dependencies; + $dependencies = array(); + } + + $path = "/wp-includes/js/dist/vendor/$handle$suffix.js"; + $version = $vendor_scripts_versions[ $handle ]; + + $scripts->add( $handle, $path, $dependencies, $version, 1 ); + } + + $scripts->add( 'wp-polyfill', null, array( 'wp-polyfill' ) ); + did_action( 'init' ) && $scripts->add_inline_script( + 'wp-polyfill', + 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();' ); + + did_action( 'init' ) && $scripts->add_inline_script( + 'moment', + sprintf( + "moment.locale( '%s', %s );", + get_user_locale(), + wp_json_encode( + array( + 'months' => array_values( $wp_locale->month ), + 'monthsShort' => array_values( $wp_locale->month_abbrev ), + 'weekdays' => array_values( $wp_locale->weekday ), + 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), + 'week' => array( + 'dow' => (int) get_option( 'start_of_week', 0 ), + ), + 'longDateFormat' => array( + 'LT' => get_option( 'time_format', __( 'g:i a', 'default' ) ), + 'LTS' => null, + 'L' => null, + 'LL' => get_option( 'date_format', __( 'F j, Y', 'default' ) ), + 'LLL' => __( 'F j, Y g:i a', 'default' ), + 'LLLL' => null, + ), + ) + ) + ), + 'after' + ); +} + +/** + * 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; + } + + $src = $scripts->registered[ $handle ]->src; + $ver = $scripts->registered[ $handle ]->ver; + + if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && 0 === strpos( $src, $scripts->content_url ) ) ) { + $src = $scripts->base_url . $src; + } + + if ( ! empty( $ver ) ) { + $src = add_query_arg( 'ver', $ver, $src ); + } + + /** This filter is documented in wp-includes/class.wp-scripts.php */ + $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); + + if ( ! $src ) { + 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( \'\n"; + echo "\n"; } - if ( !empty($wp_scripts->print_html) ) + if ( ! empty( $wp_scripts->print_html ) ) { echo $wp_scripts->print_html; + } } /** @@ -1393,7 +2485,7 @@ * @return array */ function wp_print_head_scripts() { - if ( ! did_action('wp_print_scripts') ) { + if ( ! did_action( 'wp_print_scripts' ) ) { /** This action is documented in wp-includes/functions.wp-scripts.php */ do_action( 'wp_print_scripts' ); } @@ -1463,7 +2555,7 @@ script_concat_settings(); $wp_styles->do_concat = $concatenate_scripts; - $wp_styles->do_items(false); + $wp_styles->do_items( false ); /** * Filters whether to print the admin styles. @@ -1530,8 +2622,9 @@ $wp_styles = wp_styles(); $zip = $compress_css ? 1 : 0; - if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP ) + if ( $zip && defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP ) { $zip = 'gzip'; + } if ( $concat = trim( $wp_styles->concat, ', ' ) ) { $dir = $wp_styles->text_direction; @@ -1541,17 +2634,18 @@ $concat = 'load%5B%5D=' . implode( '&load%5B%5D=', $concat ); $href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&" . $concat . '&ver=' . $ver; - echo "\n"; + echo "\n"; - if ( !empty($wp_styles->print_code) ) { + if ( ! empty( $wp_styles->print_code ) ) { echo "\n"; } } - if ( !empty($wp_styles->print_html) ) + if ( ! empty( $wp_styles->print_html ) ) { echo $wp_styles->print_html; + } } /** @@ -1566,23 +2660,97 @@ function script_concat_settings() { global $concatenate_scripts, $compress_scripts, $compress_css; - $compressed_output = ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ); + $compressed_output = ( ini_get( 'zlib.output_compression' ) || 'ob_gzhandler' == ini_get( 'output_handler' ) ); + + if ( ! isset( $concatenate_scripts ) ) { + $concatenate_scripts = defined( 'CONCATENATE_SCRIPTS' ) ? CONCATENATE_SCRIPTS : true; + if ( ( ! is_admin() && ! did_action( 'login_init' ) ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) { + $concatenate_scripts = false; + } + } + + if ( ! isset( $compress_scripts ) ) { + $compress_scripts = defined( 'COMPRESS_SCRIPTS' ) ? COMPRESS_SCRIPTS : true; + if ( $compress_scripts && ( ! get_site_option( 'can_compress_scripts' ) || $compressed_output ) ) { + $compress_scripts = false; + } + } - if ( ! isset($concatenate_scripts) ) { - $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true; - if ( ( ! is_admin() && ! did_action( 'login_init' ) ) || ( defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ) ) - $concatenate_scripts = false; + if ( ! isset( $compress_css ) ) { + $compress_css = defined( 'COMPRESS_CSS' ) ? COMPRESS_CSS : true; + if ( $compress_css && ( ! get_site_option( 'can_compress_scripts' ) || $compressed_output ) ) { + $compress_css = false; + } + } +} + +/** + * Handles the enqueueing of block scripts and styles that are common to both + * the editor and the front-end. + * + * @since 5.0.0 + * + * @global WP_Screen $current_screen + */ +function wp_common_block_scripts_and_styles() { + global $current_screen; + + if ( is_admin() && ( $current_screen instanceof WP_Screen ) && ! $current_screen->is_block_editor() ) { + return; + } + + wp_enqueue_style( 'wp-block-library' ); + + if ( current_theme_supports( 'wp-block-styles' ) ) { + wp_enqueue_style( 'wp-block-library-theme' ); } - if ( ! isset($compress_scripts) ) { - $compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true; - if ( $compress_scripts && ( ! get_site_option('can_compress_scripts') || $compressed_output ) ) - $compress_scripts = false; - } + /** + * Fires after enqueuing block assets for both editor and front-end. + * + * Call `add_action` on any hook before 'wp_enqueue_scripts'. + * + * In the function call you supply, simply use `wp_enqueue_script` and + * `wp_enqueue_style` to add your functionality to the Gutenberg editor. + * + * @since 5.0.0 + */ + do_action( 'enqueue_block_assets' ); +} + +/** + * Enqueues registered block scripts and styles, depending on current rendered + * context (only enqueuing editor scripts while in context of the editor). + * + * @since 5.0.0 + * + * @global WP_Screen $current_screen + */ +function wp_enqueue_registered_block_scripts_and_styles() { + global $current_screen; - if ( ! isset($compress_css) ) { - $compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true; - if ( $compress_css && ( ! get_site_option('can_compress_scripts') || $compressed_output ) ) - $compress_css = false; + $is_editor = ( ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor() ); + + $block_registry = WP_Block_Type_Registry::get_instance(); + foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { + // Front-end styles. + if ( ! empty( $block_type->style ) ) { + wp_enqueue_style( $block_type->style ); + } + + // Front-end script. + if ( ! empty( $block_type->script ) ) { + wp_enqueue_script( $block_type->script ); + } + + // Editor styles. + if ( $is_editor && ! empty( $block_type->editor_style ) ) { + wp_enqueue_style( $block_type->editor_style ); + } + + // Editor script. + if ( $is_editor && ! empty( $block_type->editor_script ) ) { + wp_enqueue_script( $block_type->editor_script ); + } } }