PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
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.0, at app/PartnerData.php

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