1
|
|
|
<?php |
2
|
|
|
namespace Undefined\Stash; |
3
|
|
|
|
4
|
|
|
class TransientHelper |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* Delete all transients |
8
|
|
|
*/ |
9
|
|
|
public function deleteAll() |
10
|
|
|
{ |
11
|
|
|
$transients = $this->getAll(); |
12
|
|
|
|
13
|
|
|
foreach ($transients as $result) { |
14
|
|
|
$site_wide = (strpos($result->option_name, '_site_transient') !== false); |
15
|
|
|
$name = str_replace($site_wide ? '_site_transient_timeout_' : '_transient_timeout_', '', $result->option_name); |
16
|
|
|
$name = str_replace("_transient_", "", $name); |
17
|
|
|
|
18
|
|
|
$this->delete_transient($name, $site_wide); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Delete a transient by name |
24
|
|
|
* |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
private function delete_transient($transient = '', $site_wide = false) |
28
|
|
|
{ |
29
|
|
|
if (empty($transient)) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (false !== $site_wide) { |
34
|
|
|
return delete_site_transient($transient); |
35
|
|
|
} else { |
36
|
|
|
|
37
|
|
|
return delete_transient($transient); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get all transients from database |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function getAll($args = array()) |
47
|
|
|
{ |
48
|
|
|
global $wpdb; |
49
|
|
|
|
50
|
|
|
$defaults = array( |
51
|
|
|
'offset' => 0, |
52
|
|
|
'number' => 30, |
53
|
|
|
'search' => '' |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$args = wp_parse_args($args, $defaults); |
57
|
|
|
$cache_key = md5(serialize($args)); |
58
|
|
|
$transients = wp_cache_get($cache_key); |
59
|
|
|
|
60
|
|
|
if (false === $transients) { |
61
|
|
|
|
62
|
|
|
$sql = "SELECT * FROM $wpdb->options WHERE option_name LIKE '%\_transient\_%' AND option_name NOT LIKE '%\_transient\_timeout%'"; |
63
|
|
|
|
64
|
|
|
if (!empty($args['search'])) { |
65
|
|
|
|
66
|
|
|
$search = esc_sql($args['search']); |
67
|
|
|
$sql .= " AND option_name LIKE '%{$search}%'"; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$offset = absint($args['offset']); |
71
|
|
|
$number = absint($args['number']); |
72
|
|
|
$sql .= " ORDER BY option_id DESC LIMIT $offset,$number;"; |
73
|
|
|
|
74
|
|
|
$transients = $wpdb->get_results($sql); |
75
|
|
|
|
76
|
|
|
wp_cache_set($cache_key, $transients, '', 3600); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $transients; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$transientHelper = new TransientHelper(); |
84
|
|
|
|
85
|
|
|
add_action('post_updated', function () use ($transientHelper) { |
86
|
|
|
$transientHelper->deleteAll(); |
87
|
|
|
}); |
88
|
|
|
add_action('save_post', function () use ($transientHelper) { |
89
|
|
|
$transientHelper->deleteAll(); |
90
|
|
|
}); |
91
|
|
|
add_action('add_attachment', function () use ($transientHelper) { |
92
|
|
|
$transientHelper->deleteAll(); |
93
|
|
|
}); |