From fbaa126ec9c9b4c19b02a7207039450d53e04f24 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 7 Aug 2023 15:54:32 +0100 Subject: [PATCH] v 2.54.0 Base Toolbox v 0.1.0 --- inc/WPUBaseToolbox/README.md | 35 ++++++++ inc/WPUBaseToolbox/WPUBaseToolbox.php | 110 ++++++++++++++++++++++++++ wpubaseplugin.php | 8 +- 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 inc/WPUBaseToolbox/README.md create mode 100644 inc/WPUBaseToolbox/WPUBaseToolbox.php diff --git a/inc/WPUBaseToolbox/README.md b/inc/WPUBaseToolbox/README.md new file mode 100644 index 0000000..327af7c --- /dev/null +++ b/inc/WPUBaseToolbox/README.md @@ -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' + ) +)); +``` diff --git a/inc/WPUBaseToolbox/WPUBaseToolbox.php b/inc/WPUBaseToolbox/WPUBaseToolbox.php new file mode 100644 index 0000000..e3d460d --- /dev/null +++ b/inc/WPUBaseToolbox/WPUBaseToolbox.php @@ -0,0 +1,110 @@ +'; + 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 .= ''; + } + } + + $html .= ''; + $html .= ''; + + 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 = ''; + + /* Content */ + $html = ''; + switch ($field['type']) { + case 'textarea': + $html .= $default_label; + $html .= ''; + break; + + case 'email': + case 'url': + case 'number': + case 'text': + $html .= $default_label; + $html .= ''; + break; + default: + + } + + if ($html) { + $field_html = $html; + $html = '

'; + $html .= $field_html; + $html .= '

'; + } + + return $html; + } + +} diff --git a/wpubaseplugin.php b/wpubaseplugin.php index 8e5e6bd..c9bc14e 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.53.1 +Version: 2.54.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpubaseplugin @@ -18,7 +18,7 @@ class WPUBasePlugin { - public $version = '2.53.1'; + public $version = '2.54.0'; private $utilities_classes = array( 'messages' => array( @@ -52,6 +52,10 @@ class WPUBasePlugin { 'email' => array( 'namespace' => 'wpubaseemail_0_2_0', 'name' => 'WPUBaseEmail' + ), + 'toolbox' => array( + 'namespace' => 'wpubasetoolbox_0_1_0', + 'name' => 'WPUBaseToolbox' ) );