Make WordPress Core

Ticket #46336: 46336.patch

File 46336.patch, 4.2 KB (added by ocean90, 6 years ago)
  • src/wp-includes/l10n.php

     
    948948
    949949        $src_url     = wp_parse_url( $src );
    950950        $content_url = wp_parse_url( content_url() );
     951        $plugins_url = wp_parse_url( plugins_url() );
    951952        $site_url    = wp_parse_url( site_url() );
    952953
    953954        // If the host is the same or it's a relative URL.
    954955        if (
    955                 strpos( $src_url['path'], $content_url['path'] ) === 0 &&
     956                ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
    956957                ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
    957958        ) {
    958                 // Make the src relative the specific plugin or theme.
    959                 $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
     959                // Make the src relative the specific plugin.
     960                if ( isset( $content_url['path'] ) ) {
     961                        $relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
     962                } else {
     963                        $relative = $src_url['path'];
     964                }
     965                $relative = trim( $relative, '/' );
    960966                $relative = explode( '/', $relative );
    961967
    962968                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     
    963969
    964970                $relative = array_slice( $relative, 2 );
    965971                $relative = implode( '/', $relative );
     972        } elseif (
     973                ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
     974                ( ! isset( $src_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
     975        ) {
     976                // Make the src relative the specific plugin.
     977                if ( isset( $plugins_url['path'] ) ) {
     978                        $relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
     979                } else {
     980                        $relative = $src_url['path'];
     981                }
     982                $relative = trim( $relative, '/' );
     983                $relative = explode( '/', $relative );
     984
     985                $languages_path = WP_LANG_DIR . '/plugins';
     986
     987                $relative = array_slice( $relative, 1 );
     988                $relative = implode( '/', $relative );
    966989        } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
    967990                if ( ! isset( $site_url['path'] ) ) {
    968991                        $relative = trim( $src_url['path'], '/' );
  • tests/phpunit/tests/l10n/loadScriptTextdomain.php

     
    1717                return $relative;
    1818        }
    1919
     20        public function plugins_url_custom_domain() {
     21                return 'https://plugins.example.com';
     22        }
     23
     24        public function content_url_custom_domain_with_no_path() {
     25                return 'https://content.example.com';
     26        }
     27
    2028        /**
    2129         * @ticket 45528
    2230         */
     
    3846                $this->assertEquals( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
    3947                remove_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
    4048        }
     49
     50        /**
     51         * @ticket 46336
     52         */
     53        public function test_resolve_relative_path_custom_plugins_url() {
     54                $json_translations = file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' );
     55
     56                add_filter( 'plugins_url', array( $this, 'plugins_url_custom_domain' ) );
     57                wp_enqueue_script( 'plugin-example-1', 'https://plugins.example.com/my-plugin/js/script.js', array(), null );
     58                $this->assertEquals( $json_translations, load_script_textdomain( 'plugin-example-1', 'internationalized-plugin', DIR_TESTDATA . '/languages' ) );
     59                remove_filter( 'plugins_url', array( $this, 'plugins_url_custom_domain' ) );
     60        }
     61
     62        /**
     63         * @ticket 46387
     64         */
     65        public function test_resolve_relative_path_custom_content_url() {
     66                $json_translations = file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' );
     67
     68                add_filter( 'content_url', array( $this, 'content_url_custom_domain_with_no_path' ) );
     69                wp_enqueue_script( 'plugin-example-2', 'https://content.example.com/plugins/my-plugin/js/script.js', array(), null );
     70                $this->assertEquals( $json_translations, load_script_textdomain( 'plugin-example-2', 'internationalized-plugin', DIR_TESTDATA . '/languages' ) );
     71                remove_filter( 'content_url', array( $this, 'content_url_custom_domain_with_no_path' ) );
     72        }
    4173}