name = $name;
$this->label = $label;
$this->init();
if (defined('WP_ADMIN')) {
$this->admin_init();
}
}
/* Implement this in child classes if you need to do some initialization.
This will be called immediatlly after the constructor */
function init() {}
/* Same as above, but for amin */
function admin_init() {}
/* Collects information from input and setups object value */
/*public*/ function set_value_from_input() {
$this->set_value($_REQUEST[$this->name]);
}
/* This needs to be implemented in every child class */
/*abstract*/ function render() {}
function set_value($value) {
$this->value = $value;
}
/* You can setup HTML tag attributes via this field. Pass them in hash array where key is the attribute name
and value is attribute value, i.e.:
array(
'maxlength'=>'32',
'style'=>'width: 180px;'
)
*/
function set_html_attr($attrs) {
$this->html_attrs = $attrs;
}
function get_custom_attrs() {
$html_attrs = array();
foreach ($this->html_attrs as $key => $value) {
$html_attrs[] = $key . '="' . $value . '"';
}
return implode(' ', $html_attrs);
}
function help_text($help_text) {
$this->help_text = $help_text;
}
function get_error() {
return $this->validation_error;
}
function hide() {
$this->hidden = true;
}
}
// Base class for all options that use wordpress interface for managing options via
// add_option / update_option / get_option
/*abstract*/ class wp_option extends base_wp_option {
function init() {
$this->set_value(get_option($this->name));
}
function admin_init() {
if (isset($_GET['delete']) && $_GET['delete'] == $this->name) {
$this->reset_value();
}
}
function render($field_html, $colspan = false) {
$html = '
hidden ? 'style="display: none; "' : '') . '>
' . $field_html . ' ' . $this->help_text . '
';
return $html;
}
function save() {
$this->value = get_magic_quotes_gpc() ? stripslashes($this->value) : $this->value;
update_option($this->name, $this->value);
}
function set_default_value($default_vallue) {
$current_value = get_option($this->name);
if ($current_value) {
return;
}
$this->default_value = $default_vallue;
add_option($this->name, $default_vallue);
$this->set_value($default_vallue);
}
function reset_value() {
$this->value = $this->default_value;
$this->save();
}
function add_to_get($key, $val) {
return '?' . preg_replace('~&?' . preg_quote($key) . '(=|$|&)([^&]*)?~', '', $_SERVER['QUERY_STRING']) . '&' . $key . '=' . $val;
}
function get_reset_link() {
return $this->add_to_get('delete', $this->name);
}
}
// Most basic option -- single line text
class wp_option_text extends wp_option {
function init() {
wp_option::init();
$this->html_attrs = array_merge(
array('class'=>'regular-text'),
$this->html_attrs
);
}
function render() {
$field_html = 'get_custom_attrs() . ' />';
return wp_option::render($field_html);
}
}
class wp_option_int extends wp_option_text {
function set_value_from_input() {
$val = $_REQUEST[$this->name];
if (!is_numeric($val)) {
$this->validation_error = "should be integer";
return INVALID_VALUE;
}
$this->set_value($val);
}
}
//
class wp_option_textarea extends wp_option {
function admin_init() {
wp_option::admin_init();
$this->help_text($this->help_text);
$this->html_attrs = array_merge(
array(
'class'=>'regular-text',
'rows'=>'5',
/**/
'style'=>'width: 500px',
),
$this->html_attrs
);
}
function help_text($help_text) {
$this->help_text = " $help_text";
}
function render() {
$field_html = '';
return wp_option::render($field_html);
}
}
class wp_option_header_scripts extends wp_option_textarea {
var $help_text = 'If you need to add scripts to your header, you should enter them in this box.';
function init() {
wp_option_textarea::init();
add_action('wp_head', array($this, 'print_the_code'));
}
function print_the_code() {
echo get_option($this->name);
}
}
class wp_option_footer_scripts extends wp_option_textarea {
var $help_text = 'If you need to add scripts to your footer (like Google Analytics tracking code), you should enter them in this box.';
function init() {
wp_option_textarea::init();
add_action('wp_footer', array($this, 'print_the_code'));
}
function print_the_code() {
echo get_option($this->name);
}
}
/* */
class wp_option_choose_category extends wp_option {
function render() {
$dropdown_html = wp_dropdown_categories(array(
'name'=>$this->name,
'echo'=>false,
'hide_empty'=>false,
'hierarchical'=>1,
'exclude'=>1, // Exclude uncategorized
'selected'=>$this->value,
'show_option_none'=>'Please Choose',
));
return wp_option::render($dropdown_html);
}
}
class wp_option_choose_page extends wp_option {
function render() {
$dropdown_html = wp_dropdown_pages(array(
'name'=>$this->name,
'echo'=>false,
'hierarchical'=>1,
'selected'=>$this->value,
'show_option_none'=>'Please Choose',
));
return wp_option::render($dropdown_html);
}
}
/* */
class wp_option_select extends wp_option {
var $opts = array();
function add_options($opts) {
$this->opts = $opts;
}
function render() {
$html = '';
return wp_option::render($html);
}
}
/**
*
*/
class wp_option_file extends wp_option {
/**
* http://xref.limb-project.com/tests_runner/lib/spikephpcoverage/src/util/Utility.php.source.txt
*
* Make directory recursively.
* (Taken from: http://aidan.dotgeek.org/lib/?file=function.mkdirr.php)
*
* @param $dir Directory path to create
* @param $mode=0755
* @return True on success, False on failure
* @access protected
*/
function mkdir_recursive($dir, $mode=0755) {
// Check if directory already exists
if (is_dir($dir) || empty($dir)) {
return true;
}
// Crawl up the directory tree
$next_pathname = substr($dir, 0, strrpos($dir, DIRECTORY_SEPARATOR));
if ($this->mkdir_recursive($next_pathname, $mode)) {
if (!file_exists($dir)) {
return mkdir($dir, $mode);
}
}
return false;
}
function render() {
$html = '';
if ($this->value) {
$html .= " Current File: Download";
$html .= " | Delete";
}
return wp_option::render($html);
}
function get_file_extension($filename) {
$ext = preg_replace('~.*\.~', '', $filename);
return $ext;
}
function validate_file() {
return VALID_VALUE;
}
function set_value_from_input() {
if (empty($_FILES) || !is_uploaded_file($_FILES[$this->name]['tmp_name'])) {
return;
}
$valid = $this->validate_file();
if ($valid == INVALID_VALUE) {
return $valid;
}
$upload_location = wp_upload_dir();
$upload_dir = $upload_location['path'];
$filename = preg_replace('~[^\w\.]~', '', $_FILES[$this->name]['name']);
$destination = $upload_dir . '/' . $filename;
$filename_ch = 1;
while (file_exists($destination)) {
$destination = $upload_dir . '/' . $filename_ch . '-' . $filename;
$filename_ch++;
}
if (copy($_FILES[$this->name]['tmp_name'], $destination)) {
if ($this->value && file_exists($upload_dir . $this->value)) {
unlink($upload_dir . $this->value);
}
$this->value = $upload_location['url'] . '/' . $filename;
return VALID_VALUE;
} else {
$this->validation_error = "Error occured while writing a file. Please check whether " . $upload_dir . " is a writable directory.";
return INVALID_VALUE;
}
}
}
// Emulate static propertie in PHP4
$wp_option_image__stylesheet_printed = false;
class wp_option_image extends wp_option_file {
function _print_stylesheet() {
global $wp_option_image__stylesheet_printed;
if ($wp_option_image__stylesheet_printed) {
return;
}
echo '';
$wp_option_image__stylesheet_printed = true;
}
function admin_init() {
add_action('admin_head', array($this, '_print_stylesheet'));
wp_enqueue_script('thickbox');
wp_option_file::admin_init();
}
function render() {
$html = '';
if ($this->value) {
$html .= " View Current Image";
$html .= " | Delete";
}
return wp_option::render($html);
}
function validate_file() {
$valid = getimagesize($_FILES[$this->name]['tmp_name']);
if (!$valid) {
$this->validation_error = "The uploaded filetype is invalid (".$this->get_file_extension($_FILES[$this->name]['name']).").";
return INVALID_VALUE;
}
return VALID_VALUE;
}
}
$___tiny_mce_included = false;
$___tiny_mce_loaded = false;
class wp_option_rich_text extends wp_option_textarea {
function include_js() {
global $___tiny_mce_included;
if ($___tiny_mce_included) {
return;
}
$___tiny_mce_included = true;
echo '';
}
function admin_init() {
add_action('admin_head', array($this, 'include_js'));
wp_option_textarea::admin_init();
}
function render() {
global $___tiny_mce_loaded;
ob_start();
include('rich-text-field.php');
$field_html = ob_get_contents();
ob_end_clean();
$___tiny_mce_loaded = true;
return wp_option::render($field_html);
}
}
class wp_option_separator extends wp_option {
function render() {
$html = '