| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | namespace Extendify; |
| 8 | 8 | |
| 9 | 9 | defined('ABSPATH') || die('No direct access.'); |
| 10 | 10 | |
| 11 | +use Extendify\Constants; | |
| 11 | 12 | use Extendify\Shared\Services\Sanitizer; |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | 15 | * Controller for handling partner settings |
| @@ -53,19 +54,26 @@ | ||
| 53 | 54 | 'showDomainBanner' => false, |
| 54 | 55 | 'showDomainTask' => false, |
| 55 | 56 | 'showSecondaryDomainBanner' => false, |
| 56 | 57 | 'showSecondaryDomainTask' => false, |
| 58 | + 'showPrimaryDomainRecommendationAgent' => false, | |
| 59 | + 'showSecondaryDomainRecommendationAgent' => false, | |
| 57 | 60 | 'domainTLDs' => ['com', 'net'], |
| 58 | 61 | 'priorityDomainTLDs' => [], |
| 59 | 62 | 'stagingSites' => ['wordpress'], |
| 63 | + 'trialDomains' => [], | |
| 60 | 64 | 'domainSearchURL' => '', |
| 61 | 65 | 'showDraft' => false, |
| 62 | 66 | 'showChat' => false, |
| 63 | 67 | 'showAIPageCreation' => false, |
| 68 | + 'mcpConfig' => [], | |
| 69 | + 'mcpWriteList' => [], | |
| 70 | + 'mcpReadList' => [], | |
| 64 | 71 | 'enableImageImports-1-14-6' => false, |
| 65 | 72 | 'disableLibraryAutoOpen' => false, |
| 66 | 73 | 'enableApexDomain' => false, |
| 67 | 74 | 'showLaunch' => false, |
| 75 | + 'showLaunchTitle' => false, | |
| 68 | 76 | 'deactivated' => true, |
| 69 | 77 | 'launchRedirectWebsite' => false, |
| 70 | 78 | 'showAILogo' => false, |
| 71 | 79 | 'showProductRecommendations' => false, |
| @@ -75,15 +83,35 @@ | ||
| 75 | 83 | 'customProducts' => [], |
| 76 | 84 | ], |
| 77 | 85 | 'license' => 'active', |
| 78 | 86 | 'showAIAgents' => false, |
| 87 | + 'agentAbilitiesAllowlist' => [], | |
| 88 | + 'showQuickEdit' => false, | |
| 89 | + // Simple front-end Extendify toolbar (replaces WP core admin | |
| 90 | + // bar for editors who prefer it). Default style is Launch-aware | |
| 91 | + // (simple post-Launch, full before). | |
| 92 | + 'showSimpleToolbar' => false, | |
| 79 | 93 | 'showImprint' => [], |
| 80 | 94 | 'showLaunchQuestions' => false, |
| 81 | 95 | 'pluginGroupId' => null, |
| 82 | 96 | 'requiredPlugins' => null, |
| 97 | + 'showProductActivation' => [], | |
| 83 | 98 | 'useAgentOnboarding' => false, |
| 84 | 99 | 'hidePluginNotifications' => false, |
| 85 | 100 | 'hideLaunchExitLink' => false, |
| 101 | + 'useAutoUpdate' => false, | |
| 102 | + 'showLaunchUpdate' => false, | |
| 103 | + 'activeTests' => [], | |
| 104 | + 'showExtendifyCode' => false, | |
| 105 | + 'useComingSoon' => false, | |
| 106 | + 'extendifyCodeData' => [ | |
| 107 | + 'link' => '', | |
| 108 | + 'title' => '', | |
| 109 | + 'message' => '', | |
| 110 | + 'cta-primary' => '', | |
| 111 | + ], | |
| 112 | + 'customDesign' => null, | |
| 113 | + 'strings' => [], | |
| 86 | 114 | ]; |
| 87 | 115 | |
| 88 | 116 | // phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 89 | 117 | /** |
| @@ -92,8 +120,16 @@ | ||
| 92 | 120 | * @return void |
| 93 | 121 | */ |
| 94 | 122 | public function __construct() |
| 95 | 123 | { |
| 124 | + self::load(); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * @return void | |
| 129 | + */ | |
| 130 | + public static function load() | |
| 131 | + { | |
| 96 | 132 | self::$id = defined('EXTENDIFY_PARTNER_ID') ? constant('EXTENDIFY_PARTNER_ID') : null; |
| 97 | 133 | $data = self::getPartnerData(); |
| 98 | 134 | self::$config['showDomainBanner'] = ($data['showDomainBanner'] ?? self::$config['showDomainBanner']); |
| 99 | 135 | self::$config['showDomainTask'] = ($data['showDomainTask'] ?? self::$config['showDomainTask']); |
| @@ -100,12 +136,17 @@ | ||
| 100 | 136 | self::$config['showSecondaryDomainTask'] = ($data['showSecondaryDomainTask'] |
| 101 | 137 | ?? self::$config['showSecondaryDomainTask']); |
| 102 | 138 | self::$config['showSecondaryDomainBanner'] = ($data['showSecondaryDomainBanner'] |
| 103 | 139 | ?? self::$config['showSecondaryDomainBanner']); |
| 140 | + self::$config['showPrimaryDomainRecommendationAgent'] = ($data['showPrimaryDomainRecommendationAgent'] | |
| 141 | + ?? self::$config['showPrimaryDomainRecommendationAgent']); | |
| 142 | + self::$config['showSecondaryDomainRecommendationAgent'] = ($data['showSecondaryDomainRecommendationAgent'] | |
| 143 | + ?? self::$config['showSecondaryDomainRecommendationAgent']); | |
| 104 | 144 | self::$config['domainTLDs'] = ($data['domainTLDs'] ?? self::$config['domainTLDs']); |
| 105 | 145 | self::$config['priorityDomainTLDs'] = ($data['priorityDomainTLDs'] |
| 106 | 146 | ?? self::$config['priorityDomainTLDs']); |
| 107 | 147 | self::$config['stagingSites'] = array_map('trim', ($data['stagingSites'] ?? self::$config['stagingSites'])); |
| 148 | + self::$config['trialDomains'] = array_map('trim', ($data['trialDomains'] ?? self::$config['trialDomains'])); | |
| 108 | 149 | self::$config['domainSearchURL'] = ($data['domainSearchURL'] ?? self::$config['domainSearchURL']); |
| 109 | 150 | self::$logo = isset($data['logo'][0]['thumbnails']['large']['url']) |
| 110 | 151 | ? $data['logo'][0]['thumbnails']['large']['url'] |
| 111 | 152 | : self::$logo; |
| @@ -123,9 +164,13 @@ | ||
| 123 | 164 | 'secondaryColor' => ($data['secondaryColor'] ?? ($data['backgroundColor'] ?? null)), |
| 124 | 165 | 'secondaryColorText' => '#ffffff', |
| 125 | 166 | ]; |
| 126 | 167 | self::$config['showAIPageCreation'] = ($data['showAIPageCreation'] ?? self::$config['showAIPageCreation']); |
| 168 | + self::$config['mcpConfig'] = ($data['mcpConfig'] ?? self::$config['mcpConfig']); | |
| 169 | + self::$config['mcpWriteList'] = ($data['mcpWriteList'] ?? self::$config['mcpWriteList']); | |
| 170 | + self::$config['mcpReadList'] = ($data['mcpReadList'] ?? self::$config['mcpReadList']); | |
| 127 | 171 | self::$config['showLaunch'] = ($data['showLaunch'] ?? self::$config['showLaunch']); |
| 172 | + self::$config['showLaunchTitle'] = ($data['showLaunchTitle'] ?? self::$config['showLaunchTitle']); | |
| 128 | 173 | self::$config['deactivated'] = ($data['deactivated'] ?? self::$config['deactivated']); |
| 129 | 174 | self::$config['launchRedirectWebsite'] = ($data['launchRedirectWebsite'] |
| 130 | 175 | ?? self::$config['launchRedirectWebsite']); |
| 131 | 176 | self::$config['showAILogo'] = ($data['showAILogo'] ?? self::$config['showAILogo']); |
| @@ -142,14 +187,29 @@ | ||
| 142 | 187 | self::$config['license'] = ($data['license'] ?? self::$config['license']); |
| 143 | 188 | self::$config['showImprint'] = ($data['showImprint'] ?? self::$config['showImprint']); |
| 144 | 189 | self::$config['showLaunchQuestions'] = ($data['showLaunchQuestions'] ?? self::$config['showLaunchQuestions']); |
| 145 | 190 | self::$config['showAIAgents'] = ($data['showAIAgents'] ?? self::$config['showAIAgents']); |
| 191 | + self::$config['agentAbilitiesAllowlist'] = ($data['agentAbilitiesAllowlist'] | |
| 192 | + ?? self::$config['agentAbilitiesAllowlist']); | |
| 193 | + self::$config['showQuickEdit'] = ($data['showQuickEdit'] ?? self::$config['showQuickEdit']); | |
| 194 | + self::$config['showSimpleToolbar'] = ($data['showSimpleToolbar'] | |
| 195 | + ?? self::$config['showSimpleToolbar']); | |
| 146 | 196 | self::$config['pluginGroupId'] = ($data['pluginGroup'] ?? self::$config['pluginGroupId']); |
| 147 | 197 | self::$config['requiredPlugins'] = ($data['requiredPlugins'] ?? self::$config['requiredPlugins']); |
| 198 | + self::$config['showProductActivation'] = ($data['showProductActivation'] | |
| 199 | + ?? self::$config['showProductActivation']); | |
| 148 | 200 | self::$config['useAgentOnboarding'] = ($data['useAgentOnboarding'] ?? self::$config['useAgentOnboarding']); |
| 149 | 201 | self::$config['hidePluginNotifications'] = ($data['hidePluginNotifications'] |
| 150 | 202 | ?? self::$config['hidePluginNotifications']); |
| 151 | 203 | self::$config['hideLaunchExitLink'] = ($data['hideLaunchExitLink'] ?? self::$config['hideLaunchExitLink']); |
| 204 | + self::$config['useAutoUpdate'] = ($data['useAutoUpdate'] ?? self::$config['useAutoUpdate']); | |
| 205 | + self::$config['showLaunchUpdate'] = ($data['showLaunchUpdate'] ?? self::$config['showLaunchUpdate']); | |
| 206 | + self::$config['activeTests'] = ($data['activeTests'] ?? self::$config['activeTests']); | |
| 207 | + self::$config['showExtendifyCode'] = ($data['showExtendifyCode'] ?? self::$config['showExtendifyCode']); | |
| 208 | + self::$config['useComingSoon'] = ($data['useComingSoon'] ?? self::$config['useComingSoon']); | |
| 209 | + self::$config['extendifyCodeData'] = ($data['extendifyCodeData'] ?? self::$config['extendifyCodeData']); | |
| 210 | + self::$config['customDesign'] = ($data['customDesign'] ?? self::$config['customDesign']); | |
| 211 | + self::$config['strings'] = ($data['strings'] ?? self::$config['strings']); | |
| 152 | 212 | |
| 153 | 213 | // Add the job hook to fetch the partner data. |
| 154 | 214 | \add_action('extendify_fetch_partner_data', [self::class, 'fetchPartnerData']); |
| 155 | 215 | } |
| @@ -169,15 +229,12 @@ | ||
| 169 | 229 | $partnerData = \get_option('extendify_partner_data_v2', 'empty'); |
| 170 | 230 | if ($partnerData !== 'empty') { |
| 171 | 231 | // We have data, but if it's been 10 minutes, check for new data. |
| 172 | 232 | $partnerRefresh = \get_transient('extendify_partner_data_cache_check'); |
| 173 | - if (!$partnerRefresh && \is_admin()) { | |
| 174 | - \add_action('init', function () { | |
| 175 | - if (!\wp_next_scheduled('extendify_fetch_partner_data')) { | |
| 176 | - \wp_schedule_single_event(time(), 'extendify_fetch_partner_data'); | |
| 177 | - \spawn_cron(); | |
| 178 | - } | |
| 179 | - }); | |
| 233 | + if (!$partnerRefresh && \is_user_logged_in()) { | |
| 234 | + \did_action('init') | |
| 235 | + ? self::scheduleRefresh() | |
| 236 | + : \add_action('init', [self::class, 'scheduleRefresh']); | |
| 180 | 237 | } |
| 181 | 238 | |
| 182 | 239 | return array_merge(self::$config, $partnerData); |
| 183 | 240 | } |
| @@ -189,8 +246,49 @@ | ||
| 189 | 246 | return $mergedData; |
| 190 | 247 | } |
| 191 | 248 | |
| 192 | 249 | /** |
| 250 | + * A token request may be the only visitor a site gets, so it fetches a stale config itself. | |
| 251 | + * | |
| 252 | + * @return void | |
| 253 | + */ | |
| 254 | + public static function refreshIfStale() | |
| 255 | + { | |
| 256 | + if (\get_transient('extendify_partner_data_cache_check')) { | |
| 257 | + return; | |
| 258 | + } | |
| 259 | + | |
| 260 | + // The 45s timeout the plugin gives its hosts is longer than a waiting MCP client allows. | |
| 261 | + $brief = function ($args) { | |
| 262 | + $args['timeout'] = 5; | |
| 263 | + return $args; | |
| 264 | + }; | |
| 265 | + \add_filter('http_request_args', $brief, 101); | |
| 266 | + try { | |
| 267 | + $fetched = self::fetchPartnerData(); | |
| 268 | + } finally { | |
| 269 | + \remove_filter('http_request_args', $brief, 101); | |
| 270 | + } | |
| 271 | + | |
| 272 | + if ($fetched) { | |
| 273 | + self::load(); | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * @return void | |
| 279 | + */ | |
| 280 | + public static function scheduleRefresh() | |
| 281 | + { | |
| 282 | + if (\wp_next_scheduled('extendify_fetch_partner_data')) { | |
| 283 | + return; | |
| 284 | + } | |
| 285 | + | |
| 286 | + \wp_schedule_single_event(time(), 'extendify_fetch_partner_data'); | |
| 287 | + \spawn_cron(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 193 | 291 | * Fetch or refresh the partner data |
| 194 | 292 | * |
| 195 | 293 | * @return array |
| 196 | 294 | */ |
| @@ -207,10 +305,11 @@ | ||
| 207 | 305 | [ |
| 208 | 306 | 'partner' => self::$id, |
| 209 | 307 | 'wp_language' => \get_locale(), |
| 210 | 308 | 'site_url' => \home_url(), |
| 309 | + 'site_id' => \get_option('extendify_site_id', ''), | |
| 211 | 310 | ], |
| 212 | - 'https://dashboard.extendify.com/api/onboarding/partner-data/' | |
| 311 | + Constants::DASHBOARD_HOST . '/api/onboarding/partner-data/' | |
| 213 | 312 | ); |
| 214 | 313 | |
| 215 | 314 | $response = \wp_safe_remote_get($url, ['headers' => ['Accept' => 'application/json']]); |
| 216 | 315 | |
| @@ -232,9 +331,16 @@ | ||
| 232 | 331 | } |
| 233 | 332 | |
| 234 | 333 | $sanitizedData = array_merge( |
| 235 | 334 | Sanitizer::sanitizeUnknown($result['data']), |
| 236 | - ['consentTermsCustom' => \sanitize_text_field(htmlentities(($result['data']['consentTermsCustom'] ?? '')))] | |
| 335 | + [ | |
| 336 | + 'consentTermsCustom' => \sanitize_text_field(htmlentities( | |
| 337 | + ($result['data']['consentTermsCustom'] ?? ''), | |
| 338 | + ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 | |
| 339 | + )), | |
| 340 | + 'customDesign' => self::sanitizeDesign($result['data']['customDesign'] ?? null), | |
| 341 | + 'strings' => self::sanitizeStrings($result['data']['strings'] ?? null), | |
| 342 | + ] | |
| 237 | 343 | ); |
| 238 | 344 | |
| 239 | 345 | // Merge before persisting as this data is accessed directly elsewhere. |
| 240 | 346 | $mergedData = array_merge(self::$config, $sanitizedData); |
| @@ -240,8 +346,76 @@ | ||
| 240 | 346 | $mergedData = array_merge(self::$config, $sanitizedData); |
| 241 | 347 | \update_option('extendify_partner_data_v2', $mergedData); |
| 242 | 348 | |
| 243 | 349 | return $mergedData; |
| 350 | + } | |
| 351 | + | |
| 352 | + /** | |
| 353 | + * Partner copy overriding the shipped strings. | |
| 354 | + * | |
| 355 | + * Which keys exist is the flow's to know, so only shape and text are checked here. | |
| 356 | + * | |
| 357 | + * @param mixed $strings The map as the partner-data response carried it. | |
| 358 | + * @return array | |
| 359 | + */ | |
| 360 | + public static function sanitizeStrings($strings) | |
| 361 | + { | |
| 362 | + return array_map( | |
| 363 | + 'sanitize_text_field', | |
| 364 | + self::designEntries($strings, '/^[a-zA-Z][a-zA-Z0-9]*$/', 'is_string') | |
| 365 | + ); | |
| 366 | + } | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * A design carries GLSL, which the text sanitizers break, so only its shape is checked. | |
| 370 | + * | |
| 371 | + * Empty members are left out rather than kept: json_encode writes an empty | |
| 372 | + * PHP array as [], and the page reads the design as an object. | |
| 373 | + * | |
| 374 | + * @param mixed $design The design as the partner-data response carried it. | |
| 375 | + * @return array|null | |
| 376 | + */ | |
| 377 | + private static function sanitizeDesign($design) | |
| 378 | + { | |
| 379 | + if (!is_array($design)) { | |
| 380 | + return null; | |
| 381 | + } | |
| 382 | + | |
| 383 | + $shader = ($design['shader'] ?? null); | |
| 384 | + $logo = ($design['logo'] ?? null); | |
| 385 | + $kept = array_filter([ | |
| 386 | + 'vars' => self::designEntries( | |
| 387 | + ($design['vars'] ?? null), | |
| 388 | + '/^--ext-(ui|tpl)-[a-z0-9-]+$/', | |
| 389 | + function ($value) { | |
| 390 | + // A CSS value is a string, but 0.88 is a natural way to write one. | |
| 391 | + return is_string($value) || is_int($value) || is_float($value); | |
| 392 | + } | |
| 393 | + ), | |
| 394 | + 'templates' => self::designEntries(($design['templates'] ?? null), '/^[A-Za-z0-9_-]+$/', 'is_string'), | |
| 395 | + 'shader' => is_string($shader) ? \wp_check_invalid_utf8($shader) : '', | |
| 396 | + 'logo' => is_string($logo) ? \esc_url_raw($logo) : '', | |
| 397 | + ]); | |
| 398 | + | |
| 399 | + return $kept ?: null; | |
| 400 | + } | |
| 401 | + | |
| 402 | + private static function designEntries($entries, $keyPattern, callable $accepts) | |
| 403 | + { | |
| 404 | + if (!is_array($entries)) { | |
| 405 | + return []; | |
| 406 | + } | |
| 407 | + | |
| 408 | + $kept = []; | |
| 409 | + foreach ($entries as $key => $value) { | |
| 410 | + if (!is_string($key) || !preg_match($keyPattern, $key) || !$accepts($value)) { | |
| 411 | + continue; | |
| 412 | + } | |
| 413 | + | |
| 414 | + $kept[$key] = is_string($value) ? \wp_check_invalid_utf8($value) : $value; | |
| 415 | + } | |
| 416 | + | |
| 417 | + return $kept; | |
| 244 | 418 | } |
| 245 | 419 | |
| 246 | 420 | /** |
| 247 | 421 | * Return colors mapped as css variables |