PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.02
Docket Cache – Object Cache Accelerator v22.07.02
26.04.05 trunk 22.07.01 22.07.02 22.07.03 22.07.04 22.07.05 23.08.01 23.08.02 24.07.01 24.07.02 24.07.03 24.07.04 24.07.05 24.07.06 24.07.07 26.04.03 26.04.04
docket-cache / includes / src / MoCache.php

MoCache.php in Docket Cache – Object Cache Accelerator 22.07.02, at includes/src/MoCache.php

120 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Docket Cache.
4 *
5 * @author Nawawi Jamili
6 * @license MIT
7 *
8 * @see https://github.com/nawawi/docket-cache
9 */
10
11 namespace Nawawi\DocketCache;
12
13 \defined('ABSPATH') || exit;
14
15 final class MoCache
16 {
17 private $domain = null;
18 private $cache = [];
19 private $dosave = false;
20 private $override = null;
21 private $upstream = null;
22 private $mofile = null;
23
24 // Avoid warning on get_translations_for_domain.
25 // Reference: wp-includes/pomo/translations.php.
26 public $entries = [];
27 public $headers = [];
28
29 public function __construct($mofile, $domain, $override)
30 {
31 $this->mofile = apply_filters('load_textdomain_mofile', $mofile, $domain);
32 $this->domain = $domain;
33 $this->override = $override;
34
35 $cache_key = $domain.'-'.basename($this->mofile, '.mo');
36 $cache_group = 'docketcache-mo';
37
38 $mtime = @filemtime($this->mofile);
39 $cache = wp_cache_get($cache_key, $cache_group);
40 if (false !== $cache && !empty($cache['data'])) {
41 if (!empty($cache['time']) && $mtime > $cache['time']) {
42 $this->cache = [];
43 wp_cache_delete($cache_key, $cache_group);
44 } else {
45 $this->cache = $cache['data'];
46 }
47 }
48
49 add_action(
50 'shutdown',
51 function () use ($mtime, $cache_key, $cache_group) {
52 if ($this->dosave) {
53 $cache_data = [
54 'time' => $mtime,
55 'data' => $this->cache,
56 ];
57
58 wp_cache_set($cache_key, $cache_data, $cache_group);
59 }
60 },
61 \PHP_INT_MAX
62 );
63 }
64
65 public function translate($text, $context = null)
66 {
67 $args = \func_get_args();
68
69 return $this->get_translation($this->text_key($args), $text, $args);
70 }
71
72 public function translate_plural($singular, $plural, $count, $context = null)
73 {
74 $text = (1 == abs($count)) ? $singular : $plural;
75 $args = \func_get_args();
76
77 return $this->get_translation($this->text_key([$text, $count, $context]), $text, $args);
78 }
79
80 private function text_key($args)
81 {
82 return substr(md5(serialize([$args, $this->domain])), 0, 12);
83 }
84
85 private function get_translation($text_key, $text, $args)
86 {
87 if (isset($this->cache[$text_key])) {
88 return $this->cache[$text_key];
89 }
90
91 $translate_function = \count($args) > 2 ? 'translate_plural' : 'translate';
92
93 if ($this->override) {
94 if (($translation = \call_user_func_array([$this->override, $translate_function], $args)) !== $text) {
95 $this->dosave = true;
96
97 return $this->cache[$text_key] = $translation;
98 }
99 }
100
101 if (!$this->upstream) {
102 $this->upstream = new \Mo();
103 do_action('load_textdomain', $this->domain, $this->mofile);
104 $this->upstream->import_from_file($this->mofile);
105 }
106
107 if (($translation = \call_user_func_array([$this->upstream, $translate_function], $args)) !== $text) {
108 $this->dosave = true;
109
110 return $this->cache[$text_key] = $translation;
111 }
112
113 $translation = \call_user_func_array([$this->upstream, $translate_function], $args);
114
115 $this->dosave = true;
116
117 return $this->cache[$text_key] = $translation;
118 }
119 }
120