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

Features backwards compatibility #3728

Merged
merged 3 commits into from
Oct 29, 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
12 changes: 12 additions & 0 deletions assets/js/features/components/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
RadioControl,
SelectControl,
TextControl,
TextareaControl,
ToggleControl,
} from '@wordpress/components';
import { safeHTML } from '@wordpress/dom';
Expand Down Expand Up @@ -219,6 +220,17 @@ export default ({
/>
);
}
case 'textarea': {
return (
<TextareaControl
help={helpHtml}
label={label}
onChange={onChange}
disabled={isDisabled}
value={value}
/>
);
}
default: {
return (
<TextControl
Expand Down
36 changes: 30 additions & 6 deletions includes/classes/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function output_feature_box_summary() {
*
* @since 3.0
*/
abstract public function output_feature_box_long();
public function output_feature_box_long() {}

/**
* Create feature
Expand Down Expand Up @@ -563,11 +563,7 @@ public function get_json() {
* @return array
*/
public function get_settings_schema() {
if ( [] === $this->settings_schema && method_exists( $this, 'set_settings_schema' ) ) {
$this->set_settings_schema();
}

$req_status = $this->requirements_status();
$this->set_settings_schema();

$active = [
'default' => false,
Expand Down Expand Up @@ -595,4 +591,32 @@ public function get_settings_schema() {
*/
return apply_filters( 'ep_feature_settings_schema', $settings_schema, $this->slug, $this );
}

/**
* Default implementation of `set_settings_schema` based on the `default_settings` attribute
*
* @since 5.0.0
*/
protected function set_settings_schema() {
if ( [] === $this->default_settings ) {
return;
}

foreach ( $this->default_settings as $key => $default_value ) {
$type = 'text';
if ( in_array( $default_value, [ '0', '1' ], true ) ) {
$type = 'checkbox';
}
if ( is_bool( $default_value ) ) {
$type = 'toggle';
}

$this->settings_schema[] = [
'default' => $default_value,
'key' => $key,
'label' => $key,
'type' => $type,
];
}
}
}
61 changes: 61 additions & 0 deletions tests/php/TestFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,65 @@ public function test_ep_feature_settings_schema_filter() {
$settings_schema
);
}

/**
* Test set_settings_schema.
*
* @group feature
*/
public function test_set_settings_schema() {
$stub = $this->getMockForAbstractClass( '\ElasticPress\Feature' );
$stub->slug = 'slug';
$stub->default_settings = [
'field_1' => '0',
'field_2' => '1',
'field_3' => 'text',
'field_4' => true,
'field_5' => false,
];

$this->assertSame(
[
[
'default' => false,
'key' => 'active',
'label' => 'Enable',
'requires_feature' => false,
'requires_sync' => false,
'type' => 'toggle',
],
[
'default' => '0',
'key' => 'field_1',
'label' => 'field_1',
'type' => 'checkbox',
],
[
'default' => '1',
'key' => 'field_2',
'label' => 'field_2',
'type' => 'checkbox',
],
[
'default' => 'text',
'key' => 'field_3',
'label' => 'field_3',
'type' => 'text',
],
[
'default' => true,
'key' => 'field_4',
'label' => 'field_4',
'type' => 'toggle',
],
[
'default' => false,
'key' => 'field_5',
'label' => 'field_5',
'type' => 'toggle',
],
],
$stub->get_settings_schema()
);
}
}
Loading