From 74ae0297797af9a5ca6dff6a1e8a2dfbd5043cf7 Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 9 Nov 2023 20:06:16 +0100 Subject: [PATCH] v 2.58.0 Toolbox v 0.4.0 : - Handle fieldsets. --- inc/WPUBaseToolbox/WPUBaseToolbox.php | 45 ++++++++++++++++++++++----- wpubaseplugin.php | 6 ++-- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index 26c48c4..91dcca4 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -1,10 +1,10 @@ __('Submit'), 'button_classname' => 'cssc-button', + 'fieldsets' => array( + 'default' => array( + 'label' => '' + ) + ), 'form_attributes' => '', 'form_classname' => 'cssc-form', 'field_box_classname' => 'box', @@ -44,15 +49,22 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { if (!is_array($args['hidden_fields']) || !isset($args['hidden_fields'])) { $args['hidden_fields'] = array(); } + if (!is_array($args['fieldsets']) || !isset($args['fieldsets'])) { + $args['fieldsets'] = array(); + } $extra_post_attributes = $args['form_attributes']; + /* Clean & check fields */ $has_file = false; - foreach ($fields as $field) { - if (isset($field['type']) && $field['type'] == 'file') { + foreach ($fields as $field_name => $field) { + $fields[$field_name] = $this->get_clean_field($field_name, $field, $form_id, $args); + if ($fields[$field_name]['type'] == 'file') { $has_file = true; } } + + /* Extra attributes */ if ($has_file) { $extra_post_attributes .= ' enctype="multipart/form-data"'; } @@ -61,8 +73,18 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { $html .= '
'; /* Insert fields */ - foreach ($fields as $field_name => $field) { - $html .= $this->get_field_html($field_name, $field, $form_id, $args); + foreach ($args['fieldsets'] as $fieldset_id => $fieldset) { + $html .= '
'; + if (isset($fieldset['label']) && $fieldset['label']) { + $html .= '' . esc_html($fieldset['label']) . ''; + } + foreach ($fields as $field_name => $field) { + if ($field['fieldset'] != $fieldset_id) { + continue; + } + $html .= $this->get_field_html($field_name, $field, $form_id, $args); + } + $html .= '
'; } /* Submit box */ @@ -83,11 +105,15 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { /* Field -------------------------- */ - public function get_field_html($field_name, $field, $form_id, $args = array()) { + public function get_clean_field($field_name, $field, $form_id, $args) { if (!is_array($field)) { $field = array(); } + if (!isset($field['fieldset']) || !array_key_exists($field['fieldset'], $args['fieldsets'])) { + $field['fieldset'] = array_key_first($args['fieldsets']); + } + $default_field = array( 'label' => $field_name, 'type' => 'text', @@ -103,6 +129,11 @@ public function get_field_html($field_name, $field, $form_id, $args = array()) { ); $field = array_merge($default_field, $field); + return $field; + } + + public function get_field_html($field_name, $field, $form_id, $args = array()) { + /* Data */ /* Values */ $field_id = $form_id . '__' . $field_name; diff --git a/wpubaseplugin.php b/wpubaseplugin.php index c4cd316..e99f383 100644 --- a/wpubaseplugin.php +++ b/wpubaseplugin.php @@ -5,7 +5,7 @@ Plugin URI: https://github.com/WordPressUtilities/wpubaseplugin Update URI: https://github.com/WordPressUtilities/wpubaseplugin Description: A framework for a WordPress plugin -Version: 2.57.3 +Version: 2.58.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpubaseplugin @@ -18,7 +18,7 @@ class WPUBasePlugin { - public $version = '2.57.3'; + public $version = '2.58.0'; private $utilities_classes = array( 'messages' => array( @@ -54,7 +54,7 @@ class WPUBasePlugin { 'name' => 'WPUBaseEmail' ), 'toolbox' => array( - 'namespace' => 'wpubasetoolbox_0_3_3', + 'namespace' => 'wpubasetoolbox_0_4_0', 'name' => 'WPUBaseToolbox' ), 'filecache' => array(