-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-kickbox-gf-cache.php
156 lines (122 loc) · 4.59 KB
/
class-kickbox-gf-cache.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class Kickbox_GF_Cache {
const DATABASE_KEY = 'kickbox_gf_verifications';
const CRON_ACTION = 'kickbox_gf_prune_old_verifications';
/**
* Checks stored verifications and prunes them if they are older than defined Caching Duration.
*
* @return void
*/
public static function prune_old_verifications() {
$addon = get_kickbox_gf_addon();
$results_caching_enabled = $addon->get_plugin_setting( 'results_caching_enabled' );
if ( empty( $results_caching_enabled ) ) {
$scheduled_pruning = wp_next_scheduled( self::CRON_ACTION );
if ( $scheduled_pruning ) {
wp_unschedule_event( $scheduled_pruning, self::CRON_ACTION );
}
return;
}
$stored_verifications = get_option( self::DATABASE_KEY );
do_action( 'kickbox_gf_verifications_before_pruning', $stored_verifications );
if ( empty( $stored_verifications ) ) {
return;
}
$time_now = time();
$results_caching_duration = self::get_caching_duration();
foreach ( $stored_verifications as $key => $value ) {
if ( $time_now - $value['verification-timestamp'] > $results_caching_duration * DAY_IN_SECONDS ) {
unset( $stored_verifications[ $key ] );
}
}
update_option( self::DATABASE_KEY, $stored_verifications );
do_action( 'kickbox_gf_verifications_pruned', $stored_verifications );
}
/**
* Stores a verification in the database for the defined Caching Duration.
*
* @param array $verification Verification details.
*
* @return void
*/
public static function cache_verification( array $verification ) {
$addon = get_kickbox_gf_addon();
$results_caching_enabled = $addon->get_plugin_setting( 'results_caching_enabled' );
if ( ! empty( $results_caching_enabled ) ) {
$cache_key = self::get_verification_cache_key( $verification['data']['body']['email'] );
wp_cache_set( "kickbox_gf_{$cache_key}_result", $verification['data']['body'] );
$stored_kickboxes = get_option( self::DATABASE_KEY );
$verification_to_store = apply_filters( 'kickbox_gf_cache_item', array(
'kickbox' => $verification,
'verification-timestamp' => time(),
), $cache_key );
$stored_kickboxes[ $cache_key ] = $verification_to_store;
update_option( self::DATABASE_KEY, $stored_kickboxes );
do_action( 'kickbox_gf_verification_cached', $verification_to_store );
}
}
/**
* Retrieves a cached verification from the database.
*
* @param string $key Cache key.
*
* @return array If verification is not found, returns an empty array
*/
public static function get_cached_verification( string $key ) : array {
$stored_kickboxes = get_option( self::DATABASE_KEY );
if ( empty( $stored_kickboxes ) || empty( $stored_kickboxes[ $key ] ) ) {
return array();
}
return $stored_kickboxes[ $key ];
}
/**
* Constructs a cache key to be used to identify a verification.
*
* @param string $email Email that is being verified.
*
* @return string
*/
public static function get_verification_cache_key( string $email ) : string {
$addon = get_kickbox_gf_addon();
$cache_key = $email;
$domain_caching_enabled = $addon->get_plugin_setting( 'domain_caching_enabled' );
if ( ! empty( $domain_caching_enabled ) ) {
$email_parts = explode( '@', $cache_key );
$cache_key = $email_parts[1];
}
return apply_filters( 'kickbox_gf_verification_cache_key', $cache_key, $email );
}
/**
* Returns the caching duration for verifications.
*
* @return int
*/
public static function get_caching_duration() : int {
$addon = get_kickbox_gf_addon();
$results_caching_duration = $addon->get_plugin_setting( 'results_caching_duration' );
if ( empty( $results_caching_duration ) ) {
$results_caching_duration = 7;
}
return (int) apply_filters( 'kickbox_gf_results_caching_duration', $results_caching_duration );
}
/**
* Checks if it's OK to use the cached verification.
*
* @param string $email Email that is being verified.
*
* @return bool
*/
public static function is_cached_verification_fresh( string $email ) : bool {
$is_cached_verification_fresh = false;
$cache_key = self::get_verification_cache_key( $email );
$cached_verification = self::get_cached_verification( $cache_key );
if ( ! empty( $cached_verification ) ) {
$is_cached_verification_fresh = true;
$results_caching_duration = self::get_caching_duration();
if ( time() - $cached_verification['verification-timestamp'] > $results_caching_duration * DAY_IN_SECONDS ) {
$is_cached_verification_fresh = false;
}
}
return $is_cached_verification_fresh;
}
}