WordPress.org

Make WordPress Core

Ticket #45488: 45488.diff

File 45488.diff, 11.0 KB (added by swissspidy, 11 months ago)
  • src/wp-includes/l10n.php

    diff --git src/wp-includes/l10n.php src/wp-includes/l10n.php
    index def655b414..37b7e492c1 100644
    function load_child_theme_textdomain( $domain, $path = false ) { 
    888888function load_script_textdomain( $handle, $domain, $path = null ) {
    889889        global $wp_scripts;
    890890
    891         $path   = untrailingslashit( $path );
    892         $locale = determine_locale();
    893 
    894         // If a path was given and the handle file exists simply return it.
    895         $file_base       = $domain === 'default' ? $locale : $domain . '-' . $locale;
    896         $handle_filename = $file_base . '-' . $handle . '.json';
    897         if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
    898                 return file_get_contents( $path . '/' . $handle_filename );
    899         }
     891        $path      = untrailingslashit( $path );
     892        $locale    = determine_locale();
     893        $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale;
    900894
    901895        $obj = $wp_scripts->registered[ $handle ];
    902896
    function load_script_textdomain( $handle, $domain, $path = null ) { 
    933927                }
    934928        }
    935929
    936         // If the source is not from WP.
    937         if ( false === $relative ) {
    938                 return false;
    939         }
    940 
    941930        // Translations are always based on the unminified filename.
    942         if ( substr( $relative, -7 ) === '.min.js' ) {
     931        if ( $relative && substr( $relative, -7 ) === '.min.js' ) {
    943932                $relative = substr( $relative, 0, -7 ) . '.js';
    944933        }
    945934
    946         $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    947         if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
    948                 return file_get_contents( $path . '/' . $md5_filename );
     935        $handle_filename = $file_base . '-' . $handle . '.json';
     936
     937        $md5_filename = false;
     938
     939        if ( $relative ) {
     940                $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    949941        }
    950         if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
     942
     943        if ( $md5_filename && file_exists( $languages_path . '/' . $md5_filename ) ) {
    951944                return file_get_contents( $languages_path . '/' . $md5_filename );
    952945        }
    953946
     947        if ( $path ) {
     948                if ( file_exists( $path . '/' . $handle_filename ) ) {
     949                        return file_get_contents( $path . '/' . $handle_filename );
     950                }
     951
     952                if ( $md5_filename && file_exists( $path . '/' . $md5_filename ) ) {
     953                        return file_get_contents( $path . '/' . $md5_filename );
     954                }
     955        }
     956
    954957        return false;
    955958}
    956959
  • new file tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json

    diff --git tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json
    new file mode 100644
    index 0000000000..280ccc1424
    - +  
     1{
     2  "translation-revision-data": "+0000",
     3  "generator":                 "GlotPress/2.3.0-alpha",
     4  "domain":                    "messages",
     5  "locale_data":               {
     6    "messages": {
     7      "":                                                             {
     8        "domain":       "messages",
     9        "plural-forms": "n != 1",
     10        "lang":         "en-gb"
     11      },
     12      "This is a dummy theme.": [
     13        "This is a dummy theme."
     14      ]
     15    }
     16  }
     17}
  • new file tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json

    diff --git tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json tests/phpunit/data/themedir1/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json
    new file mode 100644
    index 0000000000..280ccc1424
    - +  
     1{
     2  "translation-revision-data": "+0000",
     3  "generator":                 "GlotPress/2.3.0-alpha",
     4  "domain":                    "messages",
     5  "locale_data":               {
     6    "messages": {
     7      "":                                                             {
     8        "domain":       "messages",
     9        "plural-forms": "n != 1",
     10        "lang":         "en-gb"
     11      },
     12      "This is a dummy theme.": [
     13        "This is a dummy theme."
     14      ]
     15    }
     16  }
     17}
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git tests/phpunit/tests/dependencies/scripts.php tests/phpunit/tests/dependencies/scripts.php
    index 6b84a81ae7..07c60b4094 100644
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    773773        public function test_wp_set_script_translations() {
    774774                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    775775                wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
    776                 wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
     776                wp_set_script_translations( 'test-example', 'default' );
    777777
    778778                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    779779                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    791791        public function test_wp_set_script_translations_for_plugin() {
    792792                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    793793                wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null );
    794                 wp_set_script_translations( 'plugin-example', 'internationalized-plugin',  DIR_TESTDATA . '/languages/plugins' );
     794                wp_set_script_translations( 'plugin-example', 'internationalized-plugin',  WP_LANG_DIR . '/plugins' );
    795795
    796796                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    797797                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    809809        public function test_wp_set_script_translations_for_theme() {
    810810                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    811811                wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null );
    812                 wp_set_script_translations( 'theme-example', 'internationalized-theme',  DIR_TESTDATA . '/languages/themes' );
     812                wp_set_script_translations( 'theme-example', 'internationalized-theme',  WP_LANG_DIR . '/themes' );
    813813
    814814                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    815815                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    821821                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    822822        }
    823823
     824        /**
     825         * @ticket 45488
     826         */
     827        public function test_wp_set_script_translations_for_theme_in_theme_dir_md5() {
     828                $theme_dir = $GLOBALS['wp_theme_directories'][0];
     829
     830                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     831                wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null );
     832                wp_set_script_translations( 'theme-example', 'internationalized-theme',  $theme_dir . '/internationalized-theme/languages' );
     833
     834                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     835                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     836                             "translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" .
     837                             "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" .
     838                             "})(" . file_get_contents( $theme_dir . '/internationalized-theme/languages/internationalized-theme-en_US-9a9569e9d73f33740eada95275da7f30.json' ) . ");\n</script>\n";
     839                $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
     840
     841                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     842        }
     843
     844        /**
     845         * @ticket 45488
     846         */
     847        public function test_wp_set_script_translations_for_theme_in_theme_dir_handle() {
     848                $theme_dir = $GLOBALS['wp_theme_directories'][0];
     849
     850                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     851                wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/another-script.js', array(), null );
     852                wp_set_script_translations( 'theme-example', 'internationalized-theme',  $theme_dir . '/internationalized-theme/languages' );
     853
     854                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     855                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     856                             "translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" .
     857                             "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" .
     858                             "})(" . file_get_contents( $theme_dir . '/internationalized-theme/languages/internationalized-theme-en_US-theme-example.json' ) . ");\n</script>\n";
     859                $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/another-script.js'></script>\n";
     860
     861                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     862        }
     863
    824864        /**
    825865         * @ticket 45103
    826866         */
    827867        public function test_wp_set_script_translations_with_handle_file() {
    828868                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    829869                wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null );
    830                 wp_set_script_translations( 'script-handle', 'admin',  DIR_TESTDATA . '/languages/' );
     870                wp_set_script_translations( 'script-handle', 'admin', WP_LANG_DIR );
    831871
    832872                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    833873                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    847887
    848888                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    849889                wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
    850                 wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages/' );
     890                wp_set_script_translations( 'test-example', 'default' );
    851891
    852892                $script = $wp_scripts->registered[ 'test-example' ];
    853893
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    878918        public function test_wp_set_script_translations_after_register() {
    879919                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    880920                wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null );
    881                 wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
     921                wp_set_script_translations( 'test-example', 'default' );
    882922
    883923                wp_enqueue_script( 'test-example' );
    884924
    class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    898938        public function test_wp_set_script_translations_dependency() {
    899939                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    900940                wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null );
    901                 wp_set_script_translations( 'test-dependency', 'default',  DIR_TESTDATA . '/languages' );
     941                wp_set_script_translations( 'test-dependency', 'default' );
    902942
    903943                wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
    904944