Completed
Push — move/jitm-to-plugins-loaded ( a384d2 )
by
unknown
53:38 queued 47:06
created

Config::enable_jitm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The base Jetpack configuration class file.
4
 *
5
 * @package automattic/jetpack-config
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Connection\Manager;
11
use Automattic\Jetpack\JITM;
12
use Automattic\Jetpack\Plugin\Tracking as Plugin_Tracking;
13
use Automattic\Jetpack\Sync\Main as Sync_Main;
14
use Automattic\Jetpack\Terms_Of_Service;
15
16
/**
17
 * The configuration class.
18
 */
19
class Config {
20
21
	const FEATURE_ENSURED         = 1;
22
	const FEATURE_NOT_AVAILABLE   = 0;
23
	const FEATURE_ALREADY_ENSURED = -1;
24
25
	/**
26
	 * The initial setting values.
27
	 *
28
	 * @var Array
29
	 */
30
	protected $config = array(
31
		'jitm'       => false,
32
		'connection' => false,
33
		'sync'       => false,
34
		'tracking'   => false,
35
		'tos'        => false,
36
	);
37
38
	/**
39
	 * Creates the configuration class instance.
40
	 */
41
	public function __construct() {
42
43
		/**
44
		 * Adding the config handler to run on priority 2 because the class itself is
45
		 * being constructed on priority 1.
46
		 */
47
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
48
	}
49
50
	/**
51
	 * Require a feature to be initialized. It's up to the package consumer to actually add
52
	 * the package to their composer project. Declaring a requirement using this method
53
	 * instructs the class to initalize it.
54
	 *
55
	 * @param String $feature the feature slug.
56
	 */
57
	public function ensure( $feature ) {
58
		$this->config[ $feature ] = true;
59
	}
60
61
	/**
62
	 * Runs on plugins_loaded hook priority with priority 2.
63
	 *
64
	 * @action plugins_loaded
65
	 */
66
	public function on_plugins_loaded() {
67
		if ( $this->config['connection'] ) {
68
			$this->ensure_class( 'Automattic\Jetpack\Connection\Manager' )
69
				&& $this->ensure_feature( 'connection' );
70
		}
71
72
		if ( $this->config['tracking'] ) {
73
74
			$this->ensure_class( 'Automattic\Jetpack\Terms_Of_Service' )
75
				&& $this->ensure_class( 'Automattic\Jetpack\Tracking' )
76
				&& $this->ensure_feature( 'tracking' );
77
		}
78
79
		if ( $this->config['sync'] ) {
80
			$this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
81
				&& $this->ensure_feature( 'sync' );
82
		}
83
84
		if ( is_admin() && $this->config['jitm'] ) {
85
			$this->ensure_class( 'Automattic\Jetpack\JITM' )
86
				&& $this->ensure_feature( 'sync' );
87
		}
88
	}
89
90
	/**
91
	 * Returns true if the required class is available and alerts the user if it's not available
92
	 * in case the site is in debug mode.
93
	 *
94
	 * @param String $classname a fully qualified class name.
95
	 * @return Boolean whether the class is available.
96
	 */
97
	protected function ensure_class( $classname ) {
98
		$available = class_exists( $classname );
99
100
		if ( ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
101
			trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
102
				sprintf(
103
					/* translators: %1$s is a PHP class name. */
104
					esc_html__(
105
						'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
106
						'jetpack'
107
					),
108
					esc_html( $classname )
109
				),
110
				E_USER_NOTICE
111
			);
112
		}
113
114
		return $available;
115
	}
116
117
	/**
118
	 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
119
	 *
120
	 * @param String $feature slug of the feature.
121
	 * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
122
	 */
123
	protected function ensure_feature( $feature ) {
124
		$method = 'enable_' . $feature;
125
		if ( ! method_exists( $this, $method ) ) {
126
			return self::FEATURE_NOT_AVAILABLE;
127
		}
128
129
		if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
130
			return self::FEATURE_ALREADY_ENSURED;
131
		}
132
133
		$this->{ $method }();
134
135
		/**
136
		 * Fires when a specific Jetpack package feature is initalized using the Config package.
137
		 *
138
		 * @since 8.2.0
139
		 */
140
		do_action( 'jetpack_feature_' . $feature . '_enabled' );
141
142
		return self::FEATURE_ENSURED;
143
	}
144
145
	/**
146
	 * Dummy method to enable Terms of Service.
147
	 */
148
	protected function enable_tos() {
149
		return true;
150
	}
151
152
	/**
153
	 * Enables the tracking feature. Depends on the Terms of Service package, so enables it too.
154
	 */
155
	protected function enable_tracking() {
156
157
		// Enabling dependencies.
158
		$this->ensure_feature( 'tos' );
159
160
		$terms_of_service = new Terms_Of_Service();
161
		$tracking         = new Plugin_Tracking();
162
		if ( $terms_of_service->has_agreed() ) {
163
			add_action( 'init', array( $tracking, 'init' ) );
164
		} else {
165
			/**
166
			 * Initialize tracking right after the user agrees to the terms of service.
167
			 */
168
			add_action( 'jetpack_agreed_to_terms_of_service', array( $tracking, 'init' ) );
169
		}
170
171
		return true;
172
	}
173
174
	/**
175
	 * Enables the JITM feature.
176
	 */
177
	protected function enable_jitm() {
178
		JITM::configure();
179
180
		return true;
181
	}
182
183
	/**
184
	 * Enables the Sync feature.
185
	 */
186
	protected function enable_sync() {
187
		Sync_Main::configure();
188
189
		return true;
190
	}
191
192
	/**
193
	 * Enables the Connection feature.
194
	 */
195
	protected function enable_connection() {
196
		Manager::configure();
197
198
		return true;
199
	}
200
201
}
202