Skip to content

Commit

Permalink
v 2.54.0
Browse files Browse the repository at this point in the history
Base Toolbox v 0.1.0
  • Loading branch information
Darklg committed Aug 7, 2023
1 parent 96730a9 commit fbaa126
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
35 changes: 35 additions & 0 deletions inc/WPUBaseToolbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
WPU Base Toolbox
---

Cool helpers for WordPress Plugins.

## Insert in the INIT hook

```php
include dirname(__FILE__) . '/inc/WPUBaseToolbox/WPUBaseToolbox.php';
$this->basetoolbox = new \myplugin\WPUBaseToolbox();
```

## Use functions


### Get form HTML

```php
echo $this->basetoolbox->get_form_html('form_edit_note', array(
'note_name' => array(
'label' => __('Note Name', '`myplugin'),
'value' => 'base value'
),
'note_content' => array(
'label' => __('Note Content', '`myplugin'),
'type' => 'textarea'
)
), array(
'button_label' => __('Send modifications', '`myplugin'),
'hidden_fields' => array(
'method' => 'edit_note',
'action' => '`myplugin_callback_crud'
)
));
```
110 changes: 110 additions & 0 deletions inc/WPUBaseToolbox/WPUBaseToolbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
namespace wpubasetoolbox_0_1_0;

/*
Class Name: WPU Base Toolbox
Description: Cool helpers for WordPress Plugins
Version: 0.1.0
Class URI: https://github.com/WordPressUtilities/wpubaseplugin
Author: Darklg
Author URI: https://darklg.me/
License: MIT License
License URI: https://opensource.org/licenses/MIT
*/

class WPUBaseToolbox {
public function __construct() {}

/* ----------------------------------------------------------
Forms
---------------------------------------------------------- */

/* Wrapper
-------------------------- */

public function get_form_html($form_id, $fields = array(), $args = array()) {
$html = '';
if (!is_array($fields) || !is_array($args)) {
return '';
}
if (!isset($args['button_label'])) {
$args['button_label'] = __('Submit');
}

$html .= '<form id="' . $form_id . '" action="" method="post">';
foreach ($fields as $field_name => $field) {
$html .= $this->get_field_html($field_name, $field, $form_id, $args);
}
if (isset($args['hidden_fields']) && is_array($args['hidden_fields'])) {
foreach ($args['hidden_fields'] as $field_id => $field_value) {
$html .= '<input type="hidden" name="' . $field_id . '" value="' . esc_attr($field_value) . '" />';
}
}

$html .= '<button type="submit"><span>' . $args['button_label'] . '</span></button>';
$html .= '</form>';

return $html;
}

/* Field
-------------------------- */

public function get_field_html($field_name, $field, $form_id, $args = array()) {
if (!is_array($field)) {
$field = array();
}

$default_field = array(
'label' => $field_name,
'type' => 'text',
'value' => '',
'required' => false
);
$field = array_merge($default_field, $field);

/* Values */
$field_id = $form_id . '__' . $field_name;
$field_id_name = ' name="' . $field_name . '" id="' . $field_id . '"';
if ($field['required']) {
$field_id_name .= ' required';
}

/* Label */
$default_label = '<label for="' . $field_id . '">';
$default_label .= $field['label'];
if ($field['required']) {
$default_label .= ' <em>*</em>';
}
$default_label .= '</label>';

/* Content */
$html = '';
switch ($field['type']) {
case 'textarea':
$html .= $default_label;
$html .= '<textarea ' . $field_id_name . '>' . htmlentities($field['value']) . '</textarea>';
break;

case 'email':
case 'url':
case 'number':
case 'text':
$html .= $default_label;
$html .= '<input ' . $field_id_name . ' type="' . $field['type'] . '" value="' . esc_attr($field['value']) . '" />';
break;
default:

}

if ($html) {
$field_html = $html;
$html = '<p data-box-name="' . $field_name . '" data-box-type="' . esc_attr($field['type']) . '">';
$html .= $field_html;
$html .= '</p>';
}

return $html;
}

}
8 changes: 6 additions & 2 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.53.1
Version: 2.54.0
Author: Darklg
Author URI: https://darklg.me/
Text Domain: wpubaseplugin
Expand All @@ -18,7 +18,7 @@

class WPUBasePlugin {

public $version = '2.53.1';
public $version = '2.54.0';

private $utilities_classes = array(
'messages' => array(
Expand Down Expand Up @@ -52,6 +52,10 @@ class WPUBasePlugin {
'email' => array(
'namespace' => 'wpubaseemail_0_2_0',
'name' => 'WPUBaseEmail'
),
'toolbox' => array(
'namespace' => 'wpubasetoolbox_0_1_0',
'name' => 'WPUBaseToolbox'
)
);

Expand Down

0 comments on commit fbaa126

Please sign in to comment.