I have a multisite wordpress with multiple localized sites (es_ES, de_DE, it_IT). I want improve page speed by caching wordpress translation file.
My code works (inspired by Tristan Darricau)! and my page speed is improved of 0.50 seconds.
Now, my question is: My code is right or can break something (core, theme, plug-in), i have tested and it seem to work, but i'm a newbie of wp!
/**
* Cache translation
* @param boolean $override
* @param string $domain
* @param string $mofile
* @return boolean
*/
function my_override_load_textdomain($override, $domain, $mofile) {
global $l10n;
// check if $mofile exisiste and is readable
if ( !(is_file($mofile) && is_readable($mofile)) ){
return false;
}
// creates a unique key for cache
$key = md5($mofile);
// I try to retrive data from cache
$data = wp_cache_get($key, $domain);
// Retrieve the last modified date of the translation files
$mtime = filemtime($mofile);
$mo = new \MO();
// if cache not return data or data it's old
if ( !$data || !isset($data['mtime']) || ($mtime > $data['mtime']) ) {
// retrive data from MO file
if ( $mo->import_from_file( $mofile ) ){
$data = array(
'mtime' => $mtime,
'entries' => $mo->entries,
'headers' => $mo->headers
);
// save data in cache
wp_cache_set($key, $data, $domain, 0);
} else {
return false;
}
} else {
$mo->entries = $data['entries'];
$mo->headers = $data['headers'];
}
if ( isset( $l10n[$domain] ) ) {
$mo->merge_with( $l10n[$domain] );
}
$l10n[$domain] = &$mo;
return true;
}
add_filter('override_load_textdomain', 'my_override_load_textdomain',1,3);
Side note: i know this is a question for code review stack's platform, but all wordpress'guys are here, and this is a question for wordpress'guys.
I apologize for my bad English