cache.php
4.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
if ( ! defined( 'ICL_DISABLE_CACHE' ) ) {
define( 'ICL_DISABLE_CACHE', false );
}
// phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid
// phpcs:disable PEAR.NamingConventions.ValidClassName.StartWithCapital
/**
* Class icl_cache
*/
class icl_cache {
/** @var array */
protected $data;
/** @var string */
protected $name;
/** @var bool */
protected $cache_to_option;
/** @var bool */
protected $cache_needs_saving;
public function __construct( $name = '', $cache_to_option = false ) {
$this->data = [];
$this->name = $name;
$this->cache_to_option = $cache_to_option;
$this->cache_needs_saving = false;
$this->init();
}
public function init() {
if ( $this->cache_to_option ) {
$this->data = icl_cache_get( $this->name . '_cache_class' );
if ( false === $this->data ) {
$this->data = [];
}
add_action( 'shutdown', [ $this, 'save_cache_if_required' ] );
}
}
public function save_cache_if_required() {
if ( $this->cache_needs_saving ) {
icl_cache_set( $this->name . '_cache_class', $this->data );
$this->cache_needs_saving = false;
}
}
public function get( $key ) {
if ( ICL_DISABLE_CACHE ) {
return null;
}
return isset( $this->data[ $key ] ) ? $this->data[ $key ] : false;
}
public function has_key( $key ) {
if ( ICL_DISABLE_CACHE ) {
return false;
}
return array_key_exists( $key, (array) $this->data );
}
public function set( $key, $value ) {
if ( ICL_DISABLE_CACHE ) {
return;
}
if ( $this->cache_to_option ) {
$old_value = null;
if ( isset( $this->data[ $key ] ) ) {
$old_value = $this->data[ $key ];
}
if ( $old_value !== $value ) {
$this->data[ $key ] = $value;
$this->cache_needs_saving = true;
}
} else {
$this->data[ $key ] = $value;
}
}
public function clear() {
$this->data = array();
if ( $this->cache_to_option ) {
icl_cache_clear( $this->name . '_cache_class' );
}
}
}
if ( ! function_exists( 'icl_disable_cache' ) ) {
function icl_disable_cache() {
return defined( 'ICL_DISABLE_CACHE' ) && ICL_DISABLE_CACHE;
}
}
if ( ! function_exists( 'icl_cache_get' ) ) {
function icl_cache_get( $key ) {
$result = false;
if ( ! icl_disable_cache() ) {
$icl_cache = get_option( '_icl_cache' );
$result = isset( $icl_cache[ $key ] ) ? $icl_cache[ $key ] : false;
}
return $result;
}
}
if ( ! function_exists( 'icl_cache_set' ) ) {
function icl_cache_set( $key, $value = null ) {
global $switched;
if ( empty( $switched ) && ! icl_disable_cache() ) {
$icl_cache = get_option( '_icl_cache' );
if ( false === $icl_cache ) {
$icl_cache = [];
delete_option( '_icl_cache' );
}
if ( ! isset( $icl_cache[ $key ] ) || $icl_cache[ $key ] !== $value ) {
if ( ! is_null( $value ) ) {
$icl_cache[ $key ] = $value;
} elseif ( isset( $icl_cache[ $key ] ) ) {
unset( $icl_cache[ $key ] );
}
update_option( '_icl_cache', $icl_cache, 'no' );
}
}
}
}
if ( ! function_exists( 'icl_cache_clear' ) ) {
function icl_cache_clear( $key = false, $key_as_prefix = false ) {
if ( ! icl_disable_cache() ) {
/**
* @var WPML_Term_Translation $wpml_term_translations
* @var WPML_Post_Translation $wpml_post_translations
*/
global $wpml_term_translations, $wpml_post_translations;
$wpml_term_translations->reload();
$wpml_post_translations->reload();
if ( false === $key ) {
delete_option( '_icl_cache' );
} else {
/** @var array $icl_cache */
$icl_cache = get_option( '_icl_cache' );
if ( is_array( $icl_cache ) ) {
if ( isset( $icl_cache[ $key ] ) ) {
unset( $icl_cache[ $key ] );
}
if ( $key_as_prefix ) {
$cache_keys = array_keys( $icl_cache );
foreach ( $cache_keys as $cache_key ) {
if ( strpos( $cache_key, $key ) === 0 ) {
unset( $icl_cache[ $cache_key ] );
}
}
}
// Special cache of 'per language' - clear different statuses.
if ( false !== strpos( $key, '_per_language' ) ) {
foreach ( $icl_cache as $k => $v ) {
if ( false !== strpos( $k, $key . '#' ) ) {
unset( $icl_cache[ $k ] );
}
}
}
update_option( '_icl_cache', $icl_cache, 'no' );
}
}
}
do_action( 'wpml_cache_clear' );
}
}
function w3tc_translate_cache_key_filter( $key ) {
global $sitepress;
return $sitepress->get_current_language() . $key;
}