Initial commit of 0.2.0
This commit is contained in:
208
inc/settings.php
Normal file
208
inc/settings.php
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-obfuscation
|
||||
* @author cleverogre
|
||||
* @version 0.2.0
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Obfuscation;
|
||||
|
||||
use Ogre\Singleton;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Settings {
|
||||
use Singleton;
|
||||
|
||||
private $defaults; // Value set in get function
|
||||
|
||||
// Admin Settings
|
||||
public $email_enabled;
|
||||
|
||||
protected function __construct() {
|
||||
// Use getter/setter if not set
|
||||
foreach ($this as $key => $value) {
|
||||
unset($this->$key);
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($key, $value) {
|
||||
if (property_exists($this, $key)) {
|
||||
switch ($key) {
|
||||
/* // Read-only properties
|
||||
case 'readonly':
|
||||
break;
|
||||
*/
|
||||
|
||||
default:
|
||||
$options = get_option('obfuscation_options');
|
||||
$options['obfuscation_settings_' . $key] = $value;
|
||||
update_option('obfuscation_options', $options);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __get($key) {
|
||||
if (property_exists($this, $key)) {
|
||||
switch ($key) {
|
||||
// Constant properties
|
||||
case 'defaults':
|
||||
return array(
|
||||
'email_enabled' => '1',
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
$options = get_option('obfuscation_options');
|
||||
|
||||
if (isset($options['obfuscation_settings_' . $key]) && !empty($options['obfuscation_settings_' . $key])) {
|
||||
return $options['obfuscation_settings_' . $key];
|
||||
} else if (isset($this->defaults[$key])) {
|
||||
return $this->defaults[$key];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final class SettingsPage {
|
||||
use Singleton;
|
||||
|
||||
protected function __construct() {
|
||||
add_action('admin_init', array($this, 'init'));
|
||||
add_action('admin_menu', array($this, 'menu'));
|
||||
}
|
||||
|
||||
public function menu() {
|
||||
if (!current_user_can('manage_options')) return;
|
||||
|
||||
$page = add_options_page(__('Ogre Obfuscation Settings', 'obfuscation'), __('Ogre Obfuscation', 'obfuscation'), 'manage_options', 'obfuscation_settings', array($this, 'page'));
|
||||
add_action('load-' . $page, array($this, 'load'));
|
||||
}
|
||||
|
||||
public function load() {
|
||||
if (!current_user_can('manage_options')) return;
|
||||
|
||||
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
|
||||
}
|
||||
|
||||
public function admin_enqueue_scripts() {
|
||||
//wp_enqueue_script('obfuscation-admin', Settings::instance()->dir . 'assets/js/obfuscation.admin.js', array('jquery'), Settings::instance()->version, true);
|
||||
}
|
||||
|
||||
public function init() {
|
||||
register_setting('obfuscation', 'obfuscation_options'); //, array($this, 'validate_options'));
|
||||
|
||||
// Message Settings
|
||||
|
||||
add_settings_section('obfuscation_section_obfuscation', __('Obfuscation', 'obfuscation'), array($this, 'section'), 'obfuscation');
|
||||
|
||||
add_settings_field('obfuscation_email_enabled', __('Email Obfuscation Enabled', 'obfuscation'), array($this, 'field_checkbox'), 'obfuscation', 'obfuscation_section_obfuscation', array(
|
||||
'id' => 'obfuscation_settings_email_enabled',
|
||||
'default' => Settings::instance()->defaults['email_enabled'],
|
||||
));
|
||||
}
|
||||
|
||||
public function page() {
|
||||
if (!current_user_can('manage_options')) return;
|
||||
|
||||
// Check if the user has submitted the settings
|
||||
if (isset($_GET['settings-updated'])) {
|
||||
// Show update message
|
||||
//add_settings_error('obfuscation_messages', 'obfuscation_message', __('Settings Saved', 'obfuscation'), 'updated');
|
||||
}
|
||||
|
||||
// Show error/update messages
|
||||
settings_errors('obfuscation_messages');
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
|
||||
<form action="options.php" method="post"><?php
|
||||
settings_fields('obfuscation');
|
||||
do_settings_sections('obfuscation');
|
||||
submit_button(__('Save Settings', 'obfuscation'));
|
||||
?></form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Sections
|
||||
|
||||
public function section($args) {
|
||||
// <p>Description</p>
|
||||
}
|
||||
|
||||
// Fields
|
||||
|
||||
public function field_text($args) {
|
||||
$value = '';
|
||||
$options = get_option('obfuscation_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($args['id']) && !empty($args['id']) && isset($options[$args['id']]) && !empty($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
$type = 'text';
|
||||
if (isset($args['type']) && !empty($args['type'])) {
|
||||
$type = $args['type'];
|
||||
}
|
||||
|
||||
echo '<input class="regular-text" type="' . $type . '" value="' . esc_attr($value) . '"';
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
echo ' name="obfuscation_options[' . esc_attr($args['id']) . ']"';
|
||||
}
|
||||
if (isset($args['placeholder']) && !empty($args['placeholder'])) {
|
||||
echo ' placeholder="' . esc_attr($args['placeholder']) . '"';
|
||||
}
|
||||
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
_e($args['description'], 'obfuscation');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public function field_checkbox($args) {
|
||||
$value = false;
|
||||
$options = get_option('obfuscation_options');
|
||||
if (isset($args['default'])) $value = $args['default'];
|
||||
if (isset($args['id']) && !empty($args['id']) && isset($options[$args['id']]) && !empty($options[$args['id']])) $value = $options[$args['id']];
|
||||
|
||||
echo '<input type="checkbox" value="1" type="checkbox" ' . checked($value, 1, false);
|
||||
|
||||
if (isset($args['id']) && !empty($args['id'])) {
|
||||
echo ' id="' . esc_attr($args['id']) . '"';
|
||||
echo ' name="obfuscation_options[' . esc_attr($args['id']) . ']"';
|
||||
}
|
||||
echo ' />';
|
||||
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
echo '<p class="description">';
|
||||
esc_html_e($args['description'], 'obfuscation');
|
||||
echo '</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public function validate_options($fields) {
|
||||
$valid_fields = array();
|
||||
|
||||
foreach ($fields as $id => $field) {
|
||||
if (!isset($input[$id])) continue;
|
||||
}
|
||||
|
||||
return apply_filters('validate_options', $valid_fields, $fields);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
SettingsPage::instance();
|
||||
Reference in New Issue
Block a user