Skip to content

Instantly share code, notes, and snippets.

@TCotton
Created February 29, 2012 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TCotton/1939834 to your computer and use it in GitHub Desktop.
Save TCotton/1939834 to your computer and use it in GitHub Desktop.
A class to handle the Wordpress Settings API
class Form_Controller {
function __construct() {
} // end construct
public function ah_config_settings() {
// put together the output array
$output['ah_option_name'] = 'affiliate_hoover_plugin_options';
// the option name as used in the get_option() call.
$output['ah_page_title'] = 'Affiliate Hoover'; // the settings page title
$output['ah_page_url'] = 'affiliate-hoover-plugin-admin';
$output['ah_form_sections'] = Form_Controller::ah_options_page_sections(); // the setting section
$output['ah_form_fields_hook'] = Form_Controller::ah_form_fields(); // the setting fields
//$output['ah_contextual_help'] = ''; // the contextual help
return $output;
}
public function ah_options_page_sections() {
$sections = array(); //$sections[$id] = $title;
$sections['txt_section'] = 'Text area Form Fields';
//$sections['txtarea_section'] = 'Textarea Form Fields';
//$sections['select_section'] = 'Select Form Fields';
//$sections['checkbox_section'] = 'Checkbox Form Fields';
return $sections;
}
public function ah_form_fields() {
$options = array();
$options[] = array(
"id" => "ah_url_name_input",
"title" => 'Give the feed a name here',
"desc" => '',
"std" => '',
"type" => "text",
"section" => "txt_section",
"choices" => "",
"class" => "ah_name"); // Text Form Fields section
$options[] = array(
"id" => "ah_url_input",
"title" => 'Place the fields URL here',
"desc" => '',
"std" => '',
"type" => "text",
"section" => "txt_section",
"choices" => "",
"class" => "ah_email");
return $options;
}
private function ah_form_fields_two($args, $args2 = null) {
$options = array(
"id" => null,
"title" => null,
"desc" => null,
"std" => null,
"type" => null,
"section" => null,
"choices" => null,
"class" => null); // Text Form Fields section
return array_merge($options, $args, $args2);
}
public function ah_merge_database_and_fields() {
extract(Form_Controller::ah_config_settings());
// take the two arrays from ah_form_fields() and separate them
foreach (Form_controller::ah_form_fields() as $key => $value) {
if ($key === 0) {
$ah_url_name_input = $value;
}
if ($key === 1) {
$ah_url_input = $value;
}
} // end foreach
// call the associated fields from the options field
$option = get_option($ah_option_name);
$new_array = array();
// loop through it and marge it with ah_form_fields_two() keys and null values
foreach ($option as $result) {
foreach ($result as $key => $value) {
if ($key === 'ah_url_name_input') {
$new_array[] = Form_controller::ah_form_fields_two($ah_url_name_input, array("std" =>
$value));
} // end if
if ($key === 'ah_url_input') {
$new_array[] = Form_controller::ah_form_fields_two($ah_url_input, array("std" =>
$value));
} // end if
} // end foreach
} // end foreach
// Finally create a final super array merging all the above created arrays into one
$final_array = array();
$final_array = $new_array;
$final_array[] = $ah_url_name_input;
$final_array[] = $ah_url_input;
return $final_array;
}
public function ah_create_form_fields($args = array()) {
static $i = 0;
extract(Form_Controller::ah_config_settings());
// default array to overwrite when calling the function
$defaults = array(
'id' => 'default_field', // the ID of the setting in our options array, and the ID of the HTML form element
'title' => 'Default Field', // the label for the HTML form element
'desc' => 'This is a default description.', // the description displayed under the HTML form element
'std' => '', // the default value for this setting
'type' => 'text', // the HTML form element to use
'section' => 'main_section', // the section this setting belongs to — must match the array key of a section in wptuts_options_page_sections()
'choices' => array(), // (optional): the values in radio buttons or a drop-down menu
'class' => '' // the HTML form element class. Also used for validation purposes!
); // "extract" to be able to use the array keys as variables in our function output below
extract(wp_parse_args($args, $defaults));
// additional arguments for use in form field output in the function wptuts_form_field_fn!
$field_args = array(
'type' => $type,
'id' => $id,
'desc' => $desc,
'std' => $std,
'choices' => $choices,
'label_for' => $id,
'class' => $class);
add_settings_field($id.$i++, $title, array('Form_Controller', 'ah_form_field_fn'), $ah_page_url,
$section, $field_args);
}
public function ah_form_field_fn($args = array()) {
extract($args);
extract(Form_Controller::ah_config_settings());
$options = get_option($ah_option_name);
// DO A LOOP HERE
if (!isset($options[$id])) {
$options[$id] = $std;
}
// additional field class. output only if the class is defined in the create_setting arguments
$field_class = ($class !== '') ? ' '.$class : '';
switch ($type) {
case 'text':
$options[$id] = esc_attr(stripslashes($options[$id]));
echo "<input class='regular-text-$field_class' type='text' id='$id' name='"."$ah_option_name".
"[$id]' value='$options[$id]' />";
echo ($desc !== '') ? "<br /><span class='description'>$desc</span>" : null;
} // end switch
} //
public function ah_validate_options($input) {
// for enhanced security, create a new empty array
$valid_input = array();
$final_array = array();
extract(Form_Controller::ah_config_settings());
// run a foreach and switch on option type
foreach ($ah_form_fields_hook as $option) {
switch ($option['type']) {
case 'text':
// do something here
switch ($option['class']) {
//for numeric
case 'ah_email':
$input[$option['id']] = trim($input[$option['id']]);
if (filter_var($input[$option['id']], FILTER_VALIDATE_URL,
FILTER_FLAG_PATH_REQUIRED) === false) {
add_settings_error($input[$option['id']], 'ah_txt_numeric_error',
'Please make sure that the URL is correct', 'error');
} else {
$valid_input[$option['id']] = $input[$option['id']];
}
break;
case 'ah_name':
$input[$option['id']] = trim($input[$option['id']]);
if ($input[$option['id']] === "") {
add_settings_error($option['id'], 'ah_txt_numeric_error',
'Please supply a name for the feed', 'error');
} else {
$valid_input[$option['id']] = $input[$option['id']];
}
break;
} // end second switch
break;
} // end first switch
} // end foreach loop
$final_array = array($valid_input);
return $final_array; // return validated input
}
}
class Form_View {
function __construct() {
$this->add_action_admin_menu();
$this->add_action_admin_init();
} // end construct
public function add_action_admin_menu() {
add_action('admin_menu', function () {
$config_settings = Form_Controller::ah_config_settings();
//add_action callback function here
add_options_page('Affiliate Hoover', 'Affiliate Hoover', 'manage_options', $config_settings['ah_page_url'],function () {
$config_settings = Form_Controller::ah_config_settings();
echo '<div id="wrap">';
echo screen_icon();
echo "<h2>".$config_settings['ah_page_title']."</h2>";
echo '<p>This is the admin section for Affiliate Hoover plugin</p>';
echo '<form method="post" action="options.php">';
// http://codex.wordpress.org/Function_Reference/do_settings_sections
do_settings_sections($config_settings['ah_page_url']);
// http://codex.wordpress.org/Function_Reference/settings_fields
settings_fields($config_settings['ah_option_name']);
echo '<input type="submit" id="submit" name="submit" class="button-primary" value="Save Changes">';
echo '</form>';
echo '</div><!-- end wrap div -->'; }
); }
);
} //
public function add_action_admin_init() {
add_action('admin_init', function () {
$config_settings = Form_Controller::ah_config_settings();
register_setting($config_settings['ah_option_name'], $config_settings['ah_option_name'], array('Form_Controller', 'ah_validate_options'));
// add_settings_section( $id, $title, $callback, $page );
if (!empty($config_settings['ah_form_sections'])) {
// call the "add_settings_section" for each!
foreach ($config_settings['ah_form_sections'] as $id => $title) {
add_settings_section($id, $title, function () { echo '<p>This is the beginning the one and only section</p>'; }, $config_settings['ah_page_url']); }
}
$option = get_option($config_settings['ah_option_name']);
// call the "add_settings_field" for each!
foreach (Form_Controller::ah_merge_database_and_fields() as $options) {
Form_Controller::ah_create_form_fields($options);
}// end foreach
} // end add_action anon function
); // end add_action
}
} // end class
new Form_View();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment