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 .= '
'
. esc_html($row['callback']) . '' . \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)); } }