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

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

348 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 'domainSearchURL' => '',
64 'showDraft' => false,
65 'showChat' => false,
66 'showAIPageCreation' => false,
67 'enableImageImports-1-14-6' => false,
68 'disableLibraryAutoOpen' => false,
69 'enableApexDomain' => false,
70 'showLaunch' => false,
71 'showLaunchTitle' => false,
72 'deactivated' => true,
73 'launchRedirectWebsite' => false,
74 'showAILogo' => false,
75 'showProductRecommendations' => false,
76 'productRecommendations' => [
77 'showPartnerBranding' => false,
78 'disabledProducts' => [],
79 'customProducts' => [],
80 ],
81 'showPartnerNotifications' => false,
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['domainSearchURL'] = ($data['domainSearchURL'] ?? self::$config['domainSearchURL']);
135 self::$logo = isset($data['logo'][0]['thumbnails']['large']['url'])
136 ? $data['logo'][0]['thumbnails']['large']['url']
137 : self::$logo;
138 self::$config['showDraft'] = ($data['showDraft'] ?? self::$config['showDraft']);
139 self::$config['showChat'] = ($data['showChat'] ?? self::$config['showChat']);
140 self::$config['enableImageImports-1-14-6'] = ($data['enableImageImports-1-14-6']
141 ?? self::$config['enableImageImports-1-14-6']);
142 self::$config['disableLibraryAutoOpen'] = ($data['disableLibraryAutoOpen']
143 ?? self::$config['disableLibraryAutoOpen']);
144 self::$config['enableApexDomain'] = ($data['enableApexDomain'] ?? self::$config['enableApexDomain']);
145 self::$name = ($data['Name'] ?? self::$name);
146 self::$colors = [
147 'backgroundColor' => ($data['backgroundColor'] ?? null),
148 'foregroundColor' => ($data['foregroundColor'] ?? null),
149 'secondaryColor' => ($data['secondaryColor'] ?? ($data['backgroundColor'] ?? null)),
150 'secondaryColorText' => '#ffffff',
151 ];
152 self::$config['showAIPageCreation'] = ($data['showAIPageCreation'] ?? self::$config['showAIPageCreation']);
153 self::$config['showLaunch'] = ($data['showLaunch'] ?? self::$config['showLaunch']);
154 self::$config['showLaunchTitle'] = ($data['showLaunchTitle'] ?? self::$config['showLaunchTitle']);
155 self::$config['deactivated'] = ($data['deactivated'] ?? self::$config['deactivated']);
156 self::$config['launchRedirectWebsite'] = ($data['launchRedirectWebsite']
157 ?? self::$config['launchRedirectWebsite']);
158 self::$config['showAILogo'] = ($data['showAILogo'] ?? self::$config['showAILogo']);
159 self::$config['showProductRecommendations'] = ($data['showProductRecommendations']
160 ?? self::$config['showProductRecommendations']);
161 self::$config['productRecommendations'] = [
162 'showPartnerBranding' => ($data['productRecommendationShowPartnerBranding']
163 ?? self::$config['productRecommendations']['showPartnerBranding']),
164 'disabledProducts' => ($data['productRecommendationDisabledSlugs']
165 ?? self::$config['productRecommendations']['disabledProducts']),
166 'customProducts' => ($data['productRecommendationCustomSlugs']
167 ?? self::$config['productRecommendations']['customProducts']),
168 ];
169 self::$config['showPartnerNotifications'] = ($data['showPartnerNotifications']
170 ?? self::$config['showPartnerNotifications']);
171 self::$config['license'] = ($data['license'] ?? self::$config['license']);
172 self::$config['showImprint'] = ($data['showImprint'] ?? self::$config['showImprint']);
173 self::$config['showLaunchQuestions'] = ($data['showLaunchQuestions'] ?? self::$config['showLaunchQuestions']);
174 self::$config['showAIAgents'] = ($data['showAIAgents'] ?? self::$config['showAIAgents']);
175 self::$config['agentAbilitiesAllowlist'] = ($data['agentAbilitiesAllowlist']
176 ?? self::$config['agentAbilitiesAllowlist']);
177 self::$config['showQuickEdit'] = ($data['showQuickEdit'] ?? self::$config['showQuickEdit']);
178 self::$config['showSimpleToolbar'] = ($data['showSimpleToolbar']
179 ?? self::$config['showSimpleToolbar']);
180 self::$config['pluginGroupId'] = ($data['pluginGroup'] ?? self::$config['pluginGroupId']);
181 self::$config['requiredPlugins'] = ($data['requiredPlugins'] ?? self::$config['requiredPlugins']);
182 self::$config['showProductActivation'] = ($data['showProductActivation']
183 ?? self::$config['showProductActivation']);
184 self::$config['useAgentOnboarding'] = ($data['useAgentOnboarding'] ?? self::$config['useAgentOnboarding']);
185 self::$config['hidePluginNotifications'] = ($data['hidePluginNotifications']
186 ?? self::$config['hidePluginNotifications']);
187 self::$config['hideLaunchExitLink'] = ($data['hideLaunchExitLink'] ?? self::$config['hideLaunchExitLink']);
188 self::$config['useAutoUpdate'] = ($data['useAutoUpdate'] ?? self::$config['useAutoUpdate']);
189 self::$config['showLaunchUpdate'] = ($data['showLaunchUpdate'] ?? self::$config['showLaunchUpdate']);
190 self::$config['activeTests'] = ($data['activeTests'] ?? self::$config['activeTests']);
191 self::$config['showExtendifyCode'] = ($data['showExtendifyCode'] ?? self::$config['showExtendifyCode']);
192 self::$config['extendifyCodeData'] = ($data['extendifyCodeData'] ?? self::$config['extendifyCodeData']);
193
194 // Add the job hook to fetch the partner data.
195 \add_action('extendify_fetch_partner_data', [self::class, 'fetchPartnerData']);
196 }
197
198 /**
199 * Retrieve partner data from the options table or from the API.
200 *
201 * @return array
202 */
203 public static function getPartnerData()
204 {
205 // Do not make a request without a partner ID (i.e. it's opt in).
206 if (!defined('EXTENDIFY_PARTNER_ID')) {
207 return [];
208 }
209
210 $partnerData = \get_option('extendify_partner_data_v2', 'empty');
211 if ($partnerData !== 'empty') {
212 // We have data, but if it's been 10 minutes, check for new data.
213 $partnerRefresh = \get_transient('extendify_partner_data_cache_check');
214 if (!$partnerRefresh && \is_admin()) {
215 \add_action('init', function () {
216 if (!\wp_next_scheduled('extendify_fetch_partner_data')) {
217 \wp_schedule_single_event(time(), 'extendify_fetch_partner_data');
218 \spawn_cron();
219 }
220 });
221 }
222
223 return array_merge(self::$config, $partnerData);
224 }
225
226 $freshData = self::fetchPartnerData();
227 $mergedData = array_merge(self::$config, $freshData);
228 // Cache here even if empty [] to prevent multiple requests.
229 \update_option('extendify_partner_data_v2', $mergedData);
230 return $mergedData;
231 }
232
233 /**
234 * Fetch or refresh the partner data
235 *
236 * @return array
237 */
238 public static function fetchPartnerData()
239 {
240 if (!defined('EXTENDIFY_PARTNER_ID')) {
241 return [];
242 }
243
244 // Set a 10 minute transient to prevent multiple requests.
245 set_transient('extendify_partner_data_cache_check', true, (10 * MINUTE_IN_SECONDS));
246
247 $url = add_query_arg(
248 [
249 'partner' => self::$id,
250 'wp_language' => \get_locale(),
251 'site_url' => \home_url(),
252 'site_id' => \get_option('extendify_site_id', ''),
253 ],
254 Constants::DASHBOARD_HOST . '/api/onboarding/partner-data/'
255 );
256
257 $response = \wp_safe_remote_get($url, ['headers' => ['Accept' => 'application/json']]);
258
259 // If there was an error, we dont update the cache.
260 if (\is_wp_error($response) || \wp_remote_retrieve_response_code($response) !== 200) {
261 return [];
262 }
263
264 $result = json_decode(\wp_remote_retrieve_body($response), true);
265
266 // If the data didn't come back as we expected it to, or if we have an error, don't update the cache.
267 if (
268 json_last_error() !== JSON_ERROR_NONE
269 || !is_array($result)
270 || !array_key_exists('data', $result)
271 || empty($result['data'])
272 ) {
273 return [];
274 }
275
276 $sanitizedData = array_merge(
277 Sanitizer::sanitizeUnknown($result['data']),
278 ['consentTermsCustom' => \sanitize_text_field(htmlentities(
279 ($result['data']['consentTermsCustom'] ?? ''),
280 ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401
281 ))]
282 );
283
284 // Merge before persisting as this data is accessed directly elsewhere.
285 $mergedData = array_merge(self::$config, $sanitizedData);
286 \update_option('extendify_partner_data_v2', $mergedData);
287
288 return $mergedData;
289 }
290
291 /**
292 * Return colors mapped as css variables
293 *
294 * @return array
295 */
296 public static function cssVariableMapping()
297 {
298 $mapping = [
299 'backgroundColor' => '--ext-banner-main',
300 'foregroundColor' => '--ext-banner-text',
301 'secondaryColor' => '--ext-design-main',
302 'secondaryColorText' => '--ext-design-text',
303 ];
304
305 $cssVariables = [];
306 $adminTheme = \get_user_option('admin_color', get_current_user_id());
307 if (isset($GLOBALS['_wp_admin_css_colors'][$adminTheme])) {
308 $theme = $GLOBALS['_wp_admin_css_colors'][$adminTheme];
309 if (in_array($adminTheme, ['modern', 'blue'], true)) {
310 $cssVariables['--wp-admin-theme-main'] = $theme->colors[1];
311 $cssVariables['--wp-admin-theme-accent'] = $theme->colors[2];
312 } else {
313 $cssVariables['--wp-admin-theme-bg'] = $theme->colors[0];
314 $cssVariables['--wp-admin-theme-main'] = $theme->colors[2];
315 $cssVariables['--wp-admin-theme-accent'] = $theme->colors[3];
316 }
317 }
318
319 // Partner specific colors.
320 foreach ($mapping as $color => $variable) {
321 if (isset(self::$colors[$color])) {
322 $cssVariables[$variable] = self::$colors[$color];
323 }
324 }
325
326 return $cssVariables;
327 }
328
329 /**
330 * Retrieves the value of a setting.
331 *
332 * This method first checks if the setting exists as a static property of the class.
333 * If it does, it returns the value of that property. Otherwise, it looks for the
334 * setting in the config array and returns its value if found.
335 *
336 * @param string $settingKey The key of the setting to retrieve.
337 * @return mixed The value of the setting if found, or null if not found.
338 */
339 public static function setting($settingKey)
340 {
341 if (property_exists(self::class, $settingKey)) {
342 return self::$$settingKey;
343 }
344
345 return (self::$config[$settingKey] ?? null);
346 }
347 }
348