Make WordPress Core

Changeset 53367


Ignore:
Timestamp:
05/09/2022 12:01:48 AM (23 months ago)
Author:
peterwilsoncc
Message:

Script Loader: Fix i18n edge case breaking dependencies.

Prevent concatenation of scripts if the text domain is defined to ensure the dependency order is respected.

This accounts for an edge case in which replacing a core script via a plugin and a lack of translations (eg, for a US English site) could cause the JavaScript files to be ordered incorrectly.

Follow up to [52937].

Props audrasjb, boniu91, chaion07, costdev, hellofromtonya, jsnajdr, mukesh27, ndiego, ugyensupport, SergeyBiryukov.
Merges [53360,53366] to the 6.0 branch.
Fixes #55628.

Location:
branches/6.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0

  • branches/6.0/src/wp-includes/class.wp-scripts.php

    r53283 r53367  
    311311            $inline_script_tag = '';
    312312        }
     313
     314        /*
     315         * Prevent concatenation of scripts if the text domain is defined
     316         * to ensure the dependency order is respected.
     317         */
     318        $translations_stop_concat = ! empty( $obj->textdomain );
    313319
    314320        $translations = $this->print_translations( $handle, false );
     
    328334            $srce = apply_filters( 'script_loader_src', $src, $handle );
    329335
    330             if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) {
     336            if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) {
    331337                $this->do_concat = false;
    332338
  • branches/6.0/tests/phpunit/tests/dependencies/scripts.php

    r52937 r53367  
    14821482        );
    14831483    }
     1484
     1485    /**
     1486     * @ticket 55628
     1487     * @covers ::wp_set_script_translations
     1488     */
     1489    public function test_wp_external_wp_i18n_print_order() {
     1490        global $wp_scripts;
     1491
     1492        $wp_scripts->do_concat    = true;
     1493        $wp_scripts->default_dirs = array( '/default/' );
     1494
     1495        // wp-i18n script in a non-default directory.
     1496        wp_register_script( 'wp-i18n', '/plugins/wp-i18n.js', array(), null );
     1497        // Script in default dir that's going to be concatenated.
     1498        wp_enqueue_script( 'jquery-core', '/default/jquery-core.js', array(), null );
     1499        // Script in default dir that depends on wp-i18n.
     1500        wp_enqueue_script( 'common', '/default/common.js', array(), null );
     1501        wp_set_script_translations( 'common' );
     1502
     1503        $print_scripts = get_echo(
     1504            function() {
     1505                wp_print_scripts();
     1506                _print_scripts();
     1507            }
     1508        );
     1509
     1510        // The non-default script should end concatenation and maintain order.
     1511        $ver       = get_bloginfo( 'version' );
     1512        $expected  = "<script type='text/javascript' src='/wp-admin/load-scripts.php?c=0&amp;load%5Bchunk_0%5D=jquery-core&amp;ver={$ver}'></script>\n";
     1513        $expected .= "<script type='text/javascript' src='/plugins/wp-i18n.js' id='wp-i18n-js'></script>\n";
     1514        $expected .= "<script type='text/javascript' src='/default/common.js' id='common-js'></script>\n";
     1515
     1516        $this->assertSame( $expected, $print_scripts );
     1517    }
    14841518}
Note: See TracChangeset for help on using the changeset viewer.