array(), 'intents' => array( 'login' => true, 'register' => true, 'checkout' => true, 'my_account' => true, 'newsletter' => false, ), 'force_ssl_callback' => false, 'intro_text' => '', 'captcha' => self::captcha_defaults(), ); self::$cache = array_replace_recursive($defaults, $opt); return self::$cache; } public static function captcha_defaults() { return array( 'enabled' => false, 'site_key' => '', 'secret_key' => '', 'threshold' => 0.5, 'surfaces' => array( 'login' => false, 'register' => true, 'lost_password' => false, 'magic_link' => false, 'product_request' => false, 'contact_form' => false, ), 'fail_open' => true, 'hide_badge' => false, ); } public static function captcha_settings() { $settings = self::get_settings(); $captcha = isset($settings['captcha']) && is_array($settings['captcha']) ? $settings['captcha'] : array(); return array_replace_recursive(self::captcha_defaults(), $captcha); } public static function captcha_is_active($surface = '') { $captcha = self::captcha_settings(); if (empty($captcha['enabled'])) { return false; } if ((string) $captcha['site_key'] === '') { return false; } if ((string) $captcha['secret_key'] === '') { return false; } if ($surface === '') { return true; } $surfaces = is_array($captcha['surfaces']) ? $captcha['surfaces'] : array(); return !empty($surfaces[$surface]); } public static function captcha_has_active_surface() { if (!self::captcha_is_active()) { return false; } $captcha = self::captcha_settings(); $surfaces = is_array($captcha['surfaces']) ? $captcha['surfaces'] : array(); foreach ($surfaces as $on) { if (!empty($on)) { return true; } } return false; } public static function save_settings(array $value) { self::$cache = null; return update_option(self::OPTION_KEY, $value, false); } public static function enabled_providers_for_intent($intent) { $settings = self::get_settings(); $intents = is_array($settings['intents']) ? $settings['intents'] : array(); $settings_key = ($intent === 'subscribe') ? 'newsletter' : $intent; if (empty($intents[$settings_key])) { return array(); } $out = array(); $order = array('google', 'facebook', 'apple', 'linkedin', 'twitter', 'github', 'microsoft', 'magic_link'); foreach ($order as $code) { $row = isset($settings['providers'][$code]) ? $settings['providers'][$code] : array(); if (empty($row['enabled'])) { continue; } if ($code !== 'magic_link') { if (empty($row['client_id'])) { continue; } if (empty($row['client_secret'])) { continue; } } $button_text = isset($row['button_text']) && (string) $row['button_text'] !== '' ? (string) $row['button_text'] : self::default_button_text($code); $entry = array( 'code' => $code, 'label' => self::default_label($code), 'button_text' => $button_text, 'start_url' => add_query_arg(array( 'intent' => $intent, ), rest_url('aieo/v1/social-login/' . $code . '/start')), ); if ($code === 'magic_link') { $entry['start_url'] = '#aieo-magic-link'; $entry['modal'] = true; } $out[] = $entry; } return $out; } public static function default_label($code) { $map = array( 'google' => 'Google', 'facebook' => 'Facebook', 'apple' => 'Apple', 'linkedin' => 'LinkedIn', 'twitter' => 'X.com', 'github' => 'GitHub', 'microsoft' => 'Microsoft', 'magic_link' => 'Magic Link', ); return isset($map[$code]) ? $map[$code] : ucfirst($code); } public static function default_button_text($code) { return sprintf(__('Sign in with %s', 'ai-eshop-optimizer'), self::default_label($code)); } public static function callback_url($provider) { $settings = self::get_settings(); $url = rest_url('aieo/v1/social-login/' . $provider . '/callback'); if (!empty($settings['force_ssl_callback']) && stripos($url, 'http://') === 0) { $url = 'https://' . substr($url, 7); } return $url; } }