WordPress.org

Make WordPress Core

Changeset 44115


Ignore:
Timestamp:
12/13/2018 05:26:09 PM (2 years ago)
Author:
desrosj
Message:

Script loader: Adjust JS packages registration.

Adjusts the packages registration after [43723]:

Combine the different registration functions into one wp_default_packages function. To reach this goal move the prefix logic into a function so it can be called from different locations. Use a static variable there to prevent duplicate inclusion of version.php.

Call this function from the wp_default_scripts action by registering it as a default filter.

Combine some of the logic in _WP_Editors::print_tinymce_scripts into wp_register_tinymce_scripts. The logic to force an uncompressed TinyMCE script file stays in _WP_Editors::force_uncompressed_tinymce because that logic is very specific to the classic editor.

The script handle wp-tinymce is now a dependency of the editor script handle. In combination with the previous item, this makes the classic editor work.

Adjust the syntax of the script paths to be more consistent with other WordPress code.

Always use "production" mode for the media files to prevent people from inadvertently committing development files.

Props pento, omarreiss, atimmer.

Merges [43738] into trunk.

Fixes #45065.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/class-wp-editor.php

    r43571 r44115  
    318318            if ( is_admin() ) {
    319319                add_action( 'admin_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
     320                add_action( 'admin_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
    320321                add_action( 'admin_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
    321322            } else {
    322323                add_action( 'wp_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
     324                add_action( 'wp_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
    323325                add_action( 'wp_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
    324326            }
     
    756758
    757759    /**
     760     *
     761     * @static
     762     *
    758763     * @param bool $default_scripts Optional. Whether default scripts should be enqueued. Default false.
    759764     */
     
    820825
    821826        if ( is_admin() ) {
     827            add_action( 'admin_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
    822828            add_action( 'admin_print_footer_scripts', array( __CLASS__, 'print_default_editor_scripts' ), 45 );
    823829        } else {
     830            add_action( 'wp_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
    824831            add_action( 'wp_print_footer_scripts', array( __CLASS__, 'print_default_editor_scripts' ), 45 );
    825832        }
     
    13891396
    13901397    /**
     1398     * Force uncompressed TinyMCE when a custom theme has been defined.
     1399     *
     1400     * The compressed TinyMCE file cannot deal with custom themes, so this makes
     1401     * sure that we use the uncompressed TinyMCE file if a theme is defined.
     1402     * Even if we are on a production environment.
     1403     */
     1404    public static function force_uncompressed_tinymce() {
     1405        $has_custom_theme = false;
     1406        foreach ( self::$mce_settings as $init ) {
     1407            if ( ! empty( $init['theme_url'] ) ) {
     1408                $has_custom_theme = true;
     1409                break;
     1410            }
     1411        }
     1412
     1413        if ( ! $has_custom_theme ) {
     1414            return;
     1415        }
     1416
     1417        $wp_scripts = wp_scripts();
     1418
     1419        $wp_scripts->remove( 'wp-tinymce' );
     1420        wp_register_tinymce_scripts( $wp_scripts, true );
     1421    }
     1422
     1423    /**
    13911424     * Print (output) the main TinyMCE scripts.
    13921425     *
     
    13981431     */
    13991432    public static function print_tinymce_scripts() {
    1400         global $tinymce_version, $concatenate_scripts, $compress_scripts;
     1433        global $concatenate_scripts;
    14011434
    14021435        if ( self::$tinymce_scripts_printed ) {
     
    14091442            script_concat_settings();
    14101443        }
    1411 
    1412         $suffix  = SCRIPT_DEBUG ? '' : '.min';
    1413         $version = 'ver=' . $tinymce_version;
    1414         $baseurl = self::get_baseurl();
    1415 
    1416         $has_custom_theme = false;
    1417         foreach ( self::$mce_settings as $init ) {
    1418             if ( ! empty( $init['theme_url'] ) ) {
    1419                 $has_custom_theme = true;
    1420                 break;
    1421             }
    1422         }
    1423 
    1424         $compressed = $compress_scripts && $concatenate_scripts && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] )
    1425             && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && ! $has_custom_theme;
    1426 
    1427         // Load tinymce.js when running from /src, else load wp-tinymce.js.gz (production) or tinymce.min.js (SCRIPT_DEBUG)
    1428         $mce_suffix = false !== strpos( get_bloginfo( 'version' ), '-src' ) ? '' : '.min';
    1429 
    1430         if ( $compressed ) {
    1431             echo "<script type='text/javascript' src='{$baseurl}/wp-tinymce.php?c=1&amp;$version'></script>\n";
    1432         } else {
    1433             echo "<script type='text/javascript' src='{$baseurl}/tinymce{$mce_suffix}.js?$version'></script>\n";
    1434             echo "<script type='text/javascript' src='{$baseurl}/plugins/compat3x/plugin{$suffix}.js?$version'></script>\n";
    1435         }
    1436 
    1437         echo "<script type='text/javascript'>\n" . self::wp_mce_translation() . "</script>\n";
    14381444    }
    14391445
  • trunk/src/wp-includes/default-filters.php

    r43982 r44115  
    503503// Script Loader
    504504add_action( 'wp_default_scripts', 'wp_default_scripts' );
     505add_action( 'wp_default_scripts', 'wp_default_packages' );
     506
    505507add_action( 'wp_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
    506508add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
  • trunk/src/wp-includes/script-loader.php

    r44114 r44115  
    4242 * @param WP_Scripts $scripts WP_Scripts object.
    4343 */
    44 function wp_register_tinymce_scripts( &$scripts ) {
     44function wp_register_tinymce_scripts( &$scripts, $force_uncompressed = false ) {
    4545    global $tinymce_version, $concatenate_scripts, $compress_scripts;
    46     $suffix     = SCRIPT_DEBUG ? '' : '.min';
     46    $suffix = SCRIPT_DEBUG ? '' : '.min';
     47
     48    script_concat_settings();
     49
    4750    $compressed = $compress_scripts && $concatenate_scripts && isset( $_SERVER['HTTP_ACCEPT_ENCODING'] )
    48                   && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' );
     51                  && false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && ! $force_uncompressed;
     52
    4953    // Load tinymce.js when running from /src, otherwise load wp-tinymce.js.gz (in production) or
    5054    // tinymce.min.js (when SCRIPT_DEBUG is true).
     
    5357        $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . 'wp-tinymce.php', array(), $tinymce_version );
    5458    } else {
    55         $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce{$mce_suffix}.js", array(), $tinymce_version );
    56         $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin{$suffix}.js", array( 'wp-tinymce-root' ), $tinymce_version );
    57     }
    58 
    59     $scripts->add( 'wp-tinymce-lists', includes_url( 'js/tinymce/plugins/lists/index' . $suffix . '.js', array( 'wp-tinymce' ), $tinymce_version ) );
     59        $scripts->add( 'wp-tinymce-root', includes_url( 'js/tinymce/' ) . "tinymce$mce_suffix.js", array(), $tinymce_version );
     60        $scripts->add( 'wp-tinymce', includes_url( 'js/tinymce/' ) . "plugins/compat3x/plugin$suffix.js", array( 'wp-tinymce-root' ), $tinymce_version );
     61    }
     62
     63    $scripts->add( 'wp-tinymce-lists', includes_url( "js/tinymce/plugins/lists/plugin$suffix.js", array( 'wp-tinymce' ), $tinymce_version ) );
    6064}
    6165
     
    7074 * @param WP_Scripts $scripts WP_Scripts object.
    7175 */
    72 function wp_default_packages_vendor( &$scripts, $dev_suffix ) {
     76function wp_default_packages_vendor( &$scripts ) {
    7377    wp_register_tinymce_scripts( $scripts );
     78
     79    $dev_suffix = wp_scripts_get_suffix( 'dev' );
    7480
    7581    $vendor_scripts = array(
     
    9197        }
    9298
    93         $path = '/js/dist/vendor/' . $handle . $dev_suffix . '.js';
     99        $path = "/js/dist/vendor/$handle$dev_suffix.js";
    94100
    95101        $scripts->add( $handle, $path, $dependencies, false, 1 );
     
    125131 * @return string Conditional polyfill inline script.
    126132 */
    127 function wp_get_script_polyfill( $scripts, $tests ) {
     133function wp_get_script_polyfill( &$scripts, $tests ) {
    128134    $polyfill = '';
    129135    foreach ( $tests as $test => $handle ) {
     
    148154
    149155/**
    150  * Register all the WordPress packages scripts that are in the standardized
     156 * Registers all the WordPress packages scripts that are in the standardized
    151157 * `js/dist/` location.
    152158 *
     
    156162 *
    157163 * @param WP_Scripts $scripts WP_Scripts object.
    158  * @param string     $suffix  The suffix to use before `.js`.
    159  */
    160 function wp_default_packages_scripts( &$scripts, $suffix ) {
     164 */
     165function wp_default_packages_scripts( &$scripts ) {
     166    $suffix = wp_scripts_get_suffix();
     167
    161168    $packages_dependencies = array(
    162169        'api-fetch'                          => array( 'wp-polyfill', 'wp-hooks', 'wp-i18n' ),
     
    331338    foreach ( $packages_dependencies as $package => $dependencies ) {
    332339        $handle = 'wp-' . $package;
    333         $path   = '/js/dist/' . $package . $suffix . '.js';
     340        $path   = "/js/dist/$package$suffix.js";
    334341
    335342        $scripts->add( $handle, $path, $dependencies, false, 1 );
     
    423430    );
    424431
     432    /* This filter is documented in wp-includes/class-wp-editor.php */
    425433    $tinymce_settings = apply_filters(
    426434        'tiny_mce_before_init',
     
    429437                ',',
    430438                array_unique(
     439                    /* This filter is documented in wp-includes/class-wp-editor.php */
    431440                    apply_filters(
    432441                        'tiny_mce_plugins',
     
    457466                ',',
    458467                array_merge(
     468                    /* This filter is documented in wp-includes/class-wp-editor.php */
    459469                    apply_filters(
    460470                        'mce_buttons',
     
    482492            'toolbar2'         => implode(
    483493                ',',
     494                /* This filter is documented in wp-includes/class-wp-editor.php */
    484495                apply_filters(
    485496                    'mce_buttons_2',
     
    500511                )
    501512            ),
     513            /* This filter is documented in wp-includes/class-wp-editor.php */
    502514            'toolbar3'         => implode( ',', apply_filters( 'mce_buttons_3', array(), 'editor' ) ),
     515            /* This filter is documented in wp-includes/class-wp-editor.php */
    503516            'toolbar4'         => implode( ',', apply_filters( 'mce_buttons_4', array(), 'editor' ) ),
     517            /* This filter is documented in wp-includes/class-wp-editor.php */
    504518            'external_plugins' => apply_filters( 'mce_external_plugins', array() ),
    505519        ),
     
    526540
    527541/**
     542 * Registers all the WordPress packages scripts.
     543 *
     544 * @since 5.0.0
     545 *
     546 * @param WP_Scripts $scripts WP_Scripts object.
     547 */
     548function wp_default_packages( &$scripts ) {
     549    wp_default_packages_vendor( $scripts );
     550    wp_register_tinymce_scripts( $scripts );
     551    wp_default_packages_scripts( $scripts );
     552
     553    if ( did_action( 'init' ) ) {
     554        wp_default_packages_inline_scripts( $scripts );
     555    }
     556}
     557
     558/**
     559 * Returns the suffix that can be used for the scripts.
     560 *
     561 * There are two suffix types, the normal one and the dev suffix.
     562 *
     563 * @since 5.0.0
     564 *
     565 * @param string $type The type of suffix to retrieve.
     566 * @return string The script suffix.
     567 */
     568function wp_scripts_get_suffix( $type = '' ) {
     569    static $suffixes;
     570
     571    if ( $suffixes === null ) {
     572        include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
     573
     574        $develop_src = false !== strpos( $wp_version, '-src' );
     575
     576        if ( ! defined( 'SCRIPT_DEBUG' ) ) {
     577            define( 'SCRIPT_DEBUG', $develop_src );
     578        }
     579        $suffix     = SCRIPT_DEBUG ? '' : '.min';
     580        $dev_suffix = $develop_src ? '' : '.min';
     581
     582        $suffixes = array(
     583            'suffix'     => $suffix,
     584            'dev_suffix' => $dev_suffix,
     585        );
     586    }
     587
     588    if ( $type === 'dev' ) {
     589        return $suffixes['dev_suffix'];
     590    }
     591
     592    return $suffixes['suffix'];
     593}
     594
     595/**
    528596 * Register all WordPress scripts.
    529597 *
     
    537605 */
    538606function wp_default_scripts( &$scripts ) {
    539     include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
    540 
    541     $develop_src = false !== strpos( $wp_version, '-src' );
    542 
    543     if ( ! defined( 'SCRIPT_DEBUG' ) ) {
    544         define( 'SCRIPT_DEBUG', $develop_src );
    545     }
     607    $suffix     = wp_scripts_get_suffix();
     608    $dev_suffix = wp_scripts_get_suffix( 'dev' );
    546609
    547610    if ( ! $guessurl = site_url() ) {
     
    554617    $scripts->default_version = get_bloginfo( 'version' );
    555618    $scripts->default_dirs    = array( '/wp-admin/js/', '/wp-includes/js/' );
    556 
    557     $suffix     = SCRIPT_DEBUG ? '' : '.min';
    558     $dev_suffix = $develop_src ? '' : '.min';
    559619
    560620    $scripts->add( 'utils', "/wp-includes/js/utils$suffix.js" );
     
    625685    $scripts->add( 'colorpicker', "/wp-includes/js/colorpicker$suffix.js", array( 'prototype' ), '3517m' );
    626686
    627     $scripts->add( 'editor', "/wp-admin/js/editor$suffix.js", array( 'utils', 'jquery' ), false, 1 );
     687    $scripts->add( 'editor', "/wp-admin/js/editor$suffix.js", array( 'wp-tinymce', 'utils', 'jquery' ), false, 1 );
    628688
    629689    // Back-compat for old DFW. To-do: remove at the end of 2016.
     
    12111271    $scripts->add( 'wp-api', "/wp-includes/js/wp-api$suffix.js", array( 'jquery', 'backbone', 'underscore', 'wp-api-request' ), false, 1 );
    12121272
    1213     wp_default_packages_vendor( $scripts, $dev_suffix );
    1214     wp_default_packages_scripts( $scripts, $suffix );
    1215     if ( did_action( 'init' ) ) {
    1216         wp_default_packages_inline_scripts( $scripts );
    1217     }
    1218 
    12191273    if ( is_admin() ) {
    12201274        $scripts->add( 'admin-tags', "/wp-admin/js/tags$suffix.js", array( 'jquery', 'wp-ajax-response' ), false, 1 );
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r43565 r44115  
    1111        $this->old_wp_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null;
    1212        remove_action( 'wp_default_scripts', 'wp_default_scripts' );
     13        remove_action( 'wp_default_scripts', 'wp_default_packages' );
    1314        $GLOBALS['wp_scripts']                  = new WP_Scripts();
    1415        $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
  • trunk/tools/webpack/media.js

    r44112 r44115  
    1919
    2020module.exports = function( env = { environment: 'production', watch: false } ) {
    21     const mode = env.environment;
    2221
    2322    const mediaConfig = {
    24         mode,
     23        mode: "production",
    2524        cache: true,
    2625        entry: Object.assign( admin_files, include_files ),
Note: See TracChangeset for help on using the changeset viewer.