Skip to content

Instantly share code, notes, and snippets.

@TCotton
Created March 7, 2012 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TCotton/1996412 to your computer and use it in GitHub Desktop.
Save TCotton/1996412 to your computer and use it in GitHub Desktop.
Wordpress Options API access class
<?php
namespace OptionView;
use OptionController;
/**
* Form_View
*
* @package Wordpess Options API access class
* @author Andy Walpole
* @copyright Andy Walpole
* @link http://andywalpole.me/
* @version development
* @access public
* @license GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* add_action()
* http://codex.wordpress.org/Function_Reference/add_action
*
* add_options_page()
* http://codex.wordpress.org/Function_Reference/add_options_page
*
* screen_icon()
* http://codex.wordpress.org/Function_Reference/screen_icon
*
* wp_die()
* http://codex.wordpress.org/Function_Reference/wp_die
*
* wp_enqueue_script()
* http://codex.wordpress.org/Function_Reference/wp_enqueue_script
*
* wp_localize_script()
* http://codex.wordpress.org/Function_Reference/wp_localize_script
*
* wp_enqueue_style
* http://codex.wordpress.org/Function_Reference/wp_enqueue_style
*
* One set of radio buttons per form
* And the checkbox must have different name attribute
*
* To do:
*
* Replace strtr for preg_replace() for performance
* Replace stripos() for preg_replace() for performance
*
* Refactor methods in the model class
*
* Take a loot at remove_empty() & delete() calculations not correct on dynamic form
*/
class Form_View extends OptionController\Form_Controller {
protected static $form;
function __construct() {
/**
* The code in the Form_View constructor is essential to the functioning of
* all the script and it is not recommened that you remove them
*/
parent::__construct();
$this->add_action_admin_menu();
$args = func_get_args();
foreach ($args as $result) {
$result = array_values($result);
if (count($result) !== 4) {
wp_die("Please make sure you place the right number of values into your array");
}
list($option_name, $page_title, $page_url, $dynamic_output) = $result;
self::$form = $this->config_settings($option_name, $page_title, $page_url, $dynamic_output);
add_action('admin_enqueue_scripts', array($this, 'scripts_enqueue_cov'));
} // end foreach
} // end construct
public function scripts_enqueue_cov() {
// essential.
extract(self::$form);
if (strpos($this->find_url(), $page_url)) {
// Only display script on plugin admin page. Is there a Wordpress way of doing this?
$plugin_url = plugin_dir_url(__DIR__);
wp_enqueue_script("option_scripts", $plugin_url . "javascript/scripts.js");
if ($dynamic_output == FALSE) {
wp_localize_script("option_scripts", "option_plugin_params", get_option($option_name));
}
//wp_enqueue_style("option_styles", $plugin_url."css/styles.css");
} // emd of strpos
}
/**
* Form_View::add_action_admin_menu()
*
* Calls the Wordpress add_action() hook funciton
*
* @return calls Wordpress add_action function
*/
private function add_action_admin_menu() {
add_action('admin_menu', array($this, 'add_options_page_method_cov'));
}
/**
* Form_View::add_options_page_method_cov()
*
* callback method for add_action().
*
* @return calls wordpress add_options_page function
*/
public function add_options_page_method_cov() {
// essential.
extract(self::$form);
add_options_page('Affiliate Hoover', 'Affiliate Hoover', 'manage_options', $page_url, array
($this, 'create_html_cov'));
}
/**
* Form_View::create_html_cov()
*
* callback method for add_options_page()
*
* @return echo
*/
public function create_html_cov() {
// essential.
extract(self::$form);
$form = '<div class="wrap">';
$form .= screen_icon();
$form .= "<h2>{$page_title}</h2>";
$form .= '<p>This is the admin section for Affiliate Hoover plugin</p>';
$form .= '<div id="result">';
echo $form;
if (isset($_POST['submit'])) {
// EXAMPLE OF FORM VALIDATION AND SANITIZATION:
$error = array();
// ESSENTIAL! Do not leave this out. Needs to come first
$form = $this->security_check($_POST);
/*
$this->strip_tags_post($form, 'feedName', TRUE);
$this->sanitize($form, 'trim_post');
$this->trim_post($form, 'feedName', TRUE);
$this->sanitize($form, 'stripslashes');
$this->stripslashes($form, 'feedName', TRUE);
// EXAMPLES OF VALIDATING THE FORM DATA
// Alternatively it is possible to just sanitize individual form fields:
/*
if ($this->validate_email($form, 'feedName') === FALSE) {
$error[] = "Please make sure that the email addresses are correct";
}
if ($this->validate_url($form, 'urlName') === FALSE) {
$error[] = "URL is not right";
}
*/
/*
// Check whether there are any empty form values:
if ($this->empty_value($form) === FALSE) {
$error[] = "Please don't leave any input values empty";
}
if ($this->empty_checkboxes($form, 2) === FALSE) {
$error[] = "Please check at least two checkboxes";
}
if ($this->empty_radio_butts($form) === FALSE) {
$error[] = "Please make sure that you check one of the radio buttons";
}
// Make sure that none of the form values are duplicates
if ($this->duplicate_entries($form) === FALSE) {
$error[] = "Please make sure that all input values are unique";
}
*/
//var_dump($form);
// HOW TO DISPLAY ERROR AND SUCCESS MESSAGES
if (empty($error)) {
$this->update_option($form);
} else {
echo $this->failure_message($error);
} // end if error
} // end if isset submitForm
echo '</div>';
// EXAMPLE OF HOW TO CREATE A FORM
/*
$feedName = array(
"input" => "text", // input type
"name" => "feedName", // name attribute
"desc" => "The name of the feed:", // for use in input label
"maxlength" => "200", // max attribute
"value" => "YES", // value attribute
"select" => FALSE // array only for the select input
);
$urlName = array(
"input" => "text",
"name" => "urlName",
"desc" => "The URL of the feed:",
"maxlength" => "300",
"value" => "YES",
"select" => FALSE);
$form = array(
'method' => 'post',
'action' => '#result',
'enctype' => 'multipart/form-data',
'description' => 'Add a new feed underneath');
$this->create_form($form, $feedName, $urlName);
*/
echo '</div><!-- end of wrap div -->';
}
} // end class
/*
// EXAMPLE OF HOW TO INSTANTIATE THE CLASS
$aform = array(
'option_name' => 'a_url_here', // has to be alphanumeric and underscores only
'page_title' => 'A page title here', // Main page title
'page_url' => 'a-url-here', // URL
'dynamic_output' => FALSE); // Should the form be generated on more input
new \OptionView\Form_View($aForm);
*/
<?php
namespace OptionController;
use OptionModel;
/**
* Form_Controller
*
* @package Wordpess Options API access class
* @author Andy Walpole
* @copyright Andy Walpole
* @link http://andywalpole.me/
* @version development
* @access public
* @license GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* esc_attr()
* http://codex.wordpress.org/Function_Reference/esc_attr
*
* esc_textarea()
* http://codex.wordpress.org/Function_Reference/esc_textarea
*
*/
class Form_Controller extends OptionModel\Form_Model {
function __construct() {
parent::__construct();
} // end construct
/**
* Form_Controller::config_settings()
* Main array for important values throughout the class
* @param string $option_name
* @param string $page_title
* @param string $page_url
* @param boolean $dynamic_output
* @return array
*/
protected function config_settings($option_name, $page_title, $page_url, $dynamic_output = FALSE) {
// put together the output array
$output['option_name'] = $option_name; // name of option database field
$output['page_title'] = $page_title; // name of page
$output['page_url'] = $page_url; // url of page
$output['dynamic_output'] = $dynamic_output;
return $output;
}
/**
* Form_Controller::check_options_table()
* Checks to see if option database field is used
* @return boolean
*/
protected static function check_options_table() {
extract(static::$form);
if (get_option($option_name)) {
return TRUE;
} else {
return FALSE;
}
}
/**
* Form_Controller::create_options_fields()
*
* This method is either genius or mental
*
* Description:
* User generated field input is placed in a hidden field attached to the fields itself
* This is saved in the database with the name attribute and its value
* It is serialized but there was an error on unserialization
* Presumably, this is becuase there is a clash with Option API serialization
* So it is converted to hez using bin2hex
* However, its reverse function hex2bin is only available in PHP 5.4
* So the function is created in the model to replicated it
*
* MUST be protected and not privte for the formOne object to work
*
* @param array $field
* @return calls individual_fields() method
*
*/
protected function create_options_fields($total_fields) {
$fields = $total_fields;
// essential.
extract(static::$form);
$database = get_option($option_name);
// radio_buttons added to array if radio buttons detected
// remove to avoid error message
if (isset($database['radio_buttons'])) {
unset($database['radio_buttons']);
}
// checkboxes added to array if checkboxes detectged
// remove to avoid error message
if (isset($database['checkboxes_total'])) {
unset($database['checkboxes_total']);
}
if (self::check_options_table() && $dynamic_output && !empty($database[$option_name])) { // only create extra forms if output is dynamic
// loop through database nested array
// This is essential for removing the delete checkboxes
// They are regenerated below
foreach ($database[$option_name] as $key => $value) {
if (is_string($value)) continue;
if (preg_grep('/checkboxDeletexyz/', $value)) {
unset($database[$option_name][$key]);
} // end if
} // end foreach
// reset the array but start at one
$keys = range(1, count($database[$option_name]));
$values = array_values($database[$option_name]);
$database[$option_name] = array_combine($keys, $values);
// loop through database nested array
foreach ($database as $key => $value) {
// only use the inputs array and not any string
if (is_string($value)) continue;
foreach ($value as $b_key => $b_value) {
//eek! refactor
static $x = 1;
static $y = 1;
static $z = 1;
static $t = 1;
static $v = 1;
if (is_numeric($b_key)) {
if (is_string($b_value)) {
$fieldValue = $b_value;
} // end is_string
if ($b_key === $x++) {
if (is_string($b_value)) continue;
$field = array_values($b_value);
}
// The two separate arrays value needed to be joined together and then submitted to the individual_fields()
$user_data = array_values(unserialize($this->hex2bin($field['0'])));
// Build up the attribute value data below
// remove previously generated numbers created by the create_options_fields() method
$user_data['1'] = preg_replace('/\d$/', "", $user_data['1']);
// This is essential to make sure all name values
if ($user_data['0'] === "select") {
$user_data['4'] = $fieldValue;
$user_data['1'] = preg_replace('/kvbpy/', "", $user_data['1']);
$user_data['1'] = $user_data['1']."kvbpy";
}
// unique key for checkboxes
if ($user_data['0'] === "checkbox") {
$user_data['1'] = preg_replace('/zqxjk/', "", $user_data['1']);
$user_data['1'] = $user_data['1']."zqxjk";
}
// unique key for radio buttons
if ($user_data['0'] === "radio") {
$user_data['1'] = preg_replace('/zyxwv/', "", $user_data['1']);
$user_data['1'] = $user_data['1']."zyxwv";
}
if ($y++ % $fields === 0) {
//echo '<span class="ind-item">';
$user_data['1'] = $user_data['1'].$t++;
} else {
$user_data['1'] = $user_data['1'].$t;
}
if ($user_data['0'] !== "radio" && $user_data['0'] !== "checkbox") {
// Add previously generated user data to
$user_data['4'] = $fieldValue;
} elseif ($user_data['0'] === "radio") {
if ($user_data['4'] === $fieldValue) {
$user_data['5'] = "checked";
}
}
// This is to declare that the checked has previously been checked
if ($user_data['0'] === "checkbox") {
if ($fieldValue !== "") {
$user_data['5'] = "checked";
} else {
$user_data['5'] = "blank";
}
}
//var_dump($user_data);
// create field here
echo $this->individual_fields($user_data);
if ($z++ % $fields === 0) {
// add delete checkbox
$delete = array(
"input" => "checkbox",
"name" => "checkboxDeletexyz".($v++),
"desc" => "Delete above:",
"maxlength" => FALSE,
"value" => "1",
"select" => "blank");
// create delete checkbox here
echo $this->individual_fields($delete);
//echo "</span>";
} // end === 0
} // end is_numeric
} // end foreach
} // end foreach
} // end if
}
/**
* Form_Controller::create_form()
*
* @param array $array
* @return echo
*/
protected function create_form($array = array()) {
// essential.
extract(static::$form);
// validation to make sure that parameters are correct
$arg_list = func_get_args();
// total number of arrays entered as a parameter = indicates the number of fields the user wants
// $total_arrays array is used below
// Minus the the initial form array
$total_arrays = count($arg_list) - 1;
$form_elms = array_values($arg_list['0']);
$database = get_option($option_name);
//var_dump($option_name);
if (count($form_elms) !== 4) {
die("Make sure you enter four values in the form builder array");
}
foreach ($form_elms as $key => $value) {
static $i = 1;
switch ($key) {
case 0:
case 1:
case 2:
case 3:
if (!is_string($value) && $value != NULL) {
die("Make sure that all form parameters are a string");
}
break;
case 0:
if ($value != "post" || $value != "get") {
die("The only form methods allowed are post or get");
}
break;
case 2:
if ($value != "application/x-www-form-urlencoded" || $value !=
"multipart/form-data" || $value != "text/plain") {
die("Check the value you used for enctype in the create_form() method");
}
break;
} // end switch
} // end foreach
unset($arg_list['0']);
$field_list = array();
$field_list = array_values($arg_list);
extract(static::$form);
// create form here
$data = get_option($option_name);
$form = NULL;
$form .= "<form method=\"$form_elms[0]\" ";
// add form attributes depending on the values of the parameters
$form .= " action=\"$form_elms[1]\"";
$form .= " name=\"$option_name\"";
if ($form_elms['2'] != NULL) {
$form .= ' enctype="'.$form_elms['2'].'" ';
}
$form .= ">";
echo $form;
$database = self::check_options_table();
if ($database && $dynamic_output && !empty($data[$option_name])) {
$form = '<fieldset class="heremydear">';
$form .= "<legend><span>From the database</span></legend>";
$form .= '<table class="form-table">';
$form .= '<tbody>';
echo $form;
$this->create_options_fields($total_arrays);
$form = '</tbody>';
$form .= '</table>';
$form .= '</fieldset>';
$form .= '<p>&nbsp;</p>';
echo $form;
} // and if
$form = '<fieldset>';
$form .= "<legend><span>$form_elms[3]</span></legend>";
$form .= '<table class="form-table">';
$form .= '<tbody>';
// Create individual fields from the arrays entered into this method
$x = count($field_list);
for ($i = 0; $i <= $x - 1; $i++) {
if (sizeof($field_list[$i]) === 6) {
$form .= $this->individual_fields($field_list[$i]);
} else {
die("Please make sure that you use 6 arguments in the create_form() method fields arrays");
} // end if
} // end forloop
$form .= '</tbody>';
$form .= '</table>';
// permenant settings for every form
$form .= $this->perm_fields($total_arrays);
$form .= '<p class="submit"><input type="submit" id="submit" name="submit" class="button-primary" value="Save Changes"></p>';
$form .= '</fieldset>';
$form .= '</form>';
echo $form;
}
/**
* Form_Controller::perm_fields()
*
* @param string $total_fields
* @return string $perm
*/
private function perm_fields($total_fields) {
extract(static::$form);
$perm = '<input type="hidden" name="option_page" value="';
$perm .= $option_name;
$perm .= '">';
$perm .= '<input type="hidden" name="total_user_fields" value="'.$total_fields.'">';
$perm .= wp_nonce_field("options_form_cov", "_wpnonce_options_cov", TRUE, FALSE);
return $perm;
}
/**
* Form_Controller::individual_fields()
* Create the individual form fields here
*
* @param array $array
* @return return HTML field
*/
private function individual_fields($array = array()) {
// create an array out of the parameter values
$default = array(
1 => 'type',
2 => 'name',
3 => 'desc',
4 => 'maxlength',
5 => 'value',
6 => 'select',
);
$fields_essentials = array_combine($default, $array);
// above combines the default array with the user input data to create a multidimensial array with
// form attribute names and values
extract(static::$form);
foreach ($fields_essentials as $key => $value) {
// There needs to be a dynamic number to keep the id unique so that the HTML validates
static $i = 0;
$i++;
$value = trim((string )$value);
if ($key === "type") {
switch ($value) {
case 'text':
// text area
//make sure name values are unique - if not throw error
$form = 'input type="text" ';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
$form .= " name=\"{$option_name}[{$value}]\" ";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
$form .= " class=\"regular-text \" ";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? $form .= " maxlength=\"{$value}\" " : NULL;
}
if ($key === "value") {
if ($value != NULL && $value != "YES") {
$form .= " value=\"{$value}\" ";
}
if ($value == "YES") {
$form .= ' value="';
$form .= isset($_POST[$option_name][$name]) ? esc_attr(stripslashes
($_POST[$option_name][$name])) : NULL;
$form .= '"';
}
} // end if $key
} // end foreach
$text = '<tr>';
$text .= "<th><label for=\"$id\">$desc</label></th>";
$text .= '<td><'.$form.' ></td>';
$text .= '<td>';
if ($dynamic_output) {
$text .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\" >";
}
$text .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_type]\" value=\"text\"></td>";
$text .= '</tr>';
return $text;
break;
case 'textarea':
// textarea technically not input
$form = 'textarea ';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
$form .= " name=\"{$option_name}[{$value}]\" ";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
$form .= " class=\"large-text \" ";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? $form .= " maxlength=\"{$value}\" " : NULL;
}
if ($key === "value") {
if ($value != NULL && $value != "YES") {
$textareaValue = $value;
}
if ($value == "YES") {
$textareaValue = isset($_POST[$option_name][$name]) ? esc_attr(stripslashes
($_POST[$option_name][$name])) : NULL;
}
} // end if $key
} // end foreach
$textarea = '<tr>';
$textarea .= "<th><label for=\"$id\">$desc</label></th>";
$textarea .= '<td><'.$form.' cols="50" rows="10">'.$textareaValue.
'</textarea></td>';
$textarea .= '<td>';
if ($dynamic_output) {
$textarea .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\" >";
}
$textarea .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_type]\" value=\"textarea\"></td>";
$textarea .= '</tr>';
return $textarea;
break;
case 'checkbox': // checkbox input
$form = 'input type="checkbox" ';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
if (!preg_match("/zqxjk/", $value)) {
$value = $value."zqxjk"; // checkbox attribute names need unique identifier
}
$form .= " name=\"{$option_name}[{$value}]\" ";
$form_name = "{$option_name}[{$value}]";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? $form .= " maxlength=\"{$value}\" " : NULL;
}
if ($key === "value") {
if ($value != NULL && $value != "YES") {
$form .= " value=\"$value\" ";
}
$form_value = $value;
} // end if
// if form has been previusly checked then select will be set to TRUE
if ($key === "select") {
if ($value == NULL) {
die("You must specify the number of checkboxes in the select array key");
}
// set total number of checkboxes as specified by the user in the form field array in views
if (preg_match("/^([0-9]+)$/", $value)) {
$total = $value;
}
if (!isset($_POST[$option_name][$name])) {
if ($value === "checked") {
$form .= ' checked="checked" ';
}
}
if (isset($_POST[$option_name][$name]) && $_POST[$option_name][$name]
=== $form_value) {
$form .= ' checked="checked" ';
}
}
} // endforeach
$checkbox = '<tr>';
$checkbox .= "<th scope=\"row\">$desc</th>";
$checkbox .= "<td><fieldset><input type=\"hidden\" name=\"$form_name\" value=\"\"><label for=\"$id\">";
$checkbox .= "<".$form." >";
$checkbox .= "<span class=\"screen-reader-text\">$desc</span></label>";
if ($dynamic_output) {
$checkbox .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\" >";
}
$checkbox .= "<input type=\"hidden\" name=\"{$option_name}[$i][input_type]\" value=\"checked\">";
$checkbox .= "<input type=\"hidden\" name=\"{$option_name}[$i][checkbox_type]\" value=\"$name\">";
$checkbox .= "<input type=\"hidden\" name=\"{$option_name}[{$i}][checkbox_number]\" value=\"";
$checkbox .= isset($total) ? $total : NULL;
$checkbox .= '">';
$checkbox .= '</fieldset></td>';
$checkbox .= '</tr>';
return $checkbox;
break;
case 'radio': // radio input
$form = 'input type="radio" ';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
if (!preg_match("/zyxwv/", $value)) {
$value = $value."zyxwv"; // radio button attribute names need unique identifier
}
$form .= " name=\"{$option_name}[{$value}]\" ";
$form_name = "{$option_name}[{$value}]";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
$form .= " class=\"tog \"";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? NULL : NULL;
}
if ($key === "value") {
if ($value != NULL && $value != "YES") {
$form .= " value=\"$value\" ";
}
if (isset($_POST[$option_name][$name]) && $_POST[$option_name][$name] ==
$value) {
$form .= ' checked="checked" ';
} else {
$form .= '';
}
} // end if
// if form has been previusly checked then select will be set to TRUE
if ($key === "select") {
if ($value != "") {
if (!preg_match("/kvbpy/", $value)) {
$value = $value."kvbpy"; // radio button attribute names need unique identifier
}
if ($value !== "checked") {
$total = (int)$value;
} elseif ($value === "checked") {
$form .= ' checked="checked" ';
}
} else {
die("You must specify the number of radio buttons in the form");
}
}
} // endforeach
$radio = '<tr>';
$radio .= "<th scope=\"row\">$desc</th>";
$radio .= "<td><label for=\"$id\"><".$form." >";
$radio .= "<span class=\"screen-reader-text\">$desc</span></label>";
if ($dynamic_output) {
$radio .= "<input type=\"hidden\" name=\"{$option_name}[{$i}][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\" >";
}
$radio .= '</td>';
$radio .= "<td><input type=\"hidden\" name=\"{$option_name}[{$i}][field_type]\" value=\"radio\">";
$radio .= "<input type=\"hidden\" name=\"{$option_name}[{$i}][radio_number]\" value=\"";
$radio .= isset($total) ? $total : NULL;
$radio .= '">';
$radio .= "</td>";
$radio .= '</tr>';
return $radio;
break;
/*
case 'file': // file input
$form = 'input type="file" ';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
$form .= " name=\"{$option_name}[{$i}][{$value}]\" ";
$form_name = " name=\"{$option_name}[{$i}][{$value}]\" ";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? NULL : NULL;
}
if ($key === "value") {
if ($value != NULL && $value != "YES") {
NULL;
}
} // end if
} // endforeach
$file = '<tr>';
$file .= "<th><label for=\"$id\">$desc</label></th>";
$file .= '<td><'.$form.' ></td>';
$file .= "<td><input type=\"hidden\" name=\"{$option_name}[{$i}][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\" ></td>";
$file .= '</tr>';
return $file;
break;
*/
case 'select': // select stuff here
$form = 'select';
foreach ($fields_essentials as $key => $value) {
if ($key === "name") {
if ($value != NULL) {
$form .= " name=\"{$option_name}[{$value}]\" ";
$name = $value;
$id = $name.'-'.$i;
$form .= " id=\"$id\" ";
} else {
die("You must provide a value for the name attribute");
}
}
if ($key === "desc") {
$desc = (string )$value;
}
if ($key === "maxlength") {
($value != NULL) ? NULL : NULL;
}
if ($key === "value") {
if (preg_match('/^(select)([0-9]+)$/', $value)) {
preg_match('/([0-9]+)$/', $value, $match);
$selected = (int)array_pop($match);
}
} // end if
if ($key === "select") {
$output = array();
$output[] = '<option value="0"></option>';
foreach ($value as $key => $value) {
$key = $key + 1;
if (isset($_POST[$option_name][$name])) {
preg_match('/([0-9]+)$/', $_POST[$option_name][$name], $match);
$new_match = (int)array_pop($match);
if ($new_match === $key) {
$output[] = "<option selected=\"selected\" value=\"select$key\">$value</option>";
} else {
$output[] = "<option value=\"select$key\">$value</option>";
}
}
if (!isset($_POST[$option_name][$name])) {
if ($dynamic_output && isset($selected)) {
if ($key === $selected) {
$output[] = "<option selected=\"selected\" value=\"select$key\">$value</option>";
} else {
$output[] = "<option value=\"select$key\">$value</option>";
}
} else {
$output[] = "<option value=\"select$key\">$value</option>";
}
}
} // end foreach
} // end if select
} // endforeach
$select = '<tr>';
$select .= "<th scope=\"row\">";
$select .= "<label for=\"{$id}\">{$desc}</label>";
$select .= "</th>";
$select .= "<td>";
$select .= "<".$form.">";
foreach ($output as $result) {
$select .= $result;
}
$select .= "</select>";
if ($dynamic_output) {
$select .= "<input type=\"hidden\" name=\"{$option_name}[{$i}][input_gen]\" value=\"".
bin2hex(serialize($fields_essentials))."\">";
}
$select .= "<input type=\"hidden\" name=\"{$option_name}[{$i}][field_type]\" value=\"select\">";
$select .= '</td>';
$select .= '</tr>';
return $select;
break;
case 'button':
case 'image':
case 'password':
case 'reset':
case 'submit':
case 'file': // error here - there form inputs are not cattered for
die("You cannot use the individual_fields() method to create inputs for $key");
break;
default: // error message here
die("Make sure the input type in the individual_fields() method is correct");
break;
} // end switch statement
} // end fi
} // end foreach
}
}
<?php
namespace OptionModel;
/**
* Form_Model
*
* @package Wordpess Options API access class
* @author Andy Walpole
* @copyright Andy Walpole
* @link http://andywalpole.me/
* @version development
* @access public
* @license GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* Wordpress functions:
*
* sanitize_text_field()
* http://codex.wordpress.org/Data_Validation
*
* is_email()
* http://codex.wordpress.org/Function_Reference/is_email
*
* wp_strip_all_tags()
* http://codex.wordpress.org/Data_Validation
*
*/
class Form_Model {
function __construct() {
} // end construct
/**
* Form_Model::method_args_validation()
*
* @param digit $number
* @param function: func_num_args() $args
* @param string $method
* @return boolean
*/
private function method_args_validation($number, $args, $method) {
if ($args > (int)$number) {
die("Please make sure that you place the right number of arguments into the $method method");
}
}
/**
* Form_Model::sanitize()
*
* @param string $handle
* @param array $form_output
* @return array
*/
protected function sanitize(&$form_output, $handle) {
$this->method_args_validation(2, func_num_args(), "sanitize");
switch ($handle) {
case 'sanitize_post':
array_walk_recursive($form_output, array($this, 'sanitize_post'));
break;
case 'trim_post':
array_walk_recursive($form_output, array($this, 'trim_post'));
break;
case 'strip_tags_post':
array_walk_recursive($form_output, array($this, 'strip_tags_post'));
break;
case 'empty_value':
array_walk_recursive($form_output, array($this, 'empty_value'));
break;
case 'stripslashes':
array_walk_recursive($form_output, array($this, 'stripslashes'));
break;
default:
die("The value you ented into the sanitize() method is not recognised: $handle");
break;
} // end switch
}
/**
* Form_Model::trim_post()
*
* @param string $att
* @param string $single
* @param array $form_output
* @return array
*/
function trim_post(&$form_output, $att = null, $single = null) {
if ($single == null) {
if (is_array($form_output)) {
array_walk_recursive($form_output, 'trim');
} else {
$form_output = trim($form_output);
}
} else {
extract(static::$form);
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if (is_string($thisKey)) {
$form_output[$option_name][$thisKey] = trim($result);
}
}
}
return $form_output;
}
}
/**
* Form_Model::sanitize_post()
*
* @param string $att
* @param string $single
* @param array $form_output
* @return array
*/
protected function sanitize_post(&$form_output, $att = null, $single = null) {
if ($single == null) {
if (is_array($form_output)) {
array_walk_recursive($form_output, 'sanitize_text_field');
} else {
$form_output = sanitize_text_field($form_output);
}
} else {
extract(static::$form);
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if (is_string($thisKey)) {
$form_output[$option_name][$thisKey] = sanitize_text_field($result);
}
}
} // end foreach
return $form_output;
}
}
/**
* Form_Model::strip_tags_post()
*
* @param string $att
* @param boolean $single
* @param array $form_output
* @return array $form_output
*/
protected function strip_tags_post(&$form_output, $att = null, $single = null) {
if ($single == null) {
if (is_array($form_output)) {
array_walk_recursive($form_output, 'wp_strip_all_tags');
} else {
$form_output = wp_strip_all_tags($form_output);
}
} else {
extract(static::$form);
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if (is_string($thisKey)) {
$form_output[$option_name][$thisKey] = wp_strip_all_tags($result);
}
}
} // end foreach
return $form_output;
}
}
/**
* Form_Model::stripslashes()
*
* @param string $att
* @param string $single
* @param array $form_output
* @return array
*/
protected function stripslashes(&$form_output, $att = null, $single = null) {
if ($single == null) {
if (is_array($form_output)) {
array_walk_recursive($form_output, 'stripslashes_deep');
} else {
$form_output = stripslashes($form_output);
}
} else {
extract(static::$form);
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if (is_string($thisKey)) {
$form_output[$option_name][$thisKey] = stripslashes($result);
}
} // end if
} // end foreach
return $form_output;
}
}
/**
* Form_Model::validate_email()
*
* @param string $att
* @return boolean
*/
protected function validate_email($form_output, $att) {
extract(static::$form);
if (is_array($form_output) && is_string($att)) {
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if ($result !== "") {
if (!filter_var($result, FILTER_VALIDATE_EMAIL)) {
return FALSE;
}
} // end if
} // end if
} // end foreach
} else {
die("Make sure that the inputs for validate_url() is an array and a string");
}
}
/**
* Form_Model::validate_url()
*
* @param string $url
* @return boolean
*/
protected function validate_url($form_output, $att) {
extract(static::$form);
if (is_array($form_output) && is_string($att)) {
foreach ($form_output[$option_name] as $thisKey => $result) {
if (preg_match("/$att/i", $thisKey)) {
if ($result !== "") {
if (!filter_var($result, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
return FALSE;
}
} // end if
} // end if
} // end foreach
} else {
die("Make sure that the inputs for validate_url() is an array and a string");
}
}
/**
* Form_Model::duplicate_entries()
*
* Checks to make sure all array values are unique
*
* For an explanation of this code read my blog post here: http://www.suburban-glory.com/blog?page=152
*
* @param array $array
* @return boolean
*/
protected function duplicate_entries($array) {
extract(static::$form);
$tmp = array();
foreach ($array[$option_name] as $key => $value) {
// root out radio buttons
if (preg_match("/zyxwv/", $key)) continue;
// remove checkboxes from the loop
if (preg_match("/zqxjk/", $key)) continue;
// remove select options
if (preg_match("/kvbpy/", $key)) continue;
if (is_string($key) && !empty($value)) {
$tmp[] = $value;
}
} // end foreach
if (count($tmp) !== count(array_unique($tmp))) {
return FALSE;
}
}
/**
* Form_Model::empty_radio_butts()
*
* Checks for empty radio buttons
* Has to be a separate form due to issues around how php handles the name attribute
*
* @param array $array
* @return boolean
*/
protected function empty_radio_butts($form_output) {
// essential
extract(static::$form);
$result = FALSE;
$database = get_option($option_name);
if (static::check_options_table() && $dynamic_output && !empty($database[$option_name])) {
// space here if necessary for dynamic forms
} else {
/**
* Run through the name inputs
* If the name imput is never present for the specified radio button
* This means that none of them have been checked
* One radio button has to be checked to allow submission
*/
foreach ($form_output[$option_name] as $key => $value) {
if (preg_match("/zyxwv/", $key)) {
$result = TRUE;
}
}
if (!$result) {
return FALSE;
}
}
}
protected function empty_checkboxes($form_output, $digit = 1) {
// essential
extract(static::$form);
$result = FALSE;
$database = get_option($option_name);
$total_checkboxes = 0;
if (static::check_options_table() && $dynamic_output && !empty($database[$option_name])) {
// space here if necessary for dynamic forms
// find the total number of checkboxes
foreach ($form_output[$option_name] as $key => $value) {
if (is_string($value)) continue;
if (!isset($value['checkbox_number'])) continue;
if ($value['checkbox_number'] === "") continue;
$total_checkboxes = (int)$value['checkbox_number'];
break;
} // end foreach
// find the total number of individual form blocks - does not include original form
foreach ($form_output[$option_name] as $key => $value) {
if (is_integer($key)) continue;
// remove delete checkbox
if (preg_match('/xyz/', $key)) continue;
// only checkboxes with digits on the end
if (!preg_match('/zqxjk([0-9]+)$/', $key)) continue;
$number = $key;
} // end foreach
preg_match('/([0-9]+)$/', $number, $match);
$match = (int)array_pop($match);
$error = array();
foreach ($form_output[$option_name] as $key => $value) {
// remove delete checkbox
if (preg_match('/xyz/', $key)) continue;
if (is_array($value)) continue;
// only checkboxes
if (!preg_match('/zqxjk/', $key)) continue;
static $x = 1;
if ($value === "") {
$error[] = $x++;
}
} // end foreach
// total 0 values minus original empty form
$total_null = (array_pop($error) - $total_checkboxes);
// find total number of checkboxes minus the original empty form
$total_checks = ($match * $total_checkboxes);
// now do the maths and work out which
if ($total_null > ($total_checks - $digit)) {
return FALSE;
}
} else {
/**
* Run through the name inputs
* If the name imput is never present for the specified radio button
* This means that none of them have been checked
* One radio button has to be checked to allow submission
*/
$result = array();
$total = array();
foreach ($form_output[$option_name] as $key => $value) {
static $x = 1;
static $y = 1;
if (preg_match("/zqxjk/", $key)) {
if ($value !== "") {
$result[] = $y++;
} // end if
}
} //foreach
if ((array_pop($result)) < (int)$digit) {
return FALSE;
}
}
}
/**
* Form_Model::empty_value()
*
* Checks if form fields are empty
*
* Will only work form arrays that only include input or textarea
* Radio, select and checkboxs have to be invididually processed with a string value
*
* @param mixed $form_output
* @return boolean
*/
protected function empty_value($form_output, $single = null) {
extract(static::$form);
$database = static::check_options_table();
$output = (int)$form_output['total_user_fields'];
$data = get_option($option_name);
$total_inputs = array();
$total_arrays = array();
$total_checkboxes = 0;
$total_radio_buttons = 0;
// This is a repeat of code. Refactor it
// if new form without option database created yet make sure ALL fields are not empty
foreach ($form_output[$option_name] as $n_key => $n_value) {
// find the total amount of individual checkboxes per form block
if (is_array($n_value) && isset($n_value['checkbox_number']) && $n_value['checkbox_number']
!== "") {
$total_checkboxes = (int)$n_value['checkbox_number'];
}
// find the total amount of individual radio buttons per form block
if (is_array($n_value) && isset($n_value['radio_number']) && $n_value['radio_number']
!== "") {
$total_radio_buttons = (int)$n_value['radio_number'];
}
static $x = 1;
static $z = 0;
if (is_string($n_value)) {
// remove delete checkbox
if (preg_match('/xyz/', $n_key)) continue;
// remove regular checkboxes
if (preg_match('/zqxjk/', $n_key)) continue;
// remove radio buttons
if (preg_match('/zyxwv/', $n_key)) continue;
// the total inputs are fluid depending if the user has checked the delete box
// The only TRUE way to determine the number is to access it here
$total_inputs[] = $x++;
} // end is_string
if (is_array($n_value)) {
// the total inputs are fluid depending if the user has checked the delete box
// The only TRUE way to determine the number is to access it here
$total_arrays[] = $z++;
} // end is_string
} // end foreach loop
$total = (array_pop($total_inputs) - ($output - $total_checkboxes - $total_radio_buttons));
if ($dynamic_output) {
if ($database && !empty($data[$option_name])) {
if ($single == null) {
// if entire form is entered
// if new form without option database created yet make sure ALL fields are not empty
foreach ($form_output[$option_name] as $n_key => $n_value) {
// remove delete checkbox
if (preg_match('/xyz/', $n_key)) continue;
// remove regular checkboxes
if (preg_match('/zqxjk/', $n_key)) continue;
// remove radio buttons
if (preg_match('/zyxwv/', $n_key)) continue;
if (is_string($n_value)) {
//This is to prevent checking for empty the bottom form
static $c = 0;
if ($c++ < $total) {
if (is_string($n_value)) {
if (empty($n_value)) {
return FALSE;
} // end if
} // end is_string
} // end if
} // end if
} // end foreach loop
} else {
// if only single form input entered
foreach ($form_output[$option_name] as $key => $n_value) {
if (preg_match("/$single/i", $key)) {
if (empty($n_value)) {
return FALSE;
} // end if
} // end if
} // end foreach
} // end if single
} else {
if ($this->empty_non_dynamic($form_output, $single) === FALSE) {
return FALSE;
}
} // end if $database
// if $dynamic_output is set to FALSE
} else {
if ($this->empty_non_dynamic($form_output, $single) === FALSE) {
return FALSE;
}
} // end if $dynamic_output
}
/**
* Form_Model::empty_non_dynamic()
*
* Checks if form fields are empty if no dynamic is set
*
* A private method to work with empty_value() only
*
* @param array $form_output
* @param single $single
* @return boolean
*/
private function empty_non_dynamic($form_output, $single) {
extract(static::$form);
if ($single === null) {
// if new form without option database created yet make sure ALL fields are not empty
foreach ($form_output[$option_name] as $n_key => $n_value) {
// remove delete checkbox
if (preg_match('/xyz/', $n_key)) continue;
// remove regular checkboxes
if (preg_match('/zqxjk/', $n_key)) continue;
// remove radio buttons
if (preg_match('/zyxwv/', $n_key)) continue;
if (is_string($n_value)) {
if (empty($n_value)) {
return FALSE;
} // end if
} // end is_string
} // end foreach loop
} else {
foreach ($form_output[$option_name] as $key => $n_value) {
if (preg_match("/$single/i", $key)) {
if (empty($n_value)) {
return FALSE;
} // end if
} // end if
} // end foreach
} // end if not single
}
/**
* Form_Model::update_option()
*
* Updates databass. Includes important remove_empty() method
*
* @param array $form
* @return boolean
*/
protected function update_option($form) {
//essential
extract(static::$form);
$this->remove_empty($form);
$this->delete($form);
if (update_option($option_name, $form)) {
return $this->success_message("You have successfully updated the form");
}
}
/**
* Form_Model::success_message()
*
* @param mixed $message
* @return
*/
protected function success_message($message) {
//essential
extract(static::$form);
// necessary for javascript form values zero to work
if ($dynamic_output) {
setcookie("_multi_cov", $option_name, time() + 60);
}
$html = '<div id="message" class="updated">';
if (is_array($message)) {
foreach ($message as $line) {
$html .= '<p><strong>'.$line.'</strong></p>';
}
} else {
$html .= '<p><strong>'.$message.'</strong></p>';
} // end if
$html .= '</div>';
return $html;
}
/**
* Form_Model::failure_message()
*
* @param mixed $message
* @return
*/
protected function failure_message($message) {
//essential
extract(static::$form);
$html = '<div id="message" class="error">';
if (is_array($message)) {
foreach ($message as $line) {
$html .= '<p><strong>'.$line.'</strong></p>';
}
} else {
$html .= '<p><strong>'.$message.'</strong></p>';
} // end if
$html .= '</div>';
return $html;
}
/**
* Form_Model::remove_empty()
*
* Necessary for not including empty HTML fields in the database update if dynamic options is set to TRUE
* If this method is not used then unnessecary fields will become part of the option database field
*
* The reason this is complicated code is because the array is an irregular mix of strings and nested arrays
* There needs a method to delete both from the array before it is submitted to the database
*
* @param array $form_output
* @return array
*/
protected function remove_empty(&$form_output) {
extract(static::$form);
$database = get_option($option_name);
$output = (int)$form_output['total_user_fields'];
$fields = count($form_output[$option_name]);
$unset = FALSE;
$new_key = array();
$total_inputs = array();
$total_arrays = array();
$radio = FALSE;
if (static::check_options_table() && $dynamic_output && !empty($database[$option_name])) {
// if new form without option database created yet make sure ALL fields are not empty
foreach ($form_output[$option_name] as $n_key => $n_value) {
// need to take into caculations whether radio buttons are used
// the extra non-checked radio buttons need to be added the to the final totals
// Only one radio button will ever be checked, so the remained are left
if (preg_match("/zyxwv/", $n_key)) {
$form_output['radio_buttons'] = TRUE;
$radio[] = TRUE;
}
static $x = 0;
static $z = 0;
if (is_string($n_value)) {
$x++;
// the total inputs are fluid depending if the user has checked the delete box
// The only TRUE way to determine the number is to access it here
$total_inputs[] = $x;
} // end is_string
if (is_array($n_value)) {
$z++;
// the total inputs are fluid depending if the user has checked the delete box
// The only TRUE way to determine the number is to access it here
$total_arrays[] = $z;
} // end is_string
} // end foreach loop
// previously was total_inputs
$total = array_pop($total_arrays) - $output;
$total_minus = array_pop($total_arrays) - $output;
if (!empty($radio)) {
foreach ($form_output[$option_name] as $key => $value) {
var_dump($key);
if (is_string($value)) continue;
if (!isset($value['radio_number'])) continue;
if ($value['radio_number'] == null) continue;
if (preg_match("/^\d$/", $key)) continue;
$number = (int)$value['radio_number'];
break;
} // end foreach
$form_output[$option_name] = array_reverse($form_output[$option_name]);
$total_empties = array();
$t = null;
foreach ($form_output[$option_name] as $key => $value) {
if (!is_array($value)) {
static $t = 1;
if ($value !== "") {
// if values are not as above then they have content
// if they are as above or in the case of radio buttons not set at all
// then that means
static $n = 1;
$total_empties[] = $n++;
}
}
if ($t++ === $output) break;
} // end foreach
if (empty($total_empties)) {
// if the array is empty then all the input fields including radion buttons are empty
$unset = TRUE;
}
if ($unset === TRUE) {
// total invididual number of arrays to be deleted are
// total number of individual radio button fields
// plus the complete field arrays minus above * 2 remainder.
// This is because every non-radio button has two arrays associated with it
$total_ars = $number + (($output - $number) * 2);
// remove empty form from entire array
array_splice($form_output[$option_name], 0, $total_ars, null);
// on successful completion rearrange array to previous order but without unwanted fields
$form_output[$option_name] = array_reverse($form_output[$option_name]);
return $form_output;
} else {
// if not TRUE then put the array back to how it was before;
$form_output[$option_name] = array_reverse($form_output[$option_name]);
return $form_output;
}
// beginning of if not $radio - no radio buttons in the form submit process
} elseif (empty($radio)) {
foreach ($form_output[$option_name] as $n_key => $n_value) {
static $i = 1;
static $y = 0;
static $b = 0;
if (is_string($n_value)) {
// don't allow checkboxes to be submitted
//if (empty($n_value)) continue;
if ($i++ >= $total) {
if (empty($n_value)) {
$y++;
$new_key[] = $y;
}
if (array_pop($new_key) === $output) {
$unset = TRUE;
} // end if
} // end if
} // end if
} // end foreach
// if unset then make sure the unwanted arrays and strings are removed from the parent array before submission to the database
if ($unset === TRUE) {
foreach ($form_output[$option_name] as $n_key => $n_value) {
static $c = 0;
static $f = 0;
if (is_string($n_value)) {
if ($c++ >= $total) {
unset($form_output[$option_name][$n_key]);
} // end if
} // end if
if (is_array($n_value)) {
if ($f++ >= $total_minus) {
unset($form_output[$option_name][$n_key]);
} // end if
}
} // end foreach
} // end if unset
return $form_output;
} // if not $radio
} else {
return $form_output;
} // end if ($dynamic_output)
}
/**
* Form_Model::delete()
*
* Deletes data before submission to database if the checkbox is checked
* Unsets array items and then rebuilds array with fresh index
*
* @param array $form
* @return array $form
*/
protected function delete(&$form_output) {
// essential.
extract(static::$form);
$database = get_option($option_name);
if (static::check_options_table() && $dynamic_output && !empty($database[$option_name])) {
$delete = null;
$output = (int)$form_output['total_user_fields'];
$total_arrays = array();
$delete = array();
$radio = array();
// if new form without option database created yet make sure ALL fields are not empty
foreach ($form_output[$option_name] as $n_key => $n_value) {
if (preg_match("/zyxwv/", $n_key)) {
$radio[] = TRUE;
}
static $x = 0;
$x++;
$total_arrays[] = $x;
} // end foreach loop
$total_elements = array_pop($total_arrays);
$this->reset_array($form_output[$option_name]);
// Find any button
foreach ($form_output[$option_name] as $result => $value) {
if ($value === "1") {
$delete[] = $result;
}
} // end foreach
if ($radio && (isset($form_output['radio_buttons']) && $form_output['radio_buttons'] === TRUE)) {
if ($delete) {
// find the total number of radio buttons in the form
foreach ($form_output[$option_name] as $key => $value) {
if (is_string($value)) continue;
if (!isset($value['radio_number'])) continue;
if ($value['radio_number'] == null) continue;
$number = (int)$value['radio_number'];
break;
} // end foreach
foreach ($delete as $n_delete) {
//Work out max top and bottom keys to delete
$y_delete = $n_delete + 1;
$t_element = (int)$y_delete;
$b_element = $t_element - (int)(($output * 2) + 2) + ($number - 1); // include missing radio buttons in calcs
// use slice to remove unwanted forms from parent array
array_splice($form_output[$option_name], $b_element, $t_element, null);
} // end foreach
}
}
if (!$radio) {
if ($delete) {
foreach ($delete as $n_delete) {
//Work out max top and bottom keys to delete
$y_delete = $n_delete + 1;
$t_element = (int)$y_delete;
$b_element = $t_element - (int)(($output * 2) + 2);
// use slice to remove unwanted forms from parent array
array_splice($form_output[$option_name], $b_element, $t_element, null);
} // end foreach
} // end if delete
} // end if not radio
if (!empty($form_output[$option_name])) {
$this->reset_array($form_output[$option_name]);
}
return $form_output;
} else {
// if not dynamic
return $form_output;
} // end dynamic output
}
/**
* Form_Model::security_check()
*
* ESSENTIAL! Must include this
* Removes non-relevant HTML form fields before database update
*
* @param array $array
* @return array
*/
protected function security_check($array) {
if (!wp_verify_nonce($array['_wpnonce_options_cov'], "options_form_cov")) die("Security check failed");
if ($_SERVER['REQUEST_URI'] !== $array['_wp_http_referer']) die("Security check failed");
// The values below need to be removed before further validation and database entry
unset($array['option_page']);
unset($array['_wpnonce']);
unset($array['_wp_http_referer']);
unset($array['submit']);
//$form['unset_all'] = FALSE;
return $array;
}
/**
* Form_Model::find_url()
*
* Need to find the full URI for the admin area pages.
* Is there a suitable Wordpress function for this purpose? I couldn't find one
*
* @return string
*/
protected static function find_url() {
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
return $pageURL;
}
}
/**
* Form_Model::hex2bin()
*
* Alternative function because hex2bin is not native in PHP until version 5.4!
*
* @param array $form
* @return string
*/
protected function hex2bin($data) {
$bin = "";
$i = 0;
do {
$bin .= chr(hexdec($data{$i}.$data{($i + 1)}));
$i += 2;
} while ($i < strlen($data));
return $bin;
}
/**
* Form_Model::reset_array()
*
* Alternative function because hex2bin is not native in PHP until version 5.4!
*
* @param array $form
* @return string
*/
protected function reset_array(&$form) {
$keys = range(1, count($form));
$values = array_values($form);
$form = array_combine($keys, $values);
return $form;
}
}
/*global clearInterval: false, clearTimeout: false, document: false, event: false, frames: false, history: false, Image: false, location: false, name: false, navigator: false, Option: false, parent: false, screen: false, setInterval: false, setTimeout: false, window: false, XMLHttpRequest: false, jQuery: false */
/*
refactor loops in finalForm() for performance
This is only needed if the admin declers the dynamic output to false
Once the user navigates away from the admin page there needs to be a way of filling empty forms
This takes the javascript variables created in the constructor of the FormView class -> wp_enqueue_script()
*/
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun /*, thisp */ ) {
"use strict";
if (this == null) throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function") throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t)) res.push(val);
}
}
return res;
};
}
var OptionForm = {
//"use strict":
elementsNo: null,
optionName: null,
originalForm: null,
theForm: null,
newForm: null,
Before: null,
After: null,
Submit: null,
// declare the name of the form and keep it in memory
formName: function () {
jQuery.each(option_plugin_params, function (i, object) {
if (i !== "total_user_fields") {
OptionForm.optionName = i;
OptionForm.originalForm = document.getElementsByName(OptionForm.optionName)[0];
if (typeof OptionForm.originalForm !== "undefined") {
// Below sets a boolean to stop form JavaScript from working on submission
// This is unnecessary because the sticky values work
OptionForm.originalForm.onsubmit = function () {
OptionForm.Submit = TRUE;
};
if (OptionForm.Submit === FALSE) {
OptionForm.theForm = OptionForm.originalForm.cloneNode(true);
OptionForm.formValues();
} // end if
} // end if typeof
} // end if i !==
}); // end jQuery
},
// create the Option.Forma array. This takes all the form elements and filters out unneccessary input elements
formValues: function () {
var i, l;
OptionForm.newForm = [];
OptionForm.elementsNo = ['option_page', 'total_user_fields', '_wpnonce', '_wp_http_referer', 'submit'];
for (i = 0, l = OptionForm.theForm.length; i < l; i += 1) {
// filter out unwanted form values
if (jQuery.inArray(OptionForm.theForm[i].name, OptionForm.elementsNo) === -1 && OptionForm.theForm[i].name.indexOf('input_gen') === -1 && OptionForm.theForm[i].type !== 'hidden') {
if (OptionForm.theForm[i].type === "radio") {
OptionForm.newForm.push([OptionForm.theForm[i].name + ":" + OptionForm.theForm[i].value, "type:" + OptionForm.theForm[i].type, "checked:" + OptionForm.theForm[i].checked]);
} else if (OptionForm.theForm[i].type === "text") {
OptionForm.newForm.push([OptionForm.theForm[i].name + ":" + OptionForm.theForm[i].value, "type:" + OptionForm.theForm[i].type, "checked:" + "n/a"]);
} else if (OptionForm.theForm[i].type === "textarea") {
OptionForm.newForm.push([OptionForm.theForm[i].name + ":" + OptionForm.theForm[i].value, "type:" + OptionForm.theForm[i].type, "checked:" + "n/a"]);
} else if (OptionForm.theForm[i].type === "checkbox") {
OptionForm.newForm.push([OptionForm.theForm[i].name + ":" + OptionForm.theForm[i].value, "type:" + OptionForm.theForm[i].type, "checked:" + OptionForm.theForm[i].checked]);
} else if (OptionForm.theForm[i].type === "select-one") {
OptionForm.newForm.push([OptionForm.theForm[i].name + ":" + OptionForm.theForm[i].value, "type:" + OptionForm.theForm[i].type, "checked:" + OptionForm.theForm[i].checked]);
}
} // end if
} // end if
OptionForm.finalForm();
},
// Now put the JavaScript objects form option_plugin_params into the form if the values are empty
finalForm: function () {
var _Before, _After, x, l;
jQuery.each(option_plugin_params[OptionForm.optionName], function (i, object) {
//i = field name attribute, object.field_type = input type
jQuery.each(OptionForm.newForm, function (a_key, form) {
_Before = form[0].substring(0, form[0].indexOf(":"));
_After = form[0].substr(form[0].indexOf(":") + 1);
// Radio fields
if (form[1].indexOf("type:radio") !== -1 && form[2].indexOf("checked:false") !== -1) {
// form radio button is not checked
// if not then take value from the Wordpress variables in the option_plugin_params and add them
if (_Before.indexOf(i) !== -1) {
// Loop through the radio nodelists
for (x = 0, l = OptionForm.theForm.elements[_Before].length; x < l; x += 1) {
// if data between the form radio fields and option_plugin_params the same then put checkbox on there
if (OptionForm.theForm.elements[_Before][x].value === object) {
OptionForm.theForm.elements[_Before][x].checked = true;
} // end if
} // end for loop
} // end if
} // end if
// Text fields
// form input is text and is empty
// therefor check if Wordpress variable has a value
// If so, fill in the field
if (form[1].indexOf("type:text") !== -1 && _After === "") {
if (_Before.indexOf(i) !== -1) {
jQuery(OptionForm.theForm.elements[_Before]).val(object); // use jQuery val() so as to escape
} // end if
} // end if
// Textareas
if (form[1].indexOf("type:textarea") !== -1 && _After === "") {
if (_Before.indexOf(i) !== -1) {
jQuery(OptionForm.theForm.elements[_Before]).val(object);
} // end if
} // end if
// Checkboxes
if (form[1].indexOf("type:checkbox") !== -1 && form[2].indexOf("checked:false") !== -1) {
if (_Before.indexOf(i) !== -1) {
if (OptionForm.theForm.elements[_Before][1].value === object) {
jQuery(OptionForm.theForm.elements[_Before]).attr('checked', true);
}
} // end if
} // end if
// Select dropdown
// if set to 0 then the first select drop down has been used - meaning that it isn't set
if (form[1].indexOf("type:select-one") !== -1 && _After === "0") {
if (i === "selectName") {
// number of selected option drop down
OptionForm.theForm.elements[_Before].value = object;
}
} // end if
}); // end jQuery loop
}); // end each loop
OptionForm.recreateNode();
},
recreateNode: function () {
// delete original form from parent node
// add new admended node that is found in the OptionForm.theForm object
// This means better peformance because it minimises interaction with the DOM
var parentDiv;
parentDiv = OptionForm.originalForm.parentNode;
parentDiv.removeChild(OptionForm.originalForm);
parentDiv.appendChild(OptionForm.theForm);
},
// below clears the input values for the last form block if successfull submitted
multiFormName: function () {
var key, cookieMonster, mySplitResult, lastSplitResult;
function removeArrayElement(element, index, array) {
return (element !== "_multi_cov");
}
// Cookie is set when multi form is successfully completed
// Cookie is deleted when mutli form fails or when session ends
cookieMonster = document.cookie;
mySplitResult = cookieMonster.split(";");
for (x = 0, l = mySplitResult.length; x < l; x += 1) {
if (mySplitResult[x].indexOf("_multi_cov") !== -1) {
lastSplitResult = mySplitResult[x].split("=");
key = lastSplitResult.filter(removeArrayElement).toString();
} // end if
} // end for
OptionForm.originalForm = document.getElementsByName(key)[0];
if (typeof OptionForm.originalForm !== "undefined") {
OptionForm.theForm = OptionForm.originalForm.cloneNode(true);
OptionForm.multiFormValues();
} // end if typeof
},
multiFormValues: function () {
var i, remove;
remove = ['option_page', '_wpnonce', '_wp_http_referer', 'submit', 'total_user_fields'];
for (i = OptionForm.theForm.length - 1; i >= 0; i--) {
// ignore unimportant parts of form HTMLElementInput
if (remove.indexOf(OptionForm.theForm[i].name) === -1 && OptionForm.theForm[i].name.search(".([0-9]+)\]$") === -1 && OptionForm.theForm[i].name.search("input_gen|xyz|checkbox_number|checkbox_type") === -1 && OptionForm.theForm[i].name.search("input_gen|xyz|checkbox_number|checkbox_type") === -1 && OptionForm.theForm[i].name !== "" && OptionForm.theForm[i].type !== "hidden") {
// reset all form value back to zero
if (OptionForm.theForm[i].type === "text") {
OptionForm.theForm[i].value = "";
} else if (OptionForm.theForm[i].type === "textarea") {
OptionForm.theForm[i].value = "";
} else if (OptionForm.theForm[i].type === "radio") {
for (y = 0, l = OptionForm.theForm[i].length; y < l; y += 1) {
jQuery(OptionForm.theForm[i][y]).removeAttr("selected");
}
// something here for radio buttons
} else if (OptionForm.theForm[i].type === "select-one") {
// Loop through htmlselectelements
for (x = 0, l = OptionForm.theForm[i].length; x < l; x += 1) {
jQuery(OptionForm.theForm[i][x]).removeAttr("selected");
}
// something here for select forms
} else if (OptionForm.theForm[i].type === "checkbox") {
jQuery(OptionForm.theForm[i]).attr('checked', false);
}
} // end if giant statement
} // for (var i = OptionForm.originalForm.length - 1; i >= 0; i--)
OptionForm.multiFecreateNode();
},
multiFecreateNode: function () {
// delete original form from parent node
// add new admended node that is found in the OptionForm.theForm object
// This means better peformance because it minimises interaction with the DOM
var parentDiv;
parentDiv = OptionForm.originalForm.parentNode;
parentDiv.removeChild(OptionForm.originalForm);
parentDiv.appendChild(OptionForm.theForm);
// delete cookie after form rebuilding has been done
document.cookie = "_multi_cov" + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
},
init: function () {
if (typeof option_plugin_params !== "undefined") {
OptionForm.formName();
}
if (document.cookie.indexOf("_multi_cov") !== -1) {
OptionForm.multiFormName();
}
}
};
jQuery(document).ready(function () {
OptionForm.init();
});
@mfdeveloper
Copy link

@TCotton Thank you so much for share this code snnipets!! Did you still using wordpress these days? Do you upload this php classes like a composer library shared in packagist or wpackagist ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment