summaryrefslogtreecommitdiff
blob: 05025245bf522b3b1622b0b7d6ae2be071bfd735 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
 * The base Jetpack configuration class file.
 *
 * @package automattic/jetpack-config
 */

namespace Automattic\Jetpack;

/*
 * The Config package does not require the composer packages that
 * contain the package classes shown below. The consumer plugin
 * must require the corresponding packages to use these features.
 */
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Connection\Plugin;
use Automattic\Jetpack\JITM as JITM;
use Automattic\Jetpack\JITMS\JITM as JITMS_JITM;
use Automattic\Jetpack\Post_List\Post_List as Post_List;
use Automattic\Jetpack\Publicize\Publicize_Setup as Publicize_Setup;
use Automattic\Jetpack\Search\Initializer as Jetpack_Search_Main;
use Automattic\Jetpack\Sync\Main as Sync_Main;
use Automattic\Jetpack\Waf\Waf_Initializer as Jetpack_Waf_Main;
use Automattic\Jetpack\WordAds\Initializer as Jetpack_WordAds_Main;

/**
 * The configuration class.
 */
class Config {

	const FEATURE_ENSURED         = 1;
	const FEATURE_NOT_AVAILABLE   = 0;
	const FEATURE_ALREADY_ENSURED = -1;

	/**
	 * The initial setting values.
	 *
	 * @var Array
	 */
	protected $config = array(
		'jitm'            => false,
		'connection'      => false,
		'sync'            => false,
		'post_list'       => false,
		'identity_crisis' => false,
		'search'          => false,
		'publicize'       => false,
		'wordads'         => false,
		'waf'             => false,
	);

	/**
	 * Initialization options stored here.
	 *
	 * @var array
	 */
	protected $feature_options = array();

	/**
	 * Creates the configuration class instance.
	 */
	public function __construct() {
		/**
		 * Adding the config handler to run on priority 2 because the class itself is
		 * being constructed on priority 1.
		 */
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
	}

	/**
	 * Require a feature to be initialized. It's up to the package consumer to actually add
	 * the package to their composer project. Declaring a requirement using this method
	 * instructs the class to initialize it.
	 *
	 * @param String $feature the feature slug.
	 * @param array  $options Additional options, optional.
	 */
	public function ensure( $feature, array $options = array() ) {
		$this->config[ $feature ] = true;

		$this->set_feature_options( $feature, $options );
	}

	/**
	 * Runs on plugins_loaded hook priority with priority 2.
	 *
	 * @action plugins_loaded
	 */
	public function on_plugins_loaded() {
		if ( $this->config['connection'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Connection\Manager' )
				&& $this->ensure_feature( 'connection' );
		}

		if ( $this->config['sync'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
				&& $this->ensure_feature( 'sync' );
		}

		if ( $this->config['jitm'] ) {
			// Check for the JITM class in both namespaces. The namespace was changed in jetpack-jitm v1.6.
			( $this->ensure_class( 'Automattic\Jetpack\JITMS\JITM', false )
				|| $this->ensure_class( 'Automattic\Jetpack\JITM' ) )
			&& $this->ensure_feature( 'jitm' );
		}

		if ( $this->config['post_list'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Post_List\Post_List' )
				&& $this->ensure_feature( 'post_list' );
		}

		if ( $this->config['identity_crisis'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Identity_Crisis' )
				&& $this->ensure_feature( 'identity_crisis' );
		}

		if ( $this->config['search'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Search\Initializer' )
				&& $this->ensure_feature( 'search' );
		}

		if ( $this->config['publicize'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize_UI' ) && $this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize' )
				&& $this->ensure_feature( 'publicize' );
		}

		if ( $this->config['wordads'] ) {
			$this->ensure_class( 'Automattic\Jetpack\WordAds\Initializer' )
				&& $this->ensure_feature( 'wordads' );
		}

		if ( $this->config['waf'] ) {
			$this->ensure_class( 'Automattic\Jetpack\Waf\Waf_Initializer' )
				&& $this->ensure_feature( 'waf' );
		}
	}

	/**
	 * Returns true if the required class is available and alerts the user if it's not available
	 * in case the site is in debug mode.
	 *
	 * @param String  $classname a fully qualified class name.
	 * @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found.
	 *
	 * @return Boolean whether the class is available.
	 */
	protected function ensure_class( $classname, $log_notice = true ) {
		$available = class_exists( $classname );

		if ( $log_notice && ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
				sprintf(
					/* translators: %1$s is a PHP class name. */
					esc_html__(
						'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
						'jetpack-config'
					),
					esc_html( $classname )
				),
				E_USER_NOTICE
			);
		}

		return $available;
	}

	/**
	 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
	 * Run the options method (if exists) every time the method is called.
	 *
	 * @param String $feature slug of the feature.
	 * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
	 */
	protected function ensure_feature( $feature ) {
		$method = 'enable_' . $feature;
		if ( ! method_exists( $this, $method ) ) {
			return self::FEATURE_NOT_AVAILABLE;
		}

		$method_options = 'ensure_options_' . $feature;
		if ( method_exists( $this, $method_options ) ) {
			$this->{ $method_options }();
		}

		if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
			return self::FEATURE_ALREADY_ENSURED;
		}

		$this->{ $method }();

		/**
		 * Fires when a specific Jetpack package feature is initalized using the Config package.
		 *
		 * @since 1.1.0
		 */
		do_action( 'jetpack_feature_' . $feature . '_enabled' );

		return self::FEATURE_ENSURED;
	}

	/**
	 * Enables the JITM feature.
	 */
	protected function enable_jitm() {
		if ( class_exists( 'Automattic\Jetpack\JITMS\JITM' ) ) {
			JITMS_JITM::configure();
		} else {
			// Provides compatibility with jetpack-jitm <v1.6.
			JITM::configure();
		}

		return true;
	}

	/**
	 * Enables the Post_List feature.
	 */
	protected function enable_post_list() {
		Post_List::configure();

		return true;
	}

	/**
	 * Enables the Sync feature.
	 */
	protected function enable_sync() {
		Sync_Main::configure();

		return true;
	}

	/**
	 * Enables the Connection feature.
	 */
	protected function enable_connection() {
		Manager::configure();

		return true;
	}

	/**
	 * Enables the identity-crisis feature.
	 */
	protected function enable_identity_crisis() {
		Identity_Crisis::init();
	}

	/**
	 * Enables the search feature.
	 */
	protected function enable_search() {
		Jetpack_Search_Main::init();
	}

	/**
	 * Enables the Publicize feature.
	 */
	protected function enable_publicize() {
		Publicize_Setup::configure();

		return true;
	}

	/**
	 * Enables WordAds.
	 */
	protected function enable_wordads() {
		Jetpack_WordAds_Main::init();
	}

	/**
	 * Enables Waf.
	 */
	protected function enable_waf() {
		Jetpack_Waf_Main::init();

		return true;
	}

	/**
	 * Setup the Connection options.
	 */
	protected function ensure_options_connection() {
		$options = $this->get_feature_options( 'connection' );

		if ( ! empty( $options['slug'] ) ) {
			// The `slug` and `name` are removed from the options because they need to be passed as arguments.
			$slug = $options['slug'];
			unset( $options['slug'] );

			$name = $slug;
			if ( ! empty( $options['name'] ) ) {
				$name = $options['name'];
				unset( $options['name'] );
			}

			( new Plugin( $slug ) )->add( $name, $options );
		}

		return true;
	}

	/**
	 * Setup the Identity Crisis options.
	 *
	 * @return bool
	 */
	protected function ensure_options_identity_crisis() {
		$options = $this->get_feature_options( 'identity_crisis' );

		if ( is_array( $options ) && count( $options ) ) {
			add_filter(
				'jetpack_idc_consumers',
				function ( $consumers ) use ( $options ) {
					$consumers[] = $options;
					return $consumers;
				}
			);
		}

		return true;
	}

	/**
	 * Setup the Sync options.
	 */
	protected function ensure_options_sync() {
		$options = $this->get_feature_options( 'sync' );
		if ( method_exists( 'Automattic\Jetpack\Sync\Main', 'set_sync_data_options' ) ) {
			Sync_Main::set_sync_data_options( $options );
		}

		return true;
	}

	/**
	 * Temporary save initialization options for a feature.
	 *
	 * @param string $feature The feature slug.
	 * @param array  $options The options.
	 *
	 * @return bool
	 */
	protected function set_feature_options( $feature, array $options ) {
		if ( $options ) {
			$this->feature_options[ $feature ] = $options;
		}

		return true;
	}

	/**
	 * Get initialization options for a feature from the temporary storage.
	 *
	 * @param string $feature The feature slug.
	 *
	 * @return array
	 */
	protected function get_feature_options( $feature ) {
		return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ];
	}

}