WordPress.org

Make WordPress Core

Ticket #41498: patch.diff

File patch.diff, 9.4 KB (added by jonasemde, 23 months ago)

Patch

  • wp-includes/l10n.php

     
    138138                $determined_locale = get_user_locale();
    139139        }
    140140
    141         if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
     141        if ( ! empty( $_GET['wp_lang'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
    142142                $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
    143143        }
    144144
     
    166166 *                       Default 'default'.
    167167 * @return string Translated text
    168168 */
    169 function translate( $text, $domain = 'default' ) {
    170         $translations = get_translations_for_domain( $domain );
    171         $translation  = $translations->translate( $text );
    172 
    173         /**
    174          * Filters text with its translation.
    175          *
    176          * @since 2.0.11
    177          *
    178          * @param string $translation  Translated text.
    179          * @param string $text         Text to translate.
    180          * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
    181          */
    182         return apply_filters( 'gettext', $translation, $text, $domain );
     169if (! function_exists('translate')) {
     170        function translate( $text, $domain = 'default' ) {
     171                $translations = get_translations_for_domain( $domain );
     172                $translation  = $translations->translate( $text );
     173
     174                /**
     175                 * Filters text with its translation.
     176                 *
     177                 * @since 2.0.11
     178                 *
     179                 * @param string $translation  Translated text.
     180                 * @param string $text         Text to translate.
     181                 * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
     182                 */
     183                return apply_filters( 'gettext', $translation, $text, $domain );
     184        }
    183185}
    184186
    185187/**
     
    246248 *                       Default 'default'.
    247249 * @return string Translated text.
    248250 */
    249 function __( $text, $domain = 'default' ) {
    250         return translate( $text, $domain );
     251if (! function_exists('__')) {
     252        function __( $text, $domain = 'default' ) {
     253                return translate( $text, $domain );
     254        }
    251255}
    252256
    253257/**
     
    870874}
    871875
    872876/**
    873  * Loads the script translated strings.
    874  *
    875  * @since 5.0.0
    876  * @since 5.0.2 Uses load_script_translations() to load translation data.
     877 * Load the script translated strings.
    877878 *
    878879 * @see WP_Scripts::set_translations()
     880 * @link https://core.trac.wordpress.org/ticket/45103
     881 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     882 *
     883 * @since 5.0.0
    879884 *
    880885 * @param string $handle Name of the script to register a translation domain to.
    881  * @param string $domain The text domain.
     886 * @param string $domain The textdomain.
    882887 * @param string $path   Optional. The full file path to the directory containing translation files.
    883888 *
    884889 * @return false|string False if the script textdomain could not be loaded, the translated strings
    885890 *                      in JSON encoding otherwise.
    886891 */
    887892function load_script_textdomain( $handle, $domain, $path = null ) {
    888         $wp_scripts = wp_scripts();
    889 
    890         if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
    891                 return false;
    892         }
     893        global $wp_scripts;
    893894
    894895        $path   = untrailingslashit( $path );
    895896        $locale = determine_locale();
     
    897898        // If a path was given and the handle file exists simply return it.
    898899        $file_base       = $domain === 'default' ? $locale : $domain . '-' . $locale;
    899900        $handle_filename = $file_base . '-' . $handle . '.json';
    900 
    901         if ( $path ) {
    902                 $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
    903 
    904                 if ( $translations ) {
    905                         return $translations;
    906                 }
     901        if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
     902                return file_get_contents( $path . '/' . $handle_filename );
    907903        }
    908904
    909         $src = $wp_scripts->registered[ $handle ]->src;
     905        $obj = $wp_scripts->registered[ $handle ];
    910906
    911         if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
    912                 $src = $wp_scripts->base_url . $src;
    913         }
     907        /** This filter is documented in wp-includes/class.wp-scripts.php */
     908        $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
    914909
    915910        $relative       = false;
    916911        $languages_path = WP_LANG_DIR;
     
    922917        // If the host is the same or it's a relative URL.
    923918        if (
    924919                strpos( $src_url['path'], $content_url['path'] ) === 0 &&
    925                 ( ! isset( $src_url['host'] ) || $src_url['host'] === $content_url['host'] )
     920                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
    926921        ) {
    927922                // Make the src relative the specific plugin or theme.
    928                 $relative = trim( substr( $src_url['path'], strlen( $content_url['path'] ) ), '/' );
     923                $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
    929924                $relative = explode( '/', $relative );
    930925
    931926                $languages_path = WP_LANG_DIR . '/' . $relative[0];
    932927
    933928                $relative = array_slice( $relative, 2 );
    934929                $relative = implode( '/', $relative );
    935         } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
     930        } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
    936931                if ( ! isset( $site_url['path'] ) ) {
    937932                        $relative = trim( $src_url['path'], '/' );
    938                 } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
     933                } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
    939934                        // Make the src relative to the WP root.
    940                         $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
     935                        $relative = substr( $src, strlen( $site_url['path'] ) );
    941936                        $relative = trim( $relative, '/' );
    942937                }
    943938        }
    944939
    945         /**
    946          * Filters the relative path of scripts used for finding translation files.
    947          *
    948          * @since 5.0.2
    949          *
    950          * @param string $relative The relative path of the script. False if it could not be determined.
    951          * @param string $src      The full source url of the script.
    952          */
    953         $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
    954 
    955940        // If the source is not from WP.
    956941        if ( false === $relative ) {
    957                 return load_script_translations( false, $handle, $domain );
     942                return false;
    958943        }
    959944
    960945        // Translations are always based on the unminified filename.
     
    963948        }
    964949
    965950        $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    966 
    967         if ( $path ) {
    968                 $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
    969 
    970                 if ( $translations ) {
    971                         return $translations;
    972                 }
     951        if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
     952                return file_get_contents( $path . '/' . $md5_filename );
    973953        }
    974 
    975         $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
    976 
    977         if ( $translations ) {
    978                 return $translations;
    979         }
    980 
    981         return load_script_translations( false, $handle, $domain );
    982 }
    983 
    984 /**
    985  * Loads the translation data for the given script handle and text domain.
    986  *
    987  * @since 5.0.2
    988  *
    989  * @param string|false $file   Path to the translation file to load. False if there isn't one.
    990  * @param string       $handle Name of the script to register a translation domain to.
    991  * @param string       $domain The text domain.
    992  * @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none.
    993  */
    994 function load_script_translations( $file, $handle, $domain ) {
    995         /**
    996          * Pre-filters script translations for the given file, script handle and text domain.
    997          *
    998          * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
    999          *
    1000          * @since 5.0.2
    1001          *
    1002          * @param string|false $translations JSON-encoded translation data. Default null.
    1003          * @param string|false $file         Path to the translation file to load. False if there isn't one.
    1004          * @param string       $handle       Name of the script to register a translation domain to.
    1005          * @param string       $domain       The text domain.
    1006          */
    1007         $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
    1008 
    1009         if ( null !== $translations ) {
    1010                 return $translations;
     954        if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
     955                return file_get_contents( $languages_path . '/' . $md5_filename );
    1011956        }
    1012957
    1013         /**
    1014          * Filters the file path for loading script translations for the given script handle and text domain.
    1015          *
    1016          * @since 5.0.2
    1017          *
    1018          * @param string|false $file   Path to the translation file to load. False if there isn't one.
    1019          * @param string       $handle Name of the script to register a translation domain to.
    1020          * @param string       $domain The text domain.
    1021          */
    1022         $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
    1023 
    1024         if ( ! $file || ! is_readable( $file ) ) {
    1025                 return false;
    1026         }
    1027 
    1028         $translations = file_get_contents( $file );
    1029 
    1030         /**
    1031          * Filters script translations for the given file, script handle and text domain.
    1032          *
    1033          * @since 5.0.2
    1034          *
    1035          * @param string $translations JSON-encoded translation data.
    1036          * @param string $file         Path to the translation file that was loaded.
    1037          * @param string $handle       Name of the script to register a translation domain to.
    1038          * @param string $domain       The text domain.
    1039          */
    1040         return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
     958        return false;
    1041959}
    1042960
    1043961/**
     
    14721390
    14731391/**
    14741392 * Determines whether the current locale is right-to-left (RTL).
    1475  *
     1393 * 
    14761394 * For more information on this and similar theme functions, check out
    1477  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
     1395 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 
    14781396 * Conditional Tags} article in the Theme Developer Handbook.
    14791397 *
    14801398 * @since 3.0.0