PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
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 2.2.0, at app/PartnerData.php

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