| 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 | */ |
| 1881 | function 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 | */ |
| 1916 | function 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 | */ |
| 1937 | function 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 | */ |
| 1961 | function 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 | } |