checkout() ) { $single = array( 'text', 'textarea', 'email', 'tel', 'password', 'number', 'select', 'radio', 'country', 'state', 'hidden', 'date', 'url' ); $keys = array(); foreach ( (array) WC()->checkout()->get_checkout_fields() as $group ) { foreach ( (array) $group as $key => $field ) { if ( in_array( $field['type'] ?? 'text', $single, true ) ) { $keys[] = (string) $key; } } } $drop( $keys ); } if ( isset( $_POST['shipping_method'] ) ) { $methods = is_array( $_POST['shipping_method'] ) ? array_filter( (array) wc_clean( wp_unslash( $_POST['shipping_method'] ) ), 'is_scalar' ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- wc_clean() sanitizes every value. $_POST['shipping_method'] = wp_slash( $methods ); } // phpcs:enable } /** * Puts right a session that already holds a list where WooCommerce expects text. */ public static function repair_session(): void { $session = function_exists( 'WC' ) ? WC()->session : null; if ( ! $session || ! method_exists( $session, 'get' ) ) { return; } $payment = $session->get( 'chosen_payment_method' ); if ( null !== $payment && ! is_scalar( $payment ) ) { $session->set( 'chosen_payment_method', '' ); } $shipping = $session->get( 'chosen_shipping_methods' ); if ( null !== $shipping && ( ! is_array( $shipping ) || count( array_filter( $shipping, 'is_scalar' ) ) !== count( $shipping ) ) ) { $session->set( 'chosen_shipping_methods', is_array( $shipping ) ? array_filter( $shipping, 'is_scalar' ) : array() ); } } /** * The layout: steps or onepage. */ public static function layout(): string { return 'onepage' === Settings::get( 'checkout_layout' ) ? 'onepage' : 'steps'; } /** * Changes a product's quantity from the order summary (0 removes it). */ public static function ajax_item(): void { self::begin_ajax(); if ( ! check_ajax_referer( self::NONCE, 'security', false ) || ! Settings::enabled( 'checkout_edit_cart' ) ) { self::send_json( false, array( 'message' => __( 'Your session expired. Please refresh the page.', 'aiolos-checkout-for-woocommerce' ) ), 403 ); } $key = isset( $_POST['key'] ) && is_string( $_POST['key'] ) ? sanitize_key( wp_unslash( $_POST['key'] ) ) : ''; // Decimal quantities (sold by weight or length) stay as sent; CartItem snaps them to the item's rules. $qty = isset( $_POST['quantity'] ) && is_scalar( $_POST['quantity'] ) && is_numeric( $_POST['quantity'] ) ? max( 0.0, (float) $_POST['quantity'] ) : 0.0; $result = CartItem::set_quantity( $key, $qty ); WC()->cart->calculate_totals(); self::send_json( true, array( 'errors' => $result['errors'], 'missing' => $result['missing'], 'empty' => WC()->cart->is_empty(), ) ); } /** * Level of the output buffer opened for this AJAX request. * * @var int */ private static $ajax_level = 0; /** * Starts an AJAX answer: whatever other code prints meanwhile (notices, warnings, stray HTML of * hooks) is caught, so the JSON stays readable. */ private static function begin_ajax(): void { ob_start(); self::$ajax_level = ob_get_level(); } /** * Sends a JSON answer, dropping what was printed since begin_ajax(). * * @param bool $success Success or error. * @param array $data Data. * @param int $status HTTP status. */ private static function send_json( bool $success, array $data, int $status = 200 ): void { while ( self::$ajax_level > 0 && ob_get_level() >= self::$ajax_level ) { ob_end_clean(); } if ( $success ) { wp_send_json_success( $data, $status ); } wp_send_json_error( $data, $status ); } /** * Checks a postcode for a country with WooCommerce's rules (the checkout asks before leaving the * first step, for countries its own quick check does not know). */ public static function ajax_postcode(): void { self::begin_ajax(); $valid = true; $value = ''; if ( check_ajax_referer( self::NONCE, 'security', false ) ) { $country = isset( $_POST['country'] ) && is_string( $_POST['country'] ) ? strtoupper( substr( sanitize_key( wp_unslash( $_POST['country'] ) ), 0, 2 ) ) : ''; $postcode = isset( $_POST['postcode'] ) && is_string( $_POST['postcode'] ) ? substr( sanitize_text_field( wp_unslash( $_POST['postcode'] ) ), 0, 20 ) : ''; if ( '' !== $country && '' !== $postcode && class_exists( '\WC_Validation' ) ) { $value = wc_format_postcode( $postcode, $country ); $valid = (bool) \WC_Validation::is_postcode( $value, $country ); } } self::send_json( true, array( 'valid' => $valid, 'formatted' => $value, ) ); } /** * A new checkout nonce for this visitor: a page left open past its nonce's life (or the * session's) asks for one instead of failing. It is what reloading the checkout would give. */ public static function ajax_fresh_nonce(): void { self::begin_ajax(); self::send_json( true, array( 'nonce' => wp_create_nonce( self::NONCE ) ) ); } /** * Names are not email addresses: customers sometimes type the email into the name box, and some * payment gateways then refuse the order. * * @param array $data Posted data. * @param \WP_Error $errors Errors. */ public static function validate_names( $data, $errors ): void { if ( ! is_array( $data ) || ! $errors instanceof \WP_Error || ! self::from_our_form() ) { return; } foreach ( array( 'shipping_first_name', 'shipping_last_name', 'billing_first_name', 'billing_last_name' ) as $key ) { if ( isset( $data[ $key ] ) && self::looks_like_email( (string) $data[ $key ] ) ) { $errors->add( $key . '_email', self::name_not_email_message(), array( 'id' => $key ) ); } } } /** * Whether a text is an email address (or contains one). * * @param string $value Text. */ public static function looks_like_email( string $value ): bool { return 1 === preg_match( '/[^\s@]+@[^\s@]+\.[^\s@]{2,}/', $value ); } /** * The message for an email address in a name box. */ public static function name_not_email_message(): string { return __( 'Enter a name here, not an email address.', 'aiolos-checkout-for-woocommerce' ); } /** * Whether an email belongs to an account, for the "Log in" offer. Answers only while checkout * login is allowed, to guests, a limited number of times per visitor and hour. */ public static function ajax_email_check(): void { self::begin_ajax(); $exists = false; if ( check_ajax_referer( self::NONCE, 'security', false ) && self::login_offer_enabled() ) { $email = isset( $_POST['email'] ) && is_string( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; if ( is_email( $email ) && self::within_lookup_limit() ) { $exists = (bool) email_exists( $email ); } } self::send_json( true, array( 'exists' => $exists ) ); } /** * Whether returning customers are offered to log in. */ public static function login_offer_enabled(): bool { return ! is_user_logged_in() && Settings::enabled( 'returning_customer' ) && 'yes' === get_option( 'woocommerce_enable_checkout_login_reminder' ); } /** * Counts one account lookup for this visitor; false once the hour's limit is reached. */ private static function within_lookup_limit(): bool { /** * Filters how many emails a visitor can check for an account per hour. * * @since 1.1.0 * * @param int $limit Lookups per hour. */ $limit = (int) apply_filters( 'aiolos_email_check_limit', 10 ); $ip = class_exists( '\WC_Geolocation' ) ? (string) \WC_Geolocation::get_ip_address() : ''; $key = 'aiolos_ec_' . md5( $ip . '|' . wp_salt( 'nonce' ) ); $count = (int) get_transient( $key ); if ( $count >= $limit ) { return false; } set_transient( $key, $count + 1, HOUR_IN_SECONDS ); return true; } /** * Whether this request shows (or updates) this checkout. */ public static function active(): bool { return self::$active || self::from_our_form(); } /** * Whether an AJAX request or a checkout submission was sent by this checkout's form. */ public static function from_our_form(): bool { // phpcs:disable WordPress.Security.NonceVerification.Missing -- read-only flag; WooCommerce verifies its own nonce before acting. if ( isset( $_POST[ self::FLAG ] ) ) { return true; } if ( isset( $_POST['post_data'] ) && is_string( $_POST['post_data'] ) ) { parse_str( wp_unslash( $_POST['post_data'] ), $data ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only one key is compared. return isset( $data[ self::FLAG ] ) && is_string( $data[ self::FLAG ] ) && '1' === sanitize_key( $data[ self::FLAG ] ); } // phpcs:enable return false; } /** * Whether the checkout should replace this page. */ private static function should_load(): bool { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_wc_endpoint_url( 'order-pay' ) || is_wc_endpoint_url( 'order-received' ) ) { return false; } if ( is_customize_preview() || ! WC()->cart || WC()->cart->is_empty() || ! self::is_checkout_page() ) { return false; } if ( Settings::enabled( 'checkout_admins_only' ) && ! current_user_can( 'manage_woocommerce' ) ) { return false; } if ( self::embedded_checkout_gateway_chosen() || self::bypassed() ) { return false; } /** * Filters whether the Aiolos checkout replaces this checkout page. * * @since 1.0.0 * * @param bool $load Whether to load it. */ return (bool) apply_filters( 'aiolos_load_checkout', true ); } /** * Whether this is WooCommerce's own checkout page (is_checkout() is also true on pages that * plugins turn into checkouts: One Page Checkout products, CartFlows and FunnelKit steps; * those keep their own layout). */ public static function is_checkout_page(): bool { $page = (int) wc_get_page_id( 'checkout' ); $is = $page > 0 && ( (int) get_queried_object_id() === $page || is_page( $page ) ); /** * Filters whether this request is the shop's checkout page, for Aiolos Checkout. * * @since 1.6.0 * * @param bool $is Whether it is. */ return (bool) apply_filters( 'aiolos_is_checkout_page', $is ); } /** * Whether the chosen payment method replaces the whole checkout with its own (Klarna * Checkout, Nets Easy embedded, Walley, Qliro, Svea): then the page is left to it. */ private static function embedded_checkout_gateway_chosen(): bool { /** * Filters the payment methods that bring their own whole checkout. * * @since 1.0.0 * * @param string[] $ids Gateway ids. */ $ids = (array) apply_filters( 'aiolos_embedded_checkout_gateways', array( 'kco', 'dibs_easy', 'nets_easy', 'walley_checkout', 'collector_checkout', 'qliro_one', 'svea_checkout', 'payson_checkout', 'resurs_checkout' ) ); $chosen = WC()->session ? WC()->session->get( 'chosen_payment_method' ) : ''; $chosen = is_scalar( $chosen ) ? (string) $chosen : ''; if ( '' === $chosen ) { $available = WC()->payment_gateways() ? array_keys( WC()->payment_gateways()->get_available_payment_gateways() ) : array(); $chosen = (string) reset( $available ); } return '' !== $chosen && in_array( $chosen, $ids, true ); } /** * Decides whether this request shows the checkout and sets the page up for it. */ public static function prepare(): void { if ( ! self::should_load() ) { return; } self::$active = true; // Decided on `wp`, before the admin bar is set up (template_redirect, priority 0) and // before caches decide whether to store the page. Cache::checkout_page(); nocache_headers(); self::without_checkout_block(); // As on WooCommerce's own checkout: totals, fees and shipping plugins test this constant. wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); add_action( 'wp_head', array( self::class, 'ensure_title' ), 1 ); add_action( 'wp_head', static function () { self::print_custom_scripts( 'scripts_head' ); }, 99 ); add_action( 'wp_footer', static function () { self::print_custom_scripts( 'scripts_footer' ); }, 99 ); // Payment gateways enqueue their scripts when they are loaded: before wp_head, not during the payment section. WC()->payment_gateways()->get_available_payment_gateways(); WC()->shipping(); add_filter( 'show_admin_bar', '__return_false' ); add_action( 'wp_enqueue_scripts', array( self::class, 'enqueue' ), 20 ); add_action( 'wp_enqueue_scripts', array( self::class, 'dedupe_select2' ), PHP_INT_MAX ); add_action( 'wp_print_footer_scripts', array( self::class, 'dedupe_select2' ), 1 ); if ( Settings::enabled( 'remove_theme_assets' ) ) { add_action( 'wp_enqueue_scripts', array( self::class, 'dequeue_theme_assets' ), PHP_INT_MAX ); add_action( 'wp_head', array( self::class, 'dequeue_theme_assets' ), 1 ); add_action( 'wp_print_styles', array( self::class, 'dequeue_theme_assets' ), PHP_INT_MAX ); add_action( 'wp_print_footer_scripts', array( self::class, 'dequeue_theme_assets' ), 1 ); // The theme's own output in the page's head and footer (inline styles, menus, popups), taken // off once more right before each runs, for callbacks added late. add_action( 'wp_head', array( self::class, 'isolate_page_hooks' ), -10000 ); add_action( 'wp_footer', array( self::class, 'isolate_page_hooks' ), -10000 ); } } /** * Whether the shop manager chose to see WooCommerce's own checkout (the admin bar's "View * without it"), when the Tools part that offers it is present. */ public static function bypassed(): bool { $bypass = array( '\AiolosCheckout\Tools\Bypass', 'active' ); return is_callable( $bypass ) && (bool) call_user_func( $bypass ); } /** * A checkout page built with WooCommerce's Checkout block: for the plugins that look at the page * (has_block() decides which scripts gateways load), it is the classic [woocommerce_checkout] * page this checkout replaces. */ private static function without_checkout_block(): void { global $wp_query; $posts = array( $GLOBALS['post'] ?? null, get_queried_object() ); if ( $wp_query instanceof \WP_Query ) { $posts[] = $wp_query->post; $posts = array_merge( $posts, (array) $wp_query->posts ); } foreach ( $posts as $post ) { if ( $post instanceof \WP_Post && false !== strpos( (string) $post->post_content, '