Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
60 / 72 |
|
66.67% |
8 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Menu_Visibility | |
83.33% |
60 / 72 |
|
66.67% |
8 / 12 |
63.04 | |
0.00% |
0 / 1 |
| init | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| forget_resolved_gates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| activate | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
11.48 | |||
| can_activate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
42 | |||
| get_targets | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
5 | |||
| is_module_on_offer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_module_activated | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| is_product_activated | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| can_offer | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
7 | |||
| is_module_held_off | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| is_module_removed | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
5.20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Resolves whether a Jetpack admin menu item's feature is active. |
| 4 | * |
| 5 | * @package automattic/my-jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\My_Jetpack; |
| 9 | |
| 10 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 11 | use Automattic\Jetpack\Modules; |
| 12 | use WP_Error; |
| 13 | |
| 14 | /** |
| 15 | * The activation half of the sidebar visibility contract. |
| 16 | * |
| 17 | * Admin_Menu owns when the question is asked and what a host may do with the answer; this |
| 18 | * class owns the answer itself, because the product classes it reads live here. A menu item |
| 19 | * declares its gate at registration and this resolves it from the same product class its |
| 20 | * My Jetpack card reads. |
| 21 | */ |
| 22 | class Menu_Visibility { |
| 23 | |
| 24 | /** |
| 25 | * Gates already resolved this registration pass, keyed by type and slug. |
| 26 | * |
| 27 | * @var array<string, bool|null> |
| 28 | */ |
| 29 | private static $resolved = array(); |
| 30 | |
| 31 | /** |
| 32 | * Registers this class as Admin_Menu's visibility resolver. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public static function init() { |
| 37 | // An older admin-ui, loaded first by another plugin, may predate the resolver; items then stay visible. |
| 38 | if ( ! method_exists( Admin_Menu::class, 'set_visibility_resolver' ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | Admin_Menu::set_visibility_resolver( array( __CLASS__, 'resolve' ) ); |
| 43 | |
| 44 | // Memoized answers last one registration pass, which is as long as they can stay true. |
| 45 | add_action( 'admin_menu', array( __CLASS__, 'forget_resolved_gates' ), 0 ); |
| 46 | add_action( 'network_admin_menu', array( __CLASS__, 'forget_resolved_gates' ), 0 ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Drops the gates resolved during the previous registration pass. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public static function forget_resolved_gates() { |
| 55 | self::$resolved = array(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Answers whether a menu item's declared gate is satisfied. |
| 60 | * |
| 61 | * Answers are memoized for one registration pass: resolving a product rebuilds the product |
| 62 | * class map and rescans the installed plugins, and the sidebar asks once per gated item. |
| 63 | * |
| 64 | * @param array $args The item's visibility declaration, as passed to Admin_Menu::add_menu(). |
| 65 | * @return bool|null True or false, or null when the gate cannot be resolved here. |
| 66 | */ |
| 67 | public static function resolve( $args ) { |
| 68 | if ( ! empty( $args['product'] ) ) { |
| 69 | $key = 'product:' . $args['product']; |
| 70 | |
| 71 | if ( ! array_key_exists( $key, self::$resolved ) ) { |
| 72 | self::$resolved[ $key ] = self::is_product_activated( $args['product'] ); |
| 73 | } |
| 74 | |
| 75 | return self::$resolved[ $key ]; |
| 76 | } |
| 77 | |
| 78 | if ( ! empty( $args['module'] ) ) { |
| 79 | $key = 'module:' . $args['module']; |
| 80 | |
| 81 | if ( ! array_key_exists( $key, self::$resolved ) ) { |
| 82 | self::$resolved[ $key ] = self::is_module_activated( $args['module'] ); |
| 83 | } |
| 84 | |
| 85 | return self::$resolved[ $key ]; |
| 86 | } |
| 87 | |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Switch a gated feature on the way the Features tab does: its module, when Jetpack runs one, before its plugin. |
| 93 | * |
| 94 | * Never installs a plugin; a feature that needs one is set up from My Jetpack. |
| 95 | * |
| 96 | * @param array $args The item's visibility declaration. |
| 97 | * @return true|WP_Error |
| 98 | */ |
| 99 | public static function activate( $args ) { |
| 100 | list( $product_class, $module ) = self::get_targets( $args ); |
| 101 | |
| 102 | // Else the product fallback below would install a plugin for a module the site filtered away or holds off. |
| 103 | if ( ! self::can_offer( $args ) ) { |
| 104 | return new WP_Error( 'not_available', __( 'This feature is not available on this site.', 'jetpack-my-jetpack' ) ); |
| 105 | } |
| 106 | |
| 107 | if ( self::is_module_on_offer( $module ) ) { |
| 108 | if ( isset( Products\Protect::get_protect_page_module_urls()[ $module ] ) ) { |
| 109 | return REST_Main_Features::managed_in_protect_error(); |
| 110 | } |
| 111 | $result = REST_Main_Features::switch_module( $module, true ); |
| 112 | // A product whose standalone plugin is off stays off with its module on: activate the plugin below. |
| 113 | if ( is_wp_error( $result ) || ! $product_class || $product_class::is_activated() ) { |
| 114 | self::forget_resolved_gates(); |
| 115 | return $result; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if ( ! $product_class ) { |
| 120 | return new WP_Error( 'not_found', __( 'That feature was not found.', 'jetpack-my-jetpack' ) ); |
| 121 | } |
| 122 | // Not is_plugin_installed(): a hybrid product counts the Jetpack plugin, and activation would install its own. |
| 123 | if ( ! $product_class::get_installed_plugin_filename() ) { |
| 124 | return new WP_Error( 'not_available', __( 'This feature needs a plugin installed from My Jetpack.', 'jetpack-my-jetpack' ) ); |
| 125 | } |
| 126 | if ( ! REST_Products::edit_permissions_callback() ) { |
| 127 | return new WP_Error( 'not_allowed', __( 'Your account can’t activate plugins on this site.', 'jetpack-my-jetpack' ) ); |
| 128 | } |
| 129 | |
| 130 | $result = $product_class::activate(); |
| 131 | self::forget_resolved_gates(); |
| 132 | |
| 133 | return is_wp_error( $result ) ? $result : true; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Whether the current user has the capabilities activate() needs for this gate. |
| 138 | * |
| 139 | * @param array $args The item's visibility declaration. |
| 140 | * @return bool |
| 141 | */ |
| 142 | public static function can_activate( $args ) { |
| 143 | list( $product_class, $module ) = self::get_targets( $args ); |
| 144 | $module_on_offer = self::is_module_on_offer( $module ); |
| 145 | |
| 146 | if ( $module_on_offer && ! current_user_can( 'jetpack_manage_modules' ) ) { |
| 147 | return false; |
| 148 | } |
| 149 | // activate() goes on to the plugin when Jetpack runs no module for it, or the plugin is off. |
| 150 | if ( $product_class && ( ! $module_on_offer || ! $product_class::is_plugin_active() ) ) { |
| 151 | return REST_Products::edit_permissions_callback(); |
| 152 | } |
| 153 | |
| 154 | return $module_on_offer; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * The product class and module a gate switches. |
| 159 | * |
| 160 | * @param array $args The item's visibility declaration. |
| 161 | * @return array{0: class-string<Product>|null, 1: string|null} |
| 162 | */ |
| 163 | private static function get_targets( $args ) { |
| 164 | $product_class = empty( $args['product'] ) ? null : Products::get_product_class( $args['product'] ); |
| 165 | $module = empty( $args['module'] ) ? ( $product_class ? $product_class::get_module_name() : null ) : $args['module']; |
| 166 | |
| 167 | return array( $product_class, $module ? $module : null ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Whether this site runs the module, so switching it on needs no plugin. |
| 172 | * |
| 173 | * @param string|null $module_name A Jetpack module name. |
| 174 | * @return bool |
| 175 | */ |
| 176 | private static function is_module_on_offer( $module_name ) { |
| 177 | return $module_name && in_array( $module_name, ( new Modules() )->get_available(), true ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Whether a Jetpack module gating an item is switched on. |
| 182 | * |
| 183 | * A name this site has no module for is unanswerable rather than off, so a typo in a gate |
| 184 | * fails open like an unknown product slug does instead of silently removing the item. Off |
| 185 | * the Jetpack plugin that covers any module a standalone plugin did not declare through |
| 186 | * `jetpack_get_available_standalone_modules`. |
| 187 | * |
| 188 | * @param string $module_name A Jetpack module name. |
| 189 | * @return bool|null Null when this site has no such module. |
| 190 | */ |
| 191 | private static function is_module_activated( $module_name ) { |
| 192 | $modules = new Modules(); |
| 193 | |
| 194 | if ( ! in_array( $module_name, $modules->get_available(), true ) ) { |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | return $modules->is_active( $module_name ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Whether the site has switched a My Jetpack product on. |
| 203 | * |
| 204 | * Reads is_activated(), not is_active(): a lapsed plan is not "off", and resolving plans |
| 205 | * costs WordPress.com requests on every admin page load. No connection check is added. |
| 206 | * |
| 207 | * @param string $product_slug A My Jetpack product slug. |
| 208 | * @return bool|null Null when no product class is registered under that slug. |
| 209 | */ |
| 210 | private static function is_product_activated( $product_slug ) { |
| 211 | $product_class = Products::get_product_class( $product_slug ); |
| 212 | |
| 213 | if ( ! $product_class ) { |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | return (bool) $product_class::is_activated(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Whether activate() can switch a gate's feature on: it needs no install, and nothing removed its module or holds it off. |
| 222 | * |
| 223 | * Separate from resolve(), which also decides the sidebar: a feature held off is still off there. |
| 224 | * |
| 225 | * @param array $args The item's visibility declaration. |
| 226 | * @return bool |
| 227 | */ |
| 228 | public static function can_offer( $args ) { |
| 229 | list( $product_class, $module ) = self::get_targets( $args ); |
| 230 | |
| 231 | // A site without the plugin never had the feature on, say Stats on a Boost-only site. |
| 232 | if ( ! self::is_module_on_offer( $module ) && ! ( $product_class && $product_class::get_installed_plugin_filename() ) ) { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | return ! self::is_module_removed( $module ) |
| 237 | && ! self::is_module_held_off( $module ) |
| 238 | && ! ( $product_class && $product_class::is_held_off() ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Whether a `jetpack_active_modules` callback, such as a host's, would keep this module off once it is saved on. |
| 243 | * |
| 244 | * @param string|null $module_name A Jetpack module name. |
| 245 | * @return bool |
| 246 | */ |
| 247 | private static function is_module_held_off( $module_name ) { |
| 248 | if ( ! self::is_module_on_offer( $module_name ) ) { |
| 249 | return false; |
| 250 | } |
| 251 | $active = ( new Modules() )->get_active(); |
| 252 | if ( in_array( $module_name, $active, true ) ) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | /** This filter is documented in projects/packages/status/src/class-modules.php */ |
| 257 | return ! in_array( $module_name, (array) apply_filters( 'jetpack_active_modules', array_merge( $active, array( $module_name ) ) ), true ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Whether the Jetpack plugin ships this module but no longer offers it, as when a |
| 262 | * `jetpack_get_available_modules` filter removes it. |
| 263 | * |
| 264 | * Bundles and Scan name modules Jetpack has no file for, which are not removed. |
| 265 | * |
| 266 | * @param string|null $module_name A Jetpack module name. |
| 267 | * @return bool |
| 268 | */ |
| 269 | private static function is_module_removed( $module_name ) { |
| 270 | if ( ! $module_name || ! class_exists( 'Jetpack' ) ) { |
| 271 | return false; |
| 272 | } |
| 273 | $modules = new Modules(); |
| 274 | $path = $modules->get_path( $module_name ); |
| 275 | |
| 276 | return $path && file_exists( $path ) && ! in_array( $module_name, $modules->get_available(), true ); |
| 277 | } |
| 278 | } |