Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.31% |
35 / 52 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Hybrid_Product | |
68.00% |
34 / 50 |
|
66.67% |
6 / 9 |
78.47 | |
0.00% |
0 / 1 |
| is_plugin_installed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_plugin_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_standalone_plugin_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_active | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| is_activated | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| activate_plugin | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| do_product_specific_activation | |
38.89% |
7 / 18 |
|
0.00% |
0 / 1 |
44.86 | |||
| deactivate | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| install_and_activate_standalone | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
7.19 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Base product |
| 4 | * |
| 5 | * @package my-jetpack |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\My_Jetpack; |
| 9 | |
| 10 | use Automattic\Jetpack\Modules; |
| 11 | use Automattic\Jetpack\Plugins_Installer; |
| 12 | use WP_Error; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class responsible for handling the hybrid products |
| 20 | * |
| 21 | * Hybrid products are those that may work both as a stand-alone plugin or with the Jetpack plugin. |
| 22 | */ |
| 23 | abstract class Hybrid_Product extends Product { |
| 24 | /** |
| 25 | * All hybrid products have a standalone plugin |
| 26 | * |
| 27 | * @var bool |
| 28 | */ |
| 29 | public static $has_standalone_plugin = true; |
| 30 | |
| 31 | /** |
| 32 | * For Hybrid products, we can use either the standalone or Jetpack plugin |
| 33 | * |
| 34 | * @return bool |
| 35 | */ |
| 36 | public static function is_plugin_installed() { |
| 37 | return parent::is_plugin_installed() || parent::is_jetpack_plugin_installed(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Checks whether the Product is active |
| 42 | * |
| 43 | * @return boolean |
| 44 | */ |
| 45 | public static function is_plugin_active() { |
| 46 | return parent::is_plugin_active() || parent::is_jetpack_plugin_active(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Checks whether the standalone plugin for this product is active |
| 51 | * |
| 52 | * @return boolean |
| 53 | */ |
| 54 | public static function is_standalone_plugin_active() { |
| 55 | return parent::is_plugin_active(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Checks whether the Product is active |
| 60 | * |
| 61 | * @return boolean |
| 62 | */ |
| 63 | public static function is_active() { |
| 64 | return parent::is_active() && static::is_module_active(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Checks whether the site has switched the product on, whether or not it has a plan for it. |
| 69 | * |
| 70 | * The standalone plugin being active is enough: off the Jetpack plugin, the module may not be available at all. |
| 71 | * |
| 72 | * @return boolean |
| 73 | */ |
| 74 | public static function is_activated() { |
| 75 | return static::is_standalone_plugin_active() || ( static::is_jetpack_plugin_active() && static::is_module_active() ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Activates the plugin |
| 80 | * |
| 81 | * @return null|WP_Error Null on success, WP_Error on invalid file. |
| 82 | */ |
| 83 | public static function activate_plugin() { |
| 84 | /* |
| 85 | * Activate self-installed plugin if it's installed. |
| 86 | */ |
| 87 | if ( parent::is_plugin_installed() && static::should_use_standalone_plugin() ) { |
| 88 | return activate_plugin( static::get_installed_plugin_filename() ); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Otherwise, activate Jetpack plugin. |
| 93 | */ |
| 94 | if ( static::is_jetpack_plugin_installed() ) { |
| 95 | return activate_plugin( static::get_installed_plugin_filename( 'jetpack' ) ); |
| 96 | } |
| 97 | |
| 98 | return new WP_Error( 'plugin_not_found', __( 'Activation failed. Plugin is not installed', 'jetpack-my-jetpack' ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Activates the product. If the Hybrid product has declared a jetpack module name, let's try to activate it if Jetpack plugin is active |
| 103 | * |
| 104 | * @param bool|WP_Error $product_activation Is the result of the top level activation actions. You probably won't do anything if it is an WP_Error. |
| 105 | * @param bool $local Skip plan reads and activate only modules that work locally. |
| 106 | * @return bool|WP_Error |
| 107 | */ |
| 108 | public static function do_product_specific_activation( $product_activation, $local = false ) { |
| 109 | |
| 110 | if ( is_wp_error( $product_activation ) ) { |
| 111 | // If we failed to install the stand-alone plugin because the package was not found, let's try and install Jetpack plugin instead. |
| 112 | // This might happen, for example, while the stand-alone plugin was not released to the WP.org repository yet. |
| 113 | if ( 'no_package' === $product_activation->get_error_code() ) { |
| 114 | $product_activation = Plugins_Installer::install_plugin( self::JETPACK_PLUGIN_SLUG ); |
| 115 | if ( ! is_wp_error( $product_activation ) ) { |
| 116 | $product_activation = static::activate_plugin(); |
| 117 | } |
| 118 | } |
| 119 | if ( is_wp_error( $product_activation ) ) { |
| 120 | return $product_activation; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $module_name = static::get_module_name(); |
| 125 | if ( ! empty( $module_name ) ) { |
| 126 | if ( $local && ! Main_Features::module_works_locally( $module_name ) ) { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // Only activate the module if the plan supports it |
| 131 | // We don't want to throw an error for a missing plan here since we try activation before purchase |
| 132 | if ( ! $local && static::$requires_plan && ! static::has_any_plan_for_product() ) { |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | $module_activation = ( new Modules() )->activate( $module_name, false, false ); |
| 137 | |
| 138 | if ( ! $module_activation ) { |
| 139 | return new WP_Error( 'module_activation_failed', __( 'Error activating Jetpack module', 'jetpack-my-jetpack' ) ); |
| 140 | } |
| 141 | |
| 142 | return $module_activation; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Deactivates the product. |
| 150 | * |
| 151 | * In addition to the parent's plugin deactivation, also deactivates the |
| 152 | * matching Jetpack module when `static::get_module_name()` returns one and the |
| 153 | * module is saved and active — otherwise Hybrid products activated via |
| 154 | * the Jetpack-module path stay half-on after "deactivation". |
| 155 | * |
| 156 | * @return bool|WP_Error True on success (matches Product::deactivate()), |
| 157 | * WP_Error if the module deactivation failed. |
| 158 | */ |
| 159 | public static function deactivate() { |
| 160 | // Before the parent call: deactivating the standalone plugin can change which module the product reads. |
| 161 | $module_name = static::get_module_name(); |
| 162 | $result = parent::deactivate(); |
| 163 | $modules = new Modules(); |
| 164 | $saved_active_modules = (array) \Jetpack_Options::get_option( 'active_modules', array() ); |
| 165 | |
| 166 | // Modules::deactivate() can't remove a module only a plugin's filter forces on, nor a saved one that is unavailable. |
| 167 | if ( |
| 168 | ! empty( $module_name ) |
| 169 | && in_array( $module_name, $saved_active_modules, true ) |
| 170 | && $modules->is_active( $module_name ) |
| 171 | ) { |
| 172 | if ( ! $modules->deactivate( $module_name ) ) { |
| 173 | return new WP_Error( |
| 174 | 'module_deactivation_failed', |
| 175 | __( 'Error deactivating Jetpack module', 'jetpack-my-jetpack' ) |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return $result; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Install and activate the standalone plugin in the case it's missing. |
| 185 | * |
| 186 | * @return boolean|WP_Error |
| 187 | */ |
| 188 | public static function install_and_activate_standalone() { |
| 189 | $result = parent::install_and_activate_standalone(); |
| 190 | |
| 191 | if ( is_wp_error( $result ) ) { |
| 192 | return $result; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Activate the module as well, if the user has a plan |
| 197 | * or the product does not require a plan to work |
| 198 | */ |
| 199 | $module_name = static::get_module_name(); |
| 200 | if ( static::has_any_plan_for_product() && isset( $module_name ) ) { |
| 201 | $module_activation = ( new Modules() )->activate( $module_name, false, false ); |
| 202 | |
| 203 | if ( ! $module_activation ) { |
| 204 | return new WP_Error( 'module_activation_failed', __( 'Error activating Jetpack module', 'jetpack-my-jetpack' ) ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | } |