Passed
Push — master ( 0ea9fe...8403c0 )
by
unknown
08:02
created

MonsterInsights_Popular_Posts_Cache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 118
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A save_posts_to_cache() 0 16 2
A get_cached_posts() 0 19 4
A get_args_key() 0 2 1
A get_cache_key() 0 2 1
A get_cache_interval() 0 9 2
A __construct() 0 3 1
A delete_data() 0 2 1
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 ) );
0 ignored issues
show
Bug introduced by
It seems like wp_json_encode($args) can also be of type false; however, parameter $string of md5() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
		return md5( /** @scrutinizer ignore-type */ wp_json_encode( $args ) );
Loading history...
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.
0 ignored issues
show
Bug introduced by
It seems like wp_json_encode($args) can also be of type false; however, parameter $string of md5() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
		$args_key    = md5( /** @scrutinizer ignore-type */ wp_json_encode( $args ) ); // Generate an unique key based on the instance settings.
Loading history...
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