[wp-trac] [WordPress Trac] #58919: Add a hook to set_cached_mo_files() to allow flexible caching strategies for globbing *.mo files

WordPress Trac noreply at wordpress.org
Sun Aug 13 13:21:09 UTC 2023


#58919: Add a hook to set_cached_mo_files() to allow flexible caching strategies
for globbing *.mo files
-------------------------+-----------------------------
 Reporter:  mreishus     |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Future Release
Component:  I18N         |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+-----------------------------

Comment (by swissspidy):

 > Could you provide some guidance on a better-suited name and any changes
 in usage that would more closely follow core patterns?

 Typically in core we would do something like this:

 {{{#!php
 /**
  * Filters the .mo files retrieved from a specified path before the actual
 lookup.
  *
  * Returning a non-null value from the filter will effectively short-
 circuit
  * the MO files lookup, returning that value instead.
  *
  * This can be useful in situations where the directory contains a large
 number of files
  * and the default glob() function becomes expensive in terms of
 performance.
  *
  * @since 6.4.0
  *
  * @param null|array $mo_files List of .mo files. Default null.
  * @param string $path The path from which .mo files are being fetched.
  **/
 $mo_files = apply_filters( 'pre_set_cached_mo_files', null, $path );
 if ( null !== $mo_files ) {
         $this->cached_mo_files[ $path ] = $mo_files;
         return;
 }
 }}}

 Sometimes accompanied by a second filter to allow modifying the list after
 the regular lookup, but I don't think that's necessary here.

 > I like the idea of built-in caching with invalidation whenever
 translations get installed. Would the 'upgrader_process_complete' hook be
 the place for this?

 Yeah that seems like a good hook, especially since translations can't
 really be ''uninstalled'' (unless a plugin/theme gets uninstalled), so
 this should suffice.

 > Also, I'm curious if we should include this in the current ticket's
 scope or create a separate one?

 I'd say let's explore it in this ticket. It would help speed this function
 up by default for all sites, not just ones using this filter.
 If it turns out to be too complex, we can always create a separate one.

 > Regarding get_available_languages, my exploration revealed fewer files
 in play for core translations compared to set_cached_mo_files(). The
 impact was less significant, but it might still warrant some attention.

 Thanks for testing!

 I find caching `get_available_languages()` interesting because it's used
 in many different places and could speed up things like the settings page,
 user edit form, etc.

 Plus, the function does basically the same as `set_cached_mo_files()` (if
 you ignore the filtering).

 So there's an opportunity to reduce repetition and feed 2 birds with 1
 stone by introducing a shared function that does the globbing & caching.
 This function can then be used by `get_available_languages()` and
 `set_cached_mo_files()`.

 Does that make sense?

 @SergeyBiryukov @ocean90 curious to hear your thoughts too

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58919#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list