WordPress.org

Make WordPress Core

Ticket #45528: 45528.6.patch

File 45528.6.patch, 4.8 KB (added by herregroen, 11 months ago)
  • src/wp-includes/l10n.php

     
    888888function load_script_textdomain( $handle, $domain, $path = null ) {
    889889        global $wp_scripts;
    890890
     891        if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
     892                return false;
     893        }
     894
    891895        $path   = untrailingslashit( $path );
    892896        $locale = determine_locale();
    893897
     
    900904
    901905        $obj = $wp_scripts->registered[ $handle ];
    902906
     907        $src = $obj->src;
     908        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
     909                $src = $wp_scripts->base_url . $src;
     910        }
    903911        /** This filter is documented in wp-includes/class.wp-scripts.php */
    904         $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     912        $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
    905913
    906914        $relative       = false;
    907915        $languages_path = WP_LANG_DIR;
     
    913921        // If the host is the same or it's a relative URL.
    914922        if (
    915923                strpos( $src_url['path'], $content_url['path'] ) === 0 &&
    916                 ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     924                ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
    917925        ) {
    918926                // Make the src relative the specific plugin or theme.
    919                 $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     927                $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
    920928                $relative = explode( '/', $relative );
    921929
    922930                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     
    923931
    924932                $relative = array_slice( $relative, 2 );
    925933                $relative = implode( '/', $relative );
    926         } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
     934        } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
    927935                if ( ! isset( $site_url['path'] ) ) {
    928936                        $relative = trim( $src_url['path'], '/' );
    929                 } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
     937                } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
    930938                        // Make the src relative to the WP root.
    931                         $relative = substr( $src, strlen( $site_url['path'] ) );
     939                        $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
    932940                        $relative = trim( $relative, '/' );
    933941                }
    934942        }
    935943
     944        /**
     945         * Filters the relative path of scripts used for finding translation files.
     946         *
     947         * @since 5.0.2
     948         *
     949         * @param string $relative The relative path of the script. False if it could not be determined.
     950         * @param string $src      The full source url of the script.
     951         */
     952        $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
     953
    936954        // If the source is not from WP.
    937955        if ( false === $relative ) {
    938956                return false;
  • 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        public function site_url_subdirectory( $site_url ) {
     9                return $site_url . '/wp';
     10        }
     11
     12        public function relative_path_from_cdn( $relative, $src ) {
     13                if ( 0 === strpos( $src, 'https://my-cdn.com/wordpress/' ) ) {
     14                        return substr( $src, strlen( 'https://my-cdn.com/wordpress/' ) );
     15                }
     16
     17                return $relative;
     18        }
     19
     20        /**
     21         * @ticket 45528
     22         */
     23        public function test_resolve_relative_path() {
     24                $json_translations = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
     25
     26                wp_enqueue_script( 'test-example-root', '/wp-includes/js/script.js', array(), null );
     27                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-root', 'default', DIR_TESTDATA . '/languages' ) );
     28
     29                // Assets on a CDN.
     30                add_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 10, 2 );
     31                wp_enqueue_script( 'test-example-cdn', 'https://my-cdn.com/wordpress/wp-includes/js/script.js', array(), null );
     32                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-cdn', 'default', DIR_TESTDATA . '/languages' ) );
     33                remove_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ) );
     34
     35                // Test for WordPress installs in a subdirectory.
     36                add_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
     37                wp_enqueue_script( 'test-example-subdir', '/wp/wp-includes/js/script.js', array(), null );
     38                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
     39                remove_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
     40        }
     41}