Make WordPress Core

Ticket #45425: 45425.2.patch

File 45425.2.patch, 3.4 KB (added by strategio, 6 years ago)

Improved patch to introduce the filter pre_load_script_translation

  • wp-includes/l10n.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    895895        $file_base       = $domain === 'default' ? $locale : $domain . '-' . $locale;
    896896        $handle_filename = $file_base . '-' . $handle . '.json';
    897897        if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
    898                 return file_get_contents( $path . '/' . $handle_filename );
     898                return load_script_translation( $path . '/' . $handle_filename, $handle, $domain );
    899899        }
    900900
    901901        $obj = $wp_scripts->registered[ $handle ];
     
    935935
    936936        // If the source is not from WP.
    937937        if ( false === $relative ) {
    938                 return false;
     938                return load_script_translation( false, $handle, $domain );
    939939        }
    940940
    941941        // Translations are always based on the unminified filename.
     
    945945
    946946        $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    947947        if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
    948                 return file_get_contents( $path . '/' . $md5_filename );
     948                return load_script_translation( $path . '/' . $md5_filename, $handle, $domain );
    949949        }
    950950        if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
    951                 return file_get_contents( $languages_path . '/' . $md5_filename );
     951                return load_script_translation( $languages_path . '/' . $md5_filename, $handle, $domain );
     952        }
     953
     954        return load_script_translation( false, $handle, $domain );
     955}
     956
     957/**
     958 * Load the translated strings for the given script handle and textdomain.
     959 *
     960 * @since 5.0.0
     961 *
     962 * @param string|false $file   Path to the translation file to load. False if there isn't one.
     963 * @param string       $handle Name of the script to register a translation domain to.
     964 * @param string       $domain The textdomain.
     965 * @return string|false The JSON-encoded translated strings for the given script handle and textdomain. False if there are none.
     966 */
     967function load_script_translation( $file, $handle, $domain ) {
     968        /**
     969         * Pre-filters the script translations for the given file, script handle and textdomain.
     970         *
     971         * @since 5.0.0
     972         *
     973         * @param string|false $json_translations If not false, will immediately return the translations in JSON-encoded format
     974         * @param string       $file              Path to the translation file to load. False if there isn't one.
     975         * @param string       $handle            Name of the script to register a translation domain to.
     976         * @param string       $domain            The textdomain.
     977         */
     978        $json_translations = apply_filters( 'pre_load_script_translation', false, $file, $handle, $domain );
     979
     980        if ( $json_translations ) {
     981                return $json_translations;
    952982        }
    953983
    954         return false;
    955 }
     984        /**
     985         * Filters the file path for loading script translations for the given script handle and textdomain.
     986         *
     987         * @since 5.0.0
     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 textdomain.
     992         */
     993        $file = apply_filters( 'load_script_textdomain_file', $file, $handle, $domain );
     994
     995        if ( ! $file || ! is_readable( $file ) ) {
     996                return false;
     997        }
    956998
     999        return file_get_contents( $file );
     1000}
     1001
    9571002/**
    9581003 * Loads plugin and theme textdomains just-in-time.
    9591004 *