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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
<?php /** * A modules class for Jetpack. * * @package automattic/jetpack-status */
namespace Automattic\Jetpack;
use Automattic\Jetpack\Current_Plan as Jetpack_Plan; use Automattic\Jetpack\IP\Utils as IP_Utils; use Automattic\Jetpack\Status\Host;
/** * Class Automattic\Jetpack\Modules * * Used to retrieve information about the current status of Jetpack modules. */ class Modules {
/** * Check whether or not a Jetpack module is active. * * @param string $module The slug of a Jetpack module. * @param bool $available_only Whether to only check among available modules. * * @return bool */ public function is_active( $module, $available_only = true ) { if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { return true; }
return in_array( $module, self::get_active( $available_only ), true ); }
/** * Load module data from module file. Headers differ from WordPress * plugin headers to avoid them being identified as standalone * plugins on the WordPress plugins page. * * @param string $module The module slug. */ public function get( $module ) { static $modules_details;
// This method relies heavy on auto-generated file found in Jetpack only: module-headings.php // If it doesn't exist, it's safe to assume none of this will be helpful. if ( ! function_exists( 'jetpack_has_no_module_info' ) ) { return false; }
if ( jetpack_has_no_module_info( $module ) ) { return false; }
$file = $this->get_path( $this->get_slug( $module ) );
if ( isset( $modules_details[ $module ] ) ) { $mod = $modules_details[ $module ]; } else { $mod = jetpack_get_module_info( $module );
if ( null === $mod ) { // Try to get the module info from the file as a fallback. $mod = $this->get_file_data( $file, jetpack_get_all_module_header_names() );
if ( empty( $mod['name'] ) ) { // No info for this module. return false; } }
$mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort']; $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order']; $mod['deactivate'] = empty( $mod['deactivate'] ); $mod['free'] = empty( $mod['free'] ); $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' === $mod['requires_connection'] ) ? false : true; $mod['requires_user_connection'] = ( empty( $mod['requires_user_connection'] ) || 'No' === $mod['requires_user_connection'] ) ? false : true;
if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ), true ) ) { $mod['auto_activate'] = 'No'; } else { $mod['auto_activate'] = (string) $mod['auto_activate']; }
if ( $mod['module_tags'] ) { $mod['module_tags'] = explode( ',', $mod['module_tags'] ); $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] ); } else { $mod['module_tags'] = array( 'Other' ); }
if ( $mod['plan_classes'] ) { $mod['plan_classes'] = explode( ',', $mod['plan_classes'] ); $mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) ); } else { $mod['plan_classes'] = array( 'free' ); }
if ( $mod['feature'] ) { $mod['feature'] = explode( ',', $mod['feature'] ); $mod['feature'] = array_map( 'trim', $mod['feature'] ); } else { $mod['feature'] = array( 'Other' ); }
$modules_details[ $module ] = $mod;
}
/** * Filters the feature array on a module. * * This filter allows you to control where each module is filtered: Recommended, * and the default "Other" listing. * * @since-jetpack 3.5.0 * * @param array $mod['feature'] The areas to feature this module: * 'Recommended' shows on the main Jetpack admin screen. * 'Other' should be the default if no other value is in the array. * @param string $module The slug of the module, e.g. sharedaddy. * @param array $mod All the currently assembled module data. */ $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod );
/** * Filter the returned data about a module. * * This filter allows overriding any info about Jetpack modules. It is dangerous, * so please be careful. * * @since-jetpack 3.6.0 * * @param array $mod The details of the requested module. * @param string $module The slug of the module, e.g. sharedaddy * @param string $file The path to the module source file. */ return apply_filters( 'jetpack_get_module', $mod, $module, $file ); }
/** * Like core's get_file_data implementation, but caches the result. * * @param string $file Absolute path to the file. * @param array $headers List of headers, in the format array( 'HeaderKey' => 'Header Name' ). */ public function get_file_data( $file, $headers ) { // Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated. $file_name = basename( $file );
if ( ! Constants::is_defined( 'JETPACK__VERSION' ) ) { return get_file_data( $file, $headers ); }
$cache_key = 'jetpack_file_data_' . JETPACK__VERSION;
$file_data_option = get_transient( $cache_key );
if ( ! is_array( $file_data_option ) ) { delete_transient( $cache_key ); $file_data_option = false; }
if ( false === $file_data_option ) { $file_data_option = array(); }
$key = md5( $file_name . maybe_serialize( $headers ) ); $refresh_cache = is_admin() && isset( $_GET['page'] ) && is_string( $_GET['page'] ) && str_starts_with( $_GET['page'], 'jetpack' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput
// If we don't need to refresh the cache, and already have the value, short-circuit! if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) { return $file_data_option[ $key ]; }
$data = get_file_data( $file, $headers );
$file_data_option[ $key ] = $data;
set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS );
return $data; }
/** * Get a list of activated modules as an array of module slugs. * * @param bool $available_only Filter out the unavailable (deleted) modules. * * @return array */ public function get_active( $available_only = true ) { $active = \Jetpack_Options::get_option( 'active_modules' );
if ( ! is_array( $active ) ) { $active = array(); }
if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) { $active[] = 'vaultpress'; } else { $active = array_diff( $active, array( 'vaultpress' ) ); }
// If protect is active on the main site of a multisite, it should be active on all sites. Doesn't apply to WP.com. if ( ! in_array( 'protect', $active, true ) && ! ( new Host() )->is_wpcom_simple() && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) { $active[] = 'protect'; }
if ( $available_only ) { // If it's not available, it shouldn't be active. // We don't delete it from the options though, as it will be active again when a plugin gets reactivated. $active = array_intersect( $active, $this->get_available() ); }
/** * Allow filtering of the active modules. * * Gives theme and plugin developers the power to alter the modules that * are activated on the fly. * * @since-jetpack 5.8.0 * * @param array $active Array of active module slugs. */ $active = apply_filters( 'jetpack_active_modules', $active );
return array_unique( $active ); }
/** * Extract a module's slug from its full path. * * @param string $file Full path to a file. * * @return string Module slug. */ public function get_slug( $file ) { return str_replace( '.php', '', basename( $file ) ); }
/** * List available Jetpack modules. Simply lists .php files in /modules/. * Make sure to tuck away module "library" files in a sub-directory. * * @param bool|string $min_version Only return modules introduced in this version or later. Default is false, do not filter. * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter. * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection. * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection. * * @return array $modules Array of module slugs */ public function get_available( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) { static $modules = null;
if ( ! class_exists( 'Jetpack' ) || ! Constants::is_defined( 'JETPACK__VERSION' ) || ! Constants::is_defined( 'JETPACK__PLUGIN_DIR' ) ) { return array_unique( /** * Stand alone plugins need to use this filter to register the modules they interact with. * This will allow them to activate and deactivate these modules even when Jetpack is not present. * Note: Standalone plugins can only interact with modules that also exist in the Jetpack plugin, otherwise they'll lose the ability to control it if Jetpack is activated. * * @since 1.13.6 * * @param array $modules The list of available modules as an array of slugs. * @param bool $requires_connection Whether to list only modules that require a connection to work. * @param bool $requires_user_connection Whether to list only modules that require a user connection to work. */ apply_filters( 'jetpack_get_available_standalone_modules', array(), $requires_connection, $requires_user_connection ) ); }
if ( ! isset( $modules ) ) { $available_modules_option = \Jetpack_Options::get_option( 'available_modules', array() ); // Use the cache if we're on the front-end and it's available... if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) { $modules = $available_modules_option[ JETPACK__VERSION ]; } else { $files = ( new Files() )->glob_php( JETPACK__PLUGIN_DIR . 'modules' );
$modules = array();
foreach ( $files as $file ) { $slug = $this->get_slug( $file ); $headers = $this->get( $slug );
if ( ! $headers ) { continue; }
$modules[ $slug ] = $headers['introduced']; }
\Jetpack_Options::update_option( 'available_modules', array( JETPACK__VERSION => $modules, ) ); } }
/** * Filters the array of modules available to be activated. * * @since 2.4.0 * * @param array $modules Array of available modules. * @param string $min_version Minimum version number required to use modules. * @param string $max_version Maximum version number required to use modules. * @param bool|null $requires_connection Value of the Requires Connection filter. * @param bool|null $requires_user_connection Value of the Requires User Connection filter. */ $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version, $requires_connection, $requires_user_connection );
if ( ! $min_version && ! $max_version && $requires_connection === null && $requires_user_connection === null ) { return array_keys( $mods ); }
$r = array(); foreach ( $mods as $slug => $introduced ) { if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) { continue; }
if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) { continue; }
$mod_details = $this->get( $slug );
if ( null !== $requires_connection && (bool) $requires_connection !== $mod_details['requires_connection'] ) { continue; }
if ( null !== $requires_user_connection && (bool) $requires_user_connection !== $mod_details['requires_user_connection'] ) { continue; }
$r[] = $slug; }
return $r; }
/** * Is slug a valid module. * * @param string $module Module slug. * * @return bool */ public function is_module( $module ) { return ! empty( $module ) && ! validate_file( $module, $this->get_available() ); }
/** * Update module status. * * @param string $module - module slug. * @param boolean $active - true to activate, false to deactivate. * @param bool $exit Should exit be called after deactivation. * @param bool $redirect Should there be a redirection after activation. */ public function update_status( $module, $active, $exit = true, $redirect = true ) { return $active ? $this->activate( $module, $exit, $redirect ) : $this->deactivate( $module ); }
/** * Activate a module. * * @param string $module Module slug. * @param bool $exit Should exit be called after deactivation. * @param bool $redirect Should there be a redirection after activation. * * @return bool|void */ public function activate( $module, $exit = true, $redirect = true ) { /** * Fires before a module is activated. * * @since 2.6.0 * * @param string $module Module slug. * @param bool $exit Should we exit after the module has been activated. Default to true. * @param bool $redirect Should the user be redirected after module activation? Default to true. */ do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect );
if ( ! strlen( $module ) ) { return false; }
// If it's already active, then don't do it again. $active = $this->get_active(); foreach ( $active as $act ) { if ( $act === $module ) { return true; } }
if ( ! $this->is_module( $module ) ) { return false; }
// Jetpack plugin only if ( class_exists( 'Jetpack' ) ) {
$module_data = $this->get( $module );
$status = new Status(); $state = new CookieState();
if ( ! \Jetpack::is_connection_ready() ) { if ( ! $status->is_offline_mode() ) { return false; }
// If we're not connected but in offline mode, make sure the module doesn't require a connection. if ( $status->is_offline_mode() && $module_data['requires_connection'] ) { return false; } }
if ( class_exists( 'Jetpack_Client_Server' ) ) { $jetpack = \Jetpack::init();
// Check and see if the old plugin is active. if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) { // Deactivate the old plugins. $deactivated = array(); foreach ( $jetpack->plugins_to_deactivate[ $module ] as $idx => $deactivate_me ) { if ( \Jetpack_Client_Server::deactivate_plugin( $deactivate_me[0], $deactivate_me[1] ) ) { // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load. $deactivated[] = "$module:$idx"; } } if ( $deactivated ) { $state->state( 'deactivated_plugins', implode( ',', $deactivated ) ); wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) ); exit; } } }
// Protect won't work with mis-configured IPs. if ( 'protect' === $module ) { if ( ! IP_Utils::get_ip() ) { $state->state( 'message', 'protect_misconfigured_ip' ); return false; } }
if ( ! Jetpack_Plan::supports( $module ) ) { return false; }
// Check the file for fatal errors, a la wp-admin/plugins.php::activate. $state->state( 'module', $module ); $state->state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error.
ob_start(); $module_path = $this->get_path( $module ); if ( file_exists( $module_path ) ) { require $this->get_path( $module ); // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath }
$active[] = $module; $this->update_active( $active );
$state->state( 'error', false ); // the override. ob_end_clean(); } else { // Not a Jetpack plugin. $active[] = $module; $this->update_active( $active ); }
if ( $redirect ) { wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) ); } if ( $exit ) { exit; } return true; }
/** * Deactivate module. * * @param string $module Module slug. * * @return bool */ public function deactivate( $module ) { /** * Fires when a module is deactivated. * * @since 1.9.0 * * @param string $module Module slug. */ do_action( 'jetpack_pre_deactivate_module', $module );
$active = $this->get_active(); $new = array_filter( array_diff( $active, (array) $module ) );
return $this->update_active( $new ); }
/** * Generate a module's path from its slug. * * @param string $slug Module slug. */ public function get_path( $slug ) { if ( ! Constants::is_defined( 'JETPACK__PLUGIN_DIR' ) ) { return ''; } /** * Filters the path of a modules. * * @since 7.4.0 * * @param array $return The absolute path to a module's root php file * @param string $slug The module slug */ return apply_filters( 'jetpack_get_module_path', JETPACK__PLUGIN_DIR . "modules/$slug.php", $slug ); }
/** * Saves all the currently active modules to options. * Also fires Action hooks for each newly activated and deactivated module. * * @param array $modules Array of active modules to be saved in options. * * @return bool $success true for success, false for failure. */ public function update_active( $modules ) { $current_modules = \Jetpack_Options::get_option( 'active_modules', array() ); $active_modules = $this->get_active(); $new_active_modules = array_diff( $modules, $current_modules ); $new_inactive_modules = array_diff( $active_modules, $modules ); $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules ); $reindexed_modules = array_values( $new_current_modules ); $success = \Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) ); // Let's take `pre_update_option_jetpack_active_modules` filter into account // and actually decide for which modules we need to fire hooks by comparing // the 'active_modules' option before and after the update. $current_modules_post_update = \Jetpack_Options::get_option( 'active_modules', array() );
$new_inactive_modules = array_diff( $current_modules, $current_modules_post_update ); $new_inactive_modules = array_unique( $new_inactive_modules ); $new_inactive_modules = array_values( $new_inactive_modules );
$new_active_modules = array_diff( $current_modules_post_update, $current_modules ); $new_active_modules = array_unique( $new_active_modules ); $new_active_modules = array_values( $new_active_modules );
foreach ( $new_active_modules as $module ) { /** * Fires when a specific module is activated. * * @since 1.9.0 * * @param string $module Module slug. * @param boolean $success whether the module was activated. @since 4.2 */ do_action( 'jetpack_activate_module', $module, $success ); /** * Fires when a module is activated. * The dynamic part of the filter, $module, is the module slug. * * @since 1.9.0 * * @param string $module Module slug. */ do_action( "jetpack_activate_module_$module", $module ); }
foreach ( $new_inactive_modules as $module ) { /** * Fired after a module has been deactivated. * * @since 4.2.0 * * @param string $module Module slug. * @param boolean $success whether the module was deactivated. */ do_action( 'jetpack_deactivate_module', $module, $success ); /** * Fires when a module is deactivated. * The dynamic part of the filter, $module, is the module slug. * * @since 1.9.0 * * @param string $module Module slug. */ do_action( "jetpack_deactivate_module_$module", $module ); }
return $success; } }
|