PluginProbe
Docket Cache – Object Cache Accelerator / 22.07.05
Docket Cache – Object Cache Accelerator v22.07.05
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.05, at includes/src/MoCache.php

126 lines 3.6 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 #[AllowDynamicProperties]
16 final class MoCache
17 {
18 private $domain = null;
19 private $cache = [];
20 private $dosave = false;
21 private $override = null;
22 private $upstream = null;
23 private $mofile = null;
24
25 // Avoid warning on get_translations_for_domain.
26 // Reference: wp-includes/pomo/translations.php.
27 public $entries = [];
28 public $headers = [];
29
30 public function __construct($mofile, $domain, $override)
31 {
32 $this->mofile = apply_filters('load_textdomain_mofile', $mofile, $domain);
33 $this->domain = $domain;
34 $this->override = $override;
35
36 $cache_key = $domain.'-'.basename($this->mofile, '.mo');
37 $cache_group = 'docketcache-mo';
38
39 if (\function_exists('wp_cache_add_global_groups')) {
40 wp_cache_add_global_groups($cache_group);
41 }
42
43 $mtime = @filemtime($this->mofile);
44 $cache = wp_cache_get($cache_key, $cache_group);
45 if (false !== $cache && !empty($cache['data'])) {
46 if (!empty($cache['time']) && $mtime > $cache['time']) {
47 $this->cache = [];
48 wp_cache_delete($cache_key, $cache_group);
49 } else {
50 $this->cache = $cache['data'];
51 }
52 }
53
54 add_action(
55 'shutdown',
56 function () use ($mtime, $cache_key, $cache_group) {
57 if ($this->dosave) {
58 $cache_data = [
59 'time' => $mtime,
60 'data' => $this->cache,
61 ];
62
63 wp_cache_set($cache_key, $cache_data, $cache_group);
64 }
65 },
66 \PHP_INT_MAX
67 );
68 }
69
70 public function translate($text, $context = null)
71 {
72 $args = \func_get_args();
73
74 return $this->get_translation($this->text_key($args), $text, $args);
75 }
76
77 public function translate_plural($singular, $plural, $count, $context = null)
78 {
79 $text = (1 == abs($count)) ? $singular : $plural;
80 $args = \func_get_args();
81
82 return $this->get_translation($this->text_key([$text, $count, $context]), $text, $args);
83 }
84
85 private function text_key($args)
86 {
87 // return substr(md5(serialize([$args, $this->domain])), 0, 12);
88 return md5(serialize([$args, $this->domain]));
89 }
90
91 private function get_translation($text_key, $text, $args)
92 {
93 if (isset($this->cache[$text_key])) {
94 return $this->cache[$text_key];
95 }
96
97 $translate_function = \count($args) > 2 ? 'translate_plural' : 'translate';
98
99 if ($this->override) {
100 if (($translation = \call_user_func_array([$this->override, $translate_function], $args)) !== $text) {
101 $this->dosave = true;
102
103 return $this->cache[$text_key] = $translation;
104 }
105 }
106
107 if (!$this->upstream) {
108 $this->upstream = new \Mo();
109 do_action('load_textdomain', $this->domain, $this->mofile);
110 $this->upstream->import_from_file($this->mofile);
111 }
112
113 if (($translation = \call_user_func_array([$this->upstream, $translate_function], $args)) !== $text) {
114 $this->dosave = true;
115
116 return $this->cache[$text_key] = $translation;
117 }
118
119 $translation = \call_user_func_array([$this->upstream, $translate_function], $args);
120
121 $this->dosave = true;
122
123 return $this->cache[$text_key] = $translation;
124 }
125 }
126