Нет описания

Options.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Utils_Options
  10. */
  11. class PUM_Utils_Options {
  12. /**
  13. * Unique Prefix per plugin.
  14. *
  15. * @var string
  16. */
  17. public static $_prefix = 'popmake_';
  18. /**
  19. * Keeps static copy of the options during runtime.
  20. *
  21. * @var null|array
  22. */
  23. private static $_data;
  24. /**
  25. * Initialize Options on run.
  26. *
  27. * @param bool $force
  28. */
  29. public static function init( $force = false ) {
  30. global $popmake_options;
  31. if ( ! isset( self::$_data ) || $force ) {
  32. self::$_data = self::get_all();
  33. /** @deprecated 1.7.0 */
  34. $popmake_options = self::$_data;
  35. }
  36. }
  37. /**
  38. * Get Settings
  39. *
  40. * Retrieves all plugin settings
  41. *
  42. * @return array settings
  43. */
  44. public static function get_all() {
  45. $settings = get_option( self::$_prefix . 'settings', array() );
  46. if ( ! is_array( $settings ) ) {
  47. $settings = array();
  48. }
  49. /* @deprecated filter. */
  50. $settings = apply_filters( 'popmake_get_settings', $settings );
  51. return apply_filters( self::$_prefix . 'get_options', $settings );
  52. }
  53. /**
  54. * Get an option
  55. *
  56. * Looks to see if the specified setting exists, returns default if not
  57. *
  58. * @param string $key
  59. * @param bool $default
  60. *
  61. * @return mixed
  62. */
  63. public static function get( $key = '', $default = false ) {
  64. // Passive initialization.
  65. self::init();
  66. $value = isset( self::$_data[ $key ] ) ? self::$_data[ $key ] : $default;
  67. return apply_filters( self::$_prefix . 'get_option', $value, $key, $default );
  68. }
  69. /**
  70. * Update an option
  71. *
  72. * Updates an setting value in both the db and the global variable.
  73. * Warning: Passing in an empty, false or null string value will remove
  74. * the key from the _options array.
  75. *
  76. * @param string $key The Key to update
  77. * @param string|bool|int $value The value to set the key to
  78. *
  79. * @return boolean True if updated, false if not.
  80. */
  81. public static function update( $key = '', $value = false ) {
  82. // Passive initialization.
  83. self::init();
  84. // If no key, exit
  85. if ( empty( $key ) ) {
  86. return false;
  87. }
  88. if ( empty( $value ) ) {
  89. $remove_option = self::delete( $key );
  90. return $remove_option;
  91. }
  92. // First let's grab the current settings
  93. $options = get_option( self::$_prefix . 'settings' );
  94. // Let's let devs alter that value coming in
  95. $value = apply_filters( self::$_prefix . 'update_option', $value, $key );
  96. // Next let's try to update the value
  97. $options[ $key ] = $value;
  98. $did_update = update_option( self::$_prefix . 'settings', $options );
  99. // If it updated, let's update the global variable
  100. if ( $did_update ) {
  101. self::$_data[ $key ] = $value;
  102. }
  103. return $did_update;
  104. }
  105. /**
  106. * Update the entire settings array from a new array.
  107. *
  108. * @param array $new_options
  109. *
  110. * @return bool
  111. */
  112. public static function update_all( $new_options = array() ) {
  113. // First let's grab the current settings
  114. $options = get_option( self::$_prefix . 'settings' );
  115. // Lets merge options that may exist previously that are not existing now.
  116. $new_options = wp_parse_args( $new_options, $options );
  117. $did_update = update_option( self::$_prefix . 'settings', $new_options );
  118. // If it updated, let's update the global variable
  119. if ( $did_update ) {
  120. self::$_data = $new_options;
  121. }
  122. return $did_update;
  123. }
  124. /**
  125. * Merge the new options into the settings array.
  126. *
  127. * @param array $new_options
  128. *
  129. * @return bool
  130. */
  131. public static function merge( $new_options = array() ) {
  132. $options = self::get_all();
  133. // Merge new options.
  134. foreach ( $new_options as $key => $val ) {
  135. $options[ $key ] = ! empty( $val ) ? $val : false;
  136. }
  137. $did_update = update_option( self::$_prefix . 'settings', $options );
  138. // If it updated, let's update the global variable
  139. if ( $did_update ) {
  140. self::$_data = $options;
  141. }
  142. return $did_update;
  143. }
  144. /**
  145. * Remove an option or multiple
  146. *
  147. * Removes a setting value in both the db and the global variable.
  148. *
  149. * @param string|array $keys The Key/s to delete
  150. *
  151. * @return boolean True if updated, false if not.
  152. */
  153. public static function delete( $keys = '' ) {
  154. // Passive initialization.
  155. self::init();
  156. // If no key, exit
  157. if ( empty( $keys ) ) {
  158. return false;
  159. } else if ( is_string( $keys ) ) {
  160. $keys = array( $keys );
  161. }
  162. // First let's grab the current settings
  163. $options = get_option( self::$_prefix . 'settings' );
  164. // Remove each key/value pair.
  165. foreach ( $keys as $key ) {
  166. if ( isset( $options[ $key ] ) ) {
  167. unset( $options[ $key ] );
  168. }
  169. }
  170. $did_update = update_option( self::$_prefix . 'settings', $options );
  171. // If it updated, let's update the global variable
  172. if ( $did_update ) {
  173. self::$_data = $options;
  174. }
  175. return $did_update;
  176. }
  177. /**
  178. * Remaps option keys.
  179. *
  180. * @param array $remap_array an array of $old_key => $new_key values.
  181. *
  182. * @return bool
  183. */
  184. public static function remap_keys( $remap_array = array() ) {
  185. $options = self::get_all();
  186. foreach ( $remap_array as $key => $new_key ) {
  187. $value = self::get( $key, false );
  188. if ( ! empty( $value ) ) {
  189. $options[ $new_key ] = $value;
  190. }
  191. unset( $options[ $key ] );
  192. }
  193. $did_update = update_option( self::$_prefix . 'settings', $options );
  194. // If it updated, let's update the global variable
  195. if ( $did_update ) {
  196. self::$_data = $options;
  197. }
  198. return $did_update;
  199. }
  200. }