Completed
Push — instant-search-master ( 95535d...2c1eb1 )
by
unknown
22:37 queued 16:04
created

Config   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A ensure_class() 0 19 4
A ensure_feature() 0 21 3
A enable_tos() 0 3 1
A enable_tracking() 0 18 2
A __construct() 0 8 1
A ensure() 0 3 1
B on_plugins_loaded() 0 22 10
A enable_jitm() 0 5 1
A enable_sync() 0 5 1
A 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
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
			$this->ensure_class( 'Automattic\Jetpack\Terms_Of_Service' )
74
				&& $this->ensure_class( 'Automattic\Jetpack\Tracking' )
75
				&& $this->ensure_feature( 'tracking' );
76
		}
77
78
		if ( $this->config['sync'] ) {
79
			$this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
80
				&& $this->ensure_feature( 'sync' );
81
		}
82
83
		if ( $this->config['jitm'] ) {
84
			$this->ensure_class( 'Automattic\Jetpack\JITM' )
85
				&& $this->ensure_feature( 'jitm' );
86
		}
87
	}
88
89
	/**
90
	 * Returns true if the required class is available and alerts the user if it's not available
91
	 * in case the site is in debug mode.
92
	 *
93
	 * @param String $classname a fully qualified class name.
94
	 * @return Boolean whether the class is available.
95
	 */
96
	protected function ensure_class( $classname ) {
97
		$available = class_exists( $classname );
98
99
		if ( ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
100
			trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
101
				sprintf(
102
					/* translators: %1$s is a PHP class name. */
103
					esc_html__(
104
						'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
105
						'jetpack'
106
					),
107
					esc_html( $classname )
108
				),
109
				E_USER_NOTICE
110
			);
111
		}
112
113
		return $available;
114
	}
115
116
	/**
117
	 * Ensures a feature is enabled, sets it up if it hasn't already been set up.
118
	 *
119
	 * @param String $feature slug of the feature.
120
	 * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
121
	 */
122
	protected function ensure_feature( $feature ) {
123
		$method = 'enable_' . $feature;
124
		if ( ! method_exists( $this, $method ) ) {
125
			return self::FEATURE_NOT_AVAILABLE;
126
		}
127
128
		if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
129
			return self::FEATURE_ALREADY_ENSURED;
130
		}
131
132
		$this->{ $method }();
133
134
		/**
135
		 * Fires when a specific Jetpack package feature is initalized using the Config package.
136
		 *
137
		 * @since 8.2.0
138
		 */
139
		do_action( 'jetpack_feature_' . $feature . '_enabled' );
140
141
		return self::FEATURE_ENSURED;
142
	}
143
144
	/**
145
	 * Dummy method to enable Terms of Service.
146
	 */
147
	protected function enable_tos() {
148
		return true;
149
	}
150
151
	/**
152
	 * Enables the tracking feature. Depends on the Terms of Service package, so enables it too.
153
	 */
154
	protected function enable_tracking() {
155
156
		// Enabling dependencies.
157
		$this->ensure_feature( 'tos' );
158
159
		$terms_of_service = new Terms_Of_Service();
160
		$tracking         = new Plugin_Tracking();
161
		if ( $terms_of_service->has_agreed() ) {
162
			add_action( 'init', array( $tracking, 'init' ) );
163
		} else {
164
			/**
165
			 * Initialize tracking right after the user agrees to the terms of service.
166
			 */
167
			add_action( 'jetpack_agreed_to_terms_of_service', array( $tracking, 'init' ) );
168
		}
169
170
		return true;
171
	}
172
173
	/**
174
	 * Enables the JITM feature.
175
	 */
176
	protected function enable_jitm() {
177
		JITM::configure();
178
179
		return true;
180
	}
181
182
	/**
183
	 * Enables the Sync feature.
184
	 */
185
	protected function enable_sync() {
186
		Sync_Main::configure();
187
188
		return true;
189
	}
190
191
	/**
192
	 * Enables the Connection feature.
193
	 */
194
	protected function enable_connection() {
195
		Manager::configure();
196
197
		return true;
198
	}
199
200
}
201