PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
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 / PartnerData.php

PartnerData.php in Extendify 3.0.5, at app/PartnerData.php

303 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The Partner Settings
5 */
6
7 namespace Extendify;
8
9 defined('ABSPATH') || die('No direct access.');
10
11 use Extendify\Shared\Services\Sanitizer;
12
13 /**
14 * Controller for handling partner settings
15 */
16
17 class PartnerData
18 {
19 /**
20 * The partner id
21 *
22 * @var string
23 */
24 public static $id;
25
26 /**
27 * The partner logo
28 *
29 * @var string
30 */
31 public static $logo = '';
32
33 /**
34 * The partner display name
35 *
36 * @var string
37 */
38 public static $name = '';
39
40 /**
41 * The partner colors
42 *
43 * @var string
44 */
45 public static $colors = [];
46
47 /**
48 * The partner configuration.
49 *
50 * @var array
51 */
52 protected static $config = [
53 'showDomainBanner' => false,
54 'showDomainTask' => false,
55 'showSecondaryDomainBanner' => false,
56 'showSecondaryDomainTask' => false,
57 'domainTLDs' => ['com', 'net'],
58 'priorityDomainTLDs' => [],
59 'stagingSites' => ['wordpress'],
60 'domainSearchURL' => '',
61 'showDraft' => false,
62 'showChat' => false,
63 'showAIPageCreation' => false,
64 'enableImageImports-1-14-6' => false,
65 'disableLibraryAutoOpen' => false,
66 'enableApexDomain' => false,
67 'showLaunch' => false,
68 'deactivated' => true,
69 'launchRedirectWebsite' => false,
70 'showAILogo' => false,
71 'showProductRecommendations' => false,
72 'productRecommendations' => [
73 'showPartnerBranding' => false,
74 'disabledProducts' => [],
75 'customProducts' => [],
76 ],
77 'license' => 'active',
78 'showAIAgents' => false,
79 'showImprint' => [],
80 'showLaunchQuestions' => false,
81 'pluginGroupId' => null,
82 'requiredPlugins' => null,
83 'useAgentOnboarding' => false,
84 'hidePluginNotifications' => false,
85 'hideLaunchExitLink' => false,
86 ];
87
88 // phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
89 /**
90 * Set up and collect partner data
91 *
92 * @return void
93 */
94 public function __construct()
95 {
96 self::$id = defined('EXTENDIFY_PARTNER_ID') ? constant('EXTENDIFY_PARTNER_ID') : null;
97 $data = self::getPartnerData();
98 self::$config['showDomainBanner'] = ($data['showDomainBanner'] ?? self::$config['showDomainBanner']);
99 self::$config['showDomainTask'] = ($data['showDomainTask'] ?? self::$config['showDomainTask']);
100 self::$config['showSecondaryDomainTask'] = ($data['showSecondaryDomainTask']
101 ?? self::$config['showSecondaryDomainTask']);
102 self::$config['showSecondaryDomainBanner'] = ($data['showSecondaryDomainBanner']
103 ?? self::$config['showSecondaryDomainBanner']);
104 self::$config['domainTLDs'] = ($data['domainTLDs'] ?? self::$config['domainTLDs']);
105 self::$config['priorityDomainTLDs'] = ($data['priorityDomainTLDs']
106 ?? self::$config['priorityDomainTLDs']);
107 self::$config['stagingSites'] = array_map('trim', ($data['stagingSites'] ?? self::$config['stagingSites']));
108 self::$config['domainSearchURL'] = ($data['domainSearchURL'] ?? self::$config['domainSearchURL']);
109 self::$logo = isset($data['logo'][0]['thumbnails']['large']['url'])
110 ? $data['logo'][0]['thumbnails']['large']['url']
111 : self::$logo;
112 self::$config['showDraft'] = ($data['showDraft'] ?? self::$config['showDraft']);
113 self::$config['showChat'] = ($data['showChat'] ?? self::$config['showChat']);
114 self::$config['enableImageImports-1-14-6'] = ($data['enableImageImports-1-14-6']
115 ?? self::$config['enableImageImports-1-14-6']);
116 self::$config['disableLibraryAutoOpen'] = ($data['disableLibraryAutoOpen']
117 ?? self::$config['disableLibraryAutoOpen']);
118 self::$config['enableApexDomain'] = ($data['enableApexDomain'] ?? self::$config['enableApexDomain']);
119 self::$name = ($data['Name'] ?? self::$name);
120 self::$colors = [
121 'backgroundColor' => ($data['backgroundColor'] ?? null),
122 'foregroundColor' => ($data['foregroundColor'] ?? null),
123 'secondaryColor' => ($data['secondaryColor'] ?? ($data['backgroundColor'] ?? null)),
124 'secondaryColorText' => '#ffffff',
125 ];
126 self::$config['showAIPageCreation'] = ($data['showAIPageCreation'] ?? self::$config['showAIPageCreation']);
127 self::$config['showLaunch'] = ($data['showLaunch'] ?? self::$config['showLaunch']);
128 self::$config['deactivated'] = ($data['deactivated'] ?? self::$config['deactivated']);
129 self::$config['launchRedirectWebsite'] = ($data['launchRedirectWebsite']
130 ?? self::$config['launchRedirectWebsite']);
131 self::$config['showAILogo'] = ($data['showAILogo'] ?? self::$config['showAILogo']);
132 self::$config['showProductRecommendations'] = ($data['showProductRecommendations']
133 ?? self::$config['showProductRecommendations']);
134 self::$config['productRecommendations'] = [
135 'showPartnerBranding' => ($data['productRecommendationShowPartnerBranding']
136 ?? self::$config['productRecommendations']['showPartnerBranding']),
137 'disabledProducts' => ($data['productRecommendationDisabledSlugs']
138 ?? self::$config['productRecommendations']['disabledProducts']),
139 'customProducts' => ($data['productRecommendationCustomSlugs']
140 ?? self::$config['productRecommendations']['customProducts']),
141 ];
142 self::$config['license'] = ($data['license'] ?? self::$config['license']);
143 self::$config['showImprint'] = ($data['showImprint'] ?? self::$config['showImprint']);
144 self::$config['showLaunchQuestions'] = ($data['showLaunchQuestions'] ?? self::$config['showLaunchQuestions']);
145 self::$config['showAIAgents'] = ($data['showAIAgents'] ?? self::$config['showAIAgents']);
146 self::$config['pluginGroupId'] = ($data['pluginGroup'] ?? self::$config['pluginGroupId']);
147 self::$config['requiredPlugins'] = ($data['requiredPlugins'] ?? self::$config['requiredPlugins']);
148 self::$config['useAgentOnboarding'] = ($data['useAgentOnboarding'] ?? self::$config['useAgentOnboarding']);
149 self::$config['hidePluginNotifications'] = ($data['hidePluginNotifications']
150 ?? self::$config['hidePluginNotifications']);
151 self::$config['hideLaunchExitLink'] = ($data['hideLaunchExitLink'] ?? self::$config['hideLaunchExitLink']);
152
153 // Add the job hook to fetch the partner data.
154 \add_action('extendify_fetch_partner_data', [self::class, 'fetchPartnerData']);
155 }
156
157 /**
158 * Retrieve partner data from the options table or from the API.
159 *
160 * @return array
161 */
162 public static function getPartnerData()
163 {
164 // Do not make a request without a partner ID (i.e. it's opt in).
165 if (!defined('EXTENDIFY_PARTNER_ID')) {
166 return [];
167 }
168
169 $partnerData = \get_option('extendify_partner_data_v2', 'empty');
170 if ($partnerData !== 'empty') {
171 // We have data, but if it's been 10 minutes, check for new data.
172 $partnerRefresh = \get_transient('extendify_partner_data_cache_check');
173 if (!$partnerRefresh && \is_admin()) {
174 \add_action('init', function () {
175 if (!\wp_next_scheduled('extendify_fetch_partner_data')) {
176 \wp_schedule_single_event(time(), 'extendify_fetch_partner_data');
177 \spawn_cron();
178 }
179 });
180 }
181
182 return array_merge(self::$config, $partnerData);
183 }
184
185 $freshData = self::fetchPartnerData();
186 $mergedData = array_merge(self::$config, $freshData);
187 // Cache here even if empty [] to prevent multiple requests.
188 \update_option('extendify_partner_data_v2', $mergedData);
189 return $mergedData;
190 }
191
192 /**
193 * Fetch or refresh the partner data
194 *
195 * @return array
196 */
197 public static function fetchPartnerData()
198 {
199 if (!defined('EXTENDIFY_PARTNER_ID')) {
200 return [];
201 }
202
203 // Set a 10 minute transient to prevent multiple requests.
204 set_transient('extendify_partner_data_cache_check', true, (10 * MINUTE_IN_SECONDS));
205
206 $url = add_query_arg(
207 [
208 'partner' => self::$id,
209 'wp_language' => \get_locale(),
210 'site_url' => \home_url(),
211 ],
212 'https://dashboard.extendify.com/api/onboarding/partner-data/'
213 );
214
215 $response = \wp_safe_remote_get($url, ['headers' => ['Accept' => 'application/json']]);
216
217 // If there was an error, we dont update the cache.
218 if (\is_wp_error($response) || \wp_remote_retrieve_response_code($response) !== 200) {
219 return [];
220 }
221
222 $result = json_decode(\wp_remote_retrieve_body($response), true);
223
224 // If the data didn't come back as we expected it to, or if we have an error, don't update the cache.
225 if (
226 json_last_error() !== JSON_ERROR_NONE
227 || !is_array($result)
228 || !array_key_exists('data', $result)
229 || empty($result['data'])
230 ) {
231 return [];
232 }
233
234 $sanitizedData = array_merge(
235 Sanitizer::sanitizeUnknown($result['data']),
236 ['consentTermsCustom' => \sanitize_text_field(htmlentities(($result['data']['consentTermsCustom'] ?? '')))]
237 );
238
239 // Merge before persisting as this data is accessed directly elsewhere.
240 $mergedData = array_merge(self::$config, $sanitizedData);
241 \update_option('extendify_partner_data_v2', $mergedData);
242
243 return $mergedData;
244 }
245
246 /**
247 * Return colors mapped as css variables
248 *
249 * @return array
250 */
251 public static function cssVariableMapping()
252 {
253 $mapping = [
254 'backgroundColor' => '--ext-banner-main',
255 'foregroundColor' => '--ext-banner-text',
256 'secondaryColor' => '--ext-design-main',
257 'secondaryColorText' => '--ext-design-text',
258 ];
259
260 $cssVariables = [];
261 $adminTheme = \get_user_option('admin_color', get_current_user_id());
262 if (isset($GLOBALS['_wp_admin_css_colors'][$adminTheme])) {
263 $theme = $GLOBALS['_wp_admin_css_colors'][$adminTheme];
264 if (in_array($adminTheme, ['modern', 'blue'], true)) {
265 $cssVariables['--wp-admin-theme-main'] = $theme->colors[1];
266 $cssVariables['--wp-admin-theme-accent'] = $theme->colors[2];
267 } else {
268 $cssVariables['--wp-admin-theme-bg'] = $theme->colors[0];
269 $cssVariables['--wp-admin-theme-main'] = $theme->colors[2];
270 $cssVariables['--wp-admin-theme-accent'] = $theme->colors[3];
271 }
272 }
273
274 // Partner specific colors.
275 foreach ($mapping as $color => $variable) {
276 if (isset(self::$colors[$color])) {
277 $cssVariables[$variable] = self::$colors[$color];
278 }
279 }
280
281 return $cssVariables;
282 }
283
284 /**
285 * Retrieves the value of a setting.
286 *
287 * This method first checks if the setting exists as a static property of the class.
288 * If it does, it returns the value of that property. Otherwise, it looks for the
289 * setting in the config array and returns its value if found.
290 *
291 * @param string $settingKey The key of the setting to retrieve.
292 * @return mixed The value of the setting if found, or null if not found.
293 */
294 public static function setting($settingKey)
295 {
296 if (property_exists(self::class, $settingKey)) {
297 return self::$$settingKey;
298 }
299
300 return (self::$config[$settingKey] ?? null);
301 }
302 }
303