'yes', 'modal_title' => __('Checkout errors', 'ai-eshop-optimizer'), ), $stored); } private $stashed_errors = array(); public function suppress_inline_notices() { if (!function_exists('is_checkout') || !is_checkout()) { return; } add_action('woocommerce_before_checkout_form', array($this, 'stash_error_notices'), 9); } public function stash_error_notices() { if (!function_exists('wc_get_notices') || !function_exists('wc_set_notices')) { return; } $errors = wc_get_notices('error'); if (empty($errors) || !is_array($errors)) { return; } $this->stashed_errors = array_merge($this->stashed_errors, array_values($errors)); $all = wc_get_notices(); if (is_array($all)) { unset($all['error']); wc_set_notices($all); } } public function enqueue_assets() { if (!function_exists('is_checkout') || !is_checkout()) { return; } $version = defined('AIEO_VERSION') ? AIEO_VERSION : '1.0'; $dir = self::asset_dir(); $uri = self::asset_uri(); $css_mt = @filemtime($dir . 'assets/css/aieo-checkout-modal.css'); $js_mt = @filemtime($dir . 'assets/js/aieo-checkout-modal.js'); wp_enqueue_style( 'aieo-checkout-modal', $uri . 'assets/css/aieo-checkout-modal.css', array(), $css_mt ? $version . '.' . $css_mt : $version ); $deps = array('jquery'); if (wp_script_is('wc-checkout', 'registered')) { $deps[] = 'wc-checkout'; } wp_enqueue_script( 'aieo-checkout-modal', $uri . 'assets/js/aieo-checkout-modal.js', $deps, $js_mt ? $version . '.' . $js_mt : $version, true ); $s = $this->get_settings(); wp_add_inline_script( 'aieo-checkout-modal', 'window.omWcModalTitle = ' . wp_json_encode((string) $s['modal_title']) . ';', 'before' ); } public function output_modal_shell_and_bootstrap() { if (!function_exists('is_checkout') || !is_checkout()) { return; } $title = (string) $this->get_settings()['modal_title']; echo ''; echo ''; $errors = $this->stashed_errors; if (function_exists('wc_get_notices')) { $queued = wc_get_notices('error'); if (is_array($queued) && $queued) { $errors = array_merge($errors, array_values($queued)); if (function_exists('wc_set_notices')) { $all = wc_get_notices(); if (is_array($all)) { unset($all['error']); wc_set_notices($all); } } } } if ($errors && function_exists('wc_print_notice')) { ob_start(); foreach ($errors as $notice) { $message = is_array($notice) ? (string) ($notice['notice'] ?? '') : (string) $notice; if ('' === $message) { continue; } wc_print_notice($message, 'error', (is_array($notice) && isset($notice['data']) && is_array($notice['data'])) ? $notice['data'] : array()); } $html = trim((string) ob_get_clean()); if ($html !== '') { printf( '', wp_json_encode($html) ); } } } } }