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

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