PluginProbe
Extendify / 3.1.6
Extendify v3.1.6
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.1.6, at app/PartnerData.php

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