Completed
Push — try/jetpack-stories-block-mobi... ( d8c065...8184fb )
by
unknown
74:50 queued 66:11
created

Config::enable_tracking()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Config::enable_connection() 0 5 1
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\Connection\Manager;
16
use Automattic\Jetpack\JITMS\JITM as JITMS_JITM;
17
use Automattic\Jetpack\JITM as JITM;
18
use Automattic\Jetpack\Connection\Plugin;
19
use Automattic\Jetpack\Sync\Main as Sync_Main;
20
21
/**
22
 * The configuration class.
23
 */
24
class Config {
25
26
	const FEATURE_ENSURED         = 1;
27
	const FEATURE_NOT_AVAILABLE   = 0;
28
	const FEATURE_ALREADY_ENSURED = -1;
29
30
	/**
31
	 * The initial setting values.
32
	 *
33
	 * @var Array
34
	 */
35
	protected $config = array(
36
		'jitm'       => false,
37
		'connection' => false,
38
		'sync'       => false,
39
	);
40
41
	/**
42
	 * Initialization options stored here.
43
	 *
44
	 * @var array
45
	 */
46
	protected $feature_options = array();
47
48
	/**
49
	 * Creates the configuration class instance.
50
	 */
51
	public function __construct() {
52
		/**
53
		 * Adding the config handler to run on priority 2 because the class itself is
54
		 * being constructed on priority 1.
55
		 */
56
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
57
58
	}
59
60
	/**
61
	 * Require a feature to be initialized. It's up to the package consumer to actually add
62
	 * the package to their composer project. Declaring a requirement using this method
63
	 * instructs the class to initialize it.
64
	 *
65
	 * @param String $feature the feature slug.
66
	 * @param array  $options Additional options, optional.
67
	 */
68
	public function ensure( $feature, array $options = array() ) {
69
		$this->config[ $feature ] = true;
70
71
		$this->set_feature_options( $feature, $options );
72
	}
73
74
	/**
75
	 * Runs on plugins_loaded hook priority with priority 2.
76
	 *
77
	 * @action plugins_loaded
78
	 */
79
	public function on_plugins_loaded() {
80
		if ( $this->config['connection'] ) {
81
			$this->ensure_class( 'Automattic\Jetpack\Connection\Manager' )
82
				&& $this->ensure_feature( 'connection' );
83
		}
84
85
		if ( $this->config['sync'] ) {
86
			$this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
87
				&& $this->ensure_feature( 'sync' );
88
		}
89
90
		if ( $this->config['jitm'] ) {
91
			// Check for the JITM class in both namespaces. The namespace was changed in jetpack-jitm v1.6.
92
			( $this->ensure_class( 'Automattic\Jetpack\JITMS\JITM', false )
93
				|| $this->ensure_class( 'Automattic\Jetpack\JITM' ) )
94
			&& $this->ensure_feature( 'jitm' );
95
		}
96
	}
97
98
	/**
99
	 * Returns true if the required class is available and alerts the user if it's not available
100
	 * in case the site is in debug mode.
101
	 *
102
	 * @param String  $classname a fully qualified class name.
103
	 * @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found.
104
	 *
105
	 * @return Boolean whether the class is available.
106
	 */
107
	protected function ensure_class( $classname, $log_notice = true ) {
108
		$available = class_exists( $classname );
109
110
		if ( $log_notice && ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
111
			trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
112
				sprintf(
113
					/* translators: %1$s is a PHP class name. */
114
					esc_html__(
115
						'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
116
						'jetpack'
117
					),
118
					esc_html( $classname )
119
				),
120
				E_USER_NOTICE
121
			);
122
		}
123
124
		return $available;
125
	}
126
127
	/**
128
	 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
129
	 * Run the options method (if exists) every time the method is called.
130
	 *
131
	 * @param String $feature slug of the feature.
132
	 * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
133
	 */
134
	protected function ensure_feature( $feature ) {
135
		$method = 'enable_' . $feature;
136
		if ( ! method_exists( $this, $method ) ) {
137
			return self::FEATURE_NOT_AVAILABLE;
138
		}
139
140
		$method_options = 'ensure_options_' . $feature;
141
		if ( method_exists( $this, $method_options ) ) {
142
			$this->{ $method_options }();
143
		}
144
145
		if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
146
			return self::FEATURE_ALREADY_ENSURED;
147
		}
148
149
		$this->{ $method }();
150
151
		/**
152
		 * Fires when a specific Jetpack package feature is initalized using the Config package.
153
		 *
154
		 * @since 8.2.0
155
		 */
156
		do_action( 'jetpack_feature_' . $feature . '_enabled' );
157
158
		return self::FEATURE_ENSURED;
159
	}
160
161
	/**
162
	 * Enables the JITM feature.
163
	 */
164
	protected function enable_jitm() {
165
		if ( class_exists( 'Automattic\Jetpack\JITMS\JITM' ) ) {
166
			JITMS_JITM::configure();
167
		} else {
168
			// Provides compatibility with jetpack-jitm <v1.6.
169
			JITM::configure();
170
		}
171
172
		return true;
173
	}
174
175
	/**
176
	 * Enables the Sync feature.
177
	 */
178
	protected function enable_sync() {
179
		Sync_Main::configure();
180
181
		return true;
182
	}
183
184
	/**
185
	 * Enables the Connection feature.
186
	 */
187
	protected function enable_connection() {
188
		Manager::configure();
189
190
		return true;
191
	}
192
193
	/**
194
	 * Setup the Connection options.
195
	 */
196
	protected function ensure_options_connection() {
197
		$options = $this->get_feature_options( 'connection' );
198
199
		if ( ! empty( $options['slug'] ) ) {
200
			// The `slug` and `name` are removed from the options because they need to be passed as arguments.
201
			$slug = $options['slug'];
202
			unset( $options['slug'] );
203
204
			$name = $slug;
205
			if ( ! empty( $options['name'] ) ) {
206
				$name = $options['name'];
207
				unset( $options['name'] );
208
			}
209
210
			( new Plugin( $slug ) )->add( $name, $options );
211
		}
212
213
		return true;
214
	}
215
216
	/**
217
	 * Temporary save initialization options for a feature.
218
	 *
219
	 * @param string $feature The feature slug.
220
	 * @param array  $options The options.
221
	 *
222
	 * @return bool
223
	 */
224
	protected function set_feature_options( $feature, array $options ) {
225
		if ( $options ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $options of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
226
			$this->feature_options[ $feature ] = $options;
227
		}
228
229
		return true;
230
	}
231
232
	/**
233
	 * Get initialization options for a feature from the temporary storage.
234
	 *
235
	 * @param string $feature The feature slug.
236
	 *
237
	 * @return array
238
	 */
239
	protected function get_feature_options( $feature ) {
240
		return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ];
241
	}
242
243
}
244