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2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / jetpack_vendor / automattic / jetpack-config / src / class-config.php
jetpack / jetpack_vendor / automattic / jetpack-config / src Last commit date
class-config.php 9 months ago
class-config.php
459 lines
1 <?php
2 /**
3 * The base Jetpack configuration class file.
4 *
5 * @package automattic/jetpack-config
6 */
7
8 namespace Automattic\Jetpack;
9
10 /*
11 * The Config package does not require the composer packages that
12 * contain the package classes shown below. The consumer plugin
13 * must require the corresponding packages to use these features.
14 */
15 use Automattic\Jetpack\Account_Protection\Account_Protection as Jetpack_Account_Protection_Main;
16 use Automattic\Jetpack\Connection\Manager;
17 use Automattic\Jetpack\Connection\Plugin;
18 use Automattic\Jetpack\Import\Main as Import_Main;
19 use Automattic\Jetpack\JITMS\JITM as JITMS_JITM;
20 use Automattic\Jetpack\Post_List\Post_List;
21 use Automattic\Jetpack\Publicize\Publicize_Setup;
22 use Automattic\Jetpack\Search\Initializer as Jetpack_Search_Main;
23 use Automattic\Jetpack\Stats\Main as Stats_Main;
24 use Automattic\Jetpack\Stats_Admin\Main as Stats_Admin_Main;
25 use Automattic\Jetpack\Sync\Main as Sync_Main;
26 use Automattic\Jetpack\VideoPress\Initializer as VideoPress_Pkg_Initializer;
27 use Automattic\Jetpack\Waf\Waf_Initializer as Jetpack_Waf_Main;
28
29 /**
30 * The configuration class.
31 */
32 class Config {
33
34 const FEATURE_ENSURED = 1;
35 const FEATURE_NOT_AVAILABLE = 0;
36 const FEATURE_ALREADY_ENSURED = -1;
37
38 /**
39 * The initial setting values.
40 *
41 * @var Array
42 */
43 protected $config = array(
44 'jitm' => false,
45 'connection' => false,
46 'sync' => false,
47 'post_list' => false,
48 'identity_crisis' => false,
49 'search' => false,
50 'publicize' => false,
51 'account_protection' => false,
52 'waf' => false,
53 'videopress' => false,
54 'stats' => false,
55 'stats_admin' => false,
56 'yoast_promo' => false,
57 'import' => false,
58 );
59
60 /**
61 * Initialization options stored here.
62 *
63 * @var array
64 */
65 protected $feature_options = array();
66
67 /**
68 * Creates the configuration class instance.
69 */
70 public function __construct() {
71 /**
72 * Adding the config handler to run on priority 2 because the class itself is
73 * being constructed on priority 1.
74 */
75 add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
76 }
77
78 /**
79 * Require a feature to be initialized. It's up to the package consumer to actually add
80 * the package to their composer project. Declaring a requirement using this method
81 * instructs the class to initialize it.
82 *
83 * Run the options method (if exists) every time the method is called.
84 *
85 * @param String $feature the feature slug.
86 * @param array $options Additional options, optional.
87 */
88 public function ensure( $feature, array $options = array() ) {
89 $this->config[ $feature ] = true;
90
91 $this->set_feature_options( $feature, $options );
92
93 $method_options = 'ensure_options_' . $feature;
94 if ( method_exists( $this, $method_options ) ) {
95 $this->{ $method_options }();
96 }
97 }
98
99 /**
100 * Runs on plugins_loaded hook priority with priority 2.
101 *
102 * @action plugins_loaded
103 */
104 public function on_plugins_loaded() {
105 if ( $this->config['connection'] ) {
106 $this->ensure_class( 'Automattic\Jetpack\Connection\Manager' )
107 && $this->ensure_feature( 'connection' );
108 }
109
110 if ( $this->config['sync'] ) {
111 $this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
112 && $this->ensure_feature( 'sync' );
113 }
114
115 if ( $this->config['jitm'] ) {
116 // Check for the JITM class in both namespaces. The namespace was changed in jetpack-jitm v1.6.
117 ( $this->ensure_class( 'Automattic\Jetpack\JITMS\JITM', false )
118 || $this->ensure_class( 'Automattic\Jetpack\JITM' ) )
119 && $this->ensure_feature( 'jitm' );
120 }
121
122 if ( $this->config['post_list'] ) {
123 $this->ensure_class( 'Automattic\Jetpack\Post_List\Post_List' )
124 && $this->ensure_feature( 'post_list' );
125 }
126
127 if ( $this->config['identity_crisis'] ) {
128 $this->ensure_class( 'Automattic\Jetpack\Identity_Crisis' )
129 && $this->ensure_feature( 'identity_crisis' );
130 }
131
132 if ( $this->config['search'] ) {
133 $this->ensure_class( 'Automattic\Jetpack\Search\Initializer' )
134 && $this->ensure_feature( 'search' );
135 }
136
137 if ( $this->config['publicize'] ) {
138 $this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize_UI' ) && $this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize' )
139 && $this->ensure_feature( 'publicize' );
140 }
141
142 if ( $this->config['account_protection'] ) {
143 $this->ensure_class( 'Automattic\Jetpack\Account_Protection\Account_Protection' )
144 && $this->ensure_feature( 'account_protection' );
145 }
146
147 if ( $this->config['waf'] ) {
148 $this->ensure_class( 'Automattic\Jetpack\Waf\Waf_Initializer' )
149 && $this->ensure_feature( 'waf' );
150 }
151
152 if ( $this->config['videopress'] ) {
153 $this->ensure_class( 'Automattic\Jetpack\VideoPress\Initializer' ) && $this->ensure_feature( 'videopress' );
154 }
155 if ( $this->config['stats'] ) {
156 $this->ensure_class( 'Automattic\Jetpack\Stats\Main' ) && $this->ensure_feature( 'stats' );
157 }
158 if ( $this->config['stats_admin'] ) {
159 $this->ensure_class( 'Automattic\Jetpack\Stats_Admin\Main' ) && $this->ensure_feature( 'stats_admin' );
160 }
161
162 if ( $this->config['yoast_promo'] ) {
163 $this->ensure_class( 'Automattic\Jetpack\Yoast_Promo' ) && $this->ensure_feature( 'yoast_promo' );
164 }
165
166 if ( $this->config['import'] ) {
167 $this->ensure_class( 'Automattic\Jetpack\Import\Main' )
168 && $this->ensure_feature( 'import' );
169 }
170 }
171
172 /**
173 * Returns true if the required class is available and alerts the user if it's not available
174 * in case the site is in debug mode.
175 *
176 * @param String $classname a fully qualified class name.
177 * @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found.
178 *
179 * @return Boolean whether the class is available.
180 */
181 protected function ensure_class( $classname, $log_notice = true ) {
182 $available = class_exists( $classname );
183
184 if ( $log_notice && ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
185 trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
186 sprintf(
187 /* translators: %1$s is a PHP class name. */
188 esc_html__(
189 'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
190 'jetpack-config'
191 ),
192 esc_html( $classname )
193 ),
194 E_USER_NOTICE
195 );
196 }
197
198 return $available;
199 }
200
201 /**
202 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
203 *
204 * @param String $feature slug of the feature.
205 * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
206 */
207 protected function ensure_feature( $feature ) {
208 $method = 'enable_' . $feature;
209 if ( ! method_exists( $this, $method ) ) {
210 return self::FEATURE_NOT_AVAILABLE;
211 }
212
213 if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
214 return self::FEATURE_ALREADY_ENSURED;
215 }
216
217 $this->{ $method }();
218
219 /**
220 * Fires when a specific Jetpack package feature is initalized using the Config package.
221 *
222 * @since 1.1.0
223 */
224 do_action( 'jetpack_feature_' . $feature . '_enabled' );
225
226 return self::FEATURE_ENSURED;
227 }
228
229 /**
230 * Enables the JITM feature.
231 */
232 protected function enable_jitm() {
233 if ( class_exists( 'Automattic\Jetpack\JITMS\JITM' ) ) {
234 JITMS_JITM::configure();
235 } else {
236 // Provides compatibility with jetpack-jitm <v1.6.
237 // @phan-suppress-next-line PhanUndeclaredClassMethod
238 JITM::configure();
239 }
240
241 return true;
242 }
243
244 /**
245 * Enables the Post_List feature.
246 */
247 protected function enable_post_list() {
248 Post_List::configure();
249
250 return true;
251 }
252
253 /**
254 * Enables the Sync feature.
255 */
256 protected function enable_sync() {
257 Sync_Main::configure();
258
259 return true;
260 }
261
262 /**
263 * Enables the Connection feature.
264 */
265 protected function enable_connection() {
266 Manager::configure();
267
268 return true;
269 }
270
271 /**
272 * Enables the identity-crisis feature.
273 */
274 protected function enable_identity_crisis() {
275 Identity_Crisis::init();
276 }
277
278 /**
279 * Enables the search feature.
280 */
281 protected function enable_search() {
282 Jetpack_Search_Main::init();
283 }
284
285 /**
286 * Enables the Publicize feature.
287 */
288 protected function enable_publicize() {
289 Publicize_Setup::configure();
290
291 return true;
292 }
293
294 /**
295 * Handles Publicize options.
296 */
297 protected function ensure_options_publicize() {
298 $options = $this->get_feature_options( 'publicize' );
299
300 if ( ! empty( $options['force_refresh'] ) ) {
301 Publicize_Setup::$refresh_plan_info = true;
302 }
303 }
304
305 /**
306 * Enables Account Protection.
307 */
308 protected function enable_account_protection() {
309 $account_protection = Jetpack_Account_Protection_Main::instance();
310 $account_protection->initialize();
311
312 return true;
313 }
314
315 /**
316 * Enables Waf.
317 */
318 protected function enable_waf() {
319 Jetpack_Waf_Main::init();
320
321 return true;
322 }
323
324 /**
325 * Enables VideoPress.
326 */
327 protected function enable_videopress() {
328 VideoPress_Pkg_Initializer::init();
329 return true;
330 }
331
332 /**
333 * Enables Stats.
334 */
335 protected function enable_stats() {
336 Stats_Main::init();
337 return true;
338 }
339
340 /**
341 * Enables Stats Admin.
342 */
343 protected function enable_stats_admin() {
344 Stats_Admin_Main::init();
345 return true;
346 }
347
348 /**
349 * Handles VideoPress options
350 */
351 protected function ensure_options_videopress() {
352 $options = $this->get_feature_options( 'videopress' );
353 if ( ! empty( $options ) ) {
354 VideoPress_Pkg_Initializer::update_init_options( $options );
355 }
356 return true;
357 }
358
359 /**
360 * Enables Yoast Promo.
361 */
362 protected function enable_yoast_promo() {
363 Yoast_Promo::init();
364 return true;
365 }
366
367 /**
368 * Enables the Import feature.
369 */
370 protected function enable_import() {
371 Import_Main::configure();
372
373 return true;
374 }
375
376 /**
377 * Setup the Connection options.
378 */
379 protected function ensure_options_connection() {
380 $options = $this->get_feature_options( 'connection' );
381
382 if ( ! empty( $options['slug'] ) ) {
383 // The `slug` and `name` are removed from the options because they need to be passed as arguments.
384 $slug = $options['slug'];
385 unset( $options['slug'] );
386
387 $name = $slug;
388 if ( ! empty( $options['name'] ) ) {
389 $name = $options['name'];
390 unset( $options['name'] );
391 }
392
393 ( new Plugin( $slug ) )->add( $name, $options );
394 }
395
396 return true;
397 }
398
399 /**
400 * Setup the Identity Crisis options.
401 *
402 * @return bool
403 */
404 protected function ensure_options_identity_crisis() {
405 $options = $this->get_feature_options( 'identity_crisis' );
406
407 if ( is_array( $options ) && count( $options ) ) {
408 add_filter(
409 'jetpack_idc_consumers',
410 function ( $consumers ) use ( $options ) {
411 $consumers[] = $options;
412 return $consumers;
413 }
414 );
415 }
416
417 return true;
418 }
419
420 /**
421 * Setup the Sync options.
422 */
423 protected function ensure_options_sync() {
424 $options = $this->get_feature_options( 'sync' );
425 if ( method_exists( 'Automattic\Jetpack\Sync\Main', 'set_sync_data_options' ) ) {
426 Sync_Main::set_sync_data_options( $options );
427 }
428
429 return true;
430 }
431
432 /**
433 * Temporary save initialization options for a feature.
434 *
435 * @param string $feature The feature slug.
436 * @param array $options The options.
437 *
438 * @return bool
439 */
440 protected function set_feature_options( $feature, array $options ) {
441 if ( $options ) {
442 $this->feature_options[ $feature ] = $options;
443 }
444
445 return true;
446 }
447
448 /**
449 * Get initialization options for a feature from the temporary storage.
450 *
451 * @param string $feature The feature slug.
452 *
453 * @return array
454 */
455 protected function get_feature_options( $feature ) {
456 return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ];
457 }
458 }
459