Repository navigation
Expand file tree
/
Copy pathObjectCache.php
More file actions
142 lines (116 loc) · 3.27 KB
/
Copy pathObjectCache.php
File metadata and controls
142 lines (116 loc) · 3.27 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
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
<?php
/**
* Object Cache related functionalities
*
* @package PoweredCache
*/
namespace PoweredCache;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class ObjectCache
*/
class ObjectCache {
/**
* Return an instance of the current class
*
* @return ObjectCache
* @since 1.0
*/
public static function factory() {
static $instance;
if ( ! $instance ) {
$instance = new self();
$instance->setup();
}
return $instance;
}
/**
* Setup hooks
*
* @since 1.0
*/
public function setup() {
$settings = \PoweredCache\Utils\get_settings();
if ( 'off' === $settings['object_cache'] ) {
return;
}
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) );
add_action( 'admin_post_powered_cache_purge_object_cache', array( $this, 'purge_object_cache' ) );
add_action( 'added_option', array( $this, 'maybe_clear_alloptions_cache' ) );
add_action( 'updated_option', array( $this, 'maybe_clear_alloptions_cache' ) );
add_action( 'deleted_option', array( $this, 'maybe_clear_alloptions_cache' ) );
}
/**
* Add purge button on admin bar
*
* @param object $wp_admin_bar Admin bar object
*
* @since 1.0
*/
public function admin_bar_menu( $wp_admin_bar ) {
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$wp_admin_bar->add_menu(
array(
'id' => 'object-cache-purge',
'title' => __( 'Purge Object Cache', 'powered-cache' ),
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_object_cache' ), 'powered_cache_purge_object_cache' ),
'parent' => 'powered-cache',
)
);
}
/**
* Purge object cache
*
* @since 1.0
*/
public function purge_object_cache() {
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'powered_cache_purge_object_cache' ) ) {
wp_nonce_ays( '' );
}
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
$redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
}
if ( ! current_user_can( 'manage_options' ) ) {
$redirect_url = add_query_arg( 'pc_action', 'flush_object_cache_err_permission', wp_get_referer() );
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
}
if ( function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}
$redirect_url = add_query_arg( 'pc_action', 'flush_object_cache', wp_get_referer() );
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
}
/**
* Fix a race condition in alloptions caching
*
* @param string $option option name
*
* @see https://github.com/skopco/powered-cache/issues/47
* @see https://core.trac.wordpress.org/ticket/31245
*
*
* Ported from https://core.trac.wordpress.org/ticket/31245#comment:57
* @since 1.2.4
*/
public function maybe_clear_alloptions_cache( $option ) {
if ( ! wp_installing() ) {
$alloptions = wp_load_alloptions(); // alloptions should be cached at this point
if ( isset( $alloptions[ $option ] ) ) { // only if option is among alloptions
wp_cache_delete( 'alloptions', 'options' );
}
}
}
}