check_table($var); } /** * 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 ''; $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"; } } if ($user_data['0'] === "text") { $user_data['5'] = TRUE; } // 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 ""; } // 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) !== 8) { die("Make sure you enter eight 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 .= "
'; echo $form; } /** * Form_Controller::perm_fields() * * @param string $total_fields * @return string $perm */ private function perm_fields($total_fields) { extract(static::$form); $perm = ''; $perm .= ''; $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\" "; if ($name === "formMinRows" || $name === "formMaxRows") { $form .= " class=\"small-text \" "; } else { $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="'; $form .= isset($_POST[$option_name][$name]) ? esc_attr(stripslashes ($_POST[$option_name][$name])) : $value; $form .= '"'; $name_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 if ($key === "select") { if ($value === TRUE) { $add_link = TRUE; } else { $add_link = $value; } } } // end foreach $text = '