| @@ -126,12 +126,12 @@ | ||
| 126 | 126 | */ |
| 127 | 127 | protected $settings_schema = []; |
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | - * The slug of a feature that is required to be active. | |
| 130 | + * The slug, or array of slugs, of a feature that is required to be active. | |
| 131 | 131 | * |
| 132 | 132 | * @since 5.0.0 |
| 133 | - * @var false|string | |
| 133 | + * @var false|string|array | |
| 134 | 134 | */ |
| 135 | 135 | protected $requires_feature = false; |
| 136 | 136 | |
| 137 | 137 | /** |
| @@ -142,31 +142,29 @@ | ||
| 142 | 142 | */ |
| 143 | 143 | protected $is_powered_by_epio = false; |
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | - * Run on every page load for feature to set itself up | |
| 146 | + * The name of a group that a feature may belong to. | |
| 147 | 147 | * |
| 148 | - * @since 2.1 | |
| 148 | + * @since 5.3.0 | |
| 149 | + * @var false|string | |
| 149 | 150 | */ |
| 150 | - abstract public function setup(); | |
| 151 | + public $group = false; | |
| 151 | 152 | |
| 152 | 153 | /** |
| 153 | - * Output feature box summary | |
| 154 | + * Field groups available to a feature | |
| 154 | 155 | * |
| 155 | - * @since 2.1 | |
| 156 | + * @since 5.3.0 | |
| 157 | + * @var array | |
| 156 | 158 | */ |
| 157 | - public function output_feature_box_summary() { | |
| 158 | - if ( $this->summary ) { | |
| 159 | - echo wp_kses_post( $this->summary ); | |
| 160 | - } | |
| 161 | - } | |
| 159 | + protected $field_group_map = []; | |
| 162 | 160 | |
| 163 | 161 | /** |
| 164 | - * Implement to output feature box long text | |
| 162 | + * Run on every page load for feature to set itself up | |
| 165 | 163 | * |
| 166 | - * @since 3.0 | |
| 164 | + * @since 2.1 | |
| 167 | 165 | */ |
| 168 | - public function output_feature_box_long() {} | |
| 166 | + abstract public function setup(); | |
| 169 | 167 | |
| 170 | 168 | /** |
| 171 | 169 | * Create feature |
| 172 | 170 | * |
| @@ -189,14 +187,14 @@ | ||
| 189 | 187 | * @since 2.2 |
| 190 | 188 | * @return FeatureRequirementsStatus |
| 191 | 189 | */ |
| 192 | 190 | public function requirements_status() { |
| 193 | - $status = new FeatureRequirementsStatus( 0 ); | |
| 191 | + $status = new FeatureRequirementsStatus( 0, null, $this ); | |
| 194 | 192 | |
| 195 | 193 | /** |
| 196 | 194 | * Filter feature requirement status |
| 197 | 195 | * |
| 198 | - * @hook ep_{indexable_slug}_index_kill | |
| 196 | + * @hook ep_feature_requirements_status | |
| 199 | 197 | * @param {FeatureRequirementStatus} $status Current feature requirement status |
| 200 | 198 | * @param {Feature} $feature Current feature |
| 201 | 199 | * @since 2.2 |
| 202 | 200 | * @return {FeatureRequirementStatus} New status |
| @@ -244,9 +242,13 @@ | ||
| 244 | 242 | |
| 245 | 243 | $active = false; |
| 246 | 244 | |
| 247 | 245 | if ( ! empty( $feature_settings[ $this->slug ] ) && $feature_settings[ $this->slug ]['active'] ) { |
| 248 | - $active = true; | |
| 246 | + $active = ! in_array( | |
| 247 | + $this->requirements_status()->get_code(), | |
| 248 | + [ FeatureRequirementsStatus::FORCE_DISABLED, FeatureRequirementsStatus::TEMPORARILY_DISABLED ], | |
| 249 | + true | |
| 250 | + ); | |
| 249 | 251 | } |
| 250 | 252 | |
| 251 | 253 | /** |
| 252 | 254 | * Filter whether a feature is active or not |
| @@ -293,158 +295,31 @@ | ||
| 293 | 295 | do_action( 'ep_feature_post_activation', $this->slug, $this ); |
| 294 | 296 | } |
| 295 | 297 | |
| 296 | 298 | /** |
| 297 | - * Outputs feature box | |
| 299 | + * To be run after feature deactivation | |
| 298 | 300 | * |
| 299 | - * @since 2.1 | |
| 301 | + * @since 5.3.2 | |
| 300 | 302 | */ |
| 301 | - public function output_feature_box() { | |
| 302 | - $this->output_feature_box_summary(); | |
| 303 | - | |
| 303 | + public function post_deactivation() { | |
| 304 | 304 | /** |
| 305 | - * Fires before feature box summary is shown | |
| 305 | + * Fires after feature is deactivated | |
| 306 | 306 | * |
| 307 | - * @hook ep_feature_box_summary | |
| 308 | - * @param {string} $slug Feature slug | |
| 307 | + * @hook ep_feature_post_deactivation | |
| 308 | + * @param {string} $slug Feature slug | |
| 309 | 309 | * @param {Feature} $feature Current feature |
| 310 | - * @since 2.1 | |
| 310 | + * @since 5.3.2 | |
| 311 | 311 | */ |
| 312 | - do_action( 'ep_feature_box_summary', $this->slug, $this ); | |
| 313 | - ?> | |
| 314 | - | |
| 315 | - <button aria-expanded="false" class="learn-more button button-secondary button-small" type="button"><?php esc_html_e( 'Learn more', 'elasticpress' ); ?></button> | |
| 316 | - | |
| 317 | - <div class="long"> | |
| 318 | - <?php $this->output_feature_box_long(); ?> | |
| 319 | - | |
| 320 | - <p><button aria-expanded="true" class="collapse button button-secondary button-small" type="button"><?php esc_html_e( 'Collapse', 'elasticpress' ); ?></button></p> | |
| 321 | - | |
| 322 | - <?php | |
| 323 | - /** | |
| 324 | - * Fires after feature long description | |
| 325 | - * | |
| 326 | - * @hook ep_feature_box_long | |
| 327 | - * @param {string} $slug Feature slug | |
| 328 | - * @param {Feature} $feature Current feature | |
| 329 | - * @since 2.1 | |
| 330 | - */ | |
| 331 | - do_action( 'ep_feature_box_long', $this->slug, $this ); | |
| 332 | - ?> | |
| 333 | - | |
| 334 | - </div> | |
| 335 | - <?php | |
| 312 | + do_action( 'ep_feature_post_deactivation', $this->slug, $this ); | |
| 336 | 313 | } |
| 337 | 314 | |
| 338 | 315 | /** |
| 339 | - * Output extra feature box settings. | |
| 340 | - * | |
| 341 | - * By default this does nothing. Override to add additional settings. | |
| 342 | - * | |
| 343 | - * @since 3.0 | |
| 344 | - */ | |
| 345 | - public function output_feature_box_settings() { | |
| 346 | - /** | |
| 347 | - * Optionally override | |
| 348 | - */ | |
| 349 | - } | |
| 350 | - | |
| 351 | - /** | |
| 352 | - * Output feature settings | |
| 353 | - * | |
| 354 | - * @since 3.0 | |
| 355 | - */ | |
| 356 | - public function output_settings_box() { | |
| 357 | - $requirements_status = $this->requirements_status(); | |
| 358 | - $sync_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) | |
| 359 | - ? network_admin_url( 'admin.php?page=elasticpress-sync' ) | |
| 360 | - : admin_url( 'admin.php?page=elasticpress-sync' ); | |
| 361 | - ?> | |
| 362 | - | |
| 363 | - <form> | |
| 364 | - <?php | |
| 365 | - if ( ! empty( $requirements_status->message ) ) : | |
| 366 | - $messages = (array) $requirements_status->message; | |
| 367 | - ?> | |
| 368 | - <?php foreach ( $messages as $message ) : ?> | |
| 369 | - <div class="requirements-status-notice"> | |
| 370 | - <?php echo wp_kses_post( $message ); ?> | |
| 371 | - </div> | |
| 372 | - <?php endforeach; ?> | |
| 373 | - <?php endif; ?> | |
| 374 | - | |
| 375 | - <?php if ( $this->requires_install_reindex || $this->setting_requires_install_reindex ) : ?> | |
| 376 | - <div class="requirements-status-notice requirements-status-notice--reindex" role="status"> | |
| 377 | - <?php esc_html_e( 'Enabling this feature will require re-syncing your content.', 'elasticpress' ); ?> | |
| 378 | - </div> | |
| 379 | - <?php endif; ?> | |
| 380 | - | |
| 381 | - <div class="requirements-status-notice requirements-status-notice--syncing" role="alert"> | |
| 382 | - <?php | |
| 383 | - printf( | |
| 384 | - '%1$s <a href="%2$s">%3$s</a>', | |
| 385 | - esc_html__( 'Settings not saved. Cannot save settings while a sync is in progress.', 'elasticpress' ), | |
| 386 | - esc_url( $sync_url ), | |
| 387 | - esc_html__( 'View sync status.', 'elasticpress' ) | |
| 388 | - ); | |
| 389 | - ?> | |
| 390 | - </div> | |
| 391 | - | |
| 392 | - <h3><?php esc_html_e( 'Settings', 'elasticpress' ); ?></h3> | |
| 393 | - | |
| 394 | - <div class="feature-fields"> | |
| 395 | - <div class="field js-toggle-feature"> | |
| 396 | - <div class="field-name status"><?php esc_html_e( 'Status', 'elasticpress' ); ?></div> | |
| 397 | - <div class="input-wrap <?php if ( 2 === $requirements_status->code ) : ?>disabled<?php endif; ?>"> | |
| 398 | - <label><input name="settings[active]" <?php disabled( 2 === $requirements_status->code ); ?> type="radio" <?php checked( $this->is_active() ); ?> value="1"><?php esc_html_e( 'Enabled', 'elasticpress' ); ?></label><br> | |
| 399 | - <label><input name="settings[active]" <?php disabled( 2 === $requirements_status->code ); ?> type="radio" <?php checked( ! $this->is_active() ); ?> value="0"><?php esc_html_e( 'Disabled', 'elasticpress' ); ?></label> | |
| 400 | - </div> | |
| 401 | - </div> | |
| 402 | - | |
| 403 | - <?php | |
| 404 | - $this->output_feature_box_settings(); | |
| 405 | - ?> | |
| 406 | - </div> | |
| 407 | - | |
| 408 | - <div class="action-wrap"> | |
| 409 | - <span class="no-dash-sync"> | |
| 410 | - <?php esc_html_e( 'Setting adjustments to this feature require a re-sync. Use WP-CLI.', 'elasticpress' ); ?> | |
| 411 | - </span> | |
| 412 | - | |
| 413 | - <input type="hidden" name="action" value="ep_save_feature"> | |
| 414 | - <input type="hidden" name="feature" value="<?php echo esc_attr( $this->slug ); ?>"> | |
| 415 | - <input type="hidden" name="requires_reindex" value="<?php echo $this->requires_install_reindex ? '1' : '0'; ?>"> | |
| 416 | - <input type="hidden" name="was_active" value="<?php echo $this->is_active() ? '1' : '0'; ?>"> | |
| 417 | - <input type="hidden" name="setting_requires_reindex" value="<?php echo esc_attr( $this->setting_requires_install_reindex ); ?>"> | |
| 418 | - <input type="hidden" name="setting_requires_reindex_was" value="<?php echo esc_attr( $this->get_reindex_setting() ); ?>"> | |
| 419 | - <?php wp_nonce_field( 'ep_dashboard_nonce', 'nonce' ); ?> | |
| 420 | - | |
| 421 | - <button name="submit" <?php disabled( 2 === $requirements_status->code || ( $this->requires_install_reindex && defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) ); ?> class="button button-primary" type="submit"> | |
| 422 | - <?php esc_html_e( 'Save', 'elasticpress' ); ?> | |
| 423 | - </button> | |
| 424 | - </div> | |
| 425 | - </form> | |
| 426 | - | |
| 427 | - <?php | |
| 428 | - } | |
| 429 | - | |
| 430 | - /** | |
| 431 | - * Returns the ElasticPress.io logo. | |
| 432 | - * | |
| 433 | - * @since 4.4.1 | |
| 434 | - * @return string | |
| 435 | - */ | |
| 436 | - public function get_epio_logo() : string { | |
| 437 | - return sprintf( '<img class="feature-epio-logo" alt="ElasticPress.io logo" src="%s" width="110" height="20">', esc_url( plugins_url( '/images/logo-elasticpress-io.svg', EP_FILE ) ) ); | |
| 438 | - } | |
| 439 | - | |
| 440 | - /** | |
| 441 | 316 | * Returns the feature title. |
| 442 | 317 | * |
| 443 | 318 | * @since 4.4.1 |
| 444 | 319 | * @return string |
| 445 | 320 | */ |
| 446 | - public function get_title() : string { | |
| 321 | + public function get_title(): string { | |
| 447 | 322 | return $this->title; |
| 448 | 323 | } |
| 449 | 324 | |
| 450 | 325 | /** |
| @@ -452,9 +327,9 @@ | ||
| 452 | 327 | * |
| 453 | 328 | * @since 4.4.1 |
| 454 | 329 | * @return string |
| 455 | 330 | */ |
| 456 | - public function get_short_title() : string { | |
| 331 | + public function get_short_title(): string { | |
| 457 | 332 | if ( ! empty( $this->short_title ) ) { |
| 458 | 333 | return $this->short_title; |
| 459 | 334 | } |
| 460 | 335 | |
| @@ -500,9 +375,9 @@ | ||
| 500 | 375 | * |
| 501 | 376 | * @since 4.5.0 |
| 502 | 377 | * @return boolean |
| 503 | 378 | */ |
| 504 | - public function is_available() : bool { | |
| 379 | + public function is_available(): bool { | |
| 505 | 380 | $requirements_status = $this->requirements_status(); |
| 506 | 381 | /** |
| 507 | 382 | * Filter whether a feature is available or not. |
| 508 | 383 | * |
| @@ -524,9 +399,19 @@ | ||
| 524 | 399 | * @param {Feature} $feature Feature object |
| 525 | 400 | * @since 4.5.0 |
| 526 | 401 | * @return {bool} New $is_available value |
| 527 | 402 | */ |
| 528 | - return apply_filters( 'ep_feature_is_available', $this->is_visible() && 2 !== $requirements_status->code, $this->slug, $this ); | |
| 403 | + return apply_filters( | |
| 404 | + 'ep_feature_is_available', | |
| 405 | + $this->is_visible() | |
| 406 | + && ! in_array( | |
| 407 | + $requirements_status->get_code(), | |
| 408 | + [ FeatureRequirementsStatus::FORCE_DISABLED, FeatureRequirementsStatus::TEMPORARILY_DISABLED ], | |
| 409 | + true | |
| 410 | + ), | |
| 411 | + $this->slug, | |
| 412 | + $this | |
| 413 | + ); | |
| 529 | 414 | } |
| 530 | 415 | |
| 531 | 416 | /** |
| 532 | 417 | * Get a JSON representation of the feature |
| @@ -547,11 +432,14 @@ | ||
| 547 | 432 | 'order' => $this->order, |
| 548 | 433 | 'isAvailable' => $this->is_available(), |
| 549 | 434 | 'isPoweredByEpio' => $this->is_powered_by_epio, |
| 550 | 435 | 'isVisible' => $this->is_visible(), |
| 551 | - 'reqStatusCode' => $requirements_status->code, | |
| 552 | - 'reqStatusMessages' => (array) $requirements_status->message, | |
| 436 | + 'reqStatusCode' => $requirements_status->get_code(), | |
| 437 | + 'reqStatusMessages' => (array) $requirements_status->get_message(), | |
| 553 | 438 | 'settingsSchema' => $this->get_settings_schema(), |
| 439 | + 'group' => $this->group, | |
| 440 | + 'requiredFeature' => $this->get_required_feature(), | |
| 441 | + 'fieldGroups' => $this->get_field_group_map(), | |
| 554 | 442 | ]; |
| 555 | 443 | |
| 556 | 444 | return $feature_desc; |
| 557 | 445 | } |
| @@ -571,9 +459,9 @@ | ||
| 571 | 459 | $active = [ |
| 572 | 460 | 'default' => false, |
| 573 | 461 | 'key' => 'active', |
| 574 | 462 | 'label' => __( 'Enable', 'elasticpress' ), |
| 575 | - 'requires_feature' => $this->requires_feature, | |
| 463 | + 'requires_feature' => $this->get_required_feature(), | |
| 576 | 464 | 'requires_sync' => $this->requires_install_reindex, |
| 577 | 465 | 'type' => 'toggle', |
| 578 | 466 | ]; |
| 579 | 467 | |
| @@ -595,8 +483,17 @@ | ||
| 595 | 483 | return apply_filters( 'ep_feature_settings_schema', $settings_schema, $this->slug, $this ); |
| 596 | 484 | } |
| 597 | 485 | |
| 598 | 486 | /** |
| 487 | + * Reset the cached settings schema so it is rebuilt on next access. | |
| 488 | + * | |
| 489 | + * @since 5.3.3 | |
| 490 | + */ | |
| 491 | + public function reset_settings_schema() { | |
| 492 | + $this->settings_schema = []; | |
| 493 | + } | |
| 494 | + | |
| 495 | + /** | |
| 599 | 496 | * Default implementation of `set_settings_schema` based on the `default_settings` attribute |
| 600 | 497 | * |
| 601 | 498 | * @since 5.0.0 |
| 602 | 499 | */ |
| @@ -620,6 +517,65 @@ | ||
| 620 | 517 | 'label' => $key, |
| 621 | 518 | 'type' => $type, |
| 622 | 519 | ]; |
| 623 | 520 | } |
| 521 | + } | |
| 522 | + | |
| 523 | + /** | |
| 524 | + * Sets the i18n strings for the feature. | |
| 525 | + * | |
| 526 | + * @return void | |
| 527 | + * @since 5.2.0 | |
| 528 | + */ | |
| 529 | + public function set_i18n_strings(): void { | |
| 530 | + } | |
| 531 | + | |
| 532 | + /** | |
| 533 | + * Get all features required by this feature | |
| 534 | + * | |
| 535 | + * @since 5.3.0 | |
| 536 | + * @return array List of required feature slugs | |
| 537 | + */ | |
| 538 | + public function get_required_feature() { | |
| 539 | + return $this->requires_feature ? array_unique( (array) $this->requires_feature ) : []; | |
| 540 | + } | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Get the field group map for the feature. | |
| 544 | + * | |
| 545 | + * @since 5.3.0 | |
| 546 | + * @return array | |
| 547 | + */ | |
| 548 | + public function get_field_group_map(): array { | |
| 549 | + /** | |
| 550 | + * Filter available field groups. | |
| 551 | + * | |
| 552 | + * @hook ep_feature_field_groups | |
| 553 | + * @since 5.3.0 | |
| 554 | + * @param {array} $field_groups Current field groups | |
| 555 | + * @return {array} New field groups | |
| 556 | + */ | |
| 557 | + return apply_filters( 'ep_feature_field_groups', $this->field_group_map ); | |
| 558 | + } | |
| 559 | + | |
| 560 | + /** | |
| 561 | + * Get the feature slug. | |
| 562 | + * | |
| 563 | + * @since 5.3.0 | |
| 564 | + * @return string Feature slug. | |
| 565 | + */ | |
| 566 | + public function get_feature_slug(): string { | |
| 567 | + return $this->slug; | |
| 568 | + } | |
| 569 | + | |
| 570 | + /** | |
| 571 | + * Pre-handle feature activation | |
| 572 | + * | |
| 573 | + * This method is called before features are setup and is intended to be used | |
| 574 | + * to modify features requirements status. | |
| 575 | + * | |
| 576 | + * @since 5.3.3 | |
| 577 | + * @return void | |
| 578 | + */ | |
| 579 | + public function pre_handle_feature_activation() { | |
| 624 | 580 | } |
| 625 | 581 | } |