StateStore.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. if ( !class_exists('Puc_v4p11_StateStore', false) ):
  3. class Puc_v4p11_StateStore {
  4. /**
  5. * @var int Last update check timestamp.
  6. */
  7. protected $lastCheck = 0;
  8. /**
  9. * @var string Version number.
  10. */
  11. protected $checkedVersion = '';
  12. /**
  13. * @var Puc_v4p11_Update|null Cached update.
  14. */
  15. protected $update = null;
  16. /**
  17. * @var string Site option name.
  18. */
  19. private $optionName = '';
  20. /**
  21. * @var bool Whether we've already tried to load the state from the database.
  22. */
  23. private $isLoaded = false;
  24. public function __construct($optionName) {
  25. $this->optionName = $optionName;
  26. }
  27. /**
  28. * Get time elapsed since the last update check.
  29. *
  30. * If there are no recorded update checks, this method returns a large arbitrary number
  31. * (i.e. time since the Unix epoch).
  32. *
  33. * @return int Elapsed time in seconds.
  34. */
  35. public function timeSinceLastCheck() {
  36. $this->lazyLoad();
  37. return time() - $this->lastCheck;
  38. }
  39. /**
  40. * @return int
  41. */
  42. public function getLastCheck() {
  43. $this->lazyLoad();
  44. return $this->lastCheck;
  45. }
  46. /**
  47. * Set the time of the last update check to the current timestamp.
  48. *
  49. * @return $this
  50. */
  51. public function setLastCheckToNow() {
  52. $this->lazyLoad();
  53. $this->lastCheck = time();
  54. return $this;
  55. }
  56. /**
  57. * @return null|Puc_v4p11_Update
  58. */
  59. public function getUpdate() {
  60. $this->lazyLoad();
  61. return $this->update;
  62. }
  63. /**
  64. * @param Puc_v4p11_Update|null $update
  65. * @return $this
  66. */
  67. public function setUpdate(Puc_v4p11_Update $update = null) {
  68. $this->lazyLoad();
  69. $this->update = $update;
  70. return $this;
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function getCheckedVersion() {
  76. $this->lazyLoad();
  77. return $this->checkedVersion;
  78. }
  79. /**
  80. * @param string $version
  81. * @return $this
  82. */
  83. public function setCheckedVersion($version) {
  84. $this->lazyLoad();
  85. $this->checkedVersion = strval($version);
  86. return $this;
  87. }
  88. /**
  89. * Get translation updates.
  90. *
  91. * @return array
  92. */
  93. public function getTranslations() {
  94. $this->lazyLoad();
  95. if ( isset($this->update, $this->update->translations) ) {
  96. return $this->update->translations;
  97. }
  98. return array();
  99. }
  100. /**
  101. * Set translation updates.
  102. *
  103. * @param array $translationUpdates
  104. */
  105. public function setTranslations($translationUpdates) {
  106. $this->lazyLoad();
  107. if ( isset($this->update) ) {
  108. $this->update->translations = $translationUpdates;
  109. $this->save();
  110. }
  111. }
  112. public function save() {
  113. $state = new stdClass();
  114. $state->lastCheck = $this->lastCheck;
  115. $state->checkedVersion = $this->checkedVersion;
  116. if ( isset($this->update)) {
  117. $state->update = $this->update->toStdClass();
  118. $updateClass = get_class($this->update);
  119. $state->updateClass = $updateClass;
  120. $prefix = $this->getLibPrefix();
  121. if ( Puc_v4p11_Utils::startsWith($updateClass, $prefix) ) {
  122. $state->updateBaseClass = substr($updateClass, strlen($prefix));
  123. }
  124. }
  125. update_site_option($this->optionName, $state);
  126. $this->isLoaded = true;
  127. }
  128. /**
  129. * @return $this
  130. */
  131. public function lazyLoad() {
  132. if ( !$this->isLoaded ) {
  133. $this->load();
  134. }
  135. return $this;
  136. }
  137. protected function load() {
  138. $this->isLoaded = true;
  139. $state = get_site_option($this->optionName, null);
  140. if ( !is_object($state) ) {
  141. $this->lastCheck = 0;
  142. $this->checkedVersion = '';
  143. $this->update = null;
  144. return;
  145. }
  146. $this->lastCheck = intval(Puc_v4p11_Utils::get($state, 'lastCheck', 0));
  147. $this->checkedVersion = Puc_v4p11_Utils::get($state, 'checkedVersion', '');
  148. $this->update = null;
  149. if ( isset($state->update) ) {
  150. //This mess is due to the fact that the want the update class from this version
  151. //of the library, not the version that saved the update.
  152. $updateClass = null;
  153. if ( isset($state->updateBaseClass) ) {
  154. $updateClass = $this->getLibPrefix() . $state->updateBaseClass;
  155. } else if ( isset($state->updateClass) && class_exists($state->updateClass) ) {
  156. $updateClass = $state->updateClass;
  157. }
  158. if ( $updateClass !== null ) {
  159. $this->update = call_user_func(array($updateClass, 'fromObject'), $state->update);
  160. }
  161. }
  162. }
  163. public function delete() {
  164. delete_site_option($this->optionName);
  165. $this->lastCheck = 0;
  166. $this->checkedVersion = '';
  167. $this->update = null;
  168. }
  169. private function getLibPrefix() {
  170. $parts = explode('_', __CLASS__, 3);
  171. return $parts[0] . '_' . $parts[1] . '_';
  172. }
  173. }
  174. endif;