settings = $settings ? $settings : array();
$this->name = $name;
$this->group = $group;
$this->main = $main;
$this->permission = $permission;
$this->icon = $icon;
$this->position = $position;
$this->html = $html;
$this->join = $join;
add_action('admin_init', array($this, 'admin_init'));
add_action('admin_menu', array($this, 'admin_menu'));
}
public function admin_init() {
foreach ($this->settings as $setting) {
$setting = (object) $setting;
$type = $setting->type;
$type = $type == 'select' ? 'string' : $type;
$type = $type == 'text' ? 'string' : $type;
$type = $type == 'url' ? 'string' : $type;
$type = $type == 'password' ? 'string' : $type;
register_setting($this->group, $setting->id,
array(
'type' => $type,
'description' => $setting->title,
'default' => property_exists($setting, 'default') ? $setting->default : null
)
);
}
}
public function admin_menu() {
if ($this->main) {
add_menu_page(
$this->name,
$this->name,
$this->permission,
self::create_key($this->group, $this->join),
array($this, 'menu_page'),
$this->icon,
$this->position
);
} else {
add_submenu_page(
self::create_key($this->group, $this->join),
$this->name,
$this->name,
$this->permission,
self::create_key($this->group.' '.$this->name, $this->join),
array($this, 'menu_page')
);
}
}
public static function create_key($key, $join = '-') {
return urlencode(str_replace(' ',$join,strtolower($key)));
}
public static function notice($message, $type = 'success') {
echo '
';
}
public static function check_nounce($key) {
if (isset($_POST[$key])) {
if (wp_verify_nonce($_POST[$key], plugin_basename( __FILE__ ))) {
return true;
} else {
self::notice('Security check failed, please login and try again.');
}
}
return false;
}
public static function echo_nounce($key) {
wp_nonce_field(plugin_basename( __FILE__ ), $key);
}
public static function settings($setting) {
//TODO: add select
$setting = (object) $setting;
if ($setting->type == 'boolean') {
echo self::settings_checkbox($setting);
} elseif ($setting->type == 'select') {
echo self::settings_select($setting);
} elseif ($setting->type == 'text') {
echo self::settings_text($setting);
} else {
echo self::settings_input($setting);
}
}
public static function settings_row($setting, $html) {
return '
| '.$setting->title.($setting->type=='text'?' '.$setting->description.' ':'').' |
'.$html.' |
';
}
public static function settings_text($setting) {
$html = ''.esc_html(get_option($setting->id)).'';
return self::settings_row($setting, $html);
}
public static function settings_select($setting) {
$assoc = self::associative($setting->values);
$current = get_option($setting->id);
$html = '';
return self::settings_row($setting, $html);
}
public static function settings_checkbox($setting) {
$html = 'id) == '1' ? ' checked="checked"' : '').' />';
$html .= '';
return self::settings_row($setting, $html);
}
public static function settings_input($setting) {
$html = '';
//$html .= ''.$setting->description.'
';
return self::settings_row($setting, $html);
}
public static function checkbox_request($key) {
return isset($_REQUEST[$key]) && $_REQUEST[$key] != '' ? 1 : 0;
}
public static function process_checkbox($key) {
update_option($key, self::checkbox_request($key));
}
public static function process_option($key) {
update_option($key, sanitize_text_field($_REQUEST[$key]));
}
public static function associative($array) {
return array_keys($array) !== range(0, count($array) - 1);
}
public function menu_page() {
echo ''.$this->name.'
';
echo $this->html;
$nounce = $this->group;
if (self::check_nounce($nounce)) {
foreach ($this->settings as $setting) {
$setting = (object) $setting;
if ($setting->type == 'boolean') {
self::process_checkbox($setting->id);
} else {
self::process_option($setting->id);
}
}
self::notice('Saved.');
}
?>