PluginProbe
Extendify / 3.1.2
Extendify v3.1.2
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 / PartnerData.php

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

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