Skip to content

Instantly share code, notes, and snippets.

@littlefyr
Created January 6, 2011 05:45
Show Gist options
  • Save littlefyr/767570 to your computer and use it in GitHub Desktop.
Save littlefyr/767570 to your computer and use it in GitHub Desktop.
LittleFyr_OptionSet does all the heavy lifting of setting up an option page with a standard behaviour. Testing_Options provides the specific instance that defines the different option values.
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
abstract class LittleFyr_OptionSet {
protected $option_name;
protected $set_title;
protected $section_count = 0;
protected $section_text = array();
protected $validators = array();
protected $introduction = '';
protected $conclusion = '';
protected $option;
function __construct($option_name, $l10n=NULL) {
$this->option_name = $option_name;
if (is_null($l10n)) {
$l10n = get_template_directory() . '/lang';
}
load_theme_textdomain($this->option_name, $l10n);
add_action('admin_init', array(&$this, 'register_settings'));
add_action('admin_menu', array(&$this, 'add_page'), 0);
$this->title = get_class($this);
}
function register_settings() {
register_setting($this->option_name, $this->option_name, array(&$this, 'update'));
// update_option($this->option_name, array());
}
function update($param) {
echo "<pre>";
var_dump( $param );
echo "</pre>";
return $param;
}
function add_page() {
$options_page_hookname = add_theme_page(
$this->__($this->set_title),
$this->__($this->set_title),
'edit_theme_options',
$this->option_name,
array(&$this, 'admin_page'));
// add_action("admin_print_scripts-{$options_page_hookname}", array(&$this, 'admin_js'));
// add_action("admin_print_styles-{$options_page_hookname}", array(&$this, 'admin_css'));
}
function admin_page() {
$this->get_admin_fields();
echo '<div class="wrap">';
screen_icon();
echo '<h2>' . $this->__($this->set_title) . '</h2>';
echo apply_filters('the_content', $this->introduction);
echo '<form method="post" action="' . $_SERVER["REQUEST_URI"] . '">';
settings_fields($this->option_name);
do_settings_sections($this->option_name);
echo '<p class="submit"><input type="submit" name="Submit" class="button-primary" value="' . $this->__('Save Changes') . '"></p>';
echo '</form></div>';
?>
<?php
}
function get_section_id($id) {
return sprintf('%1$s_%2$s', $this->option_name, $id);
}
protected function add_section($title, $description) {
$section_id = $this->get_section_id(sprintf('%010X', $this->section_count++));
$this->section_text[$section_id] = $description;
add_settings_section($section_id, $title, array(&$this, 'render_section_text'), $this->option_name);
return $section_id;
}
function render_section_text($section) {
$txt = $this->section_text[$section['id']];
if ($txt) {
echo apply_filters('the_content', $txt);
}
}
function add_choice($id, $section_id, $title, $description, $choices, $multiple=0, $default=NULL, $invalid_text=NULL) {
$this->validation_rules[$id] = array_merge(compact('id', 'multiple', 'choices', 'default', 'invalid_text'), array('type' => 'choice'));
add_settings_field(
$id,
$title,
array(&$this, 'render_choices'),
$this->option_name,
$section_id,
array_merge(
compact('id', 'description', 'choices', 'default', 'null_value'),
array('multiple' => (intval($multiple) + is_bool($multiple)) > 1)
)
);
}
function render_choices($args) {
extract($args);
$option = get_option($this->option_name);
var_dump( $option );
$the_option_value = array_key_exists($id, $option) ? $option[$id] : $default;
$the_option_value = is_array($the_option_value) ? $the_option_value : array($the_option_value);
$input_type = $multiple ? 'checkbox' : 'radio';
echo '<fieldset><legend class="screen-reader-text"><span>' . esc_html($the_option['title']) . '</span></legend>';
$pos = 0;
$input_name = $this->option_name . '[' . $id . '][]';
foreach ($choices as $value => $label) {
$checked = in_array($value, $the_option_value) ? ' checked' : '';
$input_id = $this->option_name . '_' . $id . '_' . $pos;
++$pos;
if (is_int($value)) {
$value = $label;
}
echo '<div><label><input type="' . $input_type . '" name="' . $input_name . '" id="' . $input_id . '" value="' . esc_attr($value) . '"' . $checked . '/> <span>' . esc_html($label) . '</span></label></div>';
}
echo '</fieldset>';
if (isset($description) && !empty($description)) {
echo '<span class="description">' . $description . '</span>';
}
}
function add_page_choice($id, $section_id, $title, $description, $valid_params, $show_option_none='None', $default=NULL) {
$this->validation_rules[$id] = array_merge(compact('id', 'default'), array('type' => 'page'));
add_settings_field(
$id,
$title,
array($this, 'render_page_choices'),
$this->option_name,
$section_id,
compact('id', 'description', 'valid_params', 'default', 'show_option_none')
);
}
function render_page_choices($args) {
extract($args);
$option = get_option($this->option_name);
$the_option_value = array_key_exists($id, $option) ? $option[$id] : $default;
$args = array_merge($valid_params, array('show_option_none' => $show_option_none, 'selected' => (int) $the_option_value, 'name' => $this->option_name . '[' . $id . ']'));
wp_dropdown_pages($args);
if (isset($description) && !empty($description)) {
echo '<span class="description">' . $description . '</span>';
}
}
function add_text($id, $section_id, $title, $description, $default=NULL, $is_valid=NULL, $null_value=NULL) {
$this->validation_rules[$id] = array_merge(compact('id', 'null_value', 'is_valid'), array('type' => 'text'));
add_settings_field(
$id,
$title,
array($this, 'render_text'),
$this->option_name,
$section_id,
array_merge(
compact('id', 'description', 'default'),
array('classes' => 'text regular-text')
)
);
}
function add_integer($id, $section_id, $title, $description, $default=NULL, $is_valid=NULL, $null_value=NULL) {
$this->validation_rules[$id] = array_merge(compact('id', 'null_value', 'is_valid'), array('type' => 'int'));
add_settings_field(
$id,
$title,
array($this, 'render_text'),
$this->option_name,
$section_id,
array_merge(
compact('id', 'description', 'default'),
array('classes' => 'integer small-text')
)
);
}
function add_float($id, $section_id, $title, $description, $default=NULL, $is_valid=NULL, $null_value=NULL) {
$this->validation_rules[$id] = array_merge(compact('id', 'null_value', 'is_valid'), array('type' => 'int'));
add_settings_field(
$id,
$title,
array($this, 'render_text'),
$this->option_name,
$section_id,
array_merge(
compact('id', 'description', 'default'),
array('classes' => 'integer small-text')
)
);
}
function render_text($args) {
extract($args);
$option = get_option($this->option_name);
// $the_option_value = array_key_exists($id, $option) ? $option[$id] : $default;
$input_name = $this->option_name . '[' . $id . ']';
$input_id = $this->option_name . '_' . $id;
$type_class = ($the_option['type'] == 'text') ? 'regular-text' : 'small-text';
echo '<input type="text" class="' . $classes . '" name="' . $input_name . '" id="' . $input_name . '" value="' . esc_attr($the_option_value) . '"/>';
if (isset($description) && !empty($description)) {
echo '<span class="description">' . $description . '</span>';
}
}
/*
* providing shortcuts for translation functions
*/
/**
* Retrieves the translation of $text. If there is no translation, or
* the domain isn't loaded the original text is returned.
*
* @see translate() An alias of translate()
* @since 2.1.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated text
*/
function __($text) {
return __($text, $this->option_name);
}
/**
* Retrieves the translation of $text and escapes it for safe use in an attribute.
* If there is no translation, or the domain isn't loaded the original text is returned.
*
* @see translate() An alias of translate()
* @see esc_attr()
* @since 2.8.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated text
*/
function esc_attr__($text) {
return esc_attr__($text, $this->option_name);
}
/**
* Retrieves the translation of $text and escapes it for safe use in HTML output.
* If there is no translation, or the domain isn't loaded the original text is returned.
*
* @see translate() An alias of translate()
* @see esc_html()
* @since 2.8.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated text
*/
function esc_html__($text) {
return esc_html__($text, $this->option_name);
}
/**
* Displays the returned translated text from translate().
*
* @see translate() Echoes returned translate() string
* @since 1.2.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
*/
function _e($text) {
_e($text, $this->option_name);
}
/**
* Displays translated text that has been escaped for safe use in an attribute.
*
* @see translate() Echoes returned translate() string
* @see esc_attr()
* @since 2.8.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
*/
function esc_attr_e($text) {
esc_attr_e($text, $this->option_name);
}
/**
* Displays translated text that has been escaped for safe use in HTML output.
*
* @see translate() Echoes returned translate() string
* @see esc_html()
* @since 2.8.0
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
*/
function esc_html_e($text) {
esc_html_e($text, $this->option_name);
}
/**
* Retrieve translated string with gettext context
*
* Quite a few times, there will be collisions with similar translatable text
* found in more than two places but with different translated context.
*
* By including the context in the pot file translators can translate the two
* string differently.
*
* @since 2.8.0
*
* @param string $text Text to translate
* @param string $context Context information for the translators
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated context string without pipe
*/
function _x($single, $context) {
return _x($text, $this->option_name);
}
/**
* Displays translated string with gettext context
*
* @see _x
* @since 3.0.0
*
* @param string $text Text to translate
* @param string $context Context information for the translators
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated context string without pipe
*/
function _ex($single, $context) {
echo _ex($single, $context, $this->option_name);
}
function esc_attr_x($single, $context) {
return esc_attr_x($single, $context, $this->option_name);
}
function esc_html_x($single, $context) {
return esc_html_x($single, $context, $this->option_name);
}
/**
* Retrieve the plural or single form based on the amount.
*
* If the domain is not set in the $l10n list, then a comparison will be made
* and either $plural or $single parameters returned.
*
* If the domain does exist, then the parameters $single, $plural, and $number
* will first be passed to the domain's ngettext method. Then it will be passed
* to the 'ngettext' filter hook along with the same parameters. The expected
* type will be a string.
*
* @since 2.8.0
* @uses $l10n Gets list of domain translated string (gettext_reader) objects
* @uses apply_filters() Calls 'ngettext' hook on domains text returned,
* along with $single, $plural, and $number parameters. Expected to return string.
*
* @param string $single The text that will be used if $number is 1
* @param string $plural The text that will be used if $number is not 1
* @param int $number The number to compare against to use either $single or $plural
* @return string Either $single or $plural translated text
*/
function _n($single, $plural, $number) {
return _n($single, $plural, $number, $this->option_name);
}
/**
* A hybrid of _n() and _x(). It supports contexts and plurals.
*
* @see _n()
* @see _x()
*
*/
function _nx($single, $plural, $number, $context) {
return _nx($single, $plural, $number, $context, $this->option_name);
}
abstract protected function get_admin_fields();
abstract protected function validate($param);
}
?>
require_once( 'optionsets.php' );
class Testing_Options extends LittleFyr_OptionSet {
function __construct() {
$this->set_title = "AppleTree Settings";
$this->introduction = <<< INTRO
This is the introduction
I rock.
INTRO;
parent::__construct('appletree-options');
}
protected function get_admin_fields() {
$s = $this->add_section("Avatars", "An avatar is an image that follows you from weblog to weblog appearing beside your name when you comment on avatar enabled sites. Here you can enable the display of avatars for people who comment on your site.");
$this->add_choice('some_choice', $s, 'Avatar Display', false, array('value' => 'Don’t show Avatars', 'value2' => "Show Avatars"));
$this->add_choice('some_choice1', $s, 'Avatar Display', false, array('value' => 'Don’t show Avatars', 'value2' => "Show Avatars"), true);
$this->add_choice('some_choice2', $s, 'Avatar Display', false, array('value' => 'Don’t show Avatars', 'value2' => "Show Avatars"), true, array('value2', 'value'));
$this->add_choice('some_choice3', $s, 'Avatar Display', false, array('value' => 'Don’t show Avatars', 'value2' => "Show Avatars"), false, 'value2');
$s = $this->add_section("Page Choices", false);
$this->add_page_choice('some_page_choice', $s, 'Pick a Page', false, array('post_status' => 'publish'), 'None', array('post_status' => 'publish'));
$s = $this->add_section("Text Fields", false);
$this->add_text('some_text', $s, 'Set Text', 'you can do it');
$this->add_integer('some_in', $s, 'Set Int', 'you can do it');
$this->add_integer('some_in2', $s, 'Set Int Def', 'you can do it', 32);
$this->add_float('some_float', $s, 'Set Float', 'you can do it');
$this->add_float('some_float2', $s, 'Set Float with default', 'you can do it', 12.12);
}
function validate($param) {
return $param;
}
}
new Testing_Options();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment