1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plugins sync module. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Modules; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class to handle sync for plugins. |
14
|
|
|
*/ |
15
|
|
|
class Plugins extends Module { |
16
|
|
|
/** |
17
|
|
|
* Action handler callable. |
18
|
|
|
* |
19
|
|
|
* @access private |
20
|
|
|
* |
21
|
|
|
* @var callable |
22
|
|
|
*/ |
23
|
|
|
private $action_handler; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Information about plugins we store temporarily. |
27
|
|
|
* |
28
|
|
|
* @access private |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $plugin_info = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List of all plugins in the installation. |
36
|
|
|
* |
37
|
|
|
* @access private |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $plugins = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sync module name. |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function name() { |
51
|
|
|
return 'plugins'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize plugins action listeners. |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
* |
59
|
|
|
* @param callable $callable Action handler callable. |
60
|
|
|
*/ |
61
|
|
|
public function init_listeners( $callable ) { |
62
|
|
|
$this->action_handler = $callable; |
63
|
|
|
|
64
|
|
|
add_action( 'deleted_plugin', array( $this, 'deleted_plugin' ), 10, 2 ); |
65
|
|
|
add_action( 'activated_plugin', $callable, 10, 2 ); |
66
|
|
|
add_action( 'deactivated_plugin', $callable, 10, 2 ); |
67
|
|
|
add_action( 'delete_plugin', array( $this, 'delete_plugin' ) ); |
68
|
|
|
add_filter( 'upgrader_pre_install', array( $this, 'populate_plugins' ), 10, 1 ); |
69
|
|
|
add_action( 'upgrader_process_complete', array( $this, 'on_upgrader_completion' ), 10, 2 ); |
70
|
|
|
add_action( 'jetpack_plugin_installed', $callable, 10, 1 ); |
71
|
|
|
add_action( 'jetpack_plugin_update_failed', $callable, 10, 4 ); |
72
|
|
|
add_action( 'jetpack_plugins_updated', $callable, 10, 2 ); |
73
|
|
|
add_action( 'admin_action_update', array( $this, 'check_plugin_edit' ) ); |
74
|
|
|
add_action( 'jetpack_edited_plugin', $callable, 10, 2 ); |
75
|
|
|
add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'plugin_edit_ajax' ), 0 ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initialize the module in the sender. |
80
|
|
|
* |
81
|
|
|
* @access public |
82
|
|
|
*/ |
83
|
|
|
public function init_before_send() { |
84
|
|
|
add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) ); |
85
|
|
|
add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) ); |
86
|
|
|
// 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. |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Fetch and populate all current plugins before upgrader installation. |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* |
94
|
|
|
* @param bool|WP_Error $response Install response, true if successful, WP_Error if not. |
95
|
|
|
*/ |
96
|
|
|
public function populate_plugins( $response ) { |
97
|
|
|
$this->plugins = get_plugins(); |
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Handler for the upgrader success finishes. |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* |
106
|
|
|
* @param \WP_Upgrader $upgrader Upgrader instance. |
107
|
|
|
* @param array $details Array of bulk item update data. |
108
|
|
|
*/ |
109
|
|
|
public function on_upgrader_completion( $upgrader, $details ) { |
110
|
|
|
if ( ! isset( $details['type'] ) ) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
if ( 'plugin' !== $details['type'] ) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( ! isset( $details['action'] ) ) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$plugins = ( isset( $details['plugins'] ) ? $details['plugins'] : null ); |
122
|
|
|
if ( empty( $plugins ) ) { |
123
|
|
|
$plugins = ( isset( $details['plugin'] ) ? array( $details['plugin'] ) : null ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// For plugin installer. |
127
|
|
|
if ( empty( $plugins ) && method_exists( $upgrader, 'plugin_info' ) ) { |
128
|
|
|
$plugins = array( $upgrader->plugin_info() ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ( empty( $plugins ) ) { |
132
|
|
|
return; // We shouldn't be here. |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
switch ( $details['action'] ) { |
136
|
|
|
case 'update': |
137
|
|
|
$state = array( |
138
|
|
|
'is_autoupdate' => Jetpack_Constants::is_true( 'JETPACK_PLUGIN_AUTOUPDATE' ), |
139
|
|
|
); |
140
|
|
|
$errors = $this->get_errors( $upgrader->skin ); |
141
|
|
|
if ( $errors ) { |
142
|
|
|
foreach ( $plugins as $slug ) { |
143
|
|
|
/** |
144
|
|
|
* Sync that a plugin update failed |
145
|
|
|
* |
146
|
|
|
* @since 5.8.0 |
147
|
|
|
* |
148
|
|
|
* @module sync |
149
|
|
|
* |
150
|
|
|
* @param string $plugin , Plugin slug |
151
|
|
|
* @param string Error code |
152
|
|
|
* @param string Error message |
153
|
|
|
*/ |
154
|
|
|
do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $state ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
/** |
160
|
|
|
* Sync that a plugin update |
161
|
|
|
* |
162
|
|
|
* @since 5.8.0 |
163
|
|
|
* |
164
|
|
|
* @module sync |
165
|
|
|
* |
166
|
|
|
* @param array () $plugin, Plugin Data |
167
|
|
|
*/ |
168
|
|
|
do_action( 'jetpack_plugins_updated', array_map( array( $this, 'get_plugin_info' ), $plugins ), $state ); |
169
|
|
|
break; |
170
|
|
|
case 'install': |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if ( 'install' === $details['action'] ) { |
174
|
|
|
/** |
175
|
|
|
* Signals to the sync listener that a plugin was installed and a sync action |
176
|
|
|
* reflecting the installation and the plugin info should be sent |
177
|
|
|
* |
178
|
|
|
* @since 5.8.0 |
179
|
|
|
* |
180
|
|
|
* @module sync |
181
|
|
|
* |
182
|
|
|
* @param array () $plugin, Plugin Data |
183
|
|
|
*/ |
184
|
|
|
do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) ); |
185
|
|
|
|
186
|
|
|
return; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Retrieve the plugin information by a plugin slug. |
192
|
|
|
* |
193
|
|
|
* @access private |
194
|
|
|
* |
195
|
|
|
* @param string $slug Plugin slug. |
196
|
|
|
* @return array Plugin information. |
197
|
|
|
*/ |
198
|
|
|
private function get_plugin_info( $slug ) { |
199
|
|
|
$plugins = get_plugins(); // Get the most up to date info. |
200
|
|
|
if ( isset( $plugins[ $slug ] ) ) { |
201
|
|
|
return array_merge( array( 'slug' => $slug ), $plugins[ $slug ] ); |
202
|
|
|
}; |
203
|
|
|
// Try grabbing the info from before the update. |
204
|
|
|
return isset( $this->plugins[ $slug ] ) ? array_merge( array( 'slug' => $slug ), $this->plugins[ $slug ] ) : array( 'slug' => $slug ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Retrieve upgrade errors. |
209
|
|
|
* |
210
|
|
|
* @access private |
211
|
|
|
* |
212
|
|
|
* @param \Automatic_Upgrader_Skin|\WP_Upgrader_Skin $skin The upgrader skin being used. |
213
|
|
|
* @return array|boolean Error on error, false otherwise. |
214
|
|
|
*/ |
215
|
|
|
private function get_errors( $skin ) { |
216
|
|
|
$errors = method_exists( $skin, 'get_errors' ) ? $skin->get_errors() : null; |
217
|
|
|
if ( is_wp_error( $errors ) ) { |
218
|
|
|
$error_code = $errors->get_error_code(); |
219
|
|
|
if ( ! empty( $error_code ) ) { |
220
|
|
|
return array( |
221
|
|
|
'code' => $error_code, |
222
|
|
|
'message' => $errors->get_error_message(), |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if ( isset( $skin->result ) ) { |
228
|
|
|
$errors = $skin->result; |
229
|
|
|
if ( is_wp_error( $errors ) ) { |
230
|
|
|
return array( |
231
|
|
|
'code' => $errors->get_error_code(), |
232
|
|
|
'message' => $errors->get_error_message(), |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if ( empty( $skin->result ) ) { |
237
|
|
|
return array( |
238
|
|
|
'code' => 'unknown', |
239
|
|
|
'message' => __( 'Unknown Plugin Update Failure', 'jetpack' ), |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Handle plugin edit in the administration. |
248
|
|
|
* |
249
|
|
|
* @access public |
250
|
|
|
* |
251
|
|
|
* @todo The `admin_action_update` hook is called only for logged in users, but maybe implement nonce verification? |
252
|
|
|
*/ |
253
|
|
|
public function check_plugin_edit() { |
254
|
|
|
$screen = get_current_screen(); |
255
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
256
|
|
|
if ( 'plugin-editor' !== $screen->base || ! isset( $_POST['newcontent'] ) || ! isset( $_POST['plugin'] ) ) { |
257
|
|
|
return; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
261
|
|
|
$plugin = $_POST['plugin']; |
262
|
|
|
$plugins = get_plugins(); |
263
|
|
|
if ( ! isset( $plugins[ $plugin ] ) ) { |
264
|
|
|
return; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Helps Sync log that a plugin was edited |
269
|
|
|
* |
270
|
|
|
* @since 4.9.0 |
271
|
|
|
* |
272
|
|
|
* @param string $plugin, Plugin slug |
273
|
|
|
* @param mixed $plugins[ $plugin ], Array of plugin data |
274
|
|
|
*/ |
275
|
|
|
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Handle plugin ajax edit in the administration. |
280
|
|
|
* |
281
|
|
|
* @access public |
282
|
|
|
* |
283
|
|
|
* @todo Update this method to use WP_Filesystem instead of fopen/fclose. |
284
|
|
|
*/ |
285
|
|
|
public function plugin_edit_ajax() { |
286
|
|
|
// This validation is based on wp_edit_theme_plugin_file(). |
287
|
|
|
$args = wp_unslash( $_POST ); |
288
|
|
|
if ( empty( $args['file'] ) ) { |
289
|
|
|
return; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$file = $args['file']; |
293
|
|
|
if ( 0 !== validate_file( $file ) ) { |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if ( ! isset( $args['newcontent'] ) ) { |
298
|
|
|
return; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
if ( ! isset( $args['nonce'] ) ) { |
302
|
|
|
return; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if ( empty( $args['plugin'] ) ) { |
306
|
|
|
return; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$plugin = $args['plugin']; |
310
|
|
|
if ( ! current_user_can( 'edit_plugins' ) ) { |
311
|
|
|
return; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) { |
315
|
|
|
return; |
316
|
|
|
} |
317
|
|
|
$plugins = get_plugins(); |
318
|
|
|
if ( ! array_key_exists( $plugin, $plugins ) ) { |
319
|
|
|
return; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) { |
323
|
|
|
return; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$real_file = WP_PLUGIN_DIR . '/' . $file; |
327
|
|
|
|
328
|
|
|
if ( ! is_writeable( $real_file ) ) { |
329
|
|
|
return; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen |
333
|
|
|
$file_pointer = fopen( $real_file, 'w+' ); |
334
|
|
|
if ( false === $file_pointer ) { |
335
|
|
|
return; |
336
|
|
|
} |
337
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose |
338
|
|
|
fclose( $file_pointer ); |
339
|
|
|
/** |
340
|
|
|
* This action is documented already in this file |
341
|
|
|
*/ |
342
|
|
|
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Handle plugin deletion. |
347
|
|
|
* |
348
|
|
|
* @access public |
349
|
|
|
* |
350
|
|
|
* @param string $plugin_path Path to the plugin main file. |
351
|
|
|
*/ |
352
|
|
|
public function delete_plugin( $plugin_path ) { |
353
|
|
|
$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path; |
354
|
|
|
|
355
|
|
|
// Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk. |
356
|
|
|
if ( file_exists( $full_plugin_path ) ) { |
357
|
|
|
$all_plugin_data = get_plugin_data( $full_plugin_path ); |
358
|
|
|
$data = array( |
359
|
|
|
'name' => $all_plugin_data['Name'], |
360
|
|
|
'version' => $all_plugin_data['Version'], |
361
|
|
|
); |
362
|
|
|
} else { |
363
|
|
|
$data = array( |
364
|
|
|
'name' => $plugin_path, |
365
|
|
|
'version' => 'unknown', |
366
|
|
|
); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$this->plugin_info[ $plugin_path ] = $data; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Invoked after plugin deletion. |
374
|
|
|
* |
375
|
|
|
* @access public |
376
|
|
|
* |
377
|
|
|
* @param string $plugin_path Path to the plugin main file. |
378
|
|
|
* @param boolean $is_deleted Whether the plugin was deleted successfully. |
379
|
|
|
*/ |
380
|
|
|
public function deleted_plugin( $plugin_path, $is_deleted ) { |
381
|
|
|
call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] ); |
382
|
|
|
unset( $this->plugin_info[ $plugin_path ] ); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Expand the plugins within a hook before they are serialized and sent to the server. |
387
|
|
|
* |
388
|
|
|
* @access public |
389
|
|
|
* |
390
|
|
|
* @param array $args The hook parameters. |
391
|
|
|
* @return array $args The expanded hook parameters. |
392
|
|
|
*/ |
393
|
|
|
public function expand_plugin_data( $args ) { |
394
|
|
|
$plugin_path = $args[0]; |
395
|
|
|
$plugin_data = array(); |
396
|
|
|
|
397
|
|
|
if ( ! function_exists( 'get_plugins' ) ) { |
398
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
399
|
|
|
} |
400
|
|
|
$all_plugins = get_plugins(); |
401
|
|
|
if ( isset( $all_plugins[ $plugin_path ] ) ) { |
402
|
|
|
$all_plugin_data = $all_plugins[ $plugin_path ]; |
403
|
|
|
$plugin_data['name'] = $all_plugin_data['Name']; |
404
|
|
|
$plugin_data['version'] = $all_plugin_data['Version']; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return array( |
408
|
|
|
$args[0], |
409
|
|
|
$args[1], |
410
|
|
|
$plugin_data, |
411
|
|
|
); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|