PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/types/library/toolset/toolset-common/inc/toolset.settings.class.php

https://gitlab.com/Fraternal-Group/fraternal
PHP | 468 lines | 168 code | 78 blank | 222 comment | 31 complexity | 3e8a6af860de89f4a384d79445e4905e MD5 | raw file
  1. <?php
  2. /**
  3. * Toolset Settings class
  4. *
  5. * It implements both ArrayAccess and dynamic properties. ArrayAccess is deprecated.
  6. *
  7. * @since 2.0
  8. *
  9. * @property string $show_admin_bar_shortcut
  10. * @property string $shortcodes_generator
  11. */
  12. class Toolset_Settings implements ArrayAccess {
  13. /**
  14. * WP Option Name for Views settings.
  15. */
  16. const OPTION_NAME = 'toolset_options';
  17. /* ************************************************************************* *\
  18. SETTING NAMES
  19. \* ************************************************************************* */
  20. /**
  21. * Determine whether frontend admin bar menu should be displayed.
  22. *
  23. * String value, 'on' or 'off'.
  24. *
  25. * Defaults to 'on'.
  26. *
  27. * @since 2.0
  28. */
  29. const ADMIN_BAR_CREATE_EDIT = 'show_admin_bar_shortcut';
  30. /**
  31. * Determine whether the backend shortcode generator admin bar menu should be displayed.
  32. *
  33. * String value, 'unset', 'disable', 'editor' or 'always'.
  34. *
  35. * Defaults to 'unset'.
  36. *
  37. * @since 2.0
  38. */
  39. const ADMIN_BAR_SHORTCODES_GENERATOR = 'shortcodes_generator';
  40. /**
  41. * List of Types postmeta fields that we want to index in Relevanssi.
  42. *
  43. * Array.
  44. *
  45. * Defaults to an empty array.
  46. *
  47. * @since 2.2
  48. */
  49. const RELEVANSSI_FIELDS_TO_INDEX = 'relevanssi_fields_to_index';
  50. /* ************************************************************************* *\
  51. SINGLETON
  52. \* ************************************************************************* */
  53. /**
  54. * @var Toolset_Settings Instance of Toolset_Settings.
  55. */
  56. private static $instance = null;
  57. /**
  58. * @return Toolset_Settings The instance of Toolset_Settings.
  59. */
  60. public static function get_instance() {
  61. if( null == Toolset_Settings::$instance ) {
  62. Toolset_Settings::$instance = new Toolset_Settings();
  63. }
  64. return Toolset_Settings::$instance;
  65. }
  66. public static function clear_instance() {
  67. if ( Toolset_Settings::$instance ) {
  68. Toolset_Settings::$instance = null;
  69. }
  70. }
  71. /* ************************************************************************* *\
  72. DEFAULTS
  73. \* ************************************************************************* */
  74. /**
  75. * @var array Default setting values.
  76. * @todo reformat and turn strings into documented constants
  77. */
  78. protected static $defaults = array(
  79. Toolset_Settings::ADMIN_BAR_CREATE_EDIT => 'on',
  80. Toolset_Settings::ADMIN_BAR_SHORTCODES_GENERATOR => 'unset',
  81. Toolset_Settings::RELEVANSSI_FIELDS_TO_INDEX => array(),
  82. );
  83. /**
  84. * @return array Associative array of default values for settings.
  85. */
  86. public function get_defaults() {
  87. return Toolset_Settings::$defaults;
  88. }
  89. /**
  90. * Toolset_Settings constructor.
  91. *
  92. * @todo make this private
  93. */
  94. protected function __construct() {
  95. $this->load_settings();
  96. }
  97. /* ************************************************************************* *\
  98. OPTION LOADING AND SAVING
  99. \* ************************************************************************* */
  100. private $settings = null;
  101. /**
  102. * Load settings from the database.
  103. */
  104. private function load_settings() {
  105. $this->settings = get_option( self::OPTION_NAME );
  106. if ( ! is_array( $this->settings ) ) {
  107. $this->settings = array(); // Defaults will be used in this case.
  108. }
  109. }
  110. /**
  111. * Persists settings in the database
  112. *
  113. * @todo Consider some optimalization - only update options that have changed.
  114. */
  115. public function save() {
  116. update_option( self::OPTION_NAME, $this->settings );
  117. }
  118. /* ************************************************************************* *\
  119. ArrayAccess IMPLEMENTATION
  120. \* ************************************************************************* */
  121. /**
  122. * isset() for ArrayAccess interface.
  123. *
  124. * @param mixed $offset setting name
  125. * @return bool
  126. */
  127. public function offsetExists( $offset ) {
  128. return isset( $this->settings[ $offset ] );
  129. }
  130. /**
  131. * Getter for ArrayAccess interface.
  132. *
  133. * @param mixed $offset setting name
  134. * @return mixed setting value
  135. */
  136. public function offsetGet( $offset ) {
  137. if ( $offset ) {
  138. return $this->get( $offset );
  139. } else {
  140. return null;
  141. }
  142. }
  143. /**
  144. * Setter for ArrayAccess interface.
  145. *
  146. * @param mixed $offset
  147. * @param mixed $value
  148. */
  149. public function offsetSet( $offset, $value ) {
  150. $this->set( $offset, $value );
  151. }
  152. /**
  153. * unset() for ArrayAccess interface.
  154. *
  155. * @param mixed $offset
  156. */
  157. public function offsetUnset( $offset ) {
  158. if ( isset( $this->settings[ $offset ] ) ) {
  159. unset( $this->settings[ $offset ] );
  160. }
  161. }
  162. /* ************************************************************************* *\
  163. MAGIC PROPERTIES
  164. \* ************************************************************************* */
  165. /**
  166. * PHP dynamic setter.
  167. *
  168. * @param mixed $key
  169. * @return mixed
  170. */
  171. public function __get( $key ) {
  172. return $this->get( $key );
  173. }
  174. /**
  175. * PHP dynamic setter.
  176. *
  177. * @param string $key
  178. * @param mixed $value
  179. */
  180. public function __set( $key, $value ) {
  181. $this->set( $key, $value );
  182. }
  183. /**
  184. * PHP dynamic fields unset() method support
  185. * @param string $key
  186. */
  187. public function __unset( $key ) {
  188. if ( $this->offsetExists( $key ) ) {
  189. $this->offsetUnset( $key );
  190. }
  191. }
  192. /**
  193. * PHP dynamic support for isset($this->name)
  194. * @param string $key
  195. * @return boolean
  196. */
  197. public function __isset( $key ) {
  198. return $this->offsetExists( $key );
  199. }
  200. /* ************************************************************************* *\
  201. GENERIC GET/SET METHODS
  202. \* ************************************************************************* */
  203. /**
  204. * Obtain a value for a setting (or all settings).
  205. *
  206. * @param string $key name of the setting to retrieve
  207. * @return mixed value of the key or an array with all key-value pairs
  208. */
  209. public function get( $key = null ) {
  210. if ( $key ) {
  211. // Retrieve one setting
  212. $method_name = '_get_' . $key;
  213. if ( method_exists( $this, $method_name ) ) {
  214. // Use custom getter if it exists
  215. return $this->$method_name();
  216. } else {
  217. return $this->get_raw_value( $key );
  218. }
  219. } else {
  220. // Retrieve all settings
  221. return wp_parse_args( $this->settings, Toolset_Settings::$defaults );
  222. }
  223. }
  224. /**
  225. * Get "raw" value from settings or default settings, without taking custom getters into account.
  226. *
  227. * @param string $key Setting name
  228. * @return null|mixed Setting value or null if it's not defined anywhere.
  229. */
  230. private function get_raw_value( $key ) {
  231. if ( isset( $this->settings[ $key ] ) ) {
  232. // Return user-set value, if available
  233. return $this->settings[ $key ];
  234. } elseif ( isset( Toolset_Settings::$defaults[ $key ] ) ) {
  235. // Use default value, if available
  236. return Toolset_Settings::$defaults[ $key ];
  237. } else {
  238. // There isn't any key like that
  239. return null;
  240. }
  241. }
  242. /**
  243. * Set Setting(s).
  244. *
  245. * Usage:
  246. * One key-value pair
  247. * set('key', 'value');
  248. *
  249. * Multiple key-value pairs
  250. * set( array('key1' => 'value1', 'key2' => 'value2' );
  251. *
  252. * @param mixed $param1 name of the setting or an array with name-value pairs of the settings (bulk set)
  253. * @param mixed $param2 value of the setting
  254. */
  255. public function set( $param1, $param2 = null ) {
  256. if ( is_array( $param1 ) ) {
  257. foreach( $param1 as $key => $value ) {
  258. $this->settings[ $key ] = $value;
  259. }
  260. } else if (
  261. is_object( $param1 )
  262. && is_a( $param1, 'Toolset_Settings' )
  263. ) {
  264. // DO NOTHING.
  265. // It's assigned already.
  266. } else if (
  267. is_string( $param1 )
  268. || is_integer( $param1 )
  269. ) {
  270. $key = $param1;
  271. $value = $param2;
  272. // Use custom setter if it exists.
  273. $method_name = '_set_' . $key;
  274. if ( method_exists( $this, $method_name ) ) {
  275. $this->$method_name( $value );
  276. } else {
  277. // Fall back to array access mode
  278. $this->settings[ $key ] = $value;
  279. }
  280. }
  281. }
  282. /**
  283. * Find out whether we have any knowledge about setting of given name.
  284. *
  285. * Looks for it's value, default value or for custom getter.
  286. *
  287. * @param string $key Setting name.
  288. * @return bool True if setting seems to exist.
  289. */
  290. public function has_setting( $key ) {
  291. return (
  292. isset( $this->settings[ $key ] )
  293. || isset( Toolset_Settings::$defaults[ $key ] )
  294. || method_exists( $this, '_get_' . $key )
  295. );
  296. }
  297. /* ************************************************************************* *\
  298. CUSTOM GETTERS AND SETTERS
  299. \* ************************************************************************* */
  300. /**
  301. * Safe show_admin_bar_shortcut getter, allways returns a valid value.
  302. *
  303. * @since 2.0
  304. */
  305. protected function _get_show_admin_bar_shortcut() {
  306. $value = $this->get_raw_value( Toolset_Settings::ADMIN_BAR_CREATE_EDIT );
  307. if ( ! $this->_is_valid_show_admin_bar_shortcut( $value ) ) {
  308. return Toolset_Settings::$defaults[ Toolset_Settings::ADMIN_BAR_CREATE_EDIT ];
  309. }
  310. return $value;
  311. }
  312. /**
  313. * Safe show_admin_bar_shortcut setter.
  314. *
  315. * @since 2.0
  316. */
  317. protected function _set_show_admin_bar_shortcut( $value ) {
  318. if ( $this->_is_valid_show_admin_bar_shortcut( $value ) ) {
  319. $this->settings[ Toolset_Settings::ADMIN_BAR_CREATE_EDIT ] = $value;
  320. }
  321. }
  322. /**
  323. * Helper validation for show_admin_bar_shortcut.
  324. *
  325. * @since 2.0
  326. */
  327. protected function _is_valid_show_admin_bar_shortcut( $value ) {
  328. return in_array( $value, array( 'on', 'off' ) );
  329. }
  330. /**
  331. * Safe shortcodes_generator getter, allways returns a valid value.
  332. *
  333. * @since 2.0
  334. */
  335. protected function _get_shortcodes_generator() {
  336. $value = $this->get_raw_value( Toolset_Settings::ADMIN_BAR_SHORTCODES_GENERATOR );
  337. if ( ! $this->_is_valid_shortcodes_generator( $value ) ) {
  338. return Toolset_Settings::$defaults[ Toolset_Settings::ADMIN_BAR_SHORTCODES_GENERATOR ];
  339. }
  340. return $value;
  341. }
  342. /**
  343. * Safe shortcodes_generator setter.
  344. *
  345. * @since 2.0
  346. */
  347. protected function _set_shortcodes_generator( $value ) {
  348. if ( $this->_is_valid_shortcodes_generator( $value ) ) {
  349. $this->settings[ Toolset_Settings::ADMIN_BAR_SHORTCODES_GENERATOR ] = $value;
  350. }
  351. }
  352. /**
  353. * Helper validation for shortcodes_generator.
  354. *
  355. * @since 2.0
  356. */
  357. protected function _is_valid_shortcodes_generator( $value ) {
  358. return in_array( $value, array( 'unset', 'disable', 'editor', 'always' ) );
  359. }
  360. /**
  361. * Safe shortcodes_generator getter, allways returns a valid value.
  362. *
  363. * @since 2.0
  364. */
  365. protected function _get_relevanssi_fields_to_index() {
  366. $value = $this->get_raw_value( Toolset_Settings::RELEVANSSI_FIELDS_TO_INDEX );
  367. if ( ! $this->_is_valid_relevanssi_fields_to_index( $value ) ) {
  368. return Toolset_Settings::$defaults[ Toolset_Settings::RELEVANSSI_FIELDS_TO_INDEX ];
  369. }
  370. return $value;
  371. }
  372. /**
  373. * Safe shortcodes_generator setter.
  374. *
  375. * @since 2.0
  376. */
  377. protected function _set_relevanssi_fields_to_index( $value ) {
  378. if ( $this->_is_valid_relevanssi_fields_to_index( $value ) ) {
  379. $this->settings[ Toolset_Settings::RELEVANSSI_FIELDS_TO_INDEX ] = $value;
  380. }
  381. }
  382. /**
  383. * Helper validation for shortcodes_generator.
  384. *
  385. * @since 2.0
  386. */
  387. protected function _is_valid_relevanssi_fields_to_index( $value ) {
  388. return is_array( $value );
  389. }
  390. }