Bez popisu

Abstract.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace NSL\Persistent\Storage;
  3. abstract class StorageAbstract {
  4. protected $sessionId = null;
  5. protected $data = array();
  6. public function set($key, $value) {
  7. $this->load(true);
  8. $this->data[$key] = $value;
  9. $this->store();
  10. }
  11. public function get($key) {
  12. $this->load();
  13. if (isset($this->data[$key])) {
  14. return $this->data[$key];
  15. }
  16. return null;
  17. }
  18. public function delete($key) {
  19. $this->load();
  20. if (isset($this->data[$key])) {
  21. unset($this->data[$key]);
  22. $this->store();
  23. }
  24. }
  25. public function clear() {
  26. $this->data = array();
  27. $this->store();
  28. }
  29. protected function load($createSession = false) {
  30. static $isLoaded = false;
  31. if (!$isLoaded) {
  32. $data = maybe_unserialize(get_site_transient($this->sessionId));
  33. if (is_array($data)) {
  34. $this->data = $data;
  35. }
  36. $isLoaded = true;
  37. }
  38. }
  39. private function store() {
  40. if (empty($this->data)) {
  41. delete_site_transient($this->sessionId);
  42. } else {
  43. set_site_transient($this->sessionId, $this->data, apply_filters('nsl_persistent_expiration', HOUR_IN_SECONDS));
  44. }
  45. }
  46. /**
  47. * @param StorageAbstract $storage
  48. */
  49. public function transferData($storage) {
  50. $this->data = $storage->data;
  51. $this->store();
  52. $storage->clear();
  53. }
  54. }