ObjectCache_Environment.php
4.43 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
<?php
namespace W3TC;
/**
* W3 Object Cache plugin - administrative interface
*/
/**
* class ObjectCache_Environment
*/
class ObjectCache_Environment {
/**
* Fixes environment in each wp-admin request
*
* @param Config $config
* @param bool $force_all_checks
*
* @throws Util_Environment_Exceptions
*/
public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs = new Util_Environment_Exceptions();
try {
$addin_required = $config->get_boolean( 'objectcache.enabled' );
$addin_required = apply_filters( 'w3tc_objectcache_addin_required',
$addin_required );
if ( $addin_required )
$this->create_addin();
else
$this->delete_addin();
} catch ( Util_WpFile_FilesystemOperationException $ex ) {
$exs->push( $ex );
}
if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
/**
* Fixes environment once event occurs
*
* @throws Util_Environment_Exceptions
*/
public function fix_on_event( $config, $event, $old_config = null ) {
if ( $config->get_boolean( 'objectcache.enabled' ) &&
$config->get_string( 'objectcache.engine' ) == 'file' ) {
if ( !wp_next_scheduled( 'w3_objectcache_cleanup' ) ) {
wp_schedule_event( time(),
'w3_objectcache_cleanup', 'w3_objectcache_cleanup' );
}
} else {
$this->unschedule();
}
}
/**
* Fixes environment after plugin deactivation
*
* @return array
*/
public function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();
try {
$this->delete_addin();
} catch ( Util_WpFile_FilesystemOperationException $ex ) {
$exs->push( $ex );
}
$this->unschedule();
if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
/**
* Returns required rules for module
*
* @var Config $config
* @return array
*/
function get_required_rules( $config ) {
return null;
}
/**
* scheduling stuff
*/
private function unschedule() {
if ( wp_next_scheduled( 'w3_objectcache_cleanup' ) ) {
wp_clear_scheduled_hook( 'w3_objectcache_cleanup' );
}
}
/**
* Creates add-in
*
* @throws Util_WpFile_FilesystemOperationException
*/
private function create_addin() {
$src = W3TC_INSTALL_FILE_OBJECT_CACHE;
$dst = W3TC_ADDIN_FILE_OBJECT_CACHE;
if ( $this->objectcache_installed() ) {
if ( $this->is_objectcache_add_in() ) {
$script_data = @file_get_contents( $dst );
if ( $script_data == @file_get_contents( $src ) )
return;
} else if ( get_transient( 'w3tc_remove_add_in_objectcache' ) == 'yes' ) {
// user already manually asked to remove another plugin's add in,
// we should try to apply ours
// (in case of missing permissions deletion could fail)
} else if ( !$this->is_objectcache_old_add_in() ) {
if ( isset( $_GET['page'] ) )
$url = 'admin.php?page=' . $_GET['page'] . '&';
else
$url = basename( Util_Environment::remove_query(
$_SERVER['REQUEST_URI'] ) ) . '?page=w3tc_dashboard&';
$remove_url = Util_Ui::admin_url( $url .
'w3tc_default_remove_add_in=objectcache' );
throw new Util_WpFile_FilesystemOperationException(
sprintf( __( 'The Object Cache add-in file object-cache.php is not a W3 Total Cache drop-in.
Remove it or disable Object Caching. %s', 'w3-total-cache' ),
Util_Ui::button_link( __( 'Yes, remove it for me', 'w3-total-cache' ), wp_nonce_url( $remove_url, 'w3tc' ) ) ) );
}
}
Util_WpFile::copy_file( $src, $dst );
}
/**
* Deletes add-in
*
* @throws Util_WpFile_FilesystemOperationException
*/
private function delete_addin() {
if ( $this->is_objectcache_add_in() )
Util_WpFile::delete_file( W3TC_ADDIN_FILE_OBJECT_CACHE );
}
/**
* Returns true if object-cache.php is installed
*
* @return boolean
*/
public function objectcache_installed() {
return file_exists( W3TC_ADDIN_FILE_OBJECT_CACHE );
}
/**
* Returns true if object-cache.php is old version.
*
* @return boolean
*/
public function is_objectcache_old_add_in() {
return ( ( $script_data = @file_get_contents( W3TC_ADDIN_FILE_OBJECT_CACHE ) )
&& ( ( strstr( $script_data, 'W3 Total Cache Object Cache' ) !== false ) ||
strstr( $script_data, 'w3_instance' ) !== false ) );
}
/**
* Checks if object-cache.php is latest version
*
* @return boolean
*/
public function is_objectcache_add_in() {
return ( ( $script_data = @file_get_contents( W3TC_ADDIN_FILE_OBJECT_CACHE ) )
&& strstr( $script_data, '//ObjectCache Version: 1.4' ) !== false );
}
}