PluginProbe
Extendify / 3.1.4
Extendify v3.1.4
3.2.1 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 All 127 releases
extendify / app / Shared / DataProvider / PartnerNotificationData.php

PartnerNotificationData.php in Extendify 3.1.4, at app/Shared/DataProvider/PartnerNotificationData.php

108 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 * Partner 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 partner notifications.
16 */
17 class PartnerNotificationData
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_partner_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_partner_notifications_' . $locale);
43
44 if (!is_array($cached) || !isset($cached['fetchedAt'])) {
45 return self::refresh($locale) ?? [];
46 }
47
48 $age = time() - $cached['fetchedAt'];
49 if ($age > DAY_IN_SECONDS) {
50 if (!\wp_next_scheduled('extendify_partner_notifications_refresh', [$locale])) {
51 \wp_schedule_single_event(time(), 'extendify_partner_notifications_refresh', [$locale]);
52 if (\is_admin()) {
53 \spawn_cron();
54 }
55 }
56 }
57
58 return $cached['data'] ?? [];
59 }
60
61 /**
62 * Fetch partner notifications from the API and persist them.
63 * Called synchronously on cold start and via wp-cron when the cache is stale.
64 *
65 * @param string $locale - Locale to fetch (cron may run in a different site locale).
66 * @return array|null
67 */
68 public static function refresh($locale)
69 {
70 if (!PartnerData::$id) {
71 return [];
72 }
73
74 $optionKey = 'extendify_partner_notifications_' . $locale;
75
76 $url = \add_query_arg(
77 ['wp_language' => $locale],
78 Constants::AI_HOST . '/api/notifications/' . rawurlencode(PartnerData::$id)
79 );
80 $response = \wp_remote_get($url, ['headers' => ['Accept' => 'application/json']]);
81 $result = \is_wp_error($response)
82 ? null
83 : json_decode(\wp_remote_retrieve_body($response), true);
84
85 if (!is_array($result) || empty($result['success']) || !is_array($result['data'] ?? null)) {
86 // Back off: stamp the cache as fetched ~23h ago so we retry in ~1h instead of every request.
87 $cached = \get_option($optionKey);
88 \update_option(
89 $optionKey,
90 [
91 'data' => is_array($cached) ? ($cached['data'] ?? []) : [],
92 'fetchedAt' => time() - (DAY_IN_SECONDS - HOUR_IN_SECONDS),
93 ],
94 false
95 );
96 return null;
97 }
98
99 $notifications = $result['data'];
100 \update_option(
101 $optionKey,
102 ['data' => $notifications, 'fetchedAt' => time()],
103 false
104 );
105 return $notifications;
106 }
107 }
108