class.ss_options.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /*
  3. * WordPress Plugin Development Helpers - Options
  4. * @link: http://salamzadeh.net/plugins/urlyar
  5. * @author Sasan Salamzadeh
  6. * @version 1.0
  7. */
  8. /*
  9. Class Description
  10. This is a helper class for WordPress options database.
  11. Minimizes polling to the WP Database
  12. Meant to be used in WordPress plugin Development.
  13. */
  14. if (!class_exists('ss_options')) :
  15. class ss_options
  16. {
  17. /*
  18. *****************************************
  19. *
  20. * Class Variables
  21. *
  22. *****************************************
  23. */
  24. private $default_details = array(), //version and status
  25. $plugin_details = array('version' => NULL),
  26. $option_name = '', //database option name
  27. $options = array(), //working options / saved options
  28. $default_options = array(), //author defaults
  29. $master_list = array();
  30. /*
  31. *****************************************
  32. *
  33. * Constructors
  34. *
  35. *****************************************
  36. */
  37. //php 5.3.3
  38. public function __construct($name, $version, $status = 'stable')
  39. {
  40. $this->ss_options($name, $version, $status);
  41. }
  42. //backward compatibility
  43. public function ss_options($name, $version, $status = 'stable')
  44. {
  45. $this->default_details['version'] = $version;
  46. $this->default_details['status'] = $status;
  47. $this->option_name = $name;
  48. }
  49. /*
  50. *****************************************
  51. *
  52. * Initialization
  53. *
  54. *****************************************
  55. */
  56. /*
  57. *****************************************
  58. * Create Master List
  59. * (Before Saving)
  60. *****************************************
  61. */
  62. private function init_master()
  63. {
  64. $this->master_list = $this->default_details;
  65. $this->master_list['options'] = $this->options;
  66. }
  67. /*
  68. *****************************************
  69. *
  70. * Helpers
  71. *
  72. *****************************************
  73. */
  74. /*
  75. *****************************************
  76. * Refresh List from Database
  77. *****************************************
  78. */
  79. private function refresh_vars()
  80. {
  81. $saved = get_option($this->option_name);
  82. if ($saved) {
  83. $this->options = $saved['options'];
  84. $this->plugin_details['version'] = $saved['version'];
  85. $this->plugin_details['status'] = $saved['status'];
  86. }
  87. }
  88. /*
  89. *****************************************
  90. * Saving List to Database
  91. *****************************************
  92. */
  93. private function save_options()
  94. {
  95. //create save array
  96. $this->init_master();
  97. //Update WordPress DB
  98. update_option($this->option_name, $this->master_list);
  99. //update global variable
  100. $this->refresh_vars();
  101. }
  102. /*
  103. *****************************************
  104. *
  105. * Set and Retrival
  106. *
  107. *****************************************
  108. */
  109. /*
  110. *****************************************
  111. * Refresh and Return Saved options
  112. *****************************************
  113. */
  114. public function saved_options()
  115. {
  116. $this->refresh_vars();
  117. return $this->options;
  118. }
  119. /*
  120. *****************************************
  121. * Public refresh
  122. * Use this instead of refresh_vars.
  123. * This is done for possible future ext.
  124. *****************************************
  125. */
  126. public function refresh()
  127. {
  128. $this->refresh_vars();
  129. }
  130. /*
  131. *****************************************
  132. * Save to DB
  133. *****************************************
  134. */
  135. public function save()
  136. {
  137. $this->save_options();
  138. }
  139. /*
  140. *****************************************
  141. * Return a key value
  142. *
  143. * Option to refresh from db
  144. * Only supports first level keys
  145. *****************************************
  146. */
  147. public function get($key, $refresh = true)
  148. {
  149. if ($refresh) {
  150. $this->refresh_vars();
  151. }
  152. $value = $this->options[$key]??"";
  153. return ($value) ? $value : '';
  154. }
  155. /*
  156. *****************************************
  157. * Set a key value
  158. *
  159. * Option to save to / refresh from db
  160. * Only supports first level keys
  161. *****************************************
  162. */
  163. public function set($key, $value, $save = false, $refresh = false)
  164. {
  165. if ($refresh) {
  166. //update global variable
  167. $this->refresh_vars();
  168. }
  169. $this->options[$key] = $value;
  170. if ($save) {
  171. $this->save_options();
  172. }
  173. return true;
  174. }
  175. /*
  176. *****************************************
  177. *
  178. * Installation and Migration
  179. *
  180. *****************************************
  181. */
  182. /*
  183. *****************************************
  184. * Merging Two Array of Options
  185. * Check for changes and
  186. * determine of save needed
  187. *
  188. * 2nd Array defaulted to "default_options
  189. *****************************************
  190. */
  191. private function options_merge($compare, $base = NULL, $override = false)
  192. {
  193. //returns true when both sides are different and a merge is done
  194. //returns false otherwise..
  195. //allows overrides
  196. if (empty($base)) {
  197. $base = $this->default_options;
  198. }
  199. //Override defaults with saved
  200. if (!empty($compare)) {
  201. foreach ($compare as $key => $option) {
  202. $base[$key] = $option;
  203. }
  204. }
  205. //Check for changes
  206. if ($compare != $base || $override == true) {
  207. $this->options = $base;
  208. return true;
  209. }
  210. return false;
  211. }
  212. /*
  213. *****************************************
  214. * Setting Default Array of Options
  215. *
  216. * If item is an array, import array
  217. * else just set an item value
  218. *****************************************
  219. */
  220. public function set_default($item, $value = '')
  221. {
  222. //reset default
  223. $this->default_options = array();
  224. //check if first item array
  225. if (is_array($item)) {
  226. foreach ($item as $key => $value) {
  227. $this->default_options[$key] = $value;
  228. }
  229. //setting of individual items
  230. } else {
  231. $this->default_options[$item] = $value;
  232. }
  233. }
  234. /*
  235. *****************************************
  236. * Install Options
  237. *****************************************
  238. */
  239. public function install_options()
  240. {
  241. //ensure variables are updated
  242. $this->refresh_vars();
  243. if ($this->options_merge($this->options)) {
  244. $this->save_options();
  245. }
  246. }
  247. /*
  248. *****************************************
  249. * Option Migration
  250. *****************************************
  251. */
  252. public function migrate_options($remove_key = NULL)
  253. {
  254. //ensure variables are updated
  255. $this->refresh_vars();
  256. if (empty($this->plugin_details['version']) ||
  257. ($this->plugin_details['version'] < $this->default_details['version'])
  258. ) {
  259. $this->options = get_option($this->option_name);
  260. if ($remove_key) {
  261. unset($this->options[$remove_key]);
  262. }
  263. if ($this->options_merge($this->options)) {
  264. $this->save_options();
  265. }
  266. }
  267. }
  268. /*
  269. *****************************************
  270. *
  271. * Misc
  272. *
  273. *****************************************
  274. */
  275. /*
  276. *****************************************
  277. * Get Version
  278. * Either Saved DB or Plugin Default
  279. *****************************************
  280. */
  281. public function get_version($default = false)
  282. {
  283. if ($default) {
  284. return $this->default_details['version'];
  285. } else {
  286. return $this->plugin_details['version'];
  287. }
  288. }
  289. /*
  290. *****************************************
  291. * Reset Options to plugin defaults
  292. *
  293. * Option not to save first
  294. * Allows for modification after reset
  295. *****************************************
  296. */
  297. public function reset_default($save = true)
  298. {
  299. $this->options = $this->default_options;
  300. if ($save) {
  301. $this->save_options();
  302. //update global variable
  303. $this->refresh_vars();
  304. }
  305. }
  306. /*
  307. *****************************************
  308. * Value comparators
  309. * plugin detail or option
  310. *****************************************
  311. */
  312. public function option_comparator($name, $value, $default = false)
  313. {
  314. if ($default) {
  315. return ($this->default_options[$name] == $value) ? true : false;
  316. } else {
  317. return ($this->options[$name] == $value) ? true : false;
  318. }
  319. }
  320. public function detail_comparator($name, $value, $default = false)
  321. {
  322. if ($default) {
  323. return ($this->default_details[$name] == $value) ? true : false;
  324. } else {
  325. return ($this->plugin_details[$name] == $value) ? true : false;
  326. }
  327. }
  328. /*
  329. *****************************************
  330. * Bulk saving all options
  331. *
  332. * Not Recommended
  333. * (Not Complete yet)
  334. *****************************************
  335. */
  336. public function bulk_save($item)
  337. {
  338. if ($this->options_merge($item)) {
  339. $this->save_options();
  340. return true;
  341. } else {
  342. return false;
  343. }
  344. }
  345. //end class
  346. }
  347. endif;
  348. ?>