Skip to content

Commit

Permalink
Merge pull request #3728 from 10up/feature/feature-compatibility
Browse files Browse the repository at this point in the history
Features backwards compatibility
  • Loading branch information
felipeelia committed Oct 29, 2023
2 parents 47b5da5 + 1b573c7 commit e64c22a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
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()
);
}
}

0 comments on commit e64c22a

Please sign in to comment.