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

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