From bd5d1404bfcc0fa2e1d78a8dfa3a0050ceef06bd Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 27 Oct 2023 11:21:18 -0300 Subject: [PATCH 1/3] Default implementation of set_settings_schema + textarea fields --- assets/js/features/components/control.js | 12 ++++++++ includes/classes/Feature.php | 36 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/assets/js/features/components/control.js b/assets/js/features/components/control.js index 264b39ed1..c5778af4e 100644 --- a/assets/js/features/components/control.js +++ b/assets/js/features/components/control.js @@ -8,6 +8,7 @@ import { RadioControl, SelectControl, TextControl, + TextareaControl, ToggleControl, } from '@wordpress/components'; import { safeHTML } from '@wordpress/dom'; @@ -219,6 +220,17 @@ export default ({ /> ); } + case 'textarea': { + return ( + + ); + } default: { return ( settings_schema && method_exists( $this, 'set_settings_schema' ) ) { - $this->set_settings_schema(); - } - - $req_status = $this->requirements_status(); + $this->set_settings_schema(); $active = [ 'default' => false, @@ -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` basedd 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, + ]; + } + } } From 2854e97a7e4c789d5332912d7cac5b147b859efc Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Fri, 27 Oct 2023 11:47:50 -0300 Subject: [PATCH 2/3] Fix typo --- includes/classes/Feature.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/classes/Feature.php b/includes/classes/Feature.php index 90246d0e6..05a70ab65 100644 --- a/includes/classes/Feature.php +++ b/includes/classes/Feature.php @@ -593,7 +593,7 @@ public function get_settings_schema() { } /** - * Default implementation of `set_settings_schema` basedd on the `default_settings` attribute + * Default implementation of `set_settings_schema` based on the `default_settings` attribute * * @since 5.0.0 */ From 1b573c7d8b044e3896d56f60cd708546225f9a71 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Sun, 29 Oct 2023 18:10:51 -0300 Subject: [PATCH 3/3] Unit test for set_settings_schema --- tests/php/TestFeature.php | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/tests/php/TestFeature.php b/tests/php/TestFeature.php index 502ad7975..236f22b1a 100644 --- a/tests/php/TestFeature.php +++ b/tests/php/TestFeature.php @@ -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() + ); + } }