Make WordPress Core

Changeset 49639


Ignore:
Timestamp:
11/17/2020 08:50:21 PM (6 years ago)
Author:
SergeyBiryukov
Message:

I18N: Avoid PHP notices for relative URLs in load_script_textdomain().

Props hellofromTonya, SeBsZ, archon810, nourma, justinahinon, SergeyBiryukov.
Fixes #49145.

Location:
trunk
Files:
2 edited

Legend:

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

    r49566 r49639  
    10411041        if (
    10421042                ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
    1043                 ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
     1043                ( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
    10441044        ) {
    10451045                // Make the src relative the specific plugin or theme.
     
    10581058        } elseif (
    10591059                ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
    1060                 ( ! isset( $src_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
     1060                ( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
    10611061        ) {
    10621062                // Make the src relative the specific plugin.
     
    10731073                $relative = array_slice( $relative, 1 ); // Remove <plugin name>.
    10741074                $relative = implode( '/', $relative );
    1075         } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
     1075        } elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
    10761076                if ( ! isset( $site_url['path'] ) ) {
    10771077                        $relative = trim( $src_url['path'], '/' );
  • trunk/tests/phpunit/tests/l10n/loadScriptTextdomain.php

    r48937 r49639  
    66 */
    77class Tests_L10n_loadScriptTextdomain extends WP_UnitTestCase {
    8         public function site_url_subdirectory( $site_url ) {
    9                 return $site_url . '/wp';
     8
     9        /**
     10         * @ticket 45528
     11         * @ticket 46336
     12         * @ticket 46387
     13         * @ticket 49145
     14         *
     15         * @dataProvider data_test_resolve_relative_path
     16         */
     17        public function test_resolve_relative_path( $translation_path, $handle, $src, $textdomain, $filter = array() ) {
     18                if ( ! empty( $filter ) ) {
     19                        add_filter( $filter[0], $filter[1], 10, isset( $filter[2] ) ? $filter[2] : 1 );
     20                }
     21                wp_enqueue_script( $handle, $src, array(), null );
     22
     23                $expected = file_get_contents( DIR_TESTDATA . $translation_path );
     24                $this->assertSame( $expected, load_script_textdomain( $handle, $textdomain, DIR_TESTDATA . '/languages' ) );
     25        }
     26
     27        public function data_test_resolve_relative_path() {
     28                return array(
     29                        // @ticket 45528
     30                        array(
     31                                '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
     32                                'test-example-root',
     33                                '/wp-includes/js/script.js',
     34                                'default',
     35                        ),
     36                        // Assets on a CDN.
     37                        array(
     38                                '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
     39                                'test-example-cdn',
     40                                'https://my-cdn.com/wordpress/wp-includes/js/script.js',
     41                                'default',
     42                                array( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 2 ),
     43                        ),
     44                        // Test for WordPress installs in a subdirectory.
     45                        array(
     46                                '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
     47                                'test-example-subdir',
     48                                '/wp/wp-includes/js/script.js',
     49                                'default',
     50                                array(
     51                                        'site_url',
     52                                        function ( $site_url ) {
     53                                                return $site_url . '/wp';
     54                                        },
     55                                ),
     56                        ),
     57                        // @ticket 46336
     58                        array(
     59                                '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
     60                                'plugin-example-1',
     61                                'https://plugins.example.com/my-plugin/js/script.js',
     62                                'internationalized-plugin',
     63                                array(
     64                                        'plugins_url',
     65                                        function () {
     66                                                return 'https://plugins.example.com';
     67                                        },
     68                                ),
     69                        ),
     70                        // @ticket 46387
     71                        array(
     72                                '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
     73                                'plugin-example-2',
     74                                'https://content.example.com/plugins/my-plugin/js/script.js',
     75                                'internationalized-plugin',
     76                                array(
     77                                        'content_url',
     78                                        function () {
     79                                                return 'https://content.example.com';
     80                                        },
     81                                ),
     82                        ),
     83                        // @ticket 49145
     84                        array(
     85                                '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
     86                                'test-when-no-content_url-host',
     87                                'https://content.example.com/plugins/my-plugin/js/script.js',
     88                                'internationalized-plugin',
     89                                array(
     90                                        'content_url',
     91                                        function () {
     92                                                return '/';
     93                                        },
     94                                ),
     95                        ),
     96                        // @ticket 49145
     97                        array(
     98                                '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json',
     99                                'test-when-no-plugins_url-host',
     100                                'https://plugins.example.com/my-plugin/js/script.js',
     101                                'internationalized-plugin',
     102                                array(
     103                                        'plugins_url',
     104                                        function () {
     105                                                return '/';
     106                                        },
     107                                ),
     108                        ),
     109                        // @ticket 49145
     110                        array(
     111                                '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json',
     112                                'test-when-no-site_url-host',
     113                                '/wp/wp-includes/js/script.js',
     114                                'default',
     115                                array(
     116                                        'site_url',
     117                                        function () {
     118                                                return '/wp';
     119                                        },
     120                                ),
     121                        ),
     122                );
    10123        }
    11124
     
    17130                return $relative;
    18131        }
    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';
    26         }
    27 
    28         /**
    29          * @ticket 45528
    30          */
    31         public function test_resolve_relative_path() {
    32                 $json_translations = file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' );
    33 
    34                 wp_enqueue_script( 'test-example-root', '/wp-includes/js/script.js', array(), null );
    35                 $this->assertSame( $json_translations, load_script_textdomain( 'test-example-root', 'default', DIR_TESTDATA . '/languages' ) );
    36 
    37                 // Assets on a CDN.
    38                 add_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ), 10, 2 );
    39                 wp_enqueue_script( 'test-example-cdn', 'https://my-cdn.com/wordpress/wp-includes/js/script.js', array(), null );
    40                 $this->assertSame( $json_translations, load_script_textdomain( 'test-example-cdn', 'default', DIR_TESTDATA . '/languages' ) );
    41                 remove_filter( 'load_script_textdomain_relative_path', array( $this, 'relative_path_from_cdn' ) );
    42 
    43                 // Test for WordPress installs in a subdirectory.
    44                 add_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
    45                 wp_enqueue_script( 'test-example-subdir', '/wp/wp-includes/js/script.js', array(), null );
    46                 $this->assertSame( $json_translations, load_script_textdomain( 'test-example-subdir', 'default', DIR_TESTDATA . '/languages' ) );
    47                 remove_filter( 'site_url', array( $this, 'site_url_subdirectory' ) );
    48         }
    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->assertSame( $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->assertSame( $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         }
    73132}
Note: See TracChangeset for help on using the changeset viewer.