-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextdomain-cache.php
More file actions
53 lines (43 loc) · 1.27 KB
/
textdomain-cache.php
File metadata and controls
53 lines (43 loc) · 1.27 KB
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
50
51
52
53
<?php
/**
* @wordpress-plugin
* Plugin Name: Text Domain Cache
* Plugin URI: https://github.com/Kntnt/textdomain-cache
* Description: Caches MO-files for faster load_textdomain(). Install an object cache for best performance.
* Version: 1.0.0
* Author: Per Soderlind
* Author URI: https://soderlind.no/a-faster-load_textdomain-for-wordpress/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*
* Based on https://core.trac.wordpress.org/ticket/32052.
*/
add_filter( 'override_load_textdomain', function ( $retval, $domain, $mofile ) {
global $l10n;
if ( ! is_readable( $mofile ) ) {
return false;
}
$data = get_transient( md5( $mofile ) );
$mtime = filemtime( $mofile );
$mo = new MO();
if ( ! $data || ! isset( $data['mtime'] ) || $mtime > $data['mtime'] ) {
if ( ! $mo->import_from_file( $mofile ) ) {
return false;
}
$data = [
'mtime' => $mtime,
'entries' => $mo->entries,
'headers' => $mo->headers,
];
set_transient( md5( $mofile ), $data );
}
else {
$mo->entries = $data['entries'];
$mo->headers = $data['headers'];
}
if ( isset( $l10n[ $domain ] ) ) {
$mo->merge_with( $l10n[ $domain ] );
}
$l10n[ $domain ] = &$mo;
return true;
}, 1, 3 );