PluginProbe
MailPoet – Newsletters, Email Marketing, and Automation / 3.89.3
MailPoet – Newsletters, Email Marketing, and Automation v3.89.3
5.38.0 5.37.0 5.36.1 5.36.0 5.35.1 5.35.0 5.34.3 5.34.2 5.34.1 5.34.0 5.33.1 5.33.0 5.32.0 5.31.0 5.30.0 5.29.0 5.28.1 5.28.0 5.27.0 5.26.0 5.26.1 5.25.0 5.24.0 4.43.0 4.43.1 All 542 releases
mailpoet / lib / Cache / TransientCache.php

TransientCache.php in MailPoet – Newsletters, Email Marketing, and Automation 3.89.3, at lib/Cache/TransientCache.php

80 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types = 1);
2
3 namespace MailPoet\Cache;
4
5 if (!defined('ABSPATH')) exit;
6
7
8 use MailPoet\WP\Functions as WPFunctions;
9 use MailPoetVendor\Carbon\Carbon;
10
11 class TransientCache {
12 public const SUBSCRIBERS_STATISTICS_COUNT_KEY = 'mailpoet_subscribers_statistics_count_cache';
13 public const SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY = 'mailpoet_subscribers_statistics_count_global_status_cache';
14
15 /** @var WPFunctions */
16 private $wp;
17
18 public function __construct(
19 WPFunctions $wp
20 ) {
21 $this->wp = $wp;
22 }
23
24 public function getItem(string $key, int $id): ?array {
25 $items = $this->getItems($key);
26 return $items[$id] ?? null;
27 }
28
29 public function getOldestCreatedAt(string $key): ?\DateTime {
30 $oldest = $this->getOldestItem($key);
31 return $oldest['created_at'] ?? null;
32 }
33
34 public function getOldestItem(string $key): ?array {
35 $items = $this->getItems($key);
36 $oldest = null;
37 foreach ($items as $item) {
38 if ($oldest === null || $item['created_at'] < $oldest['created_at']) {
39 $oldest = $item;
40 }
41 }
42 return $oldest;
43 }
44
45 public function setItem(string $key, array $item, int $id): void {
46 $items = $this->getItems($key) ?? [];
47 $items[$id] = [
48 'item' => $item,
49 'created_at' => Carbon::now(),
50 ];
51 $this->setItems($key, $items);
52 }
53
54 public function invalidateItem(string $key, int $id): void {
55 $items = $this->getItems($key);
56 unset($items[$id]);
57 if (count($items)) {
58 $this->setItems($key, $items);
59 } else {
60 $this->deleteItems($key);
61 }
62 }
63
64 public function invalidateItems(string $key): void {
65 $this->deleteItems($key);
66 }
67
68 private function deleteItems(string $key): void {
69 $this->wp->deleteTransient($key);
70 }
71
72 private function setItems(string $key, array $items): void {
73 $this->wp->setTransient($key, $items);
74 }
75
76 public function getItems(string $key): array {
77 return $this->wp->getTransient($key) ?: [];
78 }
79 }
80