1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
<?php
namespace hji\common\interfaces;
abstract class AbstractSettingsModel
{
protected static $__CLASS__ = __CLASS__;
protected static $instance;
protected $settingsKey = false;
protected $optionGroupsKeys = array();
protected $optionGroups = array();
protected $settings = false;
function __construct($pluginSlug, $settingsKey = false)
{
if ($settingsKey)
{
$this->settingsKey = $settingsKey;
}
else
{
$this->settingsKey = 'hji-' . $pluginSlug . '-settings';
}
if (!empty($this->optionGroups))
{
$this->optionGroupsKeys = array_keys($this->optionGroups);
}
}
static function getInstance()
{
$class = static::getClass();
if (static::$instance === null)
{
static::$instance = new $class;
}
return static::$instance;
}
function all()
{
return $this->settings;
}
private static function getClass()
{
if (static::$__CLASS__ == __CLASS__)
{
die("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!");
}
return static::$__CLASS__;
}
function getSettingsKey()
{
return $this->settingsKey;
}
function getOptionGroups()
{
return $this->optionGroups;
}
function loadOptions()
{
if ($this->settingsKey)
{
$this->settings = get_option($this->settingsKey);
}
}
function getOptions($optionGroupKey = false)
{
if ($optionGroupKey)
{
return (isset($this->settings[$optionGroupKey]))
? $this->settings[$optionGroupKey] : false;
}
return !empty($this->settings) ? $this->settings : array();
}
function getOption($optionName, $optionGroupKey = false)
{
if ($this->optionGroupsKeys)
{
if (!$optionGroupKey)
{
trigger_error('$optionGroupKey is required.', E_USER_ERROR);
return;
}
else if (!in_array($optionGroupKey, $this->optionGroupsKeys))
{
trigger_error('Option Group Key "'. $optionGroupKey . '" doesn\'t exist in ' . self::getClass() .'::optionGroupsKeys.', E_USER_ERROR);
}
else
{
return (isset($this->settings[$optionGroupKey][$optionName]))
? $this->settings[$optionGroupKey][$optionName] : false;
}
}
else if ($optionGroupKey)
{
trigger_error('Option Group Key "'. $optionGroupKey . '" doesn\'t exist in ' . self::getClass() .'::optionGroupsKeys.', E_USER_ERROR);
return;
}
return (isset($this->settings[$optionName]))
? $this->settings[$optionName] : false;
}
function setOptions($newOptions, $optionGroupKey = false)
{
if ($this->optionGroupsKeys)
{
if (!$optionGroupKey)
{
trigger_error('$optionGroupKey is required.', E_USER_ERROR);
return;
}
else if (!in_array($optionGroupKey, $this->optionGroupsKeys))
{
trigger_error('Option Group Key "'. $optionGroupKey . '" doesn\'t exist in ' . self::getClass() .'::optionGroupsKeys.', E_USER_ERROR);
return;
}
$this->settings[$optionGroupKey] = $newOptions;
}
else
{
$this->settings = $newOptions;
}
update_option($this->settingsKey, $this->settings);
return true;
}
function getInputName($optionName, $optionGroupKey = false)
{
if ($this->optionGroupsKeys)
{
if (!$optionGroupKey)
{
trigger_error('$optionGroupKey is required.', E_USER_ERROR);
return;
}
else if (!in_array($optionGroupKey, $this->optionGroupsKeys))
{
trigger_error('Option Group Key "'. $optionGroupKey . '" doesn\'t exist in ' . self::getClass() .'::optionGroupsKeys.', E_USER_ERROR);
return;
}
return $this->settingsKey . '[' . $optionGroupKey .']' . '[' . $optionName .']';
}
return $this->settingsKey . '[' . $optionName .']';
}
}