-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-mo-cache.php
49 lines (38 loc) · 1.12 KB
/
wordpress-mo-cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Plugin Name: MO Cache
* Plugin URI: https://github.com/nasyrov/wordpress-mo-cache
* Description: WordPress mu-plugin for a faster load_textdomain.
* Author: Evgenii Nasyrov
* Author URI: mailto:inasyrov@ya.ru
* Version: 1.0.0
*/
add_filter('override_load_textdomain', function ($_, $domain, $mofile) {
global $l10n;
if (!is_readable($mofile)) {
return false;
}
$cacheKey = sprintf('mo-cache-%s', md5($mofile));
$cache = get_transient($cacheKey);
$mtime = filemtime($mofile);
$mo = new MO;
if (!$cache || !isset($cache['mtime']) || $mtime > $cache['mtime']) {
if (!$mo->import_from_file($mofile)) {
return false;
}
$cache = [
'mtime' => $mtime,
'entries' => $mo->entries,
'headers' => $mo->headers,
];
set_transient($cacheKey, $cache);
} else {
$mo->entries = $cache['entries'];
$mo->headers = $cache['headers'];
}
if (isset($l10n[$domain])) {
$mo->merge_with($l10n[$domain]);
}
$l10n[$domain] =& $mo;
return true;
}, 1, 3);