PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-status / src / class-modules.php

class-modules.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at jetpack_vendor/automattic/jetpack-status/src/class-modules.php

624 lines 20.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A modules class for Jetpack.
4 *
5 * @package automattic/jetpack-status
6 */
7
8 namespace Automattic\Jetpack;
9
10 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
11 use Automattic\Jetpack\IP\Utils as IP_Utils;
12 use Automattic\Jetpack\Status\Host;
13
14 /**
15 * Class Automattic\Jetpack\Modules
16 *
17 * Used to retrieve information about the current status of Jetpack modules.
18 */
19 class Modules {
20
21 /**
22 * Check whether or not a Jetpack module is active.
23 *
24 * @param string $module The slug of a Jetpack module.
25 * @param bool $available_only Whether to only check among available modules.
26 *
27 * @return bool
28 */
29 public function is_active( $module, $available_only = true ) {
30 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
31 return true;
32 }
33
34 return in_array( $module, self::get_active( $available_only ), true );
35 }
36
37 /**
38 * Load module data from module file. Headers differ from WordPress
39 * plugin headers to avoid them being identified as standalone
40 * plugins on the WordPress plugins page.
41 *
42 * @param string $module The module slug.
43 */
44 public function get( $module ) {
45 static $modules_details;
46
47 // This method relies heavy on auto-generated file found in Jetpack only: module-headings.php
48 // If it doesn't exist, it's safe to assume none of this will be helpful.
49 if ( ! function_exists( 'jetpack_has_no_module_info' ) ) {
50 return false;
51 }
52
53 if ( jetpack_has_no_module_info( $module ) ) {
54 return false;
55 }
56
57 $file = $this->get_path( $this->get_slug( $module ) );
58
59 if ( isset( $modules_details[ $module ] ) ) {
60 $mod = $modules_details[ $module ];
61 } else {
62 $mod = jetpack_get_module_info( $module );
63
64 if ( null === $mod ) {
65 // Try to get the module info from the file as a fallback.
66 $mod = $this->get_file_data( $file, jetpack_get_all_module_header_names() );
67
68 if ( empty( $mod['name'] ) ) {
69 // No info for this module.
70 return false;
71 }
72 }
73
74 $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
75 $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
76 $mod['deactivate'] = empty( $mod['deactivate'] );
77 $mod['free'] = empty( $mod['free'] );
78 $mod['requires_connection'] = empty( $mod['requires_connection'] ) || 'No' !== $mod['requires_connection'];
79 $mod['requires_user_connection'] = ! ( empty( $mod['requires_user_connection'] ) || 'No' === $mod['requires_user_connection'] );
80
81 if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ), true ) ) {
82 $mod['auto_activate'] = 'No';
83 } else {
84 $mod['auto_activate'] = (string) $mod['auto_activate'];
85 }
86
87 if ( $mod['module_tags'] ) {
88 $mod['module_tags'] = explode( ',', $mod['module_tags'] );
89 $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
90 } else {
91 $mod['module_tags'] = array( 'Other' );
92 }
93
94 if ( $mod['plan_classes'] ) {
95 $mod['plan_classes'] = explode( ',', $mod['plan_classes'] );
96 $mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) );
97 } else {
98 $mod['plan_classes'] = array( 'free' );
99 }
100
101 if ( $mod['feature'] ) {
102 $mod['feature'] = explode( ',', $mod['feature'] );
103 $mod['feature'] = array_map( 'trim', $mod['feature'] );
104 } else {
105 $mod['feature'] = array( 'Other' );
106 }
107
108 $modules_details[ $module ] = $mod;
109
110 }
111
112 /**
113 * Filters the feature array on a module.
114 *
115 * This filter allows you to control where each module is filtered: Recommended,
116 * and the default "Other" listing.
117 *
118 * @since-jetpack 3.5.0
119 *
120 * @param array $mod['feature'] The areas to feature this module:
121 * 'Recommended' shows on the main Jetpack admin screen.
122 * 'Other' should be the default if no other value is in the array.
123 * @param string $module The slug of the module, e.g. sharedaddy.
124 * @param array $mod All the currently assembled module data.
125 */
126 $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod );
127
128 /**
129 * Filter the returned data about a module.
130 *
131 * This filter allows overriding any info about Jetpack modules. It is dangerous,
132 * so please be careful.
133 *
134 * @since-jetpack 3.6.0
135 *
136 * @param array $mod The details of the requested module.
137 * @param string $module The slug of the module, e.g. sharedaddy
138 * @param string $file The path to the module source file.
139 */
140 return apply_filters( 'jetpack_get_module', $mod, $module, $file );
141 }
142
143 /**
144 * Like core's get_file_data implementation, but caches the result.
145 *
146 * @param string $file Absolute path to the file.
147 * @param array $headers List of headers, in the format array( 'HeaderKey' => 'Header Name' ).
148 */
149 public function get_file_data( $file, $headers ) {
150 // Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated.
151 $file_name = basename( $file );
152
153 if ( ! Constants::is_defined( 'JETPACK__VERSION' ) ) {
154 return get_file_data( $file, $headers );
155 }
156
157 $cache_key = 'jetpack_file_data_' . JETPACK__VERSION;
158
159 $file_data_option = get_transient( $cache_key );
160
161 if ( ! is_array( $file_data_option ) ) {
162 delete_transient( $cache_key );
163 $file_data_option = false;
164 }
165
166 if ( false === $file_data_option ) {
167 $file_data_option = array();
168 }
169
170 $key = md5( $file_name . maybe_serialize( $headers ) );
171 $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
172
173 // If we don't need to refresh the cache, and already have the value, short-circuit!
174 if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) {
175 return $file_data_option[ $key ];
176 }
177
178 $data = get_file_data( $file, $headers );
179
180 $file_data_option[ $key ] = $data;
181
182 set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS );
183
184 return $data;
185 }
186
187 /**
188 * Get a list of activated modules as an array of module slugs.
189 *
190 * @param bool $available_only Filter out the unavailable (deleted) modules.
191 *
192 * @return array
193 */
194 public function get_active( $available_only = true ) {
195 $active = \Jetpack_Options::get_option( 'active_modules' );
196
197 if ( ! is_array( $active ) ) {
198 $active = array();
199 }
200
201 if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) {
202 $active[] = 'vaultpress';
203 } else {
204 $active = array_diff( $active, array( 'vaultpress' ) );
205 }
206
207 // If protect is active on the main site of a multisite, it should be active on all sites. Doesn't apply to WP.com.
208 if ( ! in_array( 'protect', $active, true )
209 && ! ( new Host() )->is_wpcom_simple()
210 && is_multisite()
211 && get_site_option( 'jetpack_protect_active' ) ) {
212 $active[] = 'protect';
213 }
214
215 if ( $available_only ) {
216 // If it's not available, it shouldn't be active.
217 // We don't delete it from the options though, as it will be active again when a plugin gets reactivated.
218 $active = array_intersect( $active, $this->get_available() );
219 }
220
221 Feature_Policy::ensure_hooks();
222
223 /**
224 * Allow filtering of the active modules.
225 *
226 * Gives theme and plugin developers the power to alter the modules that
227 * are activated on the fly.
228 *
229 * @since-jetpack 5.8.0
230 *
231 * @param array $active Array of active module slugs.
232 */
233 $active = apply_filters( 'jetpack_active_modules', $active );
234
235 return array_unique( $active );
236 }
237
238 /**
239 * Extract a module's slug from its full path.
240 *
241 * @param string $file Full path to a file.
242 *
243 * @return string Module slug.
244 */
245 public function get_slug( $file ) {
246 return str_replace( '.php', '', basename( $file ) );
247 }
248
249 /**
250 * List available Jetpack modules. Simply lists .php files in /modules/.
251 * Make sure to tuck away module "library" files in a sub-directory.
252 *
253 * @param bool|string $min_version Only return modules introduced in this version or later. Default is false, do not filter.
254 * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter.
255 * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection.
256 * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection.
257 *
258 * @return array $modules Array of module slugs
259 */
260 public function get_available( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) {
261 static $modules = null;
262
263 if ( ! class_exists( 'Jetpack' ) || ! Constants::is_defined( 'JETPACK__VERSION' ) || ! Constants::is_defined( 'JETPACK__PLUGIN_DIR' ) ) {
264 return array_unique(
265 /**
266 * Stand alone plugins need to use this filter to register the modules they interact with.
267 * This will allow them to activate and deactivate these modules even when Jetpack is not present.
268 * 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.
269 *
270 * @since 1.13.6
271 *
272 * @param array $modules The list of available modules as an array of slugs.
273 * @param bool $requires_connection Whether to list only modules that require a connection to work.
274 * @param bool $requires_user_connection Whether to list only modules that require a user connection to work.
275 */
276 apply_filters( 'jetpack_get_available_standalone_modules', array(), $requires_connection, $requires_user_connection )
277 );
278 }
279
280 if ( ! isset( $modules ) ) {
281 $available_modules_option = \Jetpack_Options::get_option( 'available_modules', array() );
282 // Use the cache if we're on the front-end and it's available...
283 if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) {
284 $modules = $available_modules_option[ JETPACK__VERSION ];
285 } else {
286 $files = ( new Files() )->glob_php( JETPACK__PLUGIN_DIR . 'modules' );
287
288 $modules = array();
289
290 foreach ( $files as $file ) {
291 $slug = $this->get_slug( $file );
292 $headers = $this->get( $slug );
293
294 if ( ! $headers ) {
295 continue;
296 }
297
298 $modules[ $slug ] = $headers['introduced'];
299 }
300
301 \Jetpack_Options::update_option(
302 'available_modules',
303 array(
304 JETPACK__VERSION => $modules,
305 )
306 );
307 }
308 }
309
310 /**
311 * Filters the array of modules available to be activated.
312 *
313 * @since 2.4.0
314 *
315 * @param array $modules Array of available modules.
316 * @param string $min_version Minimum version number required to use modules.
317 * @param string $max_version Maximum version number required to use modules.
318 * @param bool|null $requires_connection Value of the Requires Connection filter.
319 * @param bool|null $requires_user_connection Value of the Requires User Connection filter.
320 */
321 $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version, $requires_connection, $requires_user_connection );
322
323 if ( ! $min_version && ! $max_version && $requires_connection === null && $requires_user_connection === null ) {
324 return array_keys( $mods );
325 }
326
327 $r = array();
328 foreach ( $mods as $slug => $introduced ) {
329 if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
330 continue;
331 }
332
333 if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
334 continue;
335 }
336
337 $mod_details = $this->get( $slug );
338
339 if ( null !== $requires_connection && (bool) $requires_connection !== $mod_details['requires_connection'] ) {
340 continue;
341 }
342
343 if ( null !== $requires_user_connection && (bool) $requires_user_connection !== $mod_details['requires_user_connection'] ) {
344 continue;
345 }
346
347 $r[] = $slug;
348 }
349
350 return $r;
351 }
352
353 /**
354 * Is slug a valid module.
355 *
356 * @param string $module Module slug.
357 *
358 * @return bool
359 */
360 public function is_module( $module ) {
361 return ! empty( $module ) && ! validate_file( $module, $this->get_available() );
362 }
363
364 /**
365 * Update module status.
366 *
367 * @param string $module - module slug.
368 * @param boolean $active - true to activate, false to deactivate.
369 * @param bool $exit Should exit be called after deactivation.
370 * @param bool $redirect Should there be a redirection after activation.
371 */
372 public function update_status( $module, $active, $exit = true, $redirect = true ) {
373 return $active ? $this->activate( $module, $exit, $redirect ) : $this->deactivate( $module );
374 }
375
376 /**
377 * Activate a module.
378 *
379 * @param string $module Module slug.
380 * @param bool $exit Should exit be called after deactivation.
381 * @param bool $redirect Should there be a redirection after activation.
382 *
383 * @return bool|void
384 */
385 public function activate( $module, $exit = true, $redirect = true ) {
386 /**
387 * Fires before a module is activated.
388 *
389 * @since 2.6.0
390 *
391 * @param string $module Module slug.
392 * @param bool $exit Should we exit after the module has been activated. Default to true.
393 * @param bool $redirect Should the user be redirected after module activation? Default to true.
394 */
395 do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect );
396
397 if ( ! strlen( $module ) ) {
398 return false;
399 }
400
401 // If it's already active, then don't do it again.
402 $active = $this->get_active();
403 foreach ( $active as $act ) {
404 if ( $act === $module ) {
405 return true;
406 }
407 }
408
409 if ( ! $this->is_module( $module ) ) {
410 return false;
411 }
412
413 // Jetpack plugin only
414 if ( class_exists( 'Jetpack' ) ) {
415
416 $module_data = $this->get( $module );
417
418 $status = new Status();
419 $state = new CookieState();
420
421 if ( ! \Jetpack::is_connection_ready() ) {
422 if ( ! $status->is_offline_mode() ) {
423 return false;
424 }
425
426 // If we're not connected but in offline mode, make sure the module doesn't require a connection.
427 if ( $status->is_offline_mode() && $module_data['requires_connection'] ) {
428 return false;
429 }
430 }
431
432 if ( class_exists( 'Jetpack_Client_Server' ) ) {
433 $jetpack = \Jetpack::init();
434
435 // Check and see if the old plugin is active.
436 if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
437 // Deactivate the old plugins.
438 $deactivated = array();
439 foreach ( $jetpack->plugins_to_deactivate[ $module ] as $idx => $deactivate_me ) {
440 if ( \Jetpack_Client_Server::deactivate_plugin( $deactivate_me[0], $deactivate_me[1] ) ) {
441 // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
442 // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
443 $deactivated[] = "$module:$idx";
444 }
445 }
446 if ( $deactivated ) {
447 $state->state( 'deactivated_plugins', implode( ',', $deactivated ) );
448 wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
449 exit( 0 );
450 }
451 }
452 }
453
454 // Protect won't work with mis-configured IPs.
455 if ( 'protect' === $module ) {
456 if ( ! IP_Utils::get_ip() ) {
457 $state->state( 'message', 'protect_misconfigured_ip' );
458 return false;
459 }
460 }
461
462 if ( ! Jetpack_Plan::supports( $module ) ) {
463 return false;
464 }
465
466 // Check the file for fatal errors, a la wp-admin/plugins.php::activate.
467 $state->state( 'module', $module );
468 $state->state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error.
469
470 ob_start();
471 $module_path = $this->get_path( $module );
472 if ( file_exists( $module_path ) ) {
473 require_once $this->get_path( $module );
474 }
475
476 $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
477
478 $state->state( 'error', false ); // the override.
479 ob_end_clean();
480 } else { // Not a Jetpack plugin.
481 $this->update_active( array_merge( $this->get_saved_active(), array( $module ) ) );
482 }
483
484 if ( $redirect ) {
485 wp_safe_redirect( ( new Paths() )->admin_url( 'page=jetpack' ) );
486 }
487 if ( $exit ) {
488 exit( 0 );
489 }
490 return true;
491 }
492
493 /**
494 * Deactivate module.
495 *
496 * A module a `jetpack_active_modules` callback forces on keeps running; callers that report
497 * back to a person should check is_active() afterwards.
498 *
499 * @param string $module Module slug.
500 *
501 * @return bool Whether the saved list changed.
502 */
503 public function deactivate( $module ) {
504 /**
505 * Fires when a module is deactivated.
506 *
507 * @since 1.9.0
508 *
509 * @param string $module Module slug.
510 */
511 do_action( 'jetpack_pre_deactivate_module', $module );
512
513 return $this->update_active( array_filter( array_diff( $this->get_saved_active(), (array) $module ) ) );
514 }
515
516 /**
517 * The active modules as saved, before filters add or drop any.
518 *
519 * Switching one module builds on this rather than get_active(), which would save whatever a
520 * `jetpack_active_modules` callback forces on.
521 *
522 * @return string[] Module slugs.
523 */
524 private function get_saved_active() {
525 $saved = \Jetpack_Options::get_option( 'active_modules', array() );
526
527 return is_array( $saved ) ? array_values( $saved ) : array();
528 }
529
530 /**
531 * Generate a module's path from its slug.
532 *
533 * @param string $slug Module slug.
534 */
535 public function get_path( $slug ) {
536 if ( ! Constants::is_defined( 'JETPACK__PLUGIN_DIR' ) ) {
537 return '';
538 }
539 /**
540 * Filters the path of a modules.
541 *
542 * @since 7.4.0
543 *
544 * @param array $return The absolute path to a module's root php file
545 * @param string $slug The module slug
546 */
547 return apply_filters( 'jetpack_get_module_path', JETPACK__PLUGIN_DIR . "modules/$slug.php", $slug );
548 }
549
550 /**
551 * Saves all the currently active modules to options.
552 * Also fires Action hooks for each newly activated and deactivated module.
553 *
554 * @param array $modules Array of active modules to be saved in options.
555 *
556 * @return bool $success true for success, false for failure.
557 */
558 public function update_active( $modules ) {
559 $current_modules = \Jetpack_Options::get_option( 'active_modules', array() );
560 $active_modules = $this->get_active();
561 $new_active_modules = array_diff( $modules, $current_modules );
562 $new_inactive_modules = array_diff( $active_modules, $modules );
563 $new_current_modules = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules );
564 $reindexed_modules = array_values( $new_current_modules );
565 $success = \Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) );
566 // Let's take `pre_update_option_jetpack_active_modules` filter into account
567 // and actually decide for which modules we need to fire hooks by comparing
568 // the 'active_modules' option before and after the update.
569 $current_modules_post_update = \Jetpack_Options::get_option( 'active_modules', array() );
570
571 $new_inactive_modules = array_diff( $current_modules, $current_modules_post_update );
572 $new_inactive_modules = array_unique( $new_inactive_modules );
573 $new_inactive_modules = array_values( $new_inactive_modules );
574
575 $new_active_modules = array_diff( $current_modules_post_update, $current_modules );
576 $new_active_modules = array_unique( $new_active_modules );
577 $new_active_modules = array_values( $new_active_modules );
578
579 foreach ( $new_active_modules as $module ) {
580 /**
581 * Fires when a specific module is activated.
582 *
583 * @since 1.9.0
584 *
585 * @param string $module Module slug.
586 * @param boolean $success whether the module was activated. @since 4.2
587 */
588 do_action( 'jetpack_activate_module', $module, $success );
589 /**
590 * Fires when a module is activated.
591 * The dynamic part of the filter, $module, is the module slug.
592 *
593 * @since 1.9.0
594 *
595 * @param string $module Module slug.
596 */
597 do_action( "jetpack_activate_module_$module", $module );
598 }
599
600 foreach ( $new_inactive_modules as $module ) {
601 /**
602 * Fired after a module has been deactivated.
603 *
604 * @since 4.2.0
605 *
606 * @param string $module Module slug.
607 * @param boolean $success whether the module was deactivated.
608 */
609 do_action( 'jetpack_deactivate_module', $module, $success );
610 /**
611 * Fires when a module is deactivated.
612 * The dynamic part of the filter, $module, is the module slug.
613 *
614 * @since 1.9.0
615 *
616 * @param string $module Module slug.
617 */
618 do_action( "jetpack_deactivate_module_$module", $module );
619 }
620
621 return $success;
622 }
623 }
624