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