'agevow_day', 'month' => 'agevow_month', 'year' => 'agevow_year', ); /** * The hidden field that says which minimum age the page asked (0: none). The cart * can change in another tab after the page was made: a tick is an answer only for * the age written beside it, and a page that asked less than the cart asks now is * drawn again. */ const ASKED = 'agevow_asked'; /** * Where the question goes when nothing moved it. */ const DEFAULT_HOOK = 'woocommerce_after_checkout_billing_form'; /** * Whether the question was printed in this request. * * @var bool */ private static $printed = false; /** * This request's passed check, until the order it was made for exists. * * @var array|null */ private static $passed = null; /** * Hook the question — only while the checkout question is on. * * @return bool Whether anything was hooked. */ public static function init() { if ( empty( Settings::get()['checkout'] ) ) { return false; } /** * Filters the action the checkout question is printed on (a theme whose * checkout leaves out the billing form's hooks can move it). Wherever it goes, * an order is refused until it is answered. * * @param string $hook An action of the classic checkout's form. */ $hook = apply_filters( 'agevow_checkout_field_hook', self::DEFAULT_HOOK ); add_action( is_string( $hook ) && '' !== $hook ? $hook : self::DEFAULT_HOOK, array( self::class, 'print_question' ) ); // If the theme never printed the billing form's hook, the question still shows. add_action( 'woocommerce_checkout_after_customer_details', array( self::class, 'print_question' ) ); add_action( 'woocommerce_after_checkout_validation', array( self::class, 'validate' ), 20, 2 ); add_action( 'woocommerce_checkout_order_created', array( self::class, 'record' ) ); // The pay-for-order page (P8): an order holding a restricted product and no // passed check — one a shop manager created, or one left unpaid — asks there. add_action( 'woocommerce_pay_order_before_submit', array( self::class, 'print_pay_question' ) ); add_action( 'woocommerce_before_pay_action', array( self::class, 'validate_pay' ) ); Woo::hook_checkout_style(); return true; } /** * Forget this request's state (the tests run several checkouts in one process). * * @return void */ public static function reset() { self::$printed = false; self::$passed = null; } /** * The minimum age the current cart asks at checkout (0 = it asks nothing). * * @return int */ public static function cart_age() { if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return 0; } $ids = array(); foreach ( WC()->cart->get_cart() as $item ) { if ( isset( $item['product_id'] ) ) { $ids[] = (int) $item['product_id']; } } return Woo::cart_age( $ids, Settings::get() ); } /** * The minimum age an existing order still asks: the highest of its products' * (the owner's for every order with "always"), or 0 when it holds nothing * restricted or already carries a passed check at that age or higher. * * @param \WC_Order $order The order. * @return int */ public static function order_age( $order ) { if ( ! $order instanceof \WC_Order ) { return 0; } $ids = array(); foreach ( $order->get_items() as $item ) { if ( $item instanceof \WC_Order_Item_Product ) { $ids[] = (int) $item->get_product_id(); } } $age = Woo::cart_age( $ids, Settings::get() ); return $age > 0 && ! Checks::passed( $order->get_id(), $age ) ? $age : 0; } /** * The order on the pay-for-order page being shown, or null. * * @return \WC_Order|null */ public static function pay_order() { global $wp; $id = isset( $wp->query_vars['order-pay'] ) ? absint( $wp->query_vars['order-pay'] ) : 0; $order = $id > 0 && function_exists( 'wc_get_order' ) ? wc_get_order( $id ) : false; return $order instanceof \WC_Order ? $order : null; } /** * The minimum age the page being shown asks: the pay-for-order page's order, or * the cart. * * @return int */ public static function page_age() { if ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'order-pay' ) ) { return self::order_age( self::pay_order() ); } return self::cart_age(); } // ── the question ───────────────────────────────────────────────────────── /** * Print the question once, when the cart asks for it. * * @return void */ public static function print_question() { if ( self::$printed ) { return; } $age = self::cart_age(); if ( $age <= 0 ) { // Nothing to ask: the form still says so, for a product added in another tab. self::$printed = true; self::asked_field( 0 ); return; } self::$printed = true; $mark = Remember::holds( get_current_user_id(), $age ); if ( null !== $mark ) { self::remembered( $mark ); return; } self::question( $age, Settings::get()['checkout_method'], get_locale() ); } /** * Print the question on the pay-for-order page, once, when its order asks. * * @return void */ public static function print_pay_question() { if ( self::$printed ) { return; } $order = self::pay_order(); $age = self::order_age( $order ); if ( $age <= 0 ) { return; } self::$printed = true; $mark = Remember::for_order( $order, $age ); if ( null !== $mark ) { self::remembered( $mark ); return; } self::question( $age, Settings::get()['checkout_method'], get_locale() ); } /** * What a remembered customer sees where the question would be: why they are not * asked (B1). * * @param array $mark Remember::holds(). * @return void */ public static function remembered( array $mark ) { ?>

 

$data The checkout's posted data (WooCommerce's fields). * @param \WP_Error $errors The checkout's errors. * @return void */ public static function validate( $data, $errors ) { self::$passed = null; if ( ! $errors instanceof \WP_Error ) { return; } $age = self::cart_age(); if ( $age <= 0 ) { return; } // A logged-in customer remembered from an earlier check at this age or higher. $mark = Remember::holds( get_current_user_id(), $age ); if ( null !== $mark ) { self::$passed = array( 'source' => 'remembered', 'method' => $mark['method'], 'min_age' => $age, 'ref' => $mark['time'], 'user' => get_current_user_id(), ); return; } $method = Settings::get()['checkout_method']; $asked = null; $stale = false; $answer = self::answer( $method, $age, 'checkout', $asked, $stale ); if ( 'pass' === $answer['result'] ) { self::$passed = array( 'source' => 'classic', 'method' => $method, 'min_age' => $age, 'ref' => null, 'user' => get_current_user_id(), ); return; } if ( 'under' === $answer['result'] ) { self::refused( 'classic', $method, $age ); } $errors->add( 'agevow_age', self::message( $answer, $age ), array( 'id' => $answer['field'] ) ); // The page asked less than the cart asks now (a product added in another tab), or // its box was ticked without saying for which age (a page drawn before 1.1.1): // WooCommerce reloads its checkout, the notice kept, and the question is drawn // for the cart as it is. A form sent without JavaScript gets the new page anyway. if ( $stale && wp_doing_ajax() && WC()->session ) { WC()->session->set( 'reload_checkout', true ); } } /** * The pay-for-order form: refuse the payment unless the question was answered, * and answered old enough — WooCommerce takes no payment while an error notice * stands. A pass gives the order its record at once (the order exists already). * * @param \WC_Order $order The order being paid. * @return string pass · refused · none (nothing to ask) */ public static function validate_pay( $order ) { $age = self::order_age( $order ); if ( $age <= 0 ) { return 'none'; } $mark = Remember::for_order( $order, $age ); if ( null !== $mark ) { self::write( $order, 'remembered', $mark['method'], $age, $mark['time'] ); return 'pass'; } $method = Settings::get()['checkout_method']; $answer = self::answer( $method, $age, 'pay' ); if ( 'pass' === $answer['result'] ) { self::write( $order, 'pay', $method, $age ); return 'pass'; } if ( 'under' === $answer['result'] ) { self::refused( 'pay', $method, $age ); } wc_add_notice( self::message( $answer, $age ), 'error', array( 'id' => $answer['field'] ) ); return 'refused'; } /** * Read the customer's answer from a classic form's request — which WooCommerce has * just accepted by its own check, made again here — into pass, under, missing or * a mistake. A date is turned into whole years and dropped. * * @param string $method confirm · dob. * @param int $age The minimum age. * @param string $form checkout (the checkout's form) · pay (the pay-for-order form). * @param int|null $asked Set to the minimum age the page says it asked (null: it does not say). * @param bool $stale Set to whether the page that sent the answer is out of date (stale()). * @return array{result: string, field: string, error?: string} */ public static function answer( $method, $age, $form = 'checkout', &$asked = null, &$stale = false ) { $asked = null; $stale = false; $first = 'dob' === $method ? self::PARTS[ Gate::field_order( get_locale() )[0] ] : self::CONFIRM; // Each form sends WooCommerce's token under one of two names. $name = 'pay' === $form ? 'woocommerce-pay-nonce' : 'woocommerce-process-checkout-nonce'; $action = 'pay' === $form ? 'woocommerce-pay' : 'woocommerce-process_checkout'; $key = isset( $_POST[ $name ] ) ? $name : '_wpnonce'; if ( ! isset( $_POST[ $key ] ) || false === wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ $key ] ) ), $action ) ) { return array( 'result' => 'missing', 'field' => $first, ); } $given = array( 'confirm' => isset( $_POST[ self::CONFIRM ] ) && is_string( $_POST[ self::CONFIRM ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::CONFIRM ] ) ) : '', ); foreach ( self::PARTS as $part => $field ) { $given[ $part ] = isset( $_POST[ $field ] ) && is_string( $_POST[ $field ] ) ? sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) : ''; } if ( isset( $_POST[ self::ASKED ] ) && is_string( $_POST[ self::ASKED ] ) ) { $given['asked'] = sanitize_text_field( wp_unslash( $_POST[ self::ASKED ] ) ); } $asked = self::asked( $given ); $stale = self::stale( $given, $age ); return self::judge( $method, $age, $given ); } /** * Judge an answer, wherever it came from — the classic form or the block * checkout's request: the tick box is the string "1" (a form) or true (JSON), a * date is three strings of digits. Anything else is nothing typed. A tick answers * only the age written beside it: it passes when the page says it asked this age * or higher. A date is judged against the age asked now, turned into whole years * and dropped here: only "old enough" or "not" goes on. * * @param string $method confirm · dob. * @param int $age The minimum age. * @param array $given confirm; day, month, year; asked (the age the page asked). * @return array{result: string, field: string, error?: string} */ public static function judge( $method, $age, array $given ) { if ( 'dob' !== $method ) { $ticked = isset( $given['confirm'] ) && ( true === $given['confirm'] || '1' === $given['confirm'] ); $asked = self::asked( $given ); return array( 'result' => $ticked && null !== $asked && $asked >= (int) $age ? 'pass' : 'missing', 'field' => self::CONFIRM, ); } $first = self::PARTS[ Gate::field_order( get_locale() )[0] ]; $typed = array(); foreach ( array_keys( self::PARTS ) as $part ) { $typed[ $part ] = isset( $given[ $part ] ) && is_string( $given[ $part ] ) ? $given[ $part ] : ''; } $read = Age::read( $typed['day'], $typed['month'], $typed['year'], Age::site_today() ); if ( isset( $read['error'] ) ) { // Nothing typed: the first field as the page shows it; a mistake: its field. return array( 'result' => 'empty' === $read['error'] ? 'missing' : 'mistake', 'error' => $read['error'], 'field' => 'empty' === $read['error'] && '' === implode( '', $typed ) ? $first : self::PARTS[ $read['part'] ], ); } $years = Age::years( $read['y'], $read['m'], $read['d'], Age::site_today() ); // The date is dropped here: only "old enough" or "not" goes on. $read = null; $typed = null; $given = null; return array( 'result' => $years >= (int) $age ? 'pass' : 'under', 'field' => self::PARTS['year'], ); } /** * The minimum age the page says it asked: an answer's `asked` — a form's digits or * a request's whole number — or null when the answer does not say. * * @param array $given An answer (judge()). * @return int|null */ public static function asked( array $given ) { if ( ! isset( $given['asked'] ) ) { return null; } $v = $given['asked']; if ( is_int( $v ) ) { return $v >= 0 ? $v : null; } return is_string( $v ) && 1 === preg_match( '/^[0-9]{1,3}$/', $v ) ? (int) $v : null; } /** * Whether the page that sent an answer is out of date, so it is drawn again for the * cart (or order) as it is: it asked a lower age than is asked now (a product added in * another tab), or its box was ticked without saying for which age, as a page drawn * before 1.1.1 sends it. A date, an unticked box and a request with nothing of ours * are refused as they are. * * @param array $given An answer (judge()). * @param int $age The minimum age asked now. * @return bool */ public static function stale( array $given, $age ) { $asked = self::asked( $given ); if ( null !== $asked ) { return $asked < (int) $age; } return isset( $given['confirm'] ) && ( true === $given['confirm'] || '1' === $given['confirm'] ); } /** * The plain sentence for an answer that does not place the order. * * @param array $answer answer(). * @param int $age The minimum age. * @return string */ public static function message( array $answer, $age ) { if ( 'under' === $answer['result'] ) { /* translators: %d: the minimum age, 18 to 25. */ return sprintf( __( 'Sorry, you must be %d or older to order what is in your cart.', 'agevow-age-verification-age-gate' ), (int) $age ); } if ( 'mistake' === $answer['result'] ) { $words = Gate::mistakes(); $error = isset( $answer['error'], $words[ $answer['error'] ] ) ? $words[ $answer['error'] ] : $words['empty']; /* translators: %s: what to fix in the date of birth, e.g. "Enter the year with four digits." */ return sprintf( __( 'Date of birth: %s', 'agevow-age-verification-age-gate' ), $error ); } if ( self::CONFIRM === $answer['field'] ) { /* translators: %d: the minimum age, 18 to 25. */ return sprintf( __( 'Please confirm that you are %d or older to place this order.', 'agevow-age-verification-age-gate' ), (int) $age ); } return __( 'Please enter your date of birth to place this order.', 'agevow-age-verification-age-gate' ); } // ── the record ─────────────────────────────────────────────────────────── /** * The order exists: give it its record — a row, the order's own `_agevow_check`, * a note — and count the pass. * * @param \WC_Order $order The order the classic checkout just created. * @return int The check's row id, 0 when there was nothing to record. */ public static function record( $order ) { if ( null === self::$passed || ! $order instanceof \WC_Order ) { return 0; } $check = self::$passed; self::$passed = null; // A remembered pass holds only for the customer it was read for. if ( 'remembered' === $check['source'] && ( (int) $order->get_customer_id() !== (int) $check['user'] || ! Remember::customer_of( $order ) ) ) { return 0; } return self::write( $order, $check['source'], $check['method'], (int) $check['min_age'], $check['ref'] ); } /** * Give an order its passed check: a row, the order's own `_agevow_check`, a note * — and count the pass. Shared by every path (classic, block, pay-for-order). A * question answered by the order's own logged-in customer is remembered on their * account when the owner chose so (B1); a remembered pass is not counted as an * answer (nothing was asked) and keeps the mark's own date. * * @param \WC_Order $order The order. * @param string $source classic · block · pay · remembered. * @param string $method confirm · dob. * @param int $age The minimum age. * @param string|null $ref For a remembered pass: the UTC time of the check it rests on. * @return int The check's row id, 0 when nothing was written. */ public static function write( $order, $source, $method, $age, $ref = null ) { if ( ! $order instanceof \WC_Order ) { return 0; } $id = Checks::record( array( 'order_id' => $order->get_id(), 'source' => $source, 'method' => $method, 'min_age' => (int) $age, 'result' => 'pass', 'ref' => $ref, ) ); if ( $id <= 0 ) { return 0; } if ( 'remembered' === $source ) { $note = self::remembered_note( $method, (int) $age, (string) $ref ); } else { Tally::add( 'checkout', 'pass' ); Remember::keep( $order, $method, (int) $age ); $note = self::note( $method, (int) $age, gmdate( 'Y-m-d H:i:s' ) ); } $fresh = wc_get_order( $order->get_id() ); if ( $fresh ) { $fresh->add_order_note( $note ); } return $id; } /** * An answer under the minimum age: no order is placed, and the refusal is kept — a * row with no order (where, how, at what age; never who or the date typed) and the * day's count. A missing answer or a mistake is not kept: it is not an answer yet. * * @param string $source classic · block · pay. * @param string $method confirm · dob. * @param int $age The minimum age. * @return int The row's id. */ public static function refused( $source, $method, $age ) { Tally::add( 'checkout', 'under' ); return Checks::record( array( 'source' => $source, 'method' => $method, 'min_age' => (int) $age, 'result' => 'under', ) ); } /** * The order note: "Age checked at checkout: 21+ by date of birth, 14 Oct 2026 10:32 UTC." * * @param string $method confirm · dob. * @param int $age The minimum age. * @param string $utc When, in UTC (Y-m-d H:i:s). * @return string */ public static function note( $method, $age, $utc ) { $when = gmdate( 'j M Y H:i', (int) strtotime( $utc . ' UTC' ) ) . ' UTC'; if ( 'dob' === $method ) { /* translators: 1: the minimum age, 18 to 25; 2: the date and time in UTC. */ return sprintf( __( 'Age checked at checkout: %1$d+ by date of birth, %2$s. The date of birth was not saved.', 'agevow-age-verification-age-gate' ), (int) $age, $when ); } /* translators: 1: the minimum age, 18 to 25; 2: the date and time in UTC. */ return sprintf( __( 'Age checked at checkout: %1$d+ confirmed by the customer, %2$s.', 'agevow-age-verification-age-gate' ), (int) $age, $when ); } /** * The order note of a remembered customer: "Age check: 21+, remembered from the * customer's check of 14 Oct 2026 10:32 UTC (by date of birth); not asked again." * * @param string $method confirm · dob (how the remembered check was answered). * @param int $age The minimum age of this order. * @param string $utc When the remembered check was made, in UTC (Y-m-d H:i:s). * @return string */ public static function remembered_note( $method, $age, $utc ) { $when = gmdate( 'j M Y H:i', (int) strtotime( $utc . ' UTC' ) ) . ' UTC'; $how = 'dob' === $method ? __( 'by date of birth', 'agevow-age-verification-age-gate' ) : __( 'confirmed by the customer', 'agevow-age-verification-age-gate' ); /* translators: 1: the minimum age, 18 to 25; 2: the date and time in UTC of the earlier check; 3: how it was answered ("by date of birth"). */ return sprintf( __( 'Age check: %1$d+, remembered from the customer\'s check of %2$s (%3$s); not asked again.', 'agevow-age-verification-age-gate' ), (int) $age, $when, $how ); } }