FS_Option_Manager

3-layer lazy options manager.

Defined (1)

The class is defined in the following location(s).

/freemius/includes/managers/class-fs-option-manager.php  
  1. class FS_Option_Manager { 
  2. /** 
  3. * @var string 
  4. */ 
  5. private $_id; 
  6. /** 
  7. * @var array 
  8. */ 
  9. private $_options; 
  10. /** 
  11. * @var FS_Logger 
  12. */ 
  13. private $_logger; 
  14.  
  15. /** 
  16. * @var FS_Option_Manager[] 
  17. */ 
  18. private static $_MANAGERS = array(); 
  19.  
  20. /** 
  21. * @author Vova Feldman (@svovaf) 
  22. * @since 1.0.3 
  23. * @param string $id 
  24. * @param bool $load 
  25. */ 
  26. private function __construct( $id, $load = false ) { 
  27. $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_opt_mngr_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  28.  
  29. $this->_logger->entrance(); 
  30. $this->_logger->log( 'id = ' . $id ); 
  31.  
  32. $this->_id = $id; 
  33.  
  34. if ( $load ) { 
  35. $this->load(); 
  36.  
  37. /** 
  38. * @author Vova Feldman (@svovaf) 
  39. * @since 1.0.3 
  40. * @param $id 
  41. * @param $load 
  42. * @return FS_Option_Manager 
  43. */ 
  44. static function get_manager( $id, $load = false ) { 
  45. $id = strtolower( $id ); 
  46.  
  47. if ( ! isset( self::$_MANAGERS[ $id ] ) ) { 
  48. self::$_MANAGERS[ $id ] = new FS_Option_Manager( $id, $load ); 
  49. } // If load required but not yet loaded, load. 
  50. else if ( $load && ! self::$_MANAGERS[ $id ]->is_loaded() ) { 
  51. self::$_MANAGERS[ $id ]->load(); 
  52.  
  53. return self::$_MANAGERS[ $id ]; 
  54.  
  55. private function _get_option_manager_name() { 
  56. // return WP_FS__SLUG . '_' . $this->_id; 
  57. return $this->_id; 
  58.  
  59. /** 
  60. * @author Vova Feldman (@svovaf) 
  61. * @since 1.0.3 
  62. * @param bool $flush 
  63. */ 
  64. function load( $flush = false ) { 
  65. $this->_logger->entrance(); 
  66.  
  67. $option_name = $this->_get_option_manager_name(); 
  68.  
  69. if ( $flush || ! isset( $this->_options ) ) { 
  70. if ( isset( $this->_options ) ) { 
  71. // Clear prev options. 
  72. $this->clear(); 
  73.  
  74. if ( ! WP_FS__DEBUG_SDK ) { 
  75. $this->_options = wp_cache_get( $option_name, WP_FS__SLUG ); 
  76.  
  77. // $this->_logger->info('wp_cache_get = ' . var_export($this->_options, true)); 
  78.  
  79. // if ( is_array( $this->_options ) ) { 
  80. // $this->clear(); 
  81. // } 
  82.  
  83. $cached = true; 
  84.  
  85. if ( empty( $this->_options ) ) { 
  86. $this->_options = get_option( $option_name ); 
  87.  
  88. if ( is_string( $this->_options ) ) { 
  89. $this->_options = json_decode( $this->_options ); 
  90.  
  91. // $this->_logger->info('get_option = ' . var_export($this->_options, true)); 
  92.  
  93. if ( false === $this->_options ) { 
  94. $this->clear(); 
  95.  
  96. $cached = false; 
  97.  
  98. if ( ! WP_FS__DEBUG_SDK && ! $cached ) // Set non encoded cache. 
  99. wp_cache_set( $option_name, $this->_options, WP_FS__SLUG ); 
  100.  
  101. /** 
  102. * @author Vova Feldman (@svovaf) 
  103. * @since 1.0.3 
  104. * @return bool 
  105. */ 
  106. function is_loaded() { 
  107. return isset( $this->_options ); 
  108.  
  109. /** 
  110. * @author Vova Feldman (@svovaf) 
  111. * @since 1.0.3 
  112. * @return bool 
  113. */ 
  114. function is_empty() { 
  115. return ( $this->is_loaded() && false === $this->_options ); 
  116.  
  117. /** 
  118. * @author Vova Feldman (@svovaf) 
  119. * @since 1.0.6 
  120. * @param bool $flush 
  121. */ 
  122. function clear( $flush = false ) { 
  123. $this->_logger->entrance(); 
  124.  
  125. $this->_options = array(); 
  126.  
  127. if ( $flush ) { 
  128. $this->store(); 
  129.  
  130. /** 
  131. * Delete options manager from DB. 
  132. * @author Vova Feldman (@svovaf) 
  133. * @since 1.0.9 
  134. */ 
  135. function delete() { 
  136. delete_option( $this->_get_option_manager_name() ); 
  137.  
  138. /** 
  139. * @author Vova Feldman (@svovaf) 
  140. * @since 1.0.6 
  141. * @param string $option 
  142. * @return bool 
  143. */ 
  144. function has_option( $option ) { 
  145. return array_key_exists( $option, $this->_options ); 
  146.  
  147. /** 
  148. * @author Vova Feldman (@svovaf) 
  149. * @since 1.0.3 
  150. * @param string $option 
  151. * @param mixed $default 
  152. * @return mixed 
  153. */ 
  154. function get_option( $option, $default = null ) { 
  155. $this->_logger->entrance( 'option = ' . $option ); 
  156.  
  157. if ( is_array( $this->_options ) ) { 
  158. return isset( $this->_options[ $option ] ) ? $this->_options[ $option ] : $default; 
  159. } else if ( is_object( $this->_options ) ) { 
  160. return isset( $this->_options->{$option} ) ? $this->_options->{$option} : $default; 
  161.  
  162. return $default; 
  163.  
  164. /** 
  165. * @author Vova Feldman (@svovaf) 
  166. * @since 1.0.3 
  167. * @param string $option 
  168. * @param mixed $value 
  169. * @param bool $flush 
  170. */ 
  171. function set_option( $option, $value, $flush = false ) { 
  172. $this->_logger->entrance( 'option = ' . $option ); 
  173.  
  174. if ( ! $this->is_loaded() ) { 
  175. $this->clear(); 
  176.  
  177. if ( is_array( $this->_options ) ) { 
  178. $this->_options[ $option ] = $value; 
  179. } else if ( is_object( $this->_options ) ) { 
  180. $this->_options->{$option} = $value; 
  181.  
  182. if ( $flush ) { 
  183. $this->store(); 
  184.  
  185. /** 
  186. * Unset option. 
  187. * @author Vova Feldman (@svovaf) 
  188. * @since 1.0.3 
  189. * @param string $option 
  190. * @param bool $flush 
  191. */ 
  192. function unset_option( $option, $flush = false ) { 
  193. $this->_logger->entrance( 'option = ' . $option ); 
  194.  
  195. if ( is_array( $this->_options ) ) { 
  196. if ( ! isset( $this->_options[ $option ] ) ) { 
  197. return; 
  198.  
  199. unset( $this->_options[ $option ] ); 
  200.  
  201. } else if ( is_object( $this->_options ) ) { 
  202. if ( ! isset( $this->_options->{$option} ) ) { 
  203. return; 
  204.  
  205. unset( $this->_options->{$option} ); 
  206.  
  207. if ( $flush ) { 
  208. $this->store(); 
  209.  
  210. /** 
  211. * Dump options to database. 
  212. * @author Vova Feldman (@svovaf) 
  213. * @since 1.0.3 
  214. */ 
  215. function store() { 
  216. $this->_logger->entrance(); 
  217.  
  218. $option_name = $this->_get_option_manager_name(); 
  219.  
  220. if ( $this->_logger->is_on() ) { 
  221. $this->_logger->info( $option_name . ' = ' . var_export( $this->_options, true ) ); 
  222.  
  223. // Update DB. 
  224. update_option( $option_name, $this->_options ); 
  225.  
  226. if ( ! WP_FS__DEBUG_SDK ) { 
  227. wp_cache_set( $option_name, $this->_options, WP_FS__SLUG );