class-wpml-cache-factory.php
1.64 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
<?php
class WPML_Cache_Factory {
/** @var array */
private $valid_caches = [
'TranslationManagement::get_translation_job_id' => [
'clear_actions' => [ 'wpml_tm_save_post', 'wpml_cache_clear' ],
],
'WPML_Element_Type_Translation::get_language_for_element' => [
'clear_actions' => [ 'wpml_translation_update' ],
],
'WPML_Post_Status::needs_update' => [
'clear_actions' => [ 'wpml_translation_status_update' ],
],
];
public function __construct() {
foreach ( $this->valid_caches as $cache_name => $clear_actions ) {
$this->init_clear_actions( $cache_name, $clear_actions['clear_actions'] );
}
}
/**
* @param string $cache_name
*
* @return WPML_WP_Cache
* @throws InvalidArgumentException Exception.
*/
public function get( $cache_name ) {
if ( isset( $this->valid_caches[ $cache_name ] ) ) {
return new WPML_WP_Cache( $cache_name );
} else {
throw new InvalidArgumentException( $cache_name . ' is not a valid cache for the WPML_Cache_Factory' );
}
}
/**
* @param string $cache_name
* @param array $clear_actions
*/
public function define( $cache_name, array $clear_actions ) {
if ( isset( $this->valid_caches[ $cache_name ] ) ) {
return;
}
$this->valid_caches[ $cache_name ] = [
'clear_actions' => $clear_actions,
];
$this->init_clear_actions( $cache_name, $clear_actions );
}
private function init_clear_actions( $cache_name, array $clear_actions ) {
foreach ( $clear_actions as $clear_action ) {
add_action(
$clear_action,
function () use ( $cache_name ) {
$cache = new WPML_WP_Cache( $cache_name );
$cache->flush_group_cache();
}
);
}
}
}