-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbwp_cache.php
More file actions
61 lines (53 loc) · 1.77 KB
/
bwp_cache.php
File metadata and controls
61 lines (53 loc) · 1.77 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
<?php
/*
Plugin Name: BWP Cache
Plugin URI: https://github.com/BostonWP/functionality-plugins
Description: Provides a mechanism for caching queried data to be accessed from separate parts of WP codebase
Version: 0.1
Author: Brian Zeligson
Author URI: http://brianzeligson.com
License: GPL2
*/
/*
*
* Example usage:
* $data = 'string';
* bwpCache::instance()->set('data', $data);
*
* // laterfrom widget, sidebar, footer, etc
* $data = (bwpCache::instance()->get('data')) ?: 'default';
* $data === 'string' //true
*
* Available hooks/filters:
* set_bwp_cache - as filter before setting cache. Accepts key name for cache data as second argument
* set_bwp_cache - as action before setting cache. Accepts filtered data and key name for cache data as arguments.
*
* get_bwp_cache - as filter before returning cached data. Accepts key name for cache data as second argument
* get_bwp_cache - as action before returning cached data. Accepts filtered data and key name for cache data as arguments.
*/
class bwpCache
{
private $_cache = array();
private static $_instance;
private function __construct()
{
}
public function instance()
{
if (!isset(self::$_instance)) self::$_instance = new bwpCache();
return self::$_instance;
}
public function set($name, $data)
{
$data = apply_filters('set_bwp_cache', $data, $name);
do_action('set_bwp_cache', $data, $name);
$this->_cache[$name] = $data;
}
public function get($name)
{
if (!array_key_exists($name, $this->_cache[$name])) return false;
$data = apply_filters('get_bwp_cache', $this->_cache[$name], $name);
do_action('get_bwp_cache', $data, $name);
return $data;
}
}