| 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 |
// The $mofile has already been filtered by WordPress core via |
| 33 |
// apply_filters('load_textdomain_mofile') in load_textdomain() |
| 34 |
// before calling override_load_textdomain. Re-applying the filter |
| 35 |
// here causes it to run twice, which breaks plugins with stateful |
| 36 |
// filter callbacks (e.g. Polylang Pro's PLL_Locale_Fallback::load_file |
| 37 |
// returns empty on the second pass, causing a PHP 8+ ValueError). |
| 38 |
$this->mofile = $mofile; |
| 39 |
$this->domain = $domain; |
| 40 |
$this->override = $override; |
| 41 |
|
| 42 |
$cache_key = $domain.'-'.basename($this->mofile, '.mo'); |
| 43 |
$cache_group = 'docketcache-mo'; |
| 44 |
|
| 45 |
if (\function_exists('wp_cache_add_global_groups')) { |
| 46 |
wp_cache_add_global_groups($cache_group); |
| 47 |
} |
| 48 |
|
| 49 |
$mtime = @filemtime($this->mofile); |
| 50 |
$cache = wp_cache_get($cache_key, $cache_group); |
| 51 |
if (false !== $cache && !empty($cache['data'])) { |
| 52 |
if (!empty($cache['time']) && $mtime > $cache['time']) { |
| 53 |
$this->cache = []; |
| 54 |
wp_cache_delete($cache_key, $cache_group); |
| 55 |
} else { |
| 56 |
$this->cache = $cache['data']; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
add_action( |
| 61 |
'shutdown', |
| 62 |
function () use ($mtime, $cache_key, $cache_group) { |
| 63 |
if ($this->dosave) { |
| 64 |
$cache_data = [ |
| 65 |
'time' => $mtime, |
| 66 |
'data' => $this->cache, |
| 67 |
]; |
| 68 |
|
| 69 |
wp_cache_set($cache_key, $cache_data, $cache_group); |
| 70 |
} |
| 71 |
}, |
| 72 |
\PHP_INT_MAX |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function translate($text, $context = null) |
| 77 |
{ |
| 78 |
$args = \func_get_args(); |
| 79 |
|
| 80 |
return $this->get_translation($this->text_key($args), $text, $args); |
| 81 |
} |
| 82 |
|
| 83 |
public function translate_plural($singular, $plural, $count, $context = null) |
| 84 |
{ |
| 85 |
$text = (1 == abs($count)) ? $singular : $plural; |
| 86 |
$args = \func_get_args(); |
| 87 |
|
| 88 |
return $this->get_translation($this->text_key([$text, $count, $context]), $text, $args); |
| 89 |
} |
| 90 |
|
| 91 |
private function text_key($args) |
| 92 |
{ |
| 93 |
// return substr(md5(serialize([$args, $this->domain])), 0, 12); |
| 94 |
return md5(serialize([$args, $this->domain])); |
| 95 |
} |
| 96 |
|
| 97 |
private function get_translation($text_key, $text, $args) |
| 98 |
{ |
| 99 |
if (isset($this->cache[$text_key])) { |
| 100 |
return $this->cache[$text_key]; |
| 101 |
} |
| 102 |
|
| 103 |
$translate_function = \count($args) > 2 ? 'translate_plural' : 'translate'; |
| 104 |
|
| 105 |
if ($this->override) { |
| 106 |
if (($translation = \call_user_func_array([$this->override, $translate_function], $args)) !== $text) { |
| 107 |
$this->dosave = true; |
| 108 |
|
| 109 |
return $this->cache[$text_key] = $translation; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if (!$this->upstream) { |
| 114 |
$this->upstream = new \Mo(); |
| 115 |
if (!empty($this->mofile)) { |
| 116 |
do_action('load_textdomain', $this->domain, $this->mofile); |
| 117 |
$this->upstream->import_from_file($this->mofile); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (($translation = \call_user_func_array([$this->upstream, $translate_function], $args)) !== $text) { |
| 122 |
$this->dosave = true; |
| 123 |
|
| 124 |
return $this->cache[$text_key] = $translation; |
| 125 |
} |
| 126 |
|
| 127 |
$translation = \call_user_func_array([$this->upstream, $translate_function], $args); |
| 128 |
|
| 129 |
$this->dosave = true; |
| 130 |
|
| 131 |
return $this->cache[$text_key] = $translation; |
| 132 |
} |
| 133 |
} |
| 134 |
|