load_services(); } private function load_services() { $base = AIEO_DIR . 'includes/social-login/'; if (!class_exists('AIEO_SSO_Buttons_Data')) { $bd = $base . 'class-aieo-sso-buttons-data.php'; if (file_exists($bd)) { require_once $bd; } } foreach (array( 'class-aieo-sso-state.php', 'class-aieo-sso-provider-base.php', 'class-aieo-sso-account-linker.php', 'class-aieo-sso-rest.php', 'class-aieo-sso-migrator.php', 'class-aieo-sso-newsletter-wiring.php', ) as $f) { $path = $base . $f; if (file_exists($path)) { require_once $path; } } if (class_exists('AIEO_SSO_Newsletter_Wiring')) { AIEO_SSO_Newsletter_Wiring::register(); } $providers_dir = $base . 'providers/'; if (is_dir($providers_dir)) { foreach (glob($providers_dir . 'class-aieo-sso-provider-*.php') as $provider_path) { require_once $provider_path; } } if (class_exists('AIEO_SSO_Rest')) { AIEO_SSO_Rest::register(); } } public function register_hooks() { if (is_admin()) { add_action('admin_enqueue_scripts', function () { if ((isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '') === 'ai-eshop-dmm' && (isset($_GET['dmm_tab']) ? sanitize_text_field(wp_unslash($_GET['dmm_tab'])) : '') === 'social_login') { $css = 'assets/css/aieo-social-login-admin.css'; wp_enqueue_style('aieo-social-login-admin', AIEO_URI . $css, array(), file_exists(AIEO_DIR . $css) ? (string) filemtime(AIEO_DIR . $css) : '1'); } }); } add_action('wp_ajax_aieo_sso_save_settings', array($this, 'ajax_save_settings')); add_action('wp_ajax_aieo_sso_migrate_audit', array($this, 'ajax_migrate_audit')); add_action('wp_ajax_aieo_sso_migrate_settings',array($this, 'ajax_migrate_settings')); add_action('wp_ajax_aieo_sso_migrate_users', array($this, 'ajax_migrate_users')); } public function get_tab_arg() { return 'sso_section'; } public function get_tabs() { return array( 'settings' => array('label' => __('Settings', 'ai-eshop-optimizer'), 'icon' => 'settings'), 'migrator' => array('label' => __('Migrator', 'ai-eshop-optimizer'), 'icon' => 'import'), ); } public function render_subtab() { $tabs = $this->get_tabs(); $section = isset($_GET['sso_section']) ? sanitize_key((string) $_GET['sso_section']) : 'settings'; if (!isset($tabs[$section])) { $section = 'settings'; } $base_url = admin_url('admin.php?page=' . AIEO_DMM::MENU_SLUG . '&dmm_tab=social_login'); AIEO_DMM_UI::tab_strip($this->get_id(), $tabs, $section, array( 'arg' => $this->get_tab_arg(), 'wrapper' => 'div', 'margin' => '14px', )); switch ($section) { case 'settings': $this->render_settings(); break; case 'migrator': $this->render_migrator(); break; } } private function provider_help_links() { return array( 'google' => array( 'console' => 'https://console.cloud.google.com/apis/credentials', 'docs' => 'https://developers.google.com/identity/protocols/oauth2/web-server', ), 'facebook' => array( 'console' => 'https://developers.facebook.com/apps/', 'docs' => 'https://developers.facebook.com/docs/facebook-login/web', ), 'linkedin' => array( 'console' => 'https://www.linkedin.com/developers/apps', 'docs' => 'https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2', ), 'twitter' => array( 'console' => 'https://developer.twitter.com/en/portal/dashboard', 'docs' => 'https://developer.twitter.com/en/docs/authentication/oauth-2-0/authorization-code', ), 'apple' => array( 'console' => 'https://developer.apple.com/account/resources/identifiers/list/serviceId', 'docs' => 'https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple', ), 'github' => array( 'console' => 'https://github.com/settings/developers', 'docs' => 'https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app', ), 'microsoft' => array( 'console' => 'https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade', 'docs' => 'https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app', ), ); } private function render_settings() { $settings = AIEO_SSO_Buttons_Data::get_settings(); $providers_meta = array( 'google' => __('Google', 'ai-eshop-optimizer'), 'facebook' => __('Facebook', 'ai-eshop-optimizer'), 'linkedin' => __('LinkedIn', 'ai-eshop-optimizer'), 'twitter' => __('X.com', 'ai-eshop-optimizer'), 'apple' => __('Apple', 'ai-eshop-optimizer'), 'github' => __('GitHub', 'ai-eshop-optimizer'), 'microsoft' => __('Microsoft', 'ai-eshop-optimizer'), 'magic_link' => __('Magic Link', 'ai-eshop-optimizer'), ); $help = $this->provider_help_links(); $intents_meta = array( 'login' => __('Login form', 'ai-eshop-optimizer'), 'register' => __('Register form', 'ai-eshop-optimizer'), 'checkout' => __('Checkout (classic + block)', 'ai-eshop-optimizer'), 'my_account' => __('My Account → Connected accounts', 'ai-eshop-optimizer'), 'newsletter' => __('Newsletter pattern (footer)', 'ai-eshop-optimizer'), ); $nonce = wp_create_nonce('aieo_sso_admin'); $print = static function ($html) { echo $html; }; $card = static function (array $c, $fields) use ($print, $nonce) { $form_id = 'aieo-card-' . $c['anchor'] . '-form'; $c['footer'] = \Rooster\Admin\States::save_button($form_id); list($open, $close) = AIEO_DMM_UI::card($c); $print($open . AIEO_DMM_UI::card_form('aieo_sso_save_settings', 'social_login', $form_id, $fields, array('nonce' => $nonce, 'nonce_name' => 'nonce')) . $close); }; if (function_exists('rooster_admin_band_open')) { rooster_admin_band_open(array('cols' => 2, 'min' => 35)); } $sso_rows = array(); foreach ($providers_meta as $code => $label) { $row = isset($settings['providers'][$code]) ? $settings['providers'][$code] : array(); $sso_rows[] = array( 'code' => (string) $code, 'label' => (string) $label, 'links' => isset($help[$code]) ? (array) $help[$code] : array(), 'enabled' => !empty($row['enabled']), 'client_id' => (string) ($row['client_id'] ?? ''), 'client_secret' => '', 'secret_saved' => '' !== (string) ($row['client_secret'] ?? ''), 'button_text' => (string) ($row['button_text'] ?? ''), 'placeholder' => AIEO_SSO_Buttons_Data::default_button_text($code), 'callback' => AIEO_SSO_Buttons_Data::callback_url($code), 'is_magic' => ('magic_link' === $code), ); } $named = static function ($row, $column) { return ' aria-label="' . esc_attr($row['label'] . ', ' . $column) . '"'; }; ob_start(); echo \Rooster\Admin\Guidance::text(__('Paste the OAuth Client ID + Secret from each provider\'s developer console. Enable only the providers you have configured.', 'ai-eshop-optimizer'), 'guidance', array('question' => __('Where do the keys come from?', 'ai-eshop-optimizer'))); AIEO_Table::render(array( 'columns' => array( array( 'key' => 'label', 'label' => __('Provider', 'ai-eshop-optimizer'), 'type' => 'control', 'format' => array( 'render' => static function ($row) { $out = '' . esc_html($row['label']) . '
' . '' . esc_html($row['code']) . ''; if (!empty($row['links']['console']) || !empty($row['links']['docs'])) { $out .= '
'; if (!empty($row['links']['console'])) { $out .= \Rooster\Admin\Ui::button(__('Get credentials', 'ai-eshop-optimizer'), array('href' => $row['links']['console'], 'external' => true, 'size' => 'sm')); } if (!empty($row['links']['docs'])) { $out .= \Rooster\Admin\Ui::button(__('Setup guide', 'ai-eshop-optimizer'), array('href' => $row['links']['docs'], 'external' => true, 'size' => 'sm')); } $out .= '
'; } return $out; }, ), ), array( 'key' => 'enabled', 'label' => __('Enabled', 'ai-eshop-optimizer'), 'type' => 'control', 'format' => array( 'render' => static function ($row) use ($named) { return ''; }, ), ), array( 'key' => 'client_id', 'label' => __('Client ID', 'ai-eshop-optimizer'), 'type' => 'control', 'span' => static function ($row) { return $row['is_magic'] ? 3 : 1; }, 'format' => array( 'render' => static function ($row) use ($named) { if ($row['is_magic']) { return '' . esc_html__('Email-based login — no Client ID / Secret / Callback URL required. Customer types their email in a modal; we send a one-time login link.', 'ai-eshop-optimizer') . ''; } return ''; }, ), ), array( 'key' => 'client_secret', 'label' => __('Client Secret', 'ai-eshop-optimizer'), 'type' => 'control', 'format' => array( 'render' => static function ($row) { $column = __('Client Secret', 'ai-eshop-optimizer'); return \Rooster\Admin\Fields::secret( 'providers[' . $row['code'] . '][client_secret]', !empty($row['secret_saved']), array( 'id' => 'aieo-sso-' . str_replace('_', '-', $row['code']) . '-client-secret', 'label' => $row['label'] . ', ' . $column, 'attrs' => array('aria-label' => $row['label'] . ', ' . $column), ) ); }, ), ), array( 'key' => 'callback', 'label' => __('Callback URL', 'ai-eshop-optimizer'), 'type' => 'control', 'format' => array( 'render' => static function ($row) { return '' . esc_html($row['callback']) . '
' . ''; }, ), ), array( 'key' => 'button_text', 'label' => __('Button text', 'ai-eshop-optimizer'), 'type' => 'control', 'format' => array( 'render' => static function ($row) use ($named) { return ''; }, ), ), ), 'rows' => $sso_rows, 'row_key' => 'code', 'page_id' => 'social_login', )); echo \Rooster\Admin\Guidance::text(__('Tip: paste the Callback URL into the provider\'s "Authorised redirect URIs" / "Valid OAuth Redirect URIs" field. Get credentials → opens the provider\'s developer console; Setup guide → opens the provider\'s setup documentation.', 'ai-eshop-optimizer'), 'guidance', array('question' => __('Where does the Callback URL go?', 'ai-eshop-optimizer'))); echo ''; $card(array( 'anchor' => 'social-login-providers-2', 'icon' => 'users', 'title' => __('Providers', 'ai-eshop-optimizer'), 'description' => __('Sign in with Google, Facebook, LinkedIn, X.com, Apple, GitHub or Microsoft. Surfaces buttons on the login form, register form, classic + block checkout, my-account, and the footer newsletter pattern. Replaces the SkyVerge `woocommerce-social-login` plugin.', 'ai-eshop-optimizer'), 'span' => 'all', ), (string) ob_get_clean()); $fields = '
'; foreach ($intents_meta as $code => $label) { $id = 'aieo-sso-intent-' . str_replace('_', '-', $code); $fields .= \Rooster\Admin\Ui::field_row(array( 'id' => $id, 'label' => $label, 'control' => \Rooster\Admin\Ui::toggle('intents[' . $code . ']', !empty($settings['intents'][$code]), array( 'id' => $id, 'hidden' => false, 'label' => __('Enabled', 'ai-eshop-optimizer'), )), )); } $fields .= '
'; $card(array( 'anchor' => 'social-login-where-to-render-2', 'icon' => 'layout-template', 'title' => __('Where to render', 'ai-eshop-optimizer'), 'description' => __('Pick the surfaces where the SSO buttons appear.', 'ai-eshop-optimizer'), 'col' => 1, ), $fields); $fields = '
' . \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-sso-force-ssl-callback', 'label' => __('Force HTTPS callback URLs', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::toggle('force_ssl_callback', !empty($settings['force_ssl_callback']), array( 'id' => 'aieo-sso-force-ssl-callback', 'hidden' => false, 'label' => __('Required by Apple, recommended for all providers.', 'ai-eshop-optimizer'), )), )) . \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-sso-intro-text', 'label' => __('Intro text', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::textarea('intro_text', (string) ($settings['intro_text'] ?? ''), array( 'id' => 'aieo-sso-intro-text', 'rows' => 3, 'placeholder' => __('Optional text rendered above the buttons (e.g. on the checkout).', 'ai-eshop-optimizer'), )), )) . '
'; $card(array( 'anchor' => 'social-login-security-2', 'icon' => 'shield', 'title' => __('Security', 'ai-eshop-optimizer'), 'description' => __('How the sign-in buttons talk to each provider, and the text shown above them.', 'ai-eshop-optimizer'), 'col' => 2, ), $fields); $all_roles = wp_roles()->roles; if (!empty($all_roles)) { $saved_blocked = array(); if (array_key_exists('blocked_roles', $settings) && is_array($settings['blocked_roles'])) { $saved_blocked = array_map('sanitize_key', $settings['blocked_roles']); } else { $saved_blocked = AIEO_SSO_Account_Linker::default_blocked_roles(); } ksort($all_roles); $roles = array(); $keys = array(); foreach ($all_roles as $role_key => $role_meta) { $role_key = sanitize_key($role_key); $roles[$role_key] = isset($role_meta['name']) ? (string) $role_meta['name'] : $role_key; $keys[$role_key] = $role_key; } $anchor = 'social-login-block-auto-link-for-these-roles'; $fields = '' . '
' . \Rooster\Admin\Ui::checks('blocked_roles', $roles, $saved_blocked, array('grid' => true, 'descriptions' => $keys)) . '
'; $card(array( 'anchor' => $anchor, 'icon' => 'user-lock', 'title' => __('Roles blocked from auto-link', 'ai-eshop-optimizer'), 'description' => __('When a social login matches an existing user by email, the user is logged in automatically. Tick any role here to OPT OUT of that auto-link (the user will see a friendly "account already exists" message instead).', 'ai-eshop-optimizer'), 'span' => 'all', ), \Rooster\Admin\Ui::notice(esc_html__('Recommended: block administrative roles; leave customer-side roles unticked.', 'ai-eshop-optimizer'), 'warning', array('inline' => true)) . $fields); } if (function_exists('rooster_admin_band_close')) { rooster_admin_band_close(); } } private function render_migrator() { $audit = AIEO_SSO_Migrator::audit(); $nonce = wp_create_nonce('aieo_sso_admin'); list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'social-login-migrate-from-woocommerce-social-login-2', 'icon' => 'import', 'title' => __('Migrate from the SkyVerge plugin', 'ai-eshop-optimizer'), 'description' => __('Permanent, idempotent, re-runnable. Lifts the legacy SkyVerge plugin\'s settings + per-user link meta into our keys. Safe to run multiple times.', 'ai-eshop-optimizer'), 'attrs' => array('data-aieo-sso-migrate' => '', 'data-nonce' => $nonce, 'data-linked' => (string) (int) $audit['linked_users']), )); echo $open; if (function_exists('rooster_admin_card_section_html')) { echo rooster_admin_card_section_html(__('Audit', 'ai-eshop-optimizer')); } else { echo '

' . esc_html__('Audit', 'ai-eshop-optimizer') . '

'; } echo ''; echo '

' . \Rooster\Admin\Ui::button(__('Re-audit', 'ai-eshop-optimizer'), array('size' => 'sm', 'icon' => 'refresh-cw', 'attrs' => array('id' => 'aieo-sso-migrate-audit'))) . '

'; if (function_exists('rooster_admin_card_section_html')) { echo rooster_admin_card_section_html(); } $jobs = \Rooster\Admin\States::job(array( 'id' => 'aieo-sso-migrate-settings-job', 'state' => 'idle', 'run' => __('Migrate settings', 'ai-eshop-optimizer'), 'run_attrs' => array('id' => 'aieo-sso-migrate-settings'), 'primary' => false, 'progress_label' => __('Migrating settings…', 'ai-eshop-optimizer'), )); $jobs .= \Rooster\Admin\States::job(array( 'id' => 'aieo-sso-migrate-users-job', 'state' => 'idle', 'run' => __('Migrate user links (batched)', 'ai-eshop-optimizer'), 'run_attrs' => array('id' => 'aieo-sso-migrate-users'), 'primary' => false, 'progress_label' => __('Migrating user links… processed', 'ai-eshop-optimizer'), )); echo $jobs; echo $close; $this->print_migrator_js($nonce); } private function print_migrator_js($nonce) { $ajax = admin_url('admin-ajax.php'); ?> 'permission_denied'), 403); } check_ajax_referer('aieo_sso_admin', 'nonce'); } public function ajax_save_settings() { $this->verify_admin_ajax(); $providers_in = isset($_POST['providers']) && is_array($_POST['providers']) ? wp_unslash($_POST['providers']) : array(); $intents_in = isset($_POST['intents']) && is_array($_POST['intents']) ? wp_unslash($_POST['intents']) : array(); $intro_text = isset($_POST['intro_text']) ? sanitize_textarea_field((string) wp_unslash($_POST['intro_text'])) : ''; $force_ssl = !empty($_POST['force_ssl_callback']); $blocked_roles_clean = null; if (!empty($_POST['blocked_roles_present'])) { $br_in = isset($_POST['blocked_roles']) && is_array($_POST['blocked_roles']) ? wp_unslash($_POST['blocked_roles']) : array(); $blocked_roles_clean = array_values(array_unique(array_filter(array_map('sanitize_key', (array) $br_in)))); } $stored_providers = (array) (AIEO_SSO_Buttons_Data::get_settings()['providers'] ?? array()); $clean_providers = array(); foreach ((array) $providers_in as $code => $row) { $code = sanitize_key((string) $code); if ($code === '') { continue; } $secret = isset($row['client_secret']) ? sanitize_text_field((string) $row['client_secret']) : ''; if ('' === trim(str_replace("\u{2022}", '', $secret))) { $secret = (string) ($stored_providers[$code]['client_secret'] ?? ''); } $clean_providers[$code] = array( 'enabled' => !empty($row['enabled']), 'client_id' => isset($row['client_id']) ? sanitize_text_field((string) $row['client_id']) : '', 'client_secret' => $secret, 'button_text' => isset($row['button_text']) ? sanitize_text_field((string) $row['button_text']) : '', ); } foreach ($stored_providers as $code => $row) { if (is_array($row) && !isset($clean_providers[$code])) { $clean_providers[$code] = $row; } } $clean_intents = array(); foreach (array('login', 'register', 'checkout', 'my_account', 'newsletter') as $i) { $clean_intents[$i] = !empty($intents_in[$i]); } $to_save = array( 'providers' => $clean_providers, 'intents' => $clean_intents, 'force_ssl_callback' => $force_ssl, 'intro_text' => $intro_text, 'captcha' => $this->sanitize_captcha_post(), ); if ($blocked_roles_clean !== null) { $to_save['blocked_roles'] = $blocked_roles_clean; } else { $current = AIEO_SSO_Buttons_Data::get_settings(); if (array_key_exists('blocked_roles', $current)) { $to_save['blocked_roles'] = $current['blocked_roles']; } } AIEO_SSO_Buttons_Data::save_settings($to_save); wp_send_json_success(); } private function sanitize_captcha_post() { $current = AIEO_SSO_Buttons_Data::captcha_settings(); if (empty($_POST['captcha_present'])) { return $current; } $in = isset($_POST['captcha']) && is_array($_POST['captcha']) ? wp_unslash($_POST['captcha']) : array(); $secret = isset($in['secret_key']) ? sanitize_text_field((string) $in['secret_key']) : ''; if ('' === trim(str_replace("\u{2022}", '', $secret))) { $secret = (string) $current['secret_key']; } $threshold = isset($in['threshold']) ? (float) $in['threshold'] : (float) $current['threshold']; $threshold = max(0.0, min(1.0, $threshold)); $surfaces_in = isset($in['surfaces']) && is_array($in['surfaces']) ? $in['surfaces'] : array(); $surfaces = array(); foreach (array('login', 'register', 'lost_password', 'magic_link') as $surface) { $surfaces[$surface] = !empty($surfaces_in[$surface]); } return array( 'enabled' => !empty($in['enabled']), 'site_key' => isset($in['site_key']) ? sanitize_text_field((string) $in['site_key']) : '', 'secret_key' => $secret, 'threshold' => $threshold, 'surfaces' => $surfaces, 'fail_open' => !empty($in['fail_open']), 'hide_badge' => !empty($in['hide_badge']), ); } public function ajax_migrate_audit() { $this->verify_admin_ajax(); wp_send_json_success(AIEO_SSO_Migrator::audit()); } public function ajax_migrate_settings() { $this->verify_admin_ajax(); $result = AIEO_SSO_Migrator::run_settings(); wp_send_json_success(array('providers_migrated' => (int) ($result['providers_migrated'] ?? 0))); } public function ajax_migrate_users() { $this->verify_admin_ajax(); $cursor = isset($_POST['cursor']) ? (int) $_POST['cursor'] : 0; $batch_size = isset($_POST['batch_size']) ? (int) $_POST['batch_size'] : 500; wp_send_json_success(AIEO_SSO_Migrator::run_users($batch_size, $cursor)); } }