WordPress.org

Make WordPress Core

Ticket #45528: 45528.4.patch

File 45528.4.patch, 3.7 KB (added by akirk, 11 months ago)

Add unit tests

  • src/wp-includes/l10n.php

     
    924924
    925925        $obj = $wp_scripts->registered[ $handle ];
    926926
     927        $src = $obj->src;
     928        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
     929                $src = $wp_scripts->base_url . $src;
     930        }
    927931        /** This filter is documented in wp-includes/class.wp-scripts.php */
    928         $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     932        $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
    929933
    930934        $relative       = false;
    931935        $languages_path = WP_LANG_DIR;
     
    937941        // If the host is the same or it's a relative URL.
    938942        if (
    939943                strpos( $src_url['path'], $content_url['path'] ) === 0 &&
    940                 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     944                ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
    941945        ) {
    942946                // Make the src relative the specific plugin or theme.
    943                 $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     947                $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
    944948                $relative = explode( '/', $relative );
    945949
    946950                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     
    947951
    948952                $relative = array_slice( $relative, 2 );
    949953                $relative = implode( '/', $relative );
    950         } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
     954        } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
    951955                if ( ! isset( $site_url['path'] ) ) {
    952956                        $relative = trim( $src_url['path'], '/' );
    953                 } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
     957                } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
    954958                        // Make the src relative to the WP root.
    955                         $relative = substr( $src, strlen( $site_url['path'] ) );
     959                        $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
    956960                        $relative = trim( $relative, '/' );
    957961                }
     962        } elseif ( isset( $src_url['host'] ) && $src_url['host'] !== $site_url['host'] && false !== strpos( $src_url['path'], '/wp-includes/' ) ) {
     963                // This handles CDNs that mirror all static assets.
     964                $relative = substr( $src_url['path'], 1 + strpos( $src_url['path'], '/wp-includes/' ) );
    958965        }
    959966
    960967        // If the source is not from WP.
  • tests/phpunit/tests/l10n/loadScriptTextdomain.php

     
     1<?php
     2
     3/**
     4 * @group l10n
     5 * @group i18n
     6 */
     7class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
     8        /**
     9         * @ticket 45528
     10         */
     11        public function test_resolve_relative_path() {
     12                $json_translations = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
     13
     14                wp_enqueue_script( 'test-example-root', '/wp-includes/js/script.js', array(), null );
     15                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-root', 'default', DIR_TESTDATA . '/languages' ) );
     16
     17                wp_enqueue_script( 'test-example-cdn', 'https://c0.wp.com/c/5.0.1/wp-includes/js/script.js', array(), null );
     18                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-cdn', 'default', DIR_TESTDATA . '/languages' ) );
     19
     20                update_option( 'siteurl', get_option( 'siteurl' ) . 'wp/' );
     21                wp_enqueue_script( 'test-example-subdir', '/wp/wp-includes/js/script.js', array(), null );
     22                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
     23
     24        }
     25}