PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / app / Shared / DataProvider / NotificationData.php

NotificationData.php in Extendify 3.1.5, at app/Shared/DataProvider/NotificationData.php

110 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Notification data.
5 */
6
7 namespace Extendify\Shared\DataProvider;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Constants;
12 use Extendify\PartnerData;
13
14 /**
15 * Fetches and caches the notifications.
16 */
17 class NotificationData
18 {
19 /**
20 * Registers the wp-cron handler that refreshes stale notifications.
21 *
22 * @return void
23 */
24 public static function scheduleCache()
25 {
26 \add_action('extendify_notifications_refresh', [self::class, 'refresh']);
27 }
28
29 /**
30 * Returns the cached notifications, refreshing on cold start and
31 * scheduling a background refresh when the cache is stale.
32 *
33 * @return array
34 */
35 public static function get()
36 {
37 if (!PartnerData::$id) {
38 return [];
39 }
40
41 $locale = \get_locale();
42 $cached = \get_option('extendify_notifications_' . $locale);
43
44 if (!is_array($cached) || !isset($cached['fetchedAt'])) {
45 return self::refresh($locale) ?? [];
46 }
47
48 $age = time() - $cached['fetchedAt'];
49 // A host whose requests are being refused would otherwise re-ask every TTL.
50 $ttl = empty($cached['failed']) ? (5 * MINUTE_IN_SECONDS) : HOUR_IN_SECONDS;
51 if ($age > $ttl) {
52 if (!\wp_next_scheduled('extendify_notifications_refresh', [$locale])) {
53 \wp_schedule_single_event(time(), 'extendify_notifications_refresh', [$locale]);
54 if (\is_admin()) {
55 \spawn_cron();
56 }
57 }
58 }
59
60 return $cached['data'] ?? [];
61 }
62
63 /**
64 * Fetch notifications from the API and persist them.
65 * Called synchronously on cold start and via wp-cron when the cache is stale.
66 *
67 * @param string $locale - Locale to fetch (cron may run in a different site locale).
68 * @return array|null
69 */
70 public static function refresh($locale)
71 {
72 if (!PartnerData::$id) {
73 return [];
74 }
75
76 $optionKey = 'extendify_notifications_' . $locale;
77
78 $url = \add_query_arg(
79 ['partner' => PartnerData::$id, 'wp_language' => $locale],
80 Constants::AI_HOST . '/api/notifications'
81 );
82 $response = \wp_remote_get($url, ['headers' => ['Accept' => 'application/json']]);
83 $result = \is_wp_error($response)
84 ? null
85 : json_decode(\wp_remote_retrieve_body($response), true);
86
87 if (!is_array($result) || !is_array($result['notifications'] ?? null)) {
88 $cached = \get_option($optionKey);
89 \update_option(
90 $optionKey,
91 [
92 'data' => is_array($cached) ? ($cached['data'] ?? []) : [],
93 'fetchedAt' => time(),
94 'failed' => true,
95 ],
96 false
97 );
98 return null;
99 }
100
101 $notifications = $result['notifications'];
102 \update_option(
103 $optionKey,
104 ['data' => $notifications, 'fetchedAt' => time()],
105 false
106 );
107 return $notifications;
108 }
109 }
110