Created
April 8, 2010 06:35
-
-
Save whalesalad/359843 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @package WordPress | |
| * @subpackage Settings | |
| */ | |
| /* | |
| Instantiate it with a key, like $settings = new Settings('myplugin_settings'); | |
| Then do things like, set a setting: | |
| $settings->set('default_language', 'english'); | |
| Which will overwrite a setting if it's already set. | |
| Or get a setting: | |
| $settings->get('default_language'); | |
| Which will return false if it's not set. | |
| Once all your settings have been manipulated, the save() method will write it to the db. | |
| $settings->save(); | |
| A simple reset method will nullify everything. Useful for testing. | |
| This is a LOT BETTER than what a lot of devs do, creating a new row in the settings db for each and every entry... which is retarded. | |
| At the end of a file is an example of subclassing it to not need to enter a key all the time, useful on a big project where you want to get access to your settings quickly. Of course a good practice is to keep one global setting instance, but sometimes that doesn't always work. | |
| */ | |
| class Settings { | |
| private $settings; | |
| public $key; | |
| function __construct($key = 'default_settings') { | |
| if (!isset($this->key)) $this->key = $key; | |
| $this->settings = array(); | |
| $this->load(); | |
| } | |
| function set($key, $value = null) { | |
| $this->settings[$key] = $value; | |
| } | |
| function get($key) { | |
| if (isset($this->settings[$key]) and $this->settings[$key] != '') { | |
| return $this->settings[$key]; | |
| } else { | |
| return false; | |
| } | |
| } | |
| function load() { | |
| $loaded = maybe_unserialize(get_option($this->key)); | |
| if (!empty($loaded) && is_array($loaded)) { | |
| foreach ($loaded as $setting => $value) | |
| $this->settings[$setting] = $value; | |
| } | |
| } | |
| function save() { | |
| update_option($this->key, $this->settings); | |
| } | |
| function reset() { | |
| update_option($this->key, NULL); | |
| } | |
| } | |
| class ArbeskoSettings extends Settings { | |
| function __construct() { | |
| $this->key = 'arbesko_settings'; | |
| parent::__construct(); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment