-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
199 lines (171 loc) · 5.71 KB
/
Controller.php
File metadata and controls
199 lines (171 loc) · 5.71 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace SnapCache;
class Controller {
/**
* Main controller
*
* @var Controller Instance.
*/
protected static $plugin_instance;
protected function __construct() {}
/**
* @return Controller Instance of self.
*/
public static function getInstance(): Controller {
if ( null === self::$plugin_instance ) {
self::$plugin_instance = new self();
}
return self::$plugin_instance;
}
public static function init(): Controller {
$bootstrap_file = SNAPCACHE_PATH . 'snapcache.php';
register_activation_hook(
$bootstrap_file,
self::activate( ... ),
);
register_deactivation_hook(
$bootstrap_file,
self::deactivate( ... ),
);
add_action(
'admin_init',
self::installObjectCache( ... ),
10,
0,
);
add_action(
'add_option_snapcache_object_cache',
self::onObjectCacheSet( ... ),
10,
3,
);
add_action(
'update_option_snapcache_object_cache',
fn( $old_value, $new_value, string $option )
=> self::onObjectCacheSet( $option, $new_value ),
10,
3,
);
if ( is_admin() ) {
Admin\Controller::addUIElements();
}
return self::getInstance();
}
public static function callInEachBlog(
callable $callback,
): void {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$site_ids = $wpdb->get_col(
$wpdb->prepare(
'SELECT blog_id FROM %s WHERE site_id = %d;',
$wpdb->blogs,
$wpdb->siteid
)
);
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
$callback();
}
restore_current_blog();
}
public static function activate(
bool $network_wide = false,
): void {
self::installObjectCache();
if ( $network_wide ) {
self::callInEachBlog(
self::clearAllOptionsCache( ... ),
);
} else {
self::clearAllOptionsCache();
}
}
/**
* Prevent a situation where alloptions is cached with
* the plugin not in active_plugins, and when the plugin
* is reactivated, the stale active_plugins remains cached.
* This prevents the plugin being seen as active.
* Mitigate by clearing alloptions whenever we add, change,
* or remove object-cache.php.
*/
public static function clearAllOptionsCache(): void {
// We call both wp_cache_delete and $mc->delete because
// they could be different caches in some cases, and it's
// better to make sure all possible caches get cleared.
if ( function_exists( 'wp_cache_delete' ) ) {
wp_cache_delete( 'alloptions', 'options' );
}
$smc = Memcached::getSnapCacheMemcached();
if ( $smc instanceof \SnapCacheMemcached ) {
$smc->delete( 'alloptions', 'options' );
}
}
public static function deactivate(
bool $network_wide = false,
): void {
$obj_cache = get_dropins()['object-cache.php'] ?? null;
if ( $obj_cache !== null && $obj_cache['TextDomain'] === 'snapcache' ) {
FilesHelper::deleteFile(
WP_CONTENT_DIR . '/object-cache.php',
);
if ( Options::getObjectCacheType() !== 'disabled' ) {
update_option( 'snapcache_object_cache', 'disabled', false );
}
}
if ( $network_wide ) {
self::callInEachBlog(
self::clearAllOptionsCache( ... ),
);
} else {
self::clearAllOptionsCache();
}
}
/**
* Install our object cache if one of these is true:
* - There is currently no object cache and
* the `snapcache_object_cache` option === "memcached"
* and we can get a connection to memcached.
* - There is an existing SnapCache object-cache.php
* and the version does not match our current version.
*/
public static function installObjectCache(
bool $force = false,
): void {
if ( $force ) {
FilesHelper::copyFile(
SNAPCACHE_PATH . 'src/drop-in/object-cache.php',
WP_CONTENT_DIR . '/object-cache.php',
);
if ( Options::getObjectCacheType() !== 'memcached' ) {
update_option( 'snapcache_object_cache', 'memcached', false );
}
return;
}
$obj_cache = get_dropins()['object-cache.php'] ?? null;
if ( $obj_cache === null ) {
if ( Options::getObjectCacheType() === 'memcached'
&& Memcached::extensionAvailable() ) {
$mc = Memcached::getMemcached();
if ( $mc instanceof \Memcached && $mc->getVersion() !== false ) {
self::installObjectCache( true );
}
}
} elseif ( $obj_cache['TextDomain'] === 'snapcache'
&& $obj_cache['Version'] !== SNAPCACHE_VERSION ) {
self::installObjectCache( true );
}
}
/**
* Try to update object-cache.php when the
* snapcache_object_cache option is changed.
*/
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
public static function onObjectCacheSet( string $option, mixed $new_value ): void {
if ( $new_value === 'disabled' ) {
self::deactivate();
} elseif ( $new_value === 'memcached' ) {
self::installObjectCache();
}
}
}