PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/non_pope/class.photocrati_settings_manager.php

https://github.com/Fishgate/privatecollectionswp
PHP | 301 lines | 208 code | 45 blank | 48 comment | 29 complexity | 4e8eb7062afc74b1e05c649a011ce04e MD5 | raw file
  1. <?php
  2. if (!class_exists('C_Photocrati_Settings_Manager_Base')) {
  3. /**
  4. * Provides a base abstraction for a Settings Manager
  5. * Class C_Settings_Manager_Base
  6. */
  7. abstract class C_Photocrati_Settings_Manager_Base implements ArrayAccess
  8. {
  9. static $option_name = 'pope_settings';
  10. protected $_options = array();
  11. protected $_defaults = array();
  12. protected $_option_handlers = array();
  13. abstract function save();
  14. abstract function destroy();
  15. abstract function load();
  16. protected function __construct()
  17. {
  18. $this->load();
  19. }
  20. /**
  21. * Adds a class to handle dynamic options
  22. * @param string $klass
  23. * @param array $options
  24. */
  25. function add_option_handler($klass, $options=array())
  26. {
  27. if (!is_array($options)) $options = array($options);
  28. foreach ($options as $option_name) {
  29. $this->_option_handlers[$option_name] = $klass;
  30. }
  31. }
  32. /**
  33. * Gets a handler used to provide a dynamic option
  34. * @param string $option_name
  35. * @return null|mixed
  36. */
  37. protected function _get_option_handler($option_name, $method='get')
  38. {
  39. $retval = NULL;
  40. if (isset($this->_option_handlers[$option_name])) {
  41. if (!is_object($this->_option_handlers[$option_name])) {
  42. $klass = $this->_option_handlers[$option_name];
  43. $this->_option_handlers[$option_name] = new $klass;
  44. }
  45. $retval = $this->_option_handlers[$option_name];
  46. if (!method_exists($retval, $method)) $retval = NULL;
  47. }
  48. return $retval;
  49. }
  50. /**
  51. * Gets the value of a particular setting
  52. * @param $key
  53. * @param null $default
  54. * @return null
  55. */
  56. function get($key, $default=NULL)
  57. {
  58. $retval = $default;
  59. if (($handler = $this->_get_option_handler($key, 'get'))) {
  60. $retval = $handler->get($key, $default);
  61. }
  62. else if (isset($this->_options[$key])) {
  63. $retval = $this->_options[$key];
  64. }
  65. // In case a stdObject has been passed in as a value, we
  66. // want to only return scalar values or arrays
  67. if (is_object($retval)) $retval = (array) $retval;
  68. return $retval;
  69. }
  70. /**
  71. * Sets a setting to a particular value
  72. * @param string $key
  73. * @param mixed $value
  74. * @return mixed
  75. */
  76. function set($key, $value=NULL, $skip_handlers=FALSE)
  77. {
  78. if (is_object($value)) $value = (array) $value;
  79. if (is_array($key)) {
  80. foreach ($key as $k=>$v) $this->set($k, $v);
  81. }
  82. elseif (!$skip_handlers && ($handler = $this->_get_option_handler($key, 'set'))) {
  83. $handler->set($key, $value);
  84. }
  85. else $this->_options[$key] = $value;
  86. return $this;
  87. }
  88. /**
  89. * Deletes a setting
  90. * @param string $key
  91. */
  92. function delete($key)
  93. {
  94. if (($handler = $this->_get_option_handler($key, 'delete'))) {
  95. $handler->delete($key);
  96. }
  97. else {
  98. unset($this->_options[$key]);
  99. }
  100. }
  101. /**
  102. * Determines if a setting exists or not
  103. * @param $key
  104. * @return bool
  105. */
  106. function is_set($key)
  107. {
  108. return array_key_exists($key, $this->_options);
  109. }
  110. /**
  111. * Alias to is_set()
  112. * @param $key
  113. * @return bool
  114. */
  115. function exists($key)
  116. {
  117. return $this->is_set($key);
  118. }
  119. function does_not_exist($key)
  120. {
  121. return !$this->exists($key);
  122. }
  123. function reset()
  124. {
  125. $this->_options = array();
  126. $this->_defaults = array();
  127. }
  128. /**
  129. * This function does two things:
  130. * a) If a value hasn't been set for the specified key, or it's been set to a previously set
  131. * default value, then set this key to the value specified
  132. * b) Sets a new default value for this key
  133. */
  134. function set_default_value($key, $default)
  135. {
  136. if (!isset($this->_defaults[$key])) $this->_defaults[$key] = $default;
  137. if (is_null($this->get($key, NULL)) OR $this->get($key) == $this->_defaults[$key]) {
  138. $this->set($key, $default);
  139. }
  140. $this->_defaults[$key] = $default;
  141. return $this->get($key);
  142. }
  143. function offsetExists($key)
  144. {
  145. return $this->is_set($key);
  146. }
  147. function offsetGet($key)
  148. {
  149. return $this->get($key);
  150. }
  151. function offsetSet($key, $value)
  152. {
  153. return $this->set($key, $value);
  154. }
  155. function offsetUnset($key)
  156. {
  157. return $this->delete($key);
  158. }
  159. function __get($key)
  160. {
  161. return $this->get($key);
  162. }
  163. function __set($key, $value)
  164. {
  165. return $this->set($key, $value);
  166. }
  167. function __isset($key)
  168. {
  169. return $this->is_set($key);
  170. }
  171. function __toString()
  172. {
  173. return json_encode($this->_options);
  174. }
  175. function __toArray()
  176. {
  177. return $this->_options;
  178. }
  179. function to_array()
  180. {
  181. return $this->__toArray();
  182. }
  183. function to_json()
  184. {
  185. return json_encode($this->_options);
  186. }
  187. function from_json($json)
  188. {
  189. $this->_options = (array)json_decode($json);
  190. }
  191. }
  192. }
  193. if (!class_exists('C_Photocrati_Global_Settings_Manager')) {
  194. class C_Photocrati_Global_Settings_Manager extends C_Photocrati_Settings_Manager_Base
  195. {
  196. static $_instance = NULL;
  197. public static function get_instance()
  198. {
  199. if (is_null(self::$_instance)) {
  200. $klass = get_class();
  201. self::$_instance = new $klass();
  202. }
  203. return self::$_instance;
  204. }
  205. function save()
  206. {
  207. return update_site_option(self::$option_name, $this->to_array());
  208. }
  209. function load()
  210. {
  211. $this->_options = get_site_option(self::$option_name, $this->to_array());
  212. if (!$this->_options) $this->_options = array();
  213. else if (is_string($this->_options)) $this->_options = unserialize($this->_options);
  214. }
  215. function destroy()
  216. {
  217. return delete_site_option(self::$option_name);
  218. }
  219. }
  220. }
  221. if (!class_exists('C_Photocrati_Settings_Manager')) {
  222. class C_Photocrati_Settings_Manager extends C_Photocrati_Settings_Manager_Base
  223. {
  224. static $_instance = NULL;
  225. public static function get_instance()
  226. {
  227. if (is_null(self::$_instance)) {
  228. $klass = get_class();
  229. self::$_instance = new $klass();
  230. }
  231. return self::$_instance;
  232. }
  233. function get($key, $default=NULL)
  234. {
  235. $retval = parent::get($key, NULL);
  236. if (is_null($retval)) {
  237. $retval = C_Photocrati_Global_Settings_Manager::get_instance()->get($key, $default);
  238. }
  239. return $retval;
  240. }
  241. function save()
  242. {
  243. return update_option(self::$option_name, $this->to_array());
  244. }
  245. function load()
  246. {
  247. $this->_options = get_option(self::$option_name, array());
  248. if (!$this->_options) $this->_options = array();
  249. else if (is_string($this->_options)) $this->_options = unserialize($this->_options);
  250. }
  251. function destroy()
  252. {
  253. delete_option(self::$option_name);
  254. }
  255. }
  256. }