Completed
Branch BUG-10626-dst-unit-test (cc62a6)
by
unknown
37:15 queued 23:58
created

clearPostRelatedCacheOnUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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