Skip to content

Commit

Permalink
v 2.66.0
Browse files Browse the repository at this point in the history
Base Toolbox v 0.11.0 :
- Helper to convert array to HTML attributes.
- Form :
— hook content before & after
— Arg to specify each fieldset button label & classname.
— Custom event when changing fieldset.
  • Loading branch information
Darklg committed Dec 31, 2023
1 parent d494c76 commit 2e07fe7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
48 changes: 41 additions & 7 deletions inc/WPUBaseToolbox/WPUBaseToolbox.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace wpubasetoolbox_0_10_0;
namespace wpubasetoolbox_0_11_0;

/*
Class Name: WPU Base Toolbox
Description: Cool helpers for WordPress Plugins
Version: 0.10.0
Version: 0.11.0
Class URI: https://github.com/WordPressUtilities/wpubaseplugin
Author: Darklg
Author URI: https://darklg.me/
Expand All @@ -13,7 +13,7 @@
*/

class WPUBaseToolbox {
private $plugin_version = '0.10.0';
private $plugin_version = '0.11.0';
public function __construct() {
add_action('wp_enqueue_scripts', array(&$this,
'form_scripts'
Expand Down Expand Up @@ -59,6 +59,7 @@ public function get_form_html($form_id, $fields = array(), $args = array()) {

/* Start form */
$html .= '<form class="' . esc_attr($args['form_classname']) . ' wpubasetoolbox-form" id="' . esc_attr($form_id) . '" ' . ($args['wizard_mode'] ? ' data-wizard="1"' : '') . ' action="" method="post" ' . $extra_post_attributes . '>';
$html .= $args['html_before_content'];

$html_fieldset = '';
$html_wizard = '';
Expand All @@ -73,13 +74,17 @@ public function get_form_html($form_id, $fields = array(), $args = array()) {
$fieldset['label'] = $fieldset_id;
}

$html_fieldset .= '<fieldset data-fielset-id="' . $fieldset_id . '">';
$attributes = array_merge($fieldset['attributes'], array(
'data-fielset-id' => $fieldset_id
));

$html_fieldset .= '<fieldset ' . $this->array_to_html_attributes($attributes) . '>';
$html_fieldset .= $fieldset['content_before'];
if (isset($fieldset['label']) && $fieldset['label']) {
$html_fieldset .= '<legend>' . esc_html($fieldset['label']) . '</legend>';

if ($args['wizard_steps']) {
$html_wizard .= '<button type="button" data-active="'.($fieldset_num == 1 ? '1' : '0').'" data-go="' . ($fieldset_num - 1) . '"><span>' . esc_html($fieldset['label']) . '</span></button>';
$html_wizard .= '<button type="button" data-active="' . ($fieldset_num == 1 ? '1' : '0') . '" data-go="' . ($fieldset_num - 1) . '"><span>' . esc_html($fieldset['label']) . '</span></button>';
}
}
foreach ($fields as $field_name => $field) {
Expand All @@ -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 .= '<div class="form-navigation">';
if ($fieldset_num > 1) {
$html_fieldset .= '<button data-dir="prev" class="' . $args['wizard_prev_button_class'] . '" type="button"><span>' . $args['wizard_prev_button_label'] . '</span></button>';
$html_fieldset .= '<button data-dir="prev" class="' . $btn_prev_class . '" type="button"><span>' . $btn_prev_label . '</span></button>';
}
if ($fieldset_num == $nb_fieldsets) {
$html_fieldset .= $button_submit;
} else {
$html_fieldset .= '<button data-dir="next" class="' . $args['wizard_next_button_class'] . '" type="button"><span>' . $args['wizard_next_button_label'] . '</span></button>';
$html_fieldset .= '<button data-dir="next" class="' . $btn_next_class . '" type="button"><span>' . $btn_next_label . '</span></button>';
}
$html_fieldset .= '</div>';
}
Expand All @@ -124,6 +134,7 @@ public function get_form_html($form_id, $fields = array(), $args = array()) {
}
$html .= '</div>';

$html .= $args['html_after_content'];
/* End form */
$html .= '</form>';

Expand All @@ -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' => ''
)
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

}
18 changes: 14 additions & 4 deletions inc/WPUBaseToolbox/assets/form-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}));
}
}

Expand Down
6 changes: 3 additions & 3 deletions wpubaseplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +18,7 @@

class WPUBasePlugin {

public $version = '2.65.0';
public $version = '2.66.0';

private $utilities_classes = array(
'messages' => array(
Expand Down Expand Up @@ -54,7 +54,7 @@ class WPUBasePlugin {
'name' => 'WPUBaseEmail'
),
'toolbox' => array(
'namespace' => 'wpubasetoolbox_0_10_0',
'namespace' => 'wpubasetoolbox_0_11_0',
'name' => 'WPUBaseToolbox'
),
'filecache' => array(
Expand Down

0 comments on commit 2e07fe7

Please sign in to comment.