|
1
|
|
|
<?php |
|
2
|
|
|
namespace EventEspresso\core\services\cache; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class PostRelatedCacheManager |
|
11
|
|
|
* Tracks cached content for Posts and clears them when a post is updated |
|
12
|
|
|
* |
|
13
|
|
|
* @package Event Espresso |
|
14
|
|
|
* @author Brent Christensen |
|
15
|
|
|
* @since 4.9.31 |
|
16
|
|
|
*/ |
|
17
|
|
|
class PostRelatedCacheManager extends BasicCacheManager |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @type string |
|
22
|
|
|
*/ |
|
23
|
|
|
const POST_CACHE_PREFIX = 'ee_cache_post_'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* wp-option option_name for tracking post related cache |
|
27
|
|
|
* |
|
28
|
|
|
* @type string |
|
29
|
|
|
*/ |
|
30
|
|
|
const POST_CACHE_OPTIONS_KEY = 'ee_post_cache'; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* PostRelatedCacheManager constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param CacheStorageInterface $cache_storage |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(CacheStorageInterface $cache_storage) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($cache_storage); |
|
42
|
|
|
add_action('save_post', array($this, 'clearPostRelatedCache')); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* returns a string that will be prepended to all cache identifiers |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function cachePrefix() |
|
53
|
|
|
{ |
|
54
|
|
|
return PostRelatedCacheManager::POST_CACHE_PREFIX; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getPostRelatedCache() |
|
62
|
|
|
{ |
|
63
|
|
|
$post_related_cache = get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
64
|
|
|
// verify that cached data was not truncated or corrupted and no longer an array |
|
65
|
|
|
if (! is_array($post_related_cache)) { |
|
66
|
|
|
// uh-oh... let's get rid of any transients using our cache prefix |
|
67
|
|
|
$this->clear(PostRelatedCacheManager::CACHE_PREFIX); |
|
68
|
|
|
// then update the post related cache tracking option |
|
69
|
|
|
$post_related_cache = array(); |
|
70
|
|
|
$this->updatePostRelatedCache($post_related_cache); |
|
71
|
|
|
} |
|
72
|
|
|
return $post_related_cache; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param array $post_related_cache |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function updatePostRelatedCache(array $post_related_cache = array()) |
|
80
|
|
|
{ |
|
81
|
|
|
update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* If you are caching content that pertains to a Post of any type, |
|
87
|
|
|
* then it is recommended to pass the post id and cache id prefix to this method |
|
88
|
|
|
* so that it can be added to the post related cache tracking. |
|
89
|
|
|
* Then, whenever that post is updated, the cache will automatically be deleted, |
|
90
|
|
|
* which helps to ensure that outdated cache content will not be served |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $post_ID [required] |
|
93
|
|
|
* @param string $id_prefix [required] Appended to all cache IDs. Can be helpful in finding specific cache types. |
|
94
|
|
|
* May also be helpful to include an additional specific identifier, |
|
95
|
|
|
* such as a post ID as part of the $id_prefix so that individual caches |
|
96
|
|
|
* can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
97
|
|
|
* BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
|
100
|
|
|
{ |
|
101
|
|
|
$post_related_cache = $this->getPostRelatedCache(); |
|
102
|
|
|
// if post is not already being tracked |
|
103
|
|
|
if ( ! isset($post_related_cache[$post_ID])) { |
|
104
|
|
|
// add array to add cache ids to |
|
105
|
|
|
$post_related_cache[$post_ID] = array(); |
|
106
|
|
|
} |
|
107
|
|
|
if( ! in_array($id_prefix, $post_related_cache[$post_ID], true)) { |
|
108
|
|
|
// add cache id to be tracked |
|
109
|
|
|
$post_related_cache[$post_ID][] = $id_prefix; |
|
110
|
|
|
$this->updatePostRelatedCache($post_related_cache); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* callback hooked into the WordPress "save_post" action |
|
118
|
|
|
* deletes any cache content associated with the post |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $post_ID [required] |
|
121
|
|
|
*/ |
|
122
|
|
|
public function clearPostRelatedCache($post_ID) |
|
123
|
|
|
{ |
|
124
|
|
|
$post_related_cache = $this->getPostRelatedCache(); |
|
125
|
|
|
// if post is not being tracked |
|
126
|
|
|
if ( ! isset($post_related_cache[$post_ID])) { |
|
127
|
|
|
// let's clean up some of the duplicate IDs that were getting added |
|
128
|
|
|
foreach ($post_related_cache as $other_post_ID => $cache_IDs) { |
|
129
|
|
|
//remove duplicates |
|
130
|
|
|
$post_related_cache[$other_post_ID] = array_unique($post_related_cache[$other_post_ID]); |
|
131
|
|
|
} |
|
132
|
|
|
$this->updatePostRelatedCache($post_related_cache); |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
// get cache id prefixes for post, and delete their corresponding transients |
|
136
|
|
|
$this->clear($post_related_cache[$post_ID]); |
|
137
|
|
|
unset($post_related_cache[$post_ID]); |
|
138
|
|
|
$this->updatePostRelatedCache($post_related_cache); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
// End of file PostRelatedCacheManager.php |
|
144
|
|
|
// Location: EventEspresso\core\services\cache/PostRelatedCacheManager.php |
|
145
|
|
|
|