Skip to content

Commit

Permalink
v 2.58.0
Browse files Browse the repository at this point in the history
Toolbox v 0.4.0 :
- Handle fieldsets.
  • Loading branch information
Darklg committed Nov 9, 2023
1 parent aac2bbc commit 74ae029
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
45 changes: 38 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_3_3;
namespace wpubasetoolbox_0_4_0;

/*
Class Name: WPU Base Toolbox
Description: Cool helpers for WordPress Plugins
Version: 0.3.3
Version: 0.4.0
Class URI: https://github.com/WordPressUtilities/wpubaseplugin
Author: Darklg
Author URI: https://darklg.me/
Expand All @@ -30,6 +30,11 @@ public function get_form_html($form_id, $fields = array(), $args = array()) {
$default_args = array(
'button_label' => __('Submit'),
'button_classname' => 'cssc-button',
'fieldsets' => array(
'default' => array(
'label' => ''
)
),
'form_attributes' => '',
'form_classname' => 'cssc-form',
'field_box_classname' => 'box',
Expand All @@ -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"';
}
Expand All @@ -61,8 +73,18 @@ public function get_form_html($form_id, $fields = array(), $args = array()) {
$html .= '<form class="' . esc_attr($args['form_classname']) . '" id="' . esc_attr($form_id) . '" action="" method="post" ' . $extra_post_attributes . '>';

/* 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 .= '<fieldset data-fielset-id="' . $fieldset_id . '">';
if (isset($fieldset['label']) && $fieldset['label']) {
$html .= '<legend>' . esc_html($fieldset['label']) . '</legend>';
}
foreach ($fields as $field_name => $field) {
if ($field['fieldset'] != $fieldset_id) {
continue;
}
$html .= $this->get_field_html($field_name, $field, $form_id, $args);
}
$html .= '</fieldset>';
}

/* Submit box */
Expand All @@ -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',
Expand All @@ -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;
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.57.3
Version: 2.58.0
Author: Darklg
Author URI: https://darklg.me/
Text Domain: wpubaseplugin
Expand All @@ -18,7 +18,7 @@

class WPUBasePlugin {

public $version = '2.57.3';
public $version = '2.58.0';

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

0 comments on commit 74ae029

Please sign in to comment.