diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php index 0ba88b0..8f4932a 100644 --- a/inc/WPUBaseToolbox/WPUBaseToolbox.php +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -1,10 +1,10 @@ '; + $html .= $args['html_before_content']; $html_fieldset = ''; $html_wizard = ''; @@ -73,13 +74,17 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { $fieldset['label'] = $fieldset_id; } - $html_fieldset .= '
'; + $attributes = array_merge($fieldset['attributes'], array( + 'data-fielset-id' => $fieldset_id + )); + + $html_fieldset .= '
array_to_html_attributes($attributes) . '>'; $html_fieldset .= $fieldset['content_before']; if (isset($fieldset['label']) && $fieldset['label']) { $html_fieldset .= '' . esc_html($fieldset['label']) . ''; if ($args['wizard_steps']) { - $html_wizard .= ''; + $html_wizard .= ''; } } foreach ($fields as $field_name => $field) { @@ -90,14 +95,19 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { } $html_fieldset .= $fieldset['content_after']; if ($args['wizard_mode']) { + $btn_prev_class = isset($fieldset['wizard_prev_button_class']) && $fieldset['wizard_prev_button_class'] ? $fieldset['wizard_prev_button_class'] : $args['wizard_prev_button_class']; + $btn_next_class = isset($fieldset['wizard_next_button_class']) && $fieldset['wizard_next_button_class'] ? $fieldset['wizard_next_button_class'] : $args['wizard_next_button_class']; + $btn_prev_label = isset($fieldset['wizard_prev_button_label']) && $fieldset['wizard_prev_button_label'] ? $fieldset['wizard_prev_button_label'] : $args['wizard_prev_button_label']; + $btn_next_label = isset($fieldset['wizard_next_button_label']) && $fieldset['wizard_next_button_label'] ? $fieldset['wizard_next_button_label'] : $args['wizard_next_button_label']; + $html_fieldset .= '
'; if ($fieldset_num > 1) { - $html_fieldset .= ''; + $html_fieldset .= ''; } if ($fieldset_num == $nb_fieldsets) { $html_fieldset .= $button_submit; } else { - $html_fieldset .= ''; + $html_fieldset .= ''; } $html_fieldset .= '
'; } @@ -124,6 +134,7 @@ public function get_form_html($form_id, $fields = array(), $args = array()) { } $html .= ''; + $html .= $args['html_after_content']; /* End form */ $html .= ''; @@ -140,6 +151,7 @@ public function get_clean_form_args($form_id, $fields = array(), $args = array() 'fieldsets' => array( 'default' => array( 'label' => '', + 'attributes' => array(), 'content_before' => '', 'content_after' => '' ) @@ -149,6 +161,8 @@ public function get_clean_form_args($form_id, $fields = array(), $args = array() 'field_group_classname' => 'twoboxes', 'field_box_classname' => 'box', 'submit_box_classname' => 'box--submit', + 'html_before_content' => '', + 'html_after_content' => '', 'hidden_fields' => array(), 'has_nonce' => true, 'nonce_id' => $form_id, @@ -171,6 +185,9 @@ public function get_clean_form_args($form_id, $fields = array(), $args = array() } foreach ($args['fieldsets'] as $fieldset_id => $fieldset) { $args['fieldsets'][$fieldset_id] = array_merge($default_args['fieldsets']['default'], $fieldset); + if (!is_array($args['fieldsets'][$fieldset_id]['attributes'])) { + $args['fieldsets'][$fieldset_id]['attributes'] = array(); + } } return $args; } @@ -359,4 +376,21 @@ function validate_form($source, $form_id, $fields = array(), $args = array()) { return $errors; } + /* ---------------------------------------------------------- + HTML Helpers + ---------------------------------------------------------- */ + + function array_to_html_attributes($attributes = array()) { + if (!is_array($attributes)) { + return ''; + } + + $html = ''; + foreach ($attributes as $key => $value) { + $html .= ' ' . $key . '="' . esc_attr($value) . '"'; + } + + return trim($html); + } + } diff --git a/inc/WPUBaseToolbox/assets/form-validation.js b/inc/WPUBaseToolbox/assets/form-validation.js index 0faa616..12885ca 100644 --- a/inc/WPUBaseToolbox/assets/form-validation.js +++ b/inc/WPUBaseToolbox/assets/form-validation.js @@ -76,10 +76,20 @@ function wpubasetoolbox_form_setup_wizard($form) { wpubasetoolbox_fieldset_display($fieldsets, _currentFieldset); /* Current wizard step */ - Array.prototype.forEach.call($wizardSteps, function(el) { - el.setAttribute('data-active', 0); - }); - $wizardSteps[_currentFieldset].setAttribute('data-active', 1); + if ($wizardSteps.length) { + Array.prototype.forEach.call($wizardSteps, function(el) { + el.setAttribute('data-active', 0); + }); + $wizardSteps[_currentFieldset].setAttribute('data-active', 1); + } + + /* Event */ + $form.dispatchEvent(new CustomEvent("wpubasetoolbox_form_set_fieldset", { + detail: { + id: _currentFieldset, + item: $fieldsets[_currentFieldset] + } + })); } } diff --git a/wpubaseplugin.php b/wpubaseplugin.php index d318671..9ccc5a9 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.65.0 +Version: 2.66.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpubaseplugin @@ -18,7 +18,7 @@ class WPUBasePlugin { - public $version = '2.65.0'; + public $version = '2.66.0'; private $utilities_classes = array( 'messages' => array( @@ -54,7 +54,7 @@ class WPUBasePlugin { 'name' => 'WPUBaseEmail' ), 'toolbox' => array( - 'namespace' => 'wpubasetoolbox_0_10_0', + 'namespace' => 'wpubasetoolbox_0_11_0', 'name' => 'WPUBaseToolbox' ), 'filecache' => array(