FS_Key_Value_Storage

Class FS_Key_Value_Storage.

Defined (1)

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

/freemius/includes/managers/class-fs-key-value-storage.php  
  1. class FS_Key_Value_Storage implements ArrayAccess, Iterator, Countable { 
  2. /** 
  3. * @var string 
  4. */ 
  5. protected $_id; 
  6. /** 
  7. * @var string 
  8. */ 
  9. protected $_slug; 
  10. /** 
  11. * @var array 
  12. */ 
  13. protected $_data; 
  14.  
  15. /** 
  16. * @var FS_Plugin_Manager[] 
  17. */ 
  18. private static $_instances = array(); 
  19. /** 
  20. * @var FS_Logger 
  21. */ 
  22. protected $_logger; 
  23.  
  24. /** 
  25. * @param string $id 
  26. * @param string $slug 
  27. * @return FS_Key_Value_Storage 
  28. */ 
  29. static function instance( $id, $slug ) { 
  30. $key = $id . ':' . $slug; 
  31. if ( ! isset( self::$_instances[ $key ] ) ) { 
  32. self::$_instances[ $key ] = new FS_Key_Value_Storage( $id, $slug ); 
  33.  
  34. return self::$_instances[ $key ]; 
  35.  
  36. protected function __construct( $id, $slug ) { 
  37. $this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $slug . '_' . $id, WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK ); 
  38.  
  39. $this->_slug = $slug; 
  40. $this->_id = $id; 
  41. $this->load(); 
  42.  
  43. protected function get_option_manager() { 
  44. return FS_Option_Manager::get_manager( WP_FS__ACCOUNTS_OPTION_NAME, true ); 
  45.  
  46. protected function get_all_data() { 
  47. return $this->get_option_manager()->get_option( $this->_id, array() ); 
  48.  
  49. /** 
  50. * Load plugin data from local DB. 
  51. * @author Vova Feldman (@svovaf) 
  52. * @since 1.0.7 
  53. */ 
  54. function load() { 
  55. $all_plugins_data = $this->get_all_data(); 
  56. $this->_data = isset( $all_plugins_data[ $this->_slug ] ) ? 
  57. $all_plugins_data[ $this->_slug ] : 
  58. array(); 
  59.  
  60. /** 
  61. * @author Vova Feldman (@svovaf) 
  62. * @since 1.0.7 
  63. * @param string $key 
  64. * @param mixed $value 
  65. * @param bool $flush 
  66. */ 
  67. function store( $key, $value, $flush = true ) { 
  68. if ( $this->_logger->is_on() ) { 
  69. $this->_logger->entrance( $key . ' = ' . var_export( $value, true ) ); 
  70.  
  71. if ( array_key_exists( $key, $this->_data ) && $value === $this->_data[ $key ] ) { 
  72. // No need to store data if the value wasn't changed. 
  73. return; 
  74.  
  75. $all_data = $this->get_all_data(); 
  76.  
  77. $this->_data[ $key ] = $value; 
  78.  
  79. $all_data[ $this->_slug ] = $this->_data; 
  80.  
  81. $options_manager = $this->get_option_manager(); 
  82. $options_manager->set_option( $this->_id, $all_data, $flush ); 
  83.  
  84. /** 
  85. * @author Vova Feldman (@svovaf) 
  86. * @since 1.0.7 
  87. * @param bool $store 
  88. * @param string[] $exceptions Set of keys to keep and not clear. 
  89. */ 
  90. function clear_all( $store = true, $exceptions = array() ) { 
  91. $new_data = array(); 
  92. foreach ( $exceptions as $key ) { 
  93. if ( isset( $this->_data[ $key ] ) ) { 
  94. $new_data[ $key ] = $this->_data[ $key ]; 
  95.  
  96. $this->_data = $new_data; 
  97.  
  98. if ( $store ) { 
  99. $all_data = $this->get_all_data(); 
  100. $all_data[ $this->_slug ] = $this->_data; 
  101. $options_manager = $this->get_option_manager(); 
  102. $options_manager->set_option( $this->_id, $all_data, true ); 
  103.  
  104. /** 
  105. * Delete key-value storage. 
  106. * @author Vova Feldman (@svovaf) 
  107. * @since 1.0.9 
  108. */ 
  109. function delete() { 
  110. $this->_data = array(); 
  111.  
  112. $all_data = $this->get_all_data(); 
  113. unset( $all_data[ $this->_slug ] ); 
  114. $options_manager = $this->get_option_manager(); 
  115. $options_manager->set_option( $this->_id, $all_data, true ); 
  116.  
  117. /** 
  118. * @author Vova Feldman (@svovaf) 
  119. * @since 1.0.7 
  120. * @param string $key 
  121. * @param bool $store 
  122. */ 
  123. function remove( $key, $store = true ) { 
  124. if ( ! array_key_exists( $key, $this->_data ) ) { 
  125. return; 
  126.  
  127. unset( $this->_data[ $key ] ); 
  128.  
  129. if ( $store ) { 
  130. $all_data = $this->get_all_data(); 
  131. $all_data[ $this->_slug ] = $this->_data; 
  132. $options_manager = $this->get_option_manager(); 
  133. $options_manager->set_option( $this->_id, $all_data, true ); 
  134.  
  135. /** 
  136. * @author Vova Feldman (@svovaf) 
  137. * @since 1.0.7 
  138. * @param string $key 
  139. * @param mixed $default 
  140. * @return bool|\FS_Plugin 
  141. */ 
  142. function get( $key, $default = false ) { 
  143. return array_key_exists( $key, $this->_data ) ? 
  144. $this->_data[ $key ] : 
  145. $default; 
  146.  
  147.  
  148. /** ArrayAccess + Magic Access (better for refactoring) 
  149. -----------------------------------------------------------------------------------*/ 
  150. function __set( $k, $v ) { 
  151. $this->store( $k, $v ); 
  152.  
  153. function __isset( $k ) { 
  154. return array_key_exists( $k, $this->_data ); 
  155.  
  156. function __unset( $k ) { 
  157. $this->remove( $k ); 
  158.  
  159. function __get( $k ) { 
  160. return $this->get( $k, null ); 
  161.  
  162. function offsetSet( $k, $v ) { 
  163. if ( is_null( $k ) ) { 
  164. throw new Exception( 'Can\'t append value to request params.' ); 
  165. } else { 
  166. $this->{$k} = $v; 
  167.  
  168. function offsetExists( $k ) { 
  169. return array_key_exists( $k, $this->_data ); 
  170.  
  171. function offsetUnset( $k ) { 
  172. unset( $this->$k ); 
  173.  
  174. function offsetGet( $k ) { 
  175. return $this->get( $k, null ); 
  176.  
  177. /** 
  178. * (PHP 5 >= 5.0.0)<br/> 
  179. * Return the current element 
  180. * @link http://php.net/manual/en/iterator.current.php 
  181. * @return mixed Can return any type. 
  182. */ 
  183. public function current() { 
  184. return current( $this->_data ); 
  185.  
  186. /** 
  187. * (PHP 5 >= 5.0.0)<br/> 
  188. * Move forward to next element 
  189. * @link http://php.net/manual/en/iterator.next.php 
  190. * @return void Any returned value is ignored. 
  191. */ 
  192. public function next() { 
  193. next( $this->_data ); 
  194.  
  195. /** 
  196. * (PHP 5 >= 5.0.0)<br/> 
  197. * Return the key of the current element 
  198. * @link http://php.net/manual/en/iterator.key.php 
  199. * @return mixed scalar on success, or null on failure. 
  200. */ 
  201. public function key() { 
  202. return key( $this->_data ); 
  203.  
  204. /** 
  205. * (PHP 5 >= 5.0.0)<br/> 
  206. * Checks if current position is valid 
  207. * @link http://php.net/manual/en/iterator.valid.php 
  208. * @return boolean The return value will be casted to boolean and then evaluated. 
  209. * Returns true on success or false on failure. 
  210. */ 
  211. public function valid() { 
  212. $key = key( $this->_data ); 
  213.  
  214. return ( $key !== null && $key !== false ); 
  215.  
  216. /** 
  217. * (PHP 5 >= 5.0.0)<br/> 
  218. * Rewind the Iterator to the first element 
  219. * @link http://php.net/manual/en/iterator.rewind.php 
  220. * @return void Any returned value is ignored. 
  221. */ 
  222. public function rewind() { 
  223. reset( $this->_data ); 
  224.  
  225. /** 
  226. * (PHP 5 >= 5.1.0)<br/> 
  227. * Count elements of an object 
  228. * @link http://php.net/manual/en/countable.count.php 
  229. * @return int The custom count as an integer. 
  230. * </p> 
  231. * <p> 
  232. * The return value is cast to an integer. 
  233. */ 
  234. public function count() { 
  235. return count( $this->_data );