Completed
Push — update/calendly-front-end ( 8b5db8...cffa8a )
by
unknown
13:45 queued 06:35
created

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