|
1
|
|
|
<?php |
|
2
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
|
3
|
|
|
|
|
4
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
|
5
|
|
|
|
|
6
|
|
|
class Plugins extends Module { |
|
7
|
|
|
|
|
8
|
|
|
private $action_handler; |
|
9
|
|
|
private $plugin_info = array(); |
|
10
|
|
|
private $plugins = array(); |
|
11
|
|
|
|
|
12
|
|
|
public function name() { |
|
13
|
|
|
return 'plugins'; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function init_listeners( $callable ) { |
|
17
|
|
|
$this->action_handler = $callable; |
|
18
|
|
|
|
|
19
|
|
|
add_action( 'deleted_plugin', array( $this, 'deleted_plugin' ), 10, 2 ); |
|
20
|
|
|
add_action( 'activated_plugin', $callable, 10, 2 ); |
|
21
|
|
|
add_action( 'deactivated_plugin', $callable, 10, 2 ); |
|
22
|
|
|
add_action( 'delete_plugin', array( $this, 'delete_plugin' ) ); |
|
23
|
|
|
add_filter( 'upgrader_pre_install', array( $this, 'populate_plugins' ), 10, 1 ); |
|
24
|
|
|
add_action( 'upgrader_process_complete', array( $this, 'on_upgrader_completion' ), 10, 2 ); |
|
25
|
|
|
add_action( 'jetpack_plugin_installed', $callable, 10, 1 ); |
|
26
|
|
|
add_action( 'jetpack_plugin_update_failed', $callable, 10, 4 ); |
|
27
|
|
|
add_action( 'jetpack_plugins_updated', $callable, 10, 2 ); |
|
28
|
|
|
add_action( 'admin_action_update', array( $this, 'check_plugin_edit' ) ); |
|
29
|
|
|
add_action( 'jetpack_edited_plugin', $callable, 10, 2 ); |
|
30
|
|
|
add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'plugin_edit_ajax' ), 0 ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function init_before_send() { |
|
34
|
|
|
add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) ); |
|
35
|
|
|
add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) ); |
|
36
|
|
|
// Note that we don't simply 'expand_plugin_data' on the 'delete_plugin' action here because the plugin file is deleted when that action finishes |
|
37
|
|
|
} |
|
38
|
|
|
public function populate_plugins( $response ) { |
|
39
|
|
|
$this->plugins = get_plugins(); |
|
40
|
|
|
return $response; |
|
41
|
|
|
} |
|
42
|
|
|
public function on_upgrader_completion( $upgrader, $details ) { |
|
43
|
|
|
if ( ! isset( $details['type'] ) ) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
if ( 'plugin' != $details['type'] ) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ( ! isset( $details['action'] ) ) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$plugins = ( isset( $details['plugins'] ) ? $details['plugins'] : null ); |
|
55
|
|
|
if ( empty( $plugins ) ) { |
|
56
|
|
|
$plugins = ( isset( $details['plugin'] ) ? array( $details['plugin'] ) : null ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// for plugin installer |
|
60
|
|
|
if ( empty( $plugins ) && method_exists( $upgrader, 'plugin_info' ) ) { |
|
61
|
|
|
$plugins = array( $upgrader->plugin_info() ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ( empty( $plugins ) ) { |
|
65
|
|
|
return; // We shouldn't be here |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
switch ( $details['action'] ) { |
|
69
|
|
|
case 'update': |
|
70
|
|
|
$state = array( |
|
71
|
|
|
'is_autoupdate' => Jetpack_Constants::is_true( 'JETPACK_PLUGIN_AUTOUPDATE' ), |
|
72
|
|
|
); |
|
73
|
|
|
$errors = $this->get_errors( $upgrader->skin ); |
|
74
|
|
|
if ( $errors ) { |
|
75
|
|
|
foreach ( $plugins as $slug ) { |
|
76
|
|
|
/** |
|
77
|
|
|
* Sync that a plugin update failed |
|
78
|
|
|
* |
|
79
|
|
|
* @since 5.8.0 |
|
80
|
|
|
* |
|
81
|
|
|
* @module sync |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $plugin , Plugin slug |
|
84
|
|
|
* @param string Error code |
|
85
|
|
|
* @param string Error message |
|
86
|
|
|
*/ |
|
87
|
|
|
do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $state ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* Sync that a plugin update |
|
94
|
|
|
* |
|
95
|
|
|
* @since 5.8.0 |
|
96
|
|
|
* |
|
97
|
|
|
* @module sync |
|
98
|
|
|
* |
|
99
|
|
|
* @param array () $plugin, Plugin Data |
|
100
|
|
|
*/ |
|
101
|
|
|
do_action( 'jetpack_plugins_updated', array_map( array( $this, 'get_plugin_info' ), $plugins ), $state ); |
|
102
|
|
|
break; |
|
103
|
|
|
case 'install': |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ( 'install' === $details['action'] ) { |
|
107
|
|
|
/** |
|
108
|
|
|
* Signals to the sync listener that a plugin was installed and a sync action |
|
109
|
|
|
* reflecting the installation and the plugin info should be sent |
|
110
|
|
|
* |
|
111
|
|
|
* @since 5.8.0 |
|
112
|
|
|
* |
|
113
|
|
|
* @module sync |
|
114
|
|
|
* |
|
115
|
|
|
* @param array () $plugin, Plugin Data |
|
116
|
|
|
*/ |
|
117
|
|
|
do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) ); |
|
118
|
|
|
|
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function get_plugin_info( $slug ) { |
|
124
|
|
|
$plugins = get_plugins(); // Get the most up to date info |
|
125
|
|
|
if ( isset( $plugins[ $slug ] ) ) { |
|
126
|
|
|
return array_merge( array( 'slug' => $slug ), $plugins[ $slug ] ); |
|
127
|
|
|
}; |
|
128
|
|
|
// Try grabbing the info from before the update |
|
129
|
|
|
return isset( $this->plugins[ $slug ] ) ? array_merge( array( 'slug' => $slug ), $this->plugins[ $slug ] ) : array( 'slug' => $slug ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function get_errors( $skin ) { |
|
133
|
|
|
$errors = method_exists( $skin, 'get_errors' ) ? $skin->get_errors() : null; |
|
134
|
|
|
if ( is_wp_error( $errors ) ) { |
|
135
|
|
|
$error_code = $errors->get_error_code(); |
|
136
|
|
|
if ( ! empty( $error_code ) ) { |
|
137
|
|
|
return array( |
|
138
|
|
|
'code' => $error_code, |
|
139
|
|
|
'message' => $errors->get_error_message(), |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ( isset( $skin->result ) ) { |
|
145
|
|
|
$errors = $skin->result; |
|
146
|
|
|
if ( is_wp_error( $errors ) ) { |
|
147
|
|
|
return array( |
|
148
|
|
|
'code' => $errors->get_error_code(), |
|
149
|
|
|
'message' => $errors->get_error_message(), |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if ( false == $skin->result ) { |
|
154
|
|
|
return array( |
|
155
|
|
|
'code' => 'unknown', |
|
156
|
|
|
'message' => __( 'Unknown Plugin Update Failure', 'jetpack' ), |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function check_plugin_edit() { |
|
164
|
|
|
$screen = get_current_screen(); |
|
165
|
|
|
if ( 'plugin-editor' !== $screen->base || |
|
166
|
|
|
! isset( $_POST['newcontent'] ) || |
|
167
|
|
|
! isset( $_POST['plugin'] ) |
|
168
|
|
|
) { |
|
169
|
|
|
return; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$plugin = $_POST['plugin']; |
|
173
|
|
|
$plugins = get_plugins(); |
|
174
|
|
|
if ( ! isset( $plugins[ $plugin ] ) ) { |
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Helps Sync log that a plugin was edited |
|
180
|
|
|
* |
|
181
|
|
|
* @since 4.9.0 |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $plugin, Plugin slug |
|
184
|
|
|
* @param mixed $plugins[ $plugin ], Array of plugin data |
|
185
|
|
|
*/ |
|
186
|
|
|
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function plugin_edit_ajax() { |
|
190
|
|
|
// this validation is based on wp_edit_theme_plugin_file() |
|
191
|
|
|
$args = wp_unslash( $_POST ); |
|
192
|
|
|
if ( empty( $args['file'] ) ) { |
|
193
|
|
|
return; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$file = $args['file']; |
|
197
|
|
|
if ( 0 !== validate_file( $file ) ) { |
|
198
|
|
|
return; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
if ( ! isset( $args['newcontent'] ) ) { |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
if ( ! isset( $args['nonce'] ) ) { |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if ( empty( $args['plugin'] ) ) { |
|
210
|
|
|
return; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$plugin = $args['plugin']; |
|
214
|
|
|
if ( ! current_user_can( 'edit_plugins' ) ) { |
|
215
|
|
|
return; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) { |
|
219
|
|
|
return; |
|
220
|
|
|
} |
|
221
|
|
|
$plugins = get_plugins(); |
|
222
|
|
|
if ( ! array_key_exists( $plugin, $plugins ) ) { |
|
223
|
|
|
return; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) { |
|
227
|
|
|
return; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$real_file = WP_PLUGIN_DIR . '/' . $file; |
|
231
|
|
|
|
|
232
|
|
|
if ( ! is_writeable( $real_file ) ) { |
|
233
|
|
|
return; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$file_pointer = fopen( $real_file, 'w+' ); |
|
237
|
|
|
if ( false === $file_pointer ) { |
|
238
|
|
|
return; |
|
239
|
|
|
} |
|
240
|
|
|
fclose( $file_pointer ); |
|
241
|
|
|
/** |
|
242
|
|
|
* This action is documented already in this file |
|
243
|
|
|
*/ |
|
244
|
|
|
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function delete_plugin( $plugin_path ) { |
|
248
|
|
|
$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path; |
|
249
|
|
|
|
|
250
|
|
|
// Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk |
|
251
|
|
|
if ( file_exists( $full_plugin_path ) ) { |
|
252
|
|
|
$all_plugin_data = get_plugin_data( $full_plugin_path ); |
|
253
|
|
|
$data = array( |
|
254
|
|
|
'name' => $all_plugin_data['Name'], |
|
255
|
|
|
'version' => $all_plugin_data['Version'], |
|
256
|
|
|
); |
|
257
|
|
|
} else { |
|
258
|
|
|
$data = array( |
|
259
|
|
|
'name' => $plugin_path, |
|
260
|
|
|
'version' => 'unknown', |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->plugin_info[ $plugin_path ] = $data; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function deleted_plugin( $plugin_path, $is_deleted ) { |
|
268
|
|
|
call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] ); |
|
269
|
|
|
unset( $this->plugin_info[ $plugin_path ] ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function expand_plugin_data( $args ) { |
|
273
|
|
|
$plugin_path = $args[0]; |
|
274
|
|
|
$plugin_data = array(); |
|
275
|
|
|
|
|
276
|
|
|
if ( ! function_exists( 'get_plugins' ) ) { |
|
277
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
278
|
|
|
} |
|
279
|
|
|
$all_plugins = get_plugins(); |
|
280
|
|
|
if ( isset( $all_plugins[ $plugin_path ] ) ) { |
|
281
|
|
|
$all_plugin_data = $all_plugins[ $plugin_path ]; |
|
282
|
|
|
$plugin_data['name'] = $all_plugin_data['Name']; |
|
283
|
|
|
$plugin_data['version'] = $all_plugin_data['Version']; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return array( |
|
287
|
|
|
$args[0], |
|
288
|
|
|
$args[1], |
|
289
|
|
|
$plugin_data, |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|