Make WordPress Core

Ticket #58919: add_get_mo_files_from_path_2.diff

File add_get_mo_files_from_path_2.diff, 5.0 KB (added by mreishus, 21 months ago)
  • src/wp-includes/class-wp-textdomain-registry.php

    diff --git a/src/wp-includes/class-wp-textdomain-registry.php b/src/wp-includes/class-wp-textdomain-registry.php
    index 99d72eb7a3..9d823ad7cd 100644
    a b class WP_Textdomain_Registry { 
    226226        private function set_cached_mo_files( $path ) {
    227227                $this->cached_mo_files[ $path ] = array();
    228228
    229                 $mo_files = glob( $path . '/*.mo' );
     229                $mo_files = get_mo_files_from_path( $path );
    230230
    231231                if ( $mo_files ) {
    232232                        $this->cached_mo_files[ $path ] = $mo_files;
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index bf39f29b4d..7c47878b57 100644
    a b add_action( 'init', 'wp_register_persisted_preferences_meta' ); 
    719719// CPT wp_block custom postmeta field.
    720720add_action( 'init', 'wp_create_initial_post_meta' );
    721721
     722// Localization (l10n).
     723add_filter( 'pre_fetch_mo_files', 'cache_mo_files', 10, 2 );
     724add_action( 'upgrader_process_complete', 'invalidate_mo_files_cache', 10, 2 );
     725
    722726unset( $filter, $action );
  • src/wp-includes/l10n.php

    diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
    index 951c1c316b..c2bd49e8f2 100644
    a b function translate_user_role( $name, $domain = 'default' ) { 
    13971397function get_available_languages( $dir = null ) {
    13981398        $languages = array();
    13991399
    1400         $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
     1400        $path       = is_null( $dir ) ? WP_LANG_DIR : $dir;
     1401        $lang_files = get_mo_files_from_path( $path );
     1402
    14011403        if ( $lang_files ) {
    14021404                foreach ( $lang_files as $lang_file ) {
    14031405                        $lang_file = basename( $lang_file, '.mo' );
    function wp_get_word_count_type() { 
    18641866
    18651867        return $wp_locale->get_word_count_type();
    18661868}
     1869
     1870/**
     1871 * Retrieves .mo files from the specified path.
     1872 *
     1873 * Allows early retrieval through the 'pre_fetch_mo_files' filter to optimize
     1874 * performance, especially in directories with many files.
     1875 *
     1876 * @since TBD
     1877 *
     1878 * @param string $path The directory path to search for .mo files.
     1879 * @return array Array of .mo file paths.
     1880 */
     1881function get_mo_files_from_path( $path ) {
     1882        /**
     1883        * Filters the .mo files retrieved from a specified path before the actual lookup.
     1884        *
     1885        * Returning a non-null value from the filter will effectively short-circuit
     1886        * the MO files lookup, returning that value instead.
     1887        *
     1888        * This can be useful in situations where the directory contains a large number of files
     1889        * and the default glob() function becomes expensive in terms of performance.
     1890        *
     1891        * @since TBD
     1892        *
     1893        * @param null|array $mo_files List of .mo files. Default null.
     1894        * @param string $path The path from which .mo files are being fetched.
     1895        **/
     1896        $mo_files = apply_filters( 'pre_fetch_mo_files', null, $path );
     1897        if ( null === $mo_files ) {
     1898                $mo_files = glob( $path . '/*.mo' );
     1899        }
     1900
     1901        return $mo_files;
     1902}
     1903
     1904/**
     1905 * Caches the .mo files from the specified path.
     1906 *
     1907 * This function retrieves and caches .mo files from a given path, avoiding repeated
     1908 * expensive filesystem lookups.
     1909 *
     1910 * @since TBD
     1911 *
     1912 * @param null|array $pre  List of .mo files. Default null.
     1913 * @param string     $path The directory path to search for .mo files.
     1914 * @return array|false Array of .mo file paths or false on failure.
     1915 */
     1916function cache_mo_files( $pre, $path ) {
     1917        $cache_key = 'cached_mo_files_' . md5( $path );
     1918        $mo_files  = wp_cache_get( $cache_key, 'translations' );
     1919
     1920        if ( false === $mo_files ) {
     1921                $mo_files = glob( $path . '/*.mo' );
     1922                wp_cache_set( $cache_key, $mo_files, 'translations' );
     1923                add_path_to_mo_files_cache_list( $path );
     1924        }
     1925
     1926        return $mo_files;
     1927}
     1928
     1929/**
     1930 * Adds a directory path to the list of paths with cached .mo files.
     1931 * This allows invalidate_mo_files_cache() to invalidate all cached paths.
     1932 *
     1933 * @since TBD
     1934 *
     1935 * @param string $path The directory path to add to the cached paths list.
     1936 */
     1937function add_path_to_mo_files_cache_list( $path ) {
     1938        $cached_paths = wp_cache_get( 'mo_files_cached_paths', 'translations' );
     1939
     1940        if ( false === $cached_paths || ! is_array( $cached_paths ) ) {
     1941                $cached_paths = array();
     1942        }
     1943
     1944        if ( ! in_array( $path, $cached_paths ) ) {
     1945                $cached_paths[] = $path;
     1946                wp_cache_set( 'mo_files_cached_paths', $cached_paths, 'translations' );
     1947        }
     1948}
     1949
     1950/**
     1951 * Invalidate the cache for .mo files.
     1952 *
     1953 * This function deletes the cache entries related to .mo files when triggered
     1954 * by specific actions, such as the completion of an upgrade process.
     1955 *
     1956 * @since TBD
     1957 *
     1958 * @param WP_Upgrader $upgrader_object The upgrader instance.
     1959 * @param array       $options         Array of bulk item update data.
     1960 */
     1961function invalidate_mo_files_cache( $upgrader_object, $options ) {
     1962        $cached_paths = wp_cache_get( 'mo_files_cached_paths', 'translations' );
     1963        if ( false !== $cached_paths && is_array( $cached_paths ) ) {
     1964                foreach ( $cached_paths as $path ) {
     1965                        wp_cache_delete( 'cached_mo_files_' . md5( $path ), 'translations' );
     1966                }
     1967        }
     1968        wp_cache_delete( 'mo_files_cached_paths', 'translations' );
     1969}