|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This class is used for handling Popular Posts caching. |
|
4
|
|
|
* |
|
5
|
|
|
* @package MonsterInsights |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MonsterInsights_Popular_Posts_Cache |
|
10
|
|
|
*/ |
|
11
|
|
|
class MonsterInsights_Popular_Posts_Cache { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Instance type (inline/widget/products). |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
public $type; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* MonsterInsights_Popular_Posts_Cache constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $type The instance type (inline/widget/products). |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( $type ) { |
|
26
|
|
|
|
|
27
|
|
|
$this->type = $type; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Build an unique key from the arguments so we can cache different instances. |
|
32
|
|
|
* This way, the Gutenberg block or the sidebar widget get cached with their own query settings |
|
33
|
|
|
* if they are different from the ones set in Vue. |
|
34
|
|
|
* |
|
35
|
|
|
* @param $args |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get_args_key( $args ) { |
|
40
|
|
|
return md5( wp_json_encode( $args ) ); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the specific options key for the set type. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function get_cache_key() { |
|
49
|
|
|
return 'monsterinsights_popular_posts_cache_' . $this->type; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get cached posts data and check expiration. Each query result is stored with the timestamp and that |
|
54
|
|
|
* is used to compare to the current settings for expiration. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $args This is an array with query parameters for WP_Query used to identify if this query has been cached. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function get_cached_posts( $args ) { |
|
61
|
|
|
|
|
62
|
|
|
$cache_refresh_days = $this->get_cache_interval(); |
|
63
|
|
|
$cached_data = get_option( $this->get_cache_key(), array() ); |
|
64
|
|
|
$args_key = $this->get_args_key( $args ); // Generate an unique key based on the instance settings. |
|
65
|
|
|
|
|
66
|
|
|
if ( isset( $cached_data[ $args_key ] ) && isset( $cached_data[ $args_key ]['saved_at'] ) ) { |
|
67
|
|
|
$time_since = time() - $cached_data[ $args_key ]['saved_at']; |
|
68
|
|
|
|
|
69
|
|
|
if ( $time_since < intval( $cache_refresh_days ) * DAY_IN_SECONDS ) { |
|
70
|
|
|
return $cached_data[ $args_key ]['posts']; |
|
71
|
|
|
} else { |
|
72
|
|
|
// It's expired so let's delete it. |
|
73
|
|
|
unset( $cached_data[ $args_key ] ); |
|
74
|
|
|
update_option( $this->get_cache_key(), $cached_data ); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return array(); |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the option set in the settings for cache expiration. |
|
85
|
|
|
* |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
private function get_cache_interval() { |
|
89
|
|
|
$cache_refresh_days = monsterinsights_get_option( 'popular_posts_caching_refresh', 7 ); |
|
90
|
|
|
|
|
91
|
|
|
// If they downgraded and previously used a custom interval use the default 7 until the update the option. |
|
92
|
|
|
if ( 'custom' === $cache_refresh_days ) { |
|
93
|
|
|
$cache_refresh_days = 7; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return intval( $cache_refresh_days ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Store a query result in the cache along with the arguments used to grab them. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $args Arguments used in WP_Query used to build an unique key for the loaded data. |
|
103
|
|
|
* @param array $posts An array of posts that resulted from the query to be saved in the cache. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function save_posts_to_cache( $args, $posts ) { |
|
106
|
|
|
|
|
107
|
|
|
if ( empty( $posts ) ) { |
|
108
|
|
|
// Don't save empty posts. |
|
109
|
|
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$args_key = md5( wp_json_encode( $args ) ); // Generate an unique key based on the instance settings. |
|
|
|
|
|
|
113
|
|
|
$cached_data = get_option( $this->get_cache_key(), array() ); |
|
114
|
|
|
|
|
115
|
|
|
$cached_data[ $args_key ] = array( |
|
116
|
|
|
'saved_at' => time(), |
|
117
|
|
|
'posts' => $posts, |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
update_option( $this->get_cache_key(), $cached_data ); |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Delete database option for cache. |
|
126
|
|
|
*/ |
|
127
|
|
|
public function delete_data() { |
|
128
|
|
|
delete_option( $this->get_cache_key() ); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|