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 / ResourceData.php

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

178 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Cache data.
5 */
6
7 namespace Extendify\Shared\DataProvider;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Assist\Controllers\DomainsSuggestionController;
12 use Extendify\Assist\Controllers\RecommendationsController;
13 use Extendify\HelpCenter\Controllers\SupportArticlesController;
14 use Extendify\Shared\Services\Sanitizer;
15
16 /**
17 * The cache data class.
18 */
19
20 class ResourceData
21 {
22 /**
23 * Initiate the class.
24 *
25 * @return void
26 */
27 public function __construct()
28 {
29 if (!(is_admin() || defined('DOING_CRON'))) {
30 return;
31 }
32
33 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
34 add_action('http_api_curl', function ($handle, $_req, $url) {
35 // Allow a longer timeout for this request since it happens in a scheduler.
36 if (strpos($url, 'api/domains/suggest') !== false) {
37 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 150); // phpcs:ignore
38 curl_setopt($handle, CURLOPT_TIMEOUT, 150); // phpcs:ignore
39 }
40 }, 100, 3);
41 }
42
43 /**
44 * Register the cache schedule.
45 *
46 * @return void
47 */
48 public static function scheduleCache()
49 {
50 \add_action('init', function () {
51 // phpcs:ignore WordPress.WP.CronInterval -- Verified > 30 days.
52 \add_filter('cron_schedules', function ($schedules) {
53 $schedules['extendify_every_month'] = [
54 'interval' => (30 * DAY_IN_SECONDS), // phpcs:ignore
55 'display' => __('Every month', 'extendify-local'),
56 ];
57 return $schedules;
58 });
59 });
60
61 if (! \wp_next_scheduled('extendify_cache_data')) {
62 \wp_schedule_event(
63 (\current_time('timestamp') + DAY_IN_SECONDS), // phpcs:ignore
64 'extendify_every_month',
65 'extendify_cache_data'
66 );
67 }
68
69 \add_action('extendify_cache_data', function () {
70 if (!defined('EXTENDIFY_PARTNER_ID')) {
71 return;
72 }
73
74 $resourceData = new ResourceData();
75 $resourceData->cacheData('recommendations', RecommendationsController::fetchRecommendations()->get_data());
76 $resourceData->cacheData('supportArticles', SupportArticlesController::fetchArticles()->get_data());
77 });
78
79 // For domains, only update when the site title changes.
80 \add_action('update_option_blogname', function () {
81 if (!defined('EXTENDIFY_PARTNER_ID')) {
82 return;
83 }
84
85 wp_schedule_single_event(time(), 'extendify_update_domains_cache');
86 spawn_cron();
87 });
88
89 \add_action('extendify_update_domains_cache', function () {
90 if (!defined('EXTENDIFY_PARTNER_ID')) {
91 return;
92 }
93
94 $data = DomainsSuggestionController::fetchDomainSuggestions()->get_data();
95 set_transient('extendify_domains', Sanitizer::sanitizeArray($data));
96 });
97 }
98
99 /**
100 * Return the data.
101 *
102 * @return array
103 */
104 public function getData()
105 {
106 if (!defined('EXTENDIFY_PARTNER_ID')) {
107 return [
108 'recommendations' => [],
109 'supportArticles' => [],
110 'domains' => [],
111 ];
112 }
113
114 $domains = get_transient('extendify_domains');
115 if (empty($domains)) {
116 wp_schedule_single_event(time(), 'extendify_update_domains_cache');
117 spawn_cron();
118 }
119
120 return [
121 'recommendations' => $this->recommendations(),
122 'supportArticles' => $this->supportArticles(),
123 // Domains are now cached forever until the site title changes.
124 'domains' => is_array($domains) ? $domains : [],
125 ];
126 }
127
128 /**
129 * Return the recommendations. Fetch them if not found (or on a schedule).
130 *
131 * @return mixed|\WP_REST_Response
132 */
133 protected function recommendations()
134 {
135 $recommendations = get_transient('extendify_recommendations');
136 if ($recommendations === false) {
137 // Fetch these immediately if not found.
138 $recommendations = RecommendationsController::fetchRecommendations()->get_data();
139 $this->cacheData('recommendations', $recommendations);
140 }
141
142 return $recommendations;
143 }
144
145 /**
146 * Return the support articles. Fetch them if not found (or on a schedule).
147 *
148 * @return mixed|\WP_REST_Response
149 */
150 protected function supportArticles()
151 {
152 $supportArticles = get_transient('extendify_supportArticles');
153 if ($supportArticles === false) {
154 $supportArticles = SupportArticlesController::fetchArticles()->get_data();
155 $this->cacheData('supportArticles', $supportArticles);
156 }
157
158 return $supportArticles;
159 }
160
161 /**
162 * This stores the data as a transient.
163 *
164 * @param string $functionName The function name that we use in the store.
165 * @param array $data The extracted data returned from the HTTP request.
166 * @return void
167 */
168 protected function cacheData($functionName, $data)
169 {
170 // The scheduler runs monthly, one day before the cache expires.
171 set_transient(
172 'extendify_' . $functionName,
173 Sanitizer::sanitizeArray($data),
174 (DAY_IN_SECONDS + MONTH_IN_SECONDS)
175 );
176 }
177 }
178