-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemcached-is-your-friend.php
More file actions
276 lines (223 loc) · 10.1 KB
/
memcached-is-your-friend.php
File metadata and controls
276 lines (223 loc) · 10.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/*
Plugin Name: MemcacheD Is Your Friend
Description: Memcached via PHP Memcache or Memcached Class Support for WordPress
Version: 2.0.0
Plugin URI: http://wordpress.org/extend/plugins/memcached-is-your-friend//
Author: Jeffrey Schutzman - - uses code from Ryan Boren, Denis de Bernardy, Matt Martz, Mike Schroder, Scott Taylor
*/
/*
** Copyright 2010-2015, Pye Brook Company, Inc.
**
**
** This software is provided under the GNU General Public License, version
** 2 (GPLv2), that covers its copying, distribution and modification. The
** GPLv2 license specifically states that it only covers only copying,
** distribution and modification activities. The GPLv2 further states that
** all other activities are outside of the scope of the GPLv2.
**
** All activities outside the scope of the GPLv2 are covered by the Pye Brook
** Company, Inc. License. Any right not explicitly granted by the GPLv2, and
** not explicitly granted by the Pye Brook Company, Inc. License are reserved
** by the Pye Brook Company, Inc.
**
** This software is copyrighted and the property of Pye Brook Company, Inc.
**
** Contact Pye Brook Company, Inc. at info@pyebrook.com for more information.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
** A PARTICULAR PURPOSE.
**
*/
define ( 'WORDPRESS_MEMCACHED_SUPPORT_VERSION', '2.0' );
function wordpress_memcached_support_activate() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
// do not do this work while processing AJAX
return;
}
// Activation code here...
$last_activated_version = get_option( 'wordpress_memcached_support_version', '0.0' );
if ( WORDPRESS_MEMCACHED_SUPPORT_VERSION == $last_activated_version ) {
// no work to do we already activated at least once
return;
}
$template_object_cache_file_path = plugin_dir_path( __FILE__ ) . 'object-cache-template.php';
// make sure we have an object-cache.php with full paths to plugin directory to install to WordPress
// content directory.
$distribution_object_cache_file_path = plugin_dir_path( __FILE__ ) . 'object-cache.php';
if ( ! file_exists( $distribution_object_cache_file_path ) ) {
// build an object-cache.php for this installation
$file_source = file_get_contents( $template_object_cache_file_path );
$this_installation_directory = plugin_dir_path( __FILE__ );
$file_source = str_replace( '%PLUGININSTALLDIRECTORY%', $this_installation_directory, $file_source );
file_put_contents( $distribution_object_cache_file_path, $file_source );
chmod( $distribution_object_cache_file_path, 0644 );
}
if ( ! file_exists( $distribution_object_cache_file_path ) ) {
wordpress_memcached_support_set_admin_notice( 'ERROR: could not create configured object-cache.php for your site, aborting' );
return;
}
$distribution_object_cache_file_unique_id = sha1_file( $distribution_object_cache_file_path );
$operational_object_cache_file_path = wp_content_dir() . 'object-cache.php';
if ( file_exists( $operational_object_cache_file_path ) ) {
$operational_object_cache_file_unique_id = sha1_file( $operational_object_cache_file_path );
} else {
$operational_object_cache_file_unique_id = '';
}
$backup_distribution_object_cache_file_path = plugin_dir_path( __FILE__ ) . 'object-cache.php.backup';
if ( file_exists( $backup_distribution_object_cache_file_path ) ) {
$backup_distribution_object_cache_file_unique_id = sha1_file( $backup_distribution_object_cache_file_path );
} else {
$backup_distribution_object_cache_file_unique_id = '';
}
// if there is an operational object cache, and it has not already been backed up, and it is not the file from
// our plugin directory we backup the file, then remove the operational file
if ( ! empty( $operational_object_cache_file_unique_id )
&& ( $operational_object_cache_file_unique_id != $distribution_object_cache_file_unique_id )
&& ( $operational_object_cache_file_unique_id != $backup_distribution_object_cache_file_unique_id )
) {
$result = copy( $operational_object_cache_file_path, $backup_distribution_object_cache_file_path );
if ( ! $result ) {
wordpress_memcached_support_set_admin_notice( 'ERROR: could not backup operational object-cache.php, aborting' );
return;
}
$result = unlink( $operational_object_cache_file_path );
if ( ! $result ) {
wordpress_memcached_support_set_admin_notice( 'ERROR: could not remove existing operational object-cache.php, aborting' );
return;
}
$operational_object_cache_file_unique_id = '';
}
if (
! file_exists( $operational_object_cache_file_path )
&& file_exists( $distribution_object_cache_file_path )
) {
$result = copy( $distribution_object_cache_file_path, $operational_object_cache_file_path );
if ( ! $result ) {
wordpress_memcached_support_set_admin_notice( 'ERROR: could not copy new object-cache.php from plugin directory to WordPress content directory, aborting' );
return;
}
}
wordpress_memcached_support_set_admin_notice( "WordPress Memcached Support Setup Complete" );
update_option( 'wordpress_memcached_support_version', WORDPRESS_MEMCACHED_SUPPORT_VERSION );
}
register_activation_hook( __FILE__, 'wordpress_memcached_support_activate' );
function wordpress_memcached_support_deactivate() {
// Deactivation code here...
delete_option( 'wordpress_memcached_support_version' );
$distribution_object_cache_file_path = plugin_dir_path( __FILE__ ) . 'object-cache.php';
if ( file_exists( $distribution_object_cache_file_path ) ) {
unlink( $distribution_object_cache_file_path );
if ( file_exists( $distribution_object_cache_file_path ) ) {
wordpress_memcached_support_set_admin_notice( 'ERROR DEACTIVATING: could remove object-cache.php from WordPress plugin directory. Uninstall may have failed, aborting' );
return;
}
}
$operational_object_cache_file_path = wp_content_dir() . 'object-cache.php';
if ( file_exists( $operational_object_cache_file_path ) ) {
unlink( $operational_object_cache_file_path );
if ( file_exists( $operational_object_cache_file_path ) ) {
wordpress_memcached_support_set_admin_notice( 'ERROR DEACTIVATING: could remove object-cache.php from WordPress content directory. Uninstall may have failed, aborting' );
return;
}
}
$backup_distribution_object_cache_file_path = plugin_dir_path( __FILE__ ) . 'object-cache.php.backup';
if ( file_exists( $backup_distribution_object_cache_file_path ) ) {
$result = copy( $backup_distribution_object_cache_file_path, $operational_object_cache_file_path );
if ( ! $result ) {
wordpress_memcached_support_set_admin_notice( 'ERROR DEACTIVATING: could restore backup object-cache.php to WordPress content directory. Uninstall may have failed.' );
return;
}
}
wordpress_memcached_support_set_admin_notice( 'WordPress Memcached Support object-cache.php removed from WordPress content directory. Memcached support deactivated.' );
// sadly we cant show an admin notice after we deactivate, we need to clear it so it doesn't show on future activation
wordpress_memcached_support_set_admin_notice();
return;
}
register_deactivation_hook( __FILE__, 'wordpress_memcached_support_deactivate' );
if ( ! function_exists( 'wp_content_dir' ) ) {
function wp_content_dir() {
// plugins are guided to to not use the WP_CONTENT_DIR constant, but no alternative is provided in the API :(
return trailingslashit( WP_CONTENT_DIR );
}
}
function wordpress_memcached_support_check_for_update() {
if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
// plugins can be updated without the activation hook firing, this is a check for that
$last_activated_version = get_option( 'wordpress_memcached_support_version', '0.0' );
if ( WORDPRESS_MEMCACHED_SUPPORT_VERSION == $last_activated_version ) {
// no work to do we already activated at least once
return;
}
wordpress_memcached_support_activate();
}
}
if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
add_action( 'admin_init', 'wordpress_memcached_support_check_for_update' );
}
function wordpress_memcached_support_set_admin_notice( $notice = false ) {
if ( empty( $notice ) ) {
delete_option( 'wordpress_memcached_support_notice' );
} else {
$notice = update_option( 'wordpress_memcached_support_notice', $notice );
error_log( __FILE__ . ': ' . $notice );
}
}
function wordpress_memcached_support_show_admin_notice() {
$wordpress_memcached_support_notice = get_option( 'wordpress_memcached_support_notice', '' );
if ( ! empty( $wordpress_memcached_support_notice ) ) {
$class = ( strpos( $wordpress_memcached_support_notice, 'ERROR' ) === false ) ? 'updated' : 'error';
?>
<div class="<?php echo $class; ?>">
<p><?php echo $wordpress_memcached_support_notice; ?></p>
</div>
<?php
wordpress_memcached_support_set_admin_notice( false );
}
}
$wordpress_memcached_support_notice = get_option( 'wordpress_memcached_support_notice', '' );
if ( ! empty( $wordpress_memcached_support_notice ) ) {
add_action( 'admin_notices', 'wordpress_memcached_support_show_admin_notice' );
}
if ( is_admin() ) {
add_action( 'admin_menu', 'wordpress_memcached_support_admin_menu' );
}
function wordpress_memcached_support_admin_menu() {
add_management_page(
__( 'Memcached', 'wordpress-memcached' ),
__( 'Memcached', 'wordpress-memcached' ),
'manage_options',
'wordpress_memcached_support_admin_page',
'wordpress_memcached_support_admin_page'
);
}
function wordpress_memcached_support_admin_page() {
?>
<div class="wrap">
<h2>WordPress Memcached Status</h2>
<div>
<h3>
<?php
if ( class_exists( 'Memcached' ) ) {
_e( 'Using the PHP Memcached class to interact with Memcached', 'wordpress-memcached' );
} else if ( class_exists( 'Memcache' ) ) {
_e( 'Using the PHP Memcache class to interact with Memcached', 'wordpress-memcached' );
} else {
_e( 'No PHP Memcached or Memcache class present, this is really bad!', 'wordpress-memcached' );
}
?>
</h3>
</div>
<br>
<div>
<?php
if ( function_exists( 'wordpress_memcached_get_stats' ) ) {
$stats_text = wordpress_memcached_get_stats();
echo nl2br( $stats_text );
}
?>
</div>
</div>
<?php
}