Ticket #45528: 45528.6.patch
File 45528.6.patch, 4.8 KB (added by , 6 years ago) |
---|
-
src/wp-includes/l10n.php
888 888 function load_script_textdomain( $handle, $domain, $path = null ) { 889 889 global $wp_scripts; 890 890 891 if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { 892 return false; 893 } 894 891 895 $path = untrailingslashit( $path ); 892 896 $locale = determine_locale(); 893 897 … … 900 904 901 905 $obj = $wp_scripts->registered[ $handle ]; 902 906 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 } 903 911 /** 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 ) ); 905 913 906 914 $relative = false; 907 915 $languages_path = WP_LANG_DIR; … … 913 921 // If the host is the same or it's a relative URL. 914 922 if ( 915 923 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'] ) 917 925 ) { 918 926 // 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'] ) ), '/' ); 920 928 $relative = explode( '/', $relative ); 921 929 922 930 $languages_path = WP_LANG_DIR . '/' . $relative[0]; … … 923 931 924 932 $relative = array_slice( $relative, 2 ); 925 933 $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'] ) { 927 935 if ( ! isset( $site_url['path'] ) ) { 928 936 $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 ) ) { 930 938 // 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'] ) ); 932 940 $relative = trim( $relative, '/' ); 933 941 } 934 942 } 935 943 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 936 954 // If the source is not from WP. 937 955 if ( false === $relative ) { 938 956 return false; -
tests/phpunit/tests/l10n/loadScriptTextdomain.php
1 <?php 2 3 /** 4 * @group l10n 5 * @group i18n 6 */ 7 class 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 }