| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- /*
- * WordPress Plugin Development Helpers - Options
- * @link: http://salamzadeh.net/plugins/urlyar
- * @author Sasan Salamzadeh
- * @version 1.0
- */
- /*
- Class Description
- This is a helper class for WordPress options database.
- Minimizes polling to the WP Database
- Meant to be used in WordPress plugin Development.
- */
- if (!class_exists('ss_options')) :
- class ss_options
- {
- /*
- *****************************************
- *
- * Class Variables
- *
- *****************************************
- */
- private $default_details = array(), //version and status
- $plugin_details = array('version' => NULL),
- $option_name = '', //database option name
- $options = array(), //working options / saved options
- $default_options = array(), //author defaults
- $master_list = array();
- /*
- *****************************************
- *
- * Constructors
- *
- *****************************************
- */
- //php 5.3.3
- public function __construct($name, $version, $status = 'stable')
- {
- $this->ss_options($name, $version, $status);
- }
- //backward compatibility
- public function ss_options($name, $version, $status = 'stable')
- {
- $this->default_details['version'] = $version;
- $this->default_details['status'] = $status;
- $this->option_name = $name;
- }
- /*
- *****************************************
- *
- * Initialization
- *
- *****************************************
- */
- /*
- *****************************************
- * Create Master List
- * (Before Saving)
- *****************************************
- */
- private function init_master()
- {
- $this->master_list = $this->default_details;
- $this->master_list['options'] = $this->options;
- }
- /*
- *****************************************
- *
- * Helpers
- *
- *****************************************
- */
- /*
- *****************************************
- * Refresh List from Database
- *****************************************
- */
- private function refresh_vars()
- {
- $saved = get_option($this->option_name);
- if ($saved) {
- $this->options = $saved['options'];
- $this->plugin_details['version'] = $saved['version'];
- $this->plugin_details['status'] = $saved['status'];
- }
- }
- /*
- *****************************************
- * Saving List to Database
- *****************************************
- */
- private function save_options()
- {
- //create save array
- $this->init_master();
- //Update WordPress DB
- update_option($this->option_name, $this->master_list);
- //update global variable
- $this->refresh_vars();
- }
- /*
- *****************************************
- *
- * Set and Retrival
- *
- *****************************************
- */
- /*
- *****************************************
- * Refresh and Return Saved options
- *****************************************
- */
- public function saved_options()
- {
- $this->refresh_vars();
- return $this->options;
- }
- /*
- *****************************************
- * Public refresh
- * Use this instead of refresh_vars.
- * This is done for possible future ext.
- *****************************************
- */
- public function refresh()
- {
- $this->refresh_vars();
- }
- /*
- *****************************************
- * Save to DB
- *****************************************
- */
- public function save()
- {
- $this->save_options();
- }
- /*
- *****************************************
- * Return a key value
- *
- * Option to refresh from db
- * Only supports first level keys
- *****************************************
- */
- public function get($key, $refresh = true)
- {
- if ($refresh) {
- $this->refresh_vars();
- }
- $value = $this->options[$key]??"";
- return ($value) ? $value : '';
- }
- /*
- *****************************************
- * Set a key value
- *
- * Option to save to / refresh from db
- * Only supports first level keys
- *****************************************
- */
- public function set($key, $value, $save = false, $refresh = false)
- {
- if ($refresh) {
- //update global variable
- $this->refresh_vars();
- }
- $this->options[$key] = $value;
- if ($save) {
- $this->save_options();
- }
- return true;
- }
- /*
- *****************************************
- *
- * Installation and Migration
- *
- *****************************************
- */
- /*
- *****************************************
- * Merging Two Array of Options
- * Check for changes and
- * determine of save needed
- *
- * 2nd Array defaulted to "default_options
- *****************************************
- */
- private function options_merge($compare, $base = NULL, $override = false)
- {
- //returns true when both sides are different and a merge is done
- //returns false otherwise..
- //allows overrides
- if (empty($base)) {
- $base = $this->default_options;
- }
- //Override defaults with saved
- if (!empty($compare)) {
- foreach ($compare as $key => $option) {
- $base[$key] = $option;
- }
- }
- //Check for changes
- if ($compare != $base || $override == true) {
- $this->options = $base;
- return true;
- }
- return false;
- }
- /*
- *****************************************
- * Setting Default Array of Options
- *
- * If item is an array, import array
- * else just set an item value
- *****************************************
- */
- public function set_default($item, $value = '')
- {
- //reset default
- $this->default_options = array();
- //check if first item array
- if (is_array($item)) {
- foreach ($item as $key => $value) {
- $this->default_options[$key] = $value;
- }
- //setting of individual items
- } else {
- $this->default_options[$item] = $value;
- }
- }
- /*
- *****************************************
- * Install Options
- *****************************************
- */
- public function install_options()
- {
- //ensure variables are updated
- $this->refresh_vars();
- if ($this->options_merge($this->options)) {
- $this->save_options();
- }
- }
- /*
- *****************************************
- * Option Migration
- *****************************************
- */
- public function migrate_options($remove_key = NULL)
- {
- //ensure variables are updated
- $this->refresh_vars();
- if (empty($this->plugin_details['version']) ||
- ($this->plugin_details['version'] < $this->default_details['version'])
- ) {
- $this->options = get_option($this->option_name);
- if ($remove_key) {
- unset($this->options[$remove_key]);
- }
- if ($this->options_merge($this->options)) {
- $this->save_options();
- }
- }
- }
- /*
- *****************************************
- *
- * Misc
- *
- *****************************************
- */
- /*
- *****************************************
- * Get Version
- * Either Saved DB or Plugin Default
- *****************************************
- */
- public function get_version($default = false)
- {
- if ($default) {
- return $this->default_details['version'];
- } else {
- return $this->plugin_details['version'];
- }
- }
- /*
- *****************************************
- * Reset Options to plugin defaults
- *
- * Option not to save first
- * Allows for modification after reset
- *****************************************
- */
- public function reset_default($save = true)
- {
- $this->options = $this->default_options;
- if ($save) {
- $this->save_options();
- //update global variable
- $this->refresh_vars();
- }
- }
- /*
- *****************************************
- * Value comparators
- * plugin detail or option
- *****************************************
- */
- public function option_comparator($name, $value, $default = false)
- {
- if ($default) {
- return ($this->default_options[$name] == $value) ? true : false;
- } else {
- return ($this->options[$name] == $value) ? true : false;
- }
- }
- public function detail_comparator($name, $value, $default = false)
- {
- if ($default) {
- return ($this->default_details[$name] == $value) ? true : false;
- } else {
- return ($this->plugin_details[$name] == $value) ? true : false;
- }
- }
- /*
- *****************************************
- * Bulk saving all options
- *
- * Not Recommended
- * (Not Complete yet)
- *****************************************
- */
- public function bulk_save($item)
- {
- if ($this->options_merge($item)) {
- $this->save_options();
- return true;
- } else {
- return false;
- }
- }
- //end class
- }
- endif;
- ?>
|