-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmo-cache.php
More file actions
58 lines (49 loc) · 1.44 KB
/
mo-cache.php
File metadata and controls
58 lines (49 loc) · 1.44 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
54
55
56
57
58
<?php
/**
* @package MOCache
*/
/*
Plugin Name: MO Cache
Plugin URI: http://wordpress.org/extend/plugins/mo-cache/
Description: Improving the site performance by caching translation files using the WordPress standard cache mechanism.
Author: Masaki Takeuchi
Version: 2.0
Author URI: https://github.com/m4i/wordpress-mo-cache
License: MIT
*/
class MOCache {
const VERSION = '2.0';
const GROUP = 'mo';
public static function setup() {
add_filter(
'override_load_textdomain', array( new self, 'load' ), 10, 3 );
}
public function load( $override, $domain, $mofile ) {
global $l10n;
do_action( 'load_textdomain', $domain, $mofile );
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
if ( !is_readable( $mofile ) ) return false;
if ( function_exists( 'wp_cache_add_global_groups' ) ) {
wp_cache_add_global_groups( self::GROUP );
}
$key = preg_replace('/[^-\w]/', '_',
self::VERSION . "-${GLOBALS['wp_version']}-$mofile");
$mo = new MO();
if ( $cache = wp_cache_get( $key, self::GROUP ) ) {
$mo->entries = $cache['entries'];
$mo->set_headers( $cache['headers'] );
} else {
if ( !$mo->import_from_file( $mofile ) ) return false;
$cache = array(
'entries' => $mo->entries,
'headers' => $mo->headers,
);
wp_cache_set( $key, $cache, self::GROUP );
}
if ( isset( $l10n[$domain] ) )
$mo->merge_with( $l10n[$domain] );
$l10n[$domain] = &$mo;
return true;
}
}
MOCache::setup();