1 | <?php |
||
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 ) { |
||
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 ) { |
||
143 | |||
144 | /** |
||
145 | * Dummy method to enable Terms of Service. |
||
146 | */ |
||
147 | protected function enable_tos() { |
||
150 | |||
151 | /** |
||
152 | * Enables the tracking feature. Depends on the Terms of Service package, so enables it too. |
||
153 | */ |
||
154 | protected function enable_tracking() { |
||
172 | |||
173 | /** |
||
174 | * Enables the JITM feature. |
||
175 | */ |
||
176 | protected function enable_jitm() { |
||
181 | |||
182 | /** |
||
183 | * Enables the Sync feature. |
||
184 | */ |
||
185 | protected function enable_sync() { |
||
190 | |||
191 | /** |
||
192 | * Enables the Connection feature. |
||
193 | */ |
||
194 | protected function enable_connection() { |
||
199 | |||
200 | } |
||
201 |