summaryrefslogtreecommitdiff
blob: e2cf58ef4805c1d20437ccbceabda924eef34a67 (plain)
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
<?php
/**
 * Base Module product
 *
 * @package my-jetpack
 */

namespace Automattic\Jetpack\My_Jetpack;

use Jetpack;
use WP_Error;

/**
 * Class responsible for handling the Module products
 *
 * Module products are those that are a Jetpack module behind the scenes.
 *
 * They require Jetpack plugin and will then activate/deactivate a module.
 */
abstract class Module_Product extends Product {

	/**
	 * The Jetpack module name associated with this product
	 *
	 * @var string|null
	 */
	public static $module_name = null;

	/**
	 * Get the plugin slug - ovewrite it ans return Jetpack's
	 *
	 * @return ?string
	 */
	public static function get_plugin_slug() {
		return self::JETPACK_PLUGIN_SLUG;
	}

	/**
	 * Get the plugin filename - ovewrite it ans return Jetpack's
	 *
	 * @return ?string
	 */
	public static function get_plugin_filename() {
		return self::JETPACK_PLUGIN_FILENAME;
	}

	/**
	 * Ensure that child classes define $module_name attribute
	 *
	 * @throws \Exception If required attribute is not declared in the child class.
	 * @return void
	 */
	private static function check_for_module_name() {
		if ( empty( static::$module_name ) ) {
			throw new \Exception( 'Module Product classes must declare the $module_name attribute.' );
		}
	}

	/**
	 * Checks whether the Product is active
	 *
	 * @return boolean
	 */
	public static function is_active() {
		return static::is_jetpack_plugin_active() && static::is_module_active();
	}

	/**
	 * Checks whether the Jetpack module is active
	 *
	 * @return bool
	 */
	public static function is_module_active() {
		self::check_for_module_name();
		if ( ! class_exists( 'Jetpack' ) ) {
			return false;
		}

		return Jetpack::is_module_active( static::$module_name );
	}

	/**
	 * Gets the current status of the product
	 *
	 * @return string
	 */
	public static function get_status() {
		$status = parent::get_status();
		if ( 'active' === $status && ! static::is_module_active() ) {
			$status = 'module_disabled';
		}
		return $status;
	}

	/**
	 * Activates the product by installing and activating its plugin
	 *
	 * @param bool|WP_Error $plugin_activation Is the result of the top level activation actions. You probably won't do anything if it is an WP_Error.
	 * @return boolean|\WP_Error
	 */
	public static function do_product_specific_activation( $plugin_activation ) {
		self::check_for_module_name();

		if ( is_wp_error( $plugin_activation ) ) {
			return $plugin_activation;
		}

		if ( ! class_exists( 'Jetpack' ) ) {
			return new WP_Error( 'plugin_activation_failed', __( 'Error activating Jetpack plugin', 'jetpack-my-jetpack' ) );
		}

		$module_activation = Jetpack::activate_module( static::$module_name, false, false );

		if ( ! $module_activation ) {
			return new WP_Error( 'module_activation_failed', __( 'Error activating Jetpack module', 'jetpack-my-jetpack' ) );
		}

		return $module_activation;

	}

	/**
	 * Deactivate the module
	 *
	 * @return boolean
	 */
	public static function deactivate() {
		self::check_for_module_name();
		if ( ! class_exists( 'Jetpack' ) ) {
			return true;
		}
		return Jetpack::deactivate_module( static::$module_name );
	}

}