| 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\Account_Protection\Account_Protection as Jetpack_Account_Protection_Main; |
| 16 |
use Automattic\Jetpack\Connection\Manager; |
| 17 |
use Automattic\Jetpack\Connection\Plugin; |
| 18 |
use Automattic\Jetpack\Import\Main as Import_Main; |
| 19 |
use Automattic\Jetpack\JITMS\JITM as JITMS_JITM; |
| 20 |
use Automattic\Jetpack\Post_List\Post_List; |
| 21 |
use Automattic\Jetpack\Publicize\Publicize_Setup; |
| 22 |
use Automattic\Jetpack\Search\Initializer as Jetpack_Search_Main; |
| 23 |
use Automattic\Jetpack\Stats\Main as Stats_Main; |
| 24 |
use Automattic\Jetpack\Stats_Admin\Main as Stats_Admin_Main; |
| 25 |
use Automattic\Jetpack\Sync\Main as Sync_Main; |
| 26 |
use Automattic\Jetpack\VideoPress\Initializer as VideoPress_Pkg_Initializer; |
| 27 |
use Automattic\Jetpack\Waf\Waf_Initializer as Jetpack_Waf_Main; |
| 28 |
|
| 29 |
/** |
| 30 |
* The configuration class. |
| 31 |
*/ |
| 32 |
class Config { |
| 33 |
|
| 34 |
const FEATURE_ENSURED = 1; |
| 35 |
const FEATURE_NOT_AVAILABLE = 0; |
| 36 |
const FEATURE_ALREADY_ENSURED = -1; |
| 37 |
|
| 38 |
/** |
| 39 |
* The initial setting values. |
| 40 |
* |
| 41 |
* @var Array |
| 42 |
*/ |
| 43 |
protected $config = array( |
| 44 |
'jitm' => false, |
| 45 |
'connection' => false, |
| 46 |
'sync' => false, |
| 47 |
'post_list' => false, |
| 48 |
'identity_crisis' => false, |
| 49 |
'search' => false, |
| 50 |
'publicize' => false, |
| 51 |
'account_protection' => false, |
| 52 |
'waf' => false, |
| 53 |
'videopress' => false, |
| 54 |
'stats' => false, |
| 55 |
'stats_admin' => false, |
| 56 |
'yoast_promo' => false, |
| 57 |
'import' => false, |
| 58 |
); |
| 59 |
|
| 60 |
/** |
| 61 |
* Initialization options stored here. |
| 62 |
* |
| 63 |
* @var array |
| 64 |
*/ |
| 65 |
protected $feature_options = array(); |
| 66 |
|
| 67 |
/** |
| 68 |
* Creates the configuration class instance. |
| 69 |
*/ |
| 70 |
public function __construct() { |
| 71 |
/** |
| 72 |
* Adding the config handler to run on priority 2 because the class itself is |
| 73 |
* being constructed on priority 1. |
| 74 |
*/ |
| 75 |
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Require a feature to be initialized. It's up to the package consumer to actually add |
| 80 |
* the package to their composer project. Declaring a requirement using this method |
| 81 |
* instructs the class to initialize it. |
| 82 |
* |
| 83 |
* Run the options method (if exists) every time the method is called. |
| 84 |
* |
| 85 |
* @param String $feature the feature slug. |
| 86 |
* @param array $options Additional options, optional. |
| 87 |
*/ |
| 88 |
public function ensure( $feature, array $options = array() ) { |
| 89 |
$this->config[ $feature ] = true; |
| 90 |
|
| 91 |
$this->set_feature_options( $feature, $options ); |
| 92 |
|
| 93 |
$method_options = 'ensure_options_' . $feature; |
| 94 |
if ( method_exists( $this, $method_options ) ) { |
| 95 |
$this->{ $method_options }(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Runs on plugins_loaded hook priority with priority 2. |
| 101 |
* |
| 102 |
* @action plugins_loaded |
| 103 |
*/ |
| 104 |
public function on_plugins_loaded() { |
| 105 |
if ( $this->config['connection'] ) { |
| 106 |
$this->ensure_class( 'Automattic\Jetpack\Connection\Manager' ) |
| 107 |
&& $this->ensure_feature( 'connection' ); |
| 108 |
} |
| 109 |
|
| 110 |
if ( $this->config['sync'] ) { |
| 111 |
$this->ensure_class( 'Automattic\Jetpack\Sync\Main' ) |
| 112 |
&& $this->ensure_feature( 'sync' ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( $this->config['jitm'] ) { |
| 116 |
// Check for the JITM class in both namespaces. The namespace was changed in jetpack-jitm v1.6. |
| 117 |
( $this->ensure_class( 'Automattic\Jetpack\JITMS\JITM', false ) |
| 118 |
|| $this->ensure_class( 'Automattic\Jetpack\JITM' ) ) |
| 119 |
&& $this->ensure_feature( 'jitm' ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( $this->config['post_list'] ) { |
| 123 |
$this->ensure_class( 'Automattic\Jetpack\Post_List\Post_List' ) |
| 124 |
&& $this->ensure_feature( 'post_list' ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( $this->config['identity_crisis'] ) { |
| 128 |
$this->ensure_class( 'Automattic\Jetpack\Identity_Crisis' ) |
| 129 |
&& $this->ensure_feature( 'identity_crisis' ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( $this->config['search'] ) { |
| 133 |
$this->ensure_class( 'Automattic\Jetpack\Search\Initializer' ) |
| 134 |
&& $this->ensure_feature( 'search' ); |
| 135 |
} |
| 136 |
|
| 137 |
if ( $this->config['publicize'] ) { |
| 138 |
$this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize_UI' ) && $this->ensure_class( 'Automattic\Jetpack\Publicize\Publicize' ) |
| 139 |
&& $this->ensure_feature( 'publicize' ); |
| 140 |
} |
| 141 |
|
| 142 |
if ( $this->config['account_protection'] ) { |
| 143 |
$this->ensure_class( 'Automattic\Jetpack\Account_Protection\Account_Protection' ) |
| 144 |
&& $this->ensure_feature( 'account_protection' ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( $this->config['waf'] ) { |
| 148 |
$this->ensure_class( 'Automattic\Jetpack\Waf\Waf_Initializer' ) |
| 149 |
&& $this->ensure_feature( 'waf' ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( $this->config['videopress'] ) { |
| 153 |
$this->ensure_class( 'Automattic\Jetpack\VideoPress\Initializer' ) && $this->ensure_feature( 'videopress' ); |
| 154 |
} |
| 155 |
if ( $this->config['stats'] ) { |
| 156 |
$this->ensure_class( 'Automattic\Jetpack\Stats\Main' ) && $this->ensure_feature( 'stats' ); |
| 157 |
} |
| 158 |
if ( $this->config['stats_admin'] ) { |
| 159 |
$this->ensure_class( 'Automattic\Jetpack\Stats_Admin\Main' ) && $this->ensure_feature( 'stats_admin' ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( $this->config['yoast_promo'] ) { |
| 163 |
$this->ensure_class( 'Automattic\Jetpack\Yoast_Promo' ) && $this->ensure_feature( 'yoast_promo' ); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $this->config['import'] ) { |
| 167 |
$this->ensure_class( 'Automattic\Jetpack\Import\Main' ) |
| 168 |
&& $this->ensure_feature( 'import' ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns true if the required class is available and alerts the user if it's not available |
| 174 |
* in case the site is in debug mode. |
| 175 |
* |
| 176 |
* @param String $classname a fully qualified class name. |
| 177 |
* @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found. |
| 178 |
* |
| 179 |
* @return Boolean whether the class is available. |
| 180 |
*/ |
| 181 |
protected function ensure_class( $classname, $log_notice = true ) { |
| 182 |
$available = class_exists( $classname ); |
| 183 |
|
| 184 |
if ( $log_notice && ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 185 |
trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error |
| 186 |
sprintf( |
| 187 |
/* translators: %1$s is a PHP class name. */ |
| 188 |
esc_html__( |
| 189 |
'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader', |
| 190 |
'jetpack-config' |
| 191 |
), |
| 192 |
esc_html( $classname ) |
| 193 |
), |
| 194 |
E_USER_NOTICE |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
return $available; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Ensures a feature is enabled, sets it up if it hasn't already been set up. |
| 203 |
* |
| 204 |
* @param String $feature slug of the feature. |
| 205 |
* @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants. |
| 206 |
*/ |
| 207 |
protected function ensure_feature( $feature ) { |
| 208 |
$method = 'enable_' . $feature; |
| 209 |
if ( ! method_exists( $this, $method ) ) { |
| 210 |
return self::FEATURE_NOT_AVAILABLE; |
| 211 |
} |
| 212 |
|
| 213 |
if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) { |
| 214 |
return self::FEATURE_ALREADY_ENSURED; |
| 215 |
} |
| 216 |
|
| 217 |
$this->{ $method }(); |
| 218 |
|
| 219 |
/** |
| 220 |
* Fires when a specific Jetpack package feature is initalized using the Config package. |
| 221 |
* |
| 222 |
* @since 1.1.0 |
| 223 |
*/ |
| 224 |
do_action( 'jetpack_feature_' . $feature . '_enabled' ); |
| 225 |
|
| 226 |
return self::FEATURE_ENSURED; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Enables the JITM feature. |
| 231 |
*/ |
| 232 |
protected function enable_jitm() { |
| 233 |
if ( class_exists( 'Automattic\Jetpack\JITMS\JITM' ) ) { |
| 234 |
JITMS_JITM::configure(); |
| 235 |
} else { |
| 236 |
// Provides compatibility with jetpack-jitm <v1.6. |
| 237 |
// @phan-suppress-next-line PhanUndeclaredClassMethod |
| 238 |
JITM::configure(); |
| 239 |
} |
| 240 |
|
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Enables the Post_List feature. |
| 246 |
*/ |
| 247 |
protected function enable_post_list() { |
| 248 |
Post_List::configure(); |
| 249 |
|
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Enables the Sync feature. |
| 255 |
*/ |
| 256 |
protected function enable_sync() { |
| 257 |
Sync_Main::configure(); |
| 258 |
|
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Enables the Connection feature. |
| 264 |
*/ |
| 265 |
protected function enable_connection() { |
| 266 |
Manager::configure(); |
| 267 |
|
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Enables the identity-crisis feature. |
| 273 |
*/ |
| 274 |
protected function enable_identity_crisis() { |
| 275 |
Identity_Crisis::init(); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Enables the search feature. |
| 280 |
*/ |
| 281 |
protected function enable_search() { |
| 282 |
Jetpack_Search_Main::init(); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Enables the Publicize feature. |
| 287 |
*/ |
| 288 |
protected function enable_publicize() { |
| 289 |
Publicize_Setup::configure(); |
| 290 |
|
| 291 |
return true; |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Handles Publicize options. |
| 296 |
*/ |
| 297 |
protected function ensure_options_publicize() { |
| 298 |
$options = $this->get_feature_options( 'publicize' ); |
| 299 |
|
| 300 |
if ( ! empty( $options['force_refresh'] ) ) { |
| 301 |
Publicize_Setup::$refresh_plan_info = true; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Enables Account Protection. |
| 307 |
*/ |
| 308 |
protected function enable_account_protection() { |
| 309 |
$account_protection = Jetpack_Account_Protection_Main::instance(); |
| 310 |
$account_protection->initialize(); |
| 311 |
|
| 312 |
return true; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Enables Waf. |
| 317 |
*/ |
| 318 |
protected function enable_waf() { |
| 319 |
Jetpack_Waf_Main::init(); |
| 320 |
|
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Enables VideoPress. |
| 326 |
*/ |
| 327 |
protected function enable_videopress() { |
| 328 |
VideoPress_Pkg_Initializer::init(); |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Enables Stats. |
| 334 |
*/ |
| 335 |
protected function enable_stats() { |
| 336 |
Stats_Main::init(); |
| 337 |
return true; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Enables Stats Admin. |
| 342 |
*/ |
| 343 |
protected function enable_stats_admin() { |
| 344 |
Stats_Admin_Main::init(); |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* Handles VideoPress options |
| 350 |
*/ |
| 351 |
protected function ensure_options_videopress() { |
| 352 |
$options = $this->get_feature_options( 'videopress' ); |
| 353 |
if ( ! empty( $options ) ) { |
| 354 |
VideoPress_Pkg_Initializer::update_init_options( $options ); |
| 355 |
} |
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Enables Yoast Promo. |
| 361 |
*/ |
| 362 |
protected function enable_yoast_promo() { |
| 363 |
Yoast_Promo::init(); |
| 364 |
return true; |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Enables the Import feature. |
| 369 |
*/ |
| 370 |
protected function enable_import() { |
| 371 |
Import_Main::configure(); |
| 372 |
|
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Setup the Connection options. |
| 378 |
*/ |
| 379 |
protected function ensure_options_connection() { |
| 380 |
$options = $this->get_feature_options( 'connection' ); |
| 381 |
|
| 382 |
if ( ! empty( $options['slug'] ) ) { |
| 383 |
// The `slug` and `name` are removed from the options because they need to be passed as arguments. |
| 384 |
$slug = $options['slug']; |
| 385 |
unset( $options['slug'] ); |
| 386 |
|
| 387 |
$name = $slug; |
| 388 |
if ( ! empty( $options['name'] ) ) { |
| 389 |
$name = $options['name']; |
| 390 |
unset( $options['name'] ); |
| 391 |
} |
| 392 |
|
| 393 |
( new Plugin( $slug ) )->add( $name, $options ); |
| 394 |
} |
| 395 |
|
| 396 |
return true; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Setup the Identity Crisis options. |
| 401 |
* |
| 402 |
* @return bool |
| 403 |
*/ |
| 404 |
protected function ensure_options_identity_crisis() { |
| 405 |
$options = $this->get_feature_options( 'identity_crisis' ); |
| 406 |
|
| 407 |
if ( is_array( $options ) && count( $options ) ) { |
| 408 |
add_filter( |
| 409 |
'jetpack_idc_consumers', |
| 410 |
function ( $consumers ) use ( $options ) { |
| 411 |
$consumers[] = $options; |
| 412 |
return $consumers; |
| 413 |
} |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Setup the Sync options. |
| 422 |
*/ |
| 423 |
protected function ensure_options_sync() { |
| 424 |
$options = $this->get_feature_options( 'sync' ); |
| 425 |
if ( method_exists( 'Automattic\Jetpack\Sync\Main', 'set_sync_data_options' ) ) { |
| 426 |
Sync_Main::set_sync_data_options( $options ); |
| 427 |
} |
| 428 |
|
| 429 |
return true; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Temporary save initialization options for a feature. |
| 434 |
* |
| 435 |
* @param string $feature The feature slug. |
| 436 |
* @param array $options The options. |
| 437 |
* |
| 438 |
* @return bool |
| 439 |
*/ |
| 440 |
protected function set_feature_options( $feature, array $options ) { |
| 441 |
if ( $options ) { |
| 442 |
$this->feature_options[ $feature ] = $options; |
| 443 |
} |
| 444 |
|
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Get initialization options for a feature from the temporary storage. |
| 450 |
* |
| 451 |
* @param string $feature The feature slug. |
| 452 |
* |
| 453 |
* @return array |
| 454 |
*/ |
| 455 |
protected function get_feature_options( $feature ) { |
| 456 |
return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ]; |
| 457 |
} |
| 458 |
} |
| 459 |
|