Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Features to declare settings as JSON #3655

Merged
merged 7 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ jobs:
run: composer install

- name: PHPCS check
run: './vendor/bin/phpcs . -q --report=checkstyle --runtime-set testVersion 7.0- | cs2pr'
run: './vendor/bin/phpcs . -q --report=checkstyle --runtime-set testVersion 7.4- | cs2pr'
2 changes: 1 addition & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"10up-toolkit lint-js"
],
"*.php": [
"./vendor/bin/phpcs --extensions=php --runtime-set testVersion 7.0- -s"
"./vendor/bin/phpcs --extensions=php --runtime-set testVersion 7.4- -s"
]
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"brianhenryie/strauss": "^0.14.0"
},
"scripts": {
"lint": "phpcs . -s --runtime-set testVersion 7.0-",
"lint": "phpcs . -s --runtime-set testVersion 7.4-",
"lint-fix": "phpcbf .",
"test": "phpunit",
"test-single-site": "phpunit -c single-site.xml.dist",
Expand Down
68 changes: 68 additions & 0 deletions includes/classes/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ abstract class Feature {
*/
protected $is_visible = true;

/**
* Settings description
*
* @since 5.0.0
* @var array
*/
protected $settings_schema = [];

/**
* Run on every page load for feature to set itself up
*
Expand Down Expand Up @@ -503,4 +511,64 @@ public function is_available() : bool {
*/
return apply_filters( 'ep_feature_is_available', $this->is_visible && 2 !== $requirements_status->code, $this->slug, $this );
}

/**
* Get a JSON representation of the feature
*
* @since 5.0.0
* @return string
*/
public function get_json() {
$requirements_status = $this->requirements_status();

$feature_desc = [
'slug' => $this->slug,
'title' => $this->get_title(),
'shortTitle' => $this->get_short_title(),
'summary' => $this->summary,
'docsUrl' => $this->docs_url,
'defaultSettings' => $this->default_settings,
'order' => $this->order,
'isAvailable' => $this->is_available(),
'reqStatusCode' => $requirements_status->code,
'reqStatusMessages' => $requirements_status->message,
'settingsSchema' => $this->get_settings_schema(),
];
return wp_json_encode( $feature_desc );
}

/**
* Return the feature settings schema
*
* @since 5.0.0
* @return array
*/
public function get_settings_schema() {
$req_status = $this->requirements_status();

$active = [
'default' => 0 === $req_status->code,
'key' => 'active',
'label' => __( 'Enabled', 'elasticpress' ),
'requires_sync' => $this->requires_install_reindex,
'type' => 'checkbox',
];

$settings_schema = [
$active,
...$this->settings_schema,
];

/**
* Filter the settings schema of a feature
*
* @hook ep_feature_is_available
* @since 5.0.0
* @param {array} $settings_schema True if the feature is available
* @param {string} $feature_slug Feature slug
* @param {Feature} $feature Feature object
* @return {array} New $settings_schema value
*/
return apply_filters( 'ep_feature_settings_schema', $settings_schema, $this->slug, $this );
}
}
72 changes: 72 additions & 0 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public function __construct() {

$this->available_during_installation = true;

$this->set_settings_schema();

parent::__construct();
}

Expand Down Expand Up @@ -855,6 +857,76 @@ public function intercept_remote_request() {
return true;
}

/**
* Conditionally add EP.io information to the settings schema
*
* @since 5.0.0
*/
protected function maybe_add_epio_settings_schema() {
$allowed_params = $this->epio_autosuggest_set_and_get();
if ( empty( $allowed_params ) ) {
return;
}

$epio_link = 'https://elasticpress.io';
$epio_autosuggest_kb_link = 'https://elasticpress.zendesk.com/hc/en-us/articles/360055402791';
$status_report_link = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ? network_admin_url( 'admin.php?page=elasticpress-status-report' ) : admin_url( 'admin.php?page=elasticpress-status-report' );

$this->settings_schema[] = [
'default' => sprintf(
/* translators: 1: <a> tag (ElasticPress.io); 2. </a>; 3: <a> tag (KB article); 4. </a>; 5: <a> tag (Site Health Debug Section); 6. </a>; */
__( 'You are directly connected to %1$sElasticPress.io%2$s, ensuring the most performant Autosuggest experience. %3$sLearn more about what this means%4$s or %5$sclick here for debug information%6$s.', 'elasticpress' ),
'<a href="' . esc_url( $epio_link ) . '">',
'</a>',
'<a href="' . esc_url( $epio_autosuggest_kb_link ) . '">',
'</a>',
'<a href="' . esc_url( $status_report_link ) . '">',
'</a>'
),
'key' => 'epio',
'label' => __( 'Connection', 'elasticpress' ),
'type' => 'markup',
];
}

/**
* Set the `settings_schema` attribute
*
* @since 5.0.0
*/
protected function set_settings_schema() {
$this->settings_schema = [
[
'default' => '.ep-autosuggest',
'help' => __( 'Input additional selectors where you would like to include autosuggest separated by a comma. Example: .custom-selector, #custom-id, input[type="text"]', 'elasticpress' ),
'key' => 'autosuggest_selector',
'label' => __( 'Autosuggest Selector', 'elasticpress' ),
'type' => 'text',
],
[
'key' => 'trigger_ga_event',
'help' => __( 'When enabled, a gtag tracking event is fired when an autosuggest result is clicked.', 'elasticpress' ),
'label' => __( 'Google Analytics Events', 'elasticpress' ),
'type' => 'checkbox',
],
];

if ( Utils\is_epio() ) {
$this->maybe_add_epio_settings_schema();
return;
}

$set_in_wp_config = defined( 'EP_AUTOSUGGEST_ENDPOINT' ) && EP_AUTOSUGGEST_ENDPOINT;

$this->settings_schema[] = [
'readonly' => $set_in_wp_config,
'help' => $set_in_wp_config ? __( 'This address will be exposed to the public.', 'elasticpress' ) : '',
'key' => 'endpoint_url',
'label' => __( 'Endpoint URL', 'elasticpress' ),
'type' => 'url',
];
}

/**
* DEPRECATED. Delete the cached query for autosuggest.
*
Expand Down
10 changes: 5 additions & 5 deletions includes/classes/Feature/Comments/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

namespace ElasticPress\Feature\Comments;

use ElasticPress\Feature as Feature;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Indexable as Indexable;
use ElasticPress\Features as Features;
use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus;
use ElasticPress\Feature;
use ElasticPress\FeatureRequirementsStatus;
use ElasticPress\Features;
use ElasticPress\Indexable;
use ElasticPress\Indexables;
use ElasticPress\Utils;

/**
Expand Down
33 changes: 33 additions & 0 deletions includes/classes/Feature/DidYouMean/DidYouMean.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct() {
'search_behavior' => false,
];

$this->set_settings_schema();

parent::__construct();
}

Expand Down Expand Up @@ -415,4 +417,35 @@ public function the_output( $query = null ) {

echo wp_kses_post( $html );
}

/**
* Set the `settings_schema` attribute
*
* @since 5.0.0
*/
protected function set_settings_schema() {
$this->settings_schema = [
[
'default' => false,
'key' => 'search_behavior',
'label' => __( 'Search behavior when no result is found', 'elasticpress' ),
'options' => [
[
[
'label' => __( 'Display the top suggestion', 'elasticpress' ),
'value' => 0,
],
[
'label' => __( 'Display all the suggestions', 'elasticpress' ),
'value' => 'list',
],
[
'label' => __( 'Automatically redirect the user to the top suggestion', 'elasticpress' ),
'value' => 'redirect',
],
],
],
],
];
}
}
25 changes: 25 additions & 0 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public function __construct() {
}
}

$this->set_settings_schema();

parent::__construct();
}

Expand Down Expand Up @@ -644,6 +646,29 @@ public function remove_facets_filter( $filters ) {
return $filters;
}

/**
* Set the `settings_schema` attribute
*
* @since 5.0.0
*/
protected function set_settings_schema() {
$this->settings_schema[] = [
'key' => 'match_type',
'label' => __( 'Match Type', 'elasticpress' ),
'help' => __( '"All" will only show content that matches all filters. "Any" will show content that matches any filter.', 'elasticpress' ),
'options' => [
[
'label' => __( 'Show any content tagged to <strong>all</strong> selected terms', 'elasticpress' ),
'value' => 'all',
],
[
'label' => __( 'Show all content tagged to <strong>any</strong> selected term', 'elasticpress' ),
'value' => 'any',
],
],
];
}

/**
* Figure out if Facet widget can display on page.
*
Expand Down
Loading