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