class-config.php
456 lines
| 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\Connection\Manager; |
| 16 | use Automattic\Jetpack\Connection\Plugin; |
| 17 | use Automattic\Jetpack\Import\Main as Import_Main; |
| 18 | use Automattic\Jetpack\JITMS\JITM as JITMS_JITM; |
| 19 | use Automattic\Jetpack\Post_List\Post_List; |
| 20 | use Automattic\Jetpack\Publicize\Publicize_Setup; |
| 21 | use Automattic\Jetpack\Search\Initializer as Jetpack_Search_Main; |
| 22 | use Automattic\Jetpack\Stats\Main as Stats_Main; |
| 23 | use Automattic\Jetpack\Stats_Admin\Main as Stats_Admin_Main; |
| 24 | use Automattic\Jetpack\Sync\Main as Sync_Main; |
| 25 | use Automattic\Jetpack\VideoPress\Initializer as VideoPress_Pkg_Initializer; |
| 26 | use Automattic\Jetpack\Waf\Waf_Initializer as Jetpack_Waf_Main; |
| 27 | use Automattic\Jetpack\WordAds\Initializer as Jetpack_WordAds_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 | 'wordads' => 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['wordads'] ) { |
| 143 | $this->ensure_class( 'Automattic\Jetpack\WordAds\Initializer' ) |
| 144 | && $this->ensure_feature( 'wordads' ); |
| 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 WordAds. |
| 307 | */ |
| 308 | protected function enable_wordads() { |
| 309 | Jetpack_WordAds_Main::init(); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Enables Waf. |
| 314 | */ |
| 315 | protected function enable_waf() { |
| 316 | Jetpack_Waf_Main::init(); |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Enables VideoPress. |
| 323 | */ |
| 324 | protected function enable_videopress() { |
| 325 | VideoPress_Pkg_Initializer::init(); |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Enables Stats. |
| 331 | */ |
| 332 | protected function enable_stats() { |
| 333 | Stats_Main::init(); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Enables Stats Admin. |
| 339 | */ |
| 340 | protected function enable_stats_admin() { |
| 341 | Stats_Admin_Main::init(); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Handles VideoPress options |
| 347 | */ |
| 348 | protected function ensure_options_videopress() { |
| 349 | $options = $this->get_feature_options( 'videopress' ); |
| 350 | if ( ! empty( $options ) ) { |
| 351 | VideoPress_Pkg_Initializer::update_init_options( $options ); |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Enables Yoast Promo. |
| 358 | */ |
| 359 | protected function enable_yoast_promo() { |
| 360 | Yoast_Promo::init(); |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Enables the Import feature. |
| 366 | */ |
| 367 | protected function enable_import() { |
| 368 | Import_Main::configure(); |
| 369 | |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Setup the Connection options. |
| 375 | */ |
| 376 | protected function ensure_options_connection() { |
| 377 | $options = $this->get_feature_options( 'connection' ); |
| 378 | |
| 379 | if ( ! empty( $options['slug'] ) ) { |
| 380 | // The `slug` and `name` are removed from the options because they need to be passed as arguments. |
| 381 | $slug = $options['slug']; |
| 382 | unset( $options['slug'] ); |
| 383 | |
| 384 | $name = $slug; |
| 385 | if ( ! empty( $options['name'] ) ) { |
| 386 | $name = $options['name']; |
| 387 | unset( $options['name'] ); |
| 388 | } |
| 389 | |
| 390 | ( new Plugin( $slug ) )->add( $name, $options ); |
| 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Setup the Identity Crisis options. |
| 398 | * |
| 399 | * @return bool |
| 400 | */ |
| 401 | protected function ensure_options_identity_crisis() { |
| 402 | $options = $this->get_feature_options( 'identity_crisis' ); |
| 403 | |
| 404 | if ( is_array( $options ) && count( $options ) ) { |
| 405 | add_filter( |
| 406 | 'jetpack_idc_consumers', |
| 407 | function ( $consumers ) use ( $options ) { |
| 408 | $consumers[] = $options; |
| 409 | return $consumers; |
| 410 | } |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Setup the Sync options. |
| 419 | */ |
| 420 | protected function ensure_options_sync() { |
| 421 | $options = $this->get_feature_options( 'sync' ); |
| 422 | if ( method_exists( 'Automattic\Jetpack\Sync\Main', 'set_sync_data_options' ) ) { |
| 423 | Sync_Main::set_sync_data_options( $options ); |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Temporary save initialization options for a feature. |
| 431 | * |
| 432 | * @param string $feature The feature slug. |
| 433 | * @param array $options The options. |
| 434 | * |
| 435 | * @return bool |
| 436 | */ |
| 437 | protected function set_feature_options( $feature, array $options ) { |
| 438 | if ( $options ) { |
| 439 | $this->feature_options[ $feature ] = $options; |
| 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Get initialization options for a feature from the temporary storage. |
| 447 | * |
| 448 | * @param string $feature The feature slug. |
| 449 | * |
| 450 | * @return array |
| 451 | */ |
| 452 | protected function get_feature_options( $feature ) { |
| 453 | return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ]; |
| 454 | } |
| 455 | } |
| 456 |