'intent:.aieo-sso-buttons,.aieo-sso-btn,[data-aieo-magic-link]');
return $handles;
});
}
public static function enqueue_magic_link_assets() {
if (is_admin()) { return; }
if ((function_exists('is_checkout') && is_checkout()) || (function_exists('is_account_page') && is_account_page())) {
AIEO_SSO_Buttons::enqueue_css();
}
$rel = 'assets/js/aieo-magic-link-modal.js';
$abs = defined('AIEO_DIR') ? AIEO_DIR . $rel : '';
if ($abs === '' || !file_exists($abs)) { return; }
wp_enqueue_script(
'aieo-magic-link-modal',
plugins_url($rel, AIEO_DIR . 'ai-eshop-optimizer.php'),
array(),
(string) filemtime($abs),
array('in_footer' => true, 'strategy' => 'defer')
);
$redirect_to = home_url('/');
if (function_exists('is_checkout') && is_checkout()) { $redirect_to = wc_get_checkout_url(); }
elseif (function_exists('is_account_page') && is_account_page()) { $redirect_to = wc_get_page_permalink('myaccount'); }
elseif (isset($_SERVER['REQUEST_URI'])) { $redirect_to = home_url(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI']))); }
wp_localize_script('aieo-magic-link-modal', 'aieoMagicLinkCfg', array(
'endpoint' => rest_url('aieo/v1/social-login/magic-link/request'),
'intent' => function_exists('is_checkout') && is_checkout() ? 'checkout' : 'login',
'redirectTo' => $redirect_to,
'i18n' => array(
'title' => __('Sign in with email', 'ai-eshop-optimizer'),
'intro' => __('Give us your email and we will send you a sign-in link. No password needed.', 'ai-eshop-optimizer'),
'emailLabel' => __('Email', 'ai-eshop-optimizer'),
'emailPh' => __('name@example.com', 'ai-eshop-optimizer'),
'submit' => __('Send me a link', 'ai-eshop-optimizer'),
'sending' => __('Sending...', 'ai-eshop-optimizer'),
'sent' => __('We have sent you a sign-in link. Check your email.', 'ai-eshop-optimizer'),
'failed' => __('Something went wrong. Try again.', 'ai-eshop-optimizer'),
'invalidEmail' => __('Please give a valid email.', 'ai-eshop-optimizer'),
'rateLimited' => __('Too many attempts. Try again shortly.', 'ai-eshop-optimizer'),
'close' => __('Close', 'ai-eshop-optimizer'),
),
));
}
public static function render_login() {
echo AIEO_SSO_Buttons::render('login', array('layout' => 'inline'));
}
public static function render_register() {
echo AIEO_SSO_Buttons::render('register', array('layout' => 'inline'));
}
public static function render_my_account_login() {
$html = AIEO_SSO_Buttons::render('login', array('layout' => 'inline'));
if ($html === '') { return; }
echo '
';
echo '
'
. esc_html(
sprintf(
__('Use your Facebook or Google account for a quick, secure sign-in to %s. You will not need to type your email or create a separate password.', 'ai-eshop-optimizer'),
wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES)
)
)
. '
';
echo '
' . $html . '
';
echo '
';
}
public static function render_classic_checkout() {
if (is_user_logged_in()) { return; }
echo AIEO_SSO_Buttons::render('checkout', array('layout' => 'inline'));
}
public static function render_newsletter() {
$html = AIEO_SSO_Buttons::render('subscribe', array(
'layout' => 'inline',
'compact' => true,
'intro' => __('or sign up with:', 'ai-eshop-optimizer'),
));
if ($html === '') { return; }
echo '' . $html . '
';
}
public static function register_block_checkout_integration() {
if (!class_exists('Automattic\\WooCommerce\\StoreApi\\StoreApi')) { return; }
if (!class_exists('Automattic\\WooCommerce\\StoreApi\\Schemas\\ExtendSchema')) { return; }
try {
$extend = Automattic\WooCommerce\StoreApi\StoreApi::container()->get(
Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::class
);
} catch (\Throwable $e) {
return;
}
$extend->register_endpoint_data(array(
'endpoint' => Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER,
'namespace' => 'aieo_social_login',
'data_callback' => array(__CLASS__, 'block_checkout_data'),
'schema_callback' => array(__CLASS__, 'block_checkout_schema'),
));
}
public static function block_checkout_data() {
if (is_user_logged_in()) { return array('html' => ''); }
$html = AIEO_SSO_Buttons::render('checkout', array('layout' => 'inline'));
return array('html' => $html);
}
public static function block_checkout_schema() {
return array(
'html' => array(
'description' => __('Pre-rendered HTML of SSO buttons.', 'ai-eshop-optimizer'),
'type' => 'string',
'context' => array('view', 'edit'),
'readonly' => true,
),
);
}
public static function enqueue_block_checkout_script() {
if (!function_exists('has_block') || !function_exists('is_checkout')) { return; }
if (!is_checkout()) { return; }
wp_register_script('aieo-sso-block-checkout-shim', '', array(), '1.0', true);
wp_enqueue_script('aieo-sso-block-checkout-shim');
wp_add_inline_script('aieo-sso-block-checkout-shim', self::block_checkout_inline_js());
}
public static function noop_filter($x) { return $x; }
private static function block_checkout_inline_js() {
return <<<'JS'
(function(){
function inject(html) {
if (window.__aieoSsoInjected) return;
var contact = document.querySelector('[data-block-name="woocommerce/checkout-contact-information-block"]');
if (!contact || !html) return;
var holder = document.createElement('div');
holder.className = 'aieo-sso-block-checkout-slot';
holder.style.margin = '0 0 18px';
holder.innerHTML = html;
contact.parentNode.insertBefore(holder, contact);
window.__aieoSsoInjected = true;
}
function tryFromStore() {
if (window.__aieoSsoInjected) return true;
if (typeof wp === 'undefined' || !wp.data) return false;
try {
var sel = wp.data.select('wc/store/cart');
if (!sel || typeof sel.getCartData !== 'function') return false;
var cart = sel.getCartData();
if (!cart || !cart.extensions || !cart.extensions.aieo_social_login) return false;
inject(cart.extensions.aieo_social_login.html || '');
return !!window.__aieoSsoInjected;
} catch(e) { return false; }
}
function tryFromFetch() {
fetch('/wp-json/wc/store/v1/cart', { credentials:'same-origin', headers:{ 'Accept':'application/json' } })
.then(function(r){ return r.ok ? r.json() : null; })
.then(function(j){
if (!j || !j.extensions || !j.extensions.aieo_social_login) return;
inject(j.extensions.aieo_social_login.html || '');
})
.catch(function(){});
}
// Poll the redux store first (zero network); fall back to a single
// fetch on the cart endpoint if the store isn't populated yet.
var tries = 0;
var t = setInterval(function(){
if (tryFromStore()) { clearInterval(t); return; }
if (++tries > 8) { clearInterval(t); tryFromFetch(); }
}, 300);
})();
JS;
}
}