WordPress.org

Make WordPress Core

Changeset 45685 for trunk


Ignore:
Timestamp:
07/27/2019 12:43:56 PM (2 years ago)
Author:
ocean90
Message:

I18N: Add support for custom WP_PLUGIN_URL in load_script_textdomain().

Plugins may not be on the same host/path as the rest of the content. To support loading translations for this setup check if the script source matches plugins_url().
Also fixes an undefined index notice when a custom content URL has no path.

Props odminstudios, ocean90.
Fixes #46336, #46387.

Location:
trunk
Files:
2 edited

Legend:

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

    r45590 r45685  
    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    ) {
    958959        // Make the src relative the specific plugin or theme.
    959         $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
     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
    964         $relative = array_slice( $relative, 2 );
     970        $relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
     971        $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 ); // Remove <plugin name>.
    965988        $relative = implode( '/', $relative );
    966989    } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
  • trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php

    r44310 r45685  
    1616
    1717        return $relative;
     18    }
     19
     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';
    1826    }
    1927
     
    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}
Note: See TracChangeset for help on using the changeset viewer.