Make WordPress Core

Changeset 53360


Ignore:
Timestamp:
05/07/2022 02:51:54 AM (3 years 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.
Fixes #55628.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class.wp-scripts.php

    r53283 r53360  
    311311            $inline_script_tag = '';
    312312        }
     313
     314        $translations_stop_concat = ! empty( $obj->textdomain );
    313315
    314316        $translations = $this->print_translations( $handle, false );
     
    328330            $srce = apply_filters( 'script_loader_src', $src, $handle );
    329331
    330             if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) {
     332            if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) {
    331333                $this->do_concat = false;
    332334
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r52937 r53360  
    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.