PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 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 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-my-jetpack / src / class-historically-active-modules.php

class-historically-active-modules.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-my-jetpack/src/class-historically-active-modules.php

113 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sets up the Historically Active Modules rest api endpoint and helper functions
4 *
5 * @package automattic/my-jetpack
6 */
7
8 namespace Automattic\Jetpack\My_Jetpack;
9
10 use WP_Error;
11
12 /**
13 * Registers REST route for updating historically active modules
14 * and includes all helper functions for triggering an update elsewhere
15 */
16 class Historically_Active_Modules {
17 public const UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY = 'jetpack_update_historically_active_modules';
18
19 /**
20 * Register the REST API routes.
21 *
22 * @return void
23 */
24 public static function register_rest_endpoints() {
25 register_rest_route(
26 'my-jetpack/v1',
27 'site/update-historically-active-modules',
28 array(
29 'methods' => \WP_REST_Server::EDITABLE,
30 'callback' => __CLASS__ . '::rest_trigger_historically_active_modules_update',
31 'permission_callback' => __CLASS__ . '::permissions_callback',
32 )
33 );
34 }
35
36 /**
37 * Check user capabilities to access historically active modules.
38 *
39 * @access public
40 * @static
41 *
42 * @return true|WP_Error
43 */
44 public static function permissions_callback() {
45 return current_user_can( 'edit_posts' );
46 }
47
48 /**
49 * Update historically active Jetpack plugins
50 * Historically active is defined as the Jetpack plugins that are installed and active with the required connections
51 * This array will consist of any plugins that were active at one point in time and are still enabled on the site
52 *
53 * @return void
54 */
55 public static function update_historically_active_jetpack_modules() {
56 $historically_active_modules = \Jetpack_Options::get_option( 'historically_active_modules', array() );
57 $products = Products::get_products();
58 $product_classes = Products::get_products_classes();
59
60 foreach ( $products as $product ) {
61 $product_slug = $product['slug'];
62 $status = $product_classes[ $product_slug ]::get_status();
63 // We want to leave modules in the array if they've been active in the past
64 // and were not manually disabled by the user.
65 if ( in_array( $status, Products::$broken_module_statuses, true ) ) {
66 continue;
67 }
68
69 // If the module is active and not already in the array, add it
70 if (
71 in_array( $status, Products::$active_module_statuses, true ) &&
72 ! in_array( $product_slug, $historically_active_modules, true )
73 ) {
74 $historically_active_modules[] = $product_slug;
75 }
76
77 // If the module has been disabled due to a manual user action,
78 // or because of a missing plan error, remove it from the array
79 if ( in_array( $status, Products::$disabled_module_statuses, true ) ) {
80 $historically_active_modules = array_values( array_diff( $historically_active_modules, array( $product_slug ) ) );
81 }
82 }
83
84 \Jetpack_Options::update_option( 'historically_active_modules', array_unique( $historically_active_modules ) );
85 }
86
87 /**
88 * REST API endpoint to trigger an update to the historically active Jetpack modules
89 *
90 * @return WP_Error|\WP_REST_Response
91 */
92 public static function rest_trigger_historically_active_modules_update() {
93 self::update_historically_active_jetpack_modules();
94 $historically_active_modules = \Jetpack_Options::get_option( 'historically_active_modules', array() );
95 return rest_ensure_response( $historically_active_modules );
96 }
97
98 /**
99 * Set transient to queue an update to the historically active Jetpack modules on the next wp-admin load
100 *
101 * @param string $plugin The plugin that triggered the update. This will be present if the function was queued by a plugin activation.
102 *
103 * @return void
104 */
105 public static function queue_historically_active_jetpack_modules_update( $plugin = null ) {
106 $plugin_filenames = Products::get_all_plugin_filenames();
107
108 if ( ! $plugin || in_array( $plugin, $plugin_filenames, true ) ) {
109 set_transient( self::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY, true );
110 }
111 }
112 }
113