`) runs through the same hook. * * A date of birth is read from the request, turned into whole years and dropped (it * is never stored, logged or sent anywhere). A passed order gets the same record as * the classic checkout's — a row, `_agevow_check`, a note, the day's count — once * WooCommerce says the order is complete and ready for payment * (`woocommerce_store_api_checkout_order_processed`). * * A logged-in customer remembered from an earlier check (Remember, B1) gets no * question: the cart's answer says age 0 and carries the sentence that tells them * why, and the server lets their order through on the same mark, recorded as * remembered. Guests are always asked. */ final class Checkout_Block { /** * The namespace of our data in the Store API, and the integration's name. */ const NS = 'agevow'; /** * The question's script handle. */ const SCRIPT = 'agevow-checkout-block'; /** * This request's passed check, until the order it was made for is processed. * * @var array|null */ private static $passed = null; /** * Whether the Store API was extended in this request. * * @var bool */ private static $extended = false; /** * Hook everything — only while the checkout question is on. * * @return bool Whether anything was hooked. */ public static function init() { if ( empty( Settings::get()['checkout'] ) ) { return false; } add_action( 'woocommerce_blocks_checkout_block_registration', array( self::class, 'integrate' ) ); add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( self::class, 'validate' ), 10, 2 ); add_action( 'woocommerce_store_api_checkout_order_processed', array( self::class, 'record' ) ); // The question's stylesheet, on a checkout page whose cart asks (shared). Woo::hook_checkout_style(); self::extend(); return true; } /** * Forget this request's state (the tests run several checkouts in one process). * * @return void */ public static function reset() { self::$passed = null; } /** * Add our data to the Store API's cart answer and our fields to its checkout * request — once, and only when the Store API is there. * * @return bool Whether the Store API is extended. */ public static function extend() { if ( self::$extended || ! function_exists( 'woocommerce_store_api_register_endpoint_data' ) ) { return self::$extended; } $cart = woocommerce_store_api_register_endpoint_data( array( 'endpoint' => 'cart', 'namespace' => self::NS, 'data_callback' => array( self::class, 'cart_data' ), 'schema_callback' => array( self::class, 'cart_schema' ), 'schema_type' => ARRAY_A, ) ); $checkout = woocommerce_store_api_register_endpoint_data( array( 'endpoint' => 'checkout', 'namespace' => self::NS, 'data_callback' => array( self::class, 'checkout_data' ), 'schema_callback' => array( self::class, 'checkout_schema' ), 'schema_type' => ARRAY_A, ) ); self::$extended = true === $cart && true === $checkout; return self::$extended; } /** * Register the integration that loads the question's script with the block * checkout (WooCommerce's `woocommerce_blocks_checkout_block_registration`). * * @param object $registry WooCommerce's integration registry. * @return bool */ public static function integrate( $registry ) { if ( ! is_object( $registry ) || ! method_exists( $registry, 'register' ) ) { return false; } if ( method_exists( $registry, 'is_registered' ) && $registry->is_registered( self::NS ) ) { return true; } return (bool) $registry->register( new Checkout_Block_Integration() ); } // ── what the Store API carries ─────────────────────────────────────────── /** * The cart's part: the minimum age it asks at checkout (0 = it asks nothing), and * for a remembered customer, 0 and the sentence that says why (B1). * * @return array */ public static function cart_data() { $age = Checkout_Classic::cart_age(); $mark = $age > 0 ? Remember::holds( get_current_user_id(), $age ) : null; return array( 'age' => null === $mark ? $age : 0, 'remembered' => null === $mark ? '' : Remember::sentence( $mark ), ); } /** * The cart part's shape. * * @return array> */ public static function cart_schema() { return array( 'age' => array( 'description' => __( 'The minimum age the checkout asks for this cart, 0 when it asks nothing.', 'agevow-age-verification-age-gate' ), 'type' => 'integer', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'remembered' => array( 'description' => __( 'For a customer remembered from an earlier age check: why the checkout does not ask; otherwise empty.', 'agevow-age-verification-age-gate' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ); } /** * The checkout answer's part: nothing — the answer is never sent back. * * @return array */ public static function checkout_data() { return array(); } /** * The answer's shape in an order request. Loose on purpose: the words for a * mistake are ours (Checkout_Classic::judge()), never a generic "invalid". Each * part may be left out — the Store API reads a part that was not sent as null * (measured on WooCommerce 11.2: without "null" a date alone was refused for its * missing tick). * * @return array> */ public static function checkout_schema() { $part = array( 'type' => array( 'string', 'null' ), 'maxLength' => 10, 'context' => array( 'view', 'edit' ), ); return array( 'confirm' => array( 'description' => __( 'The customer confirmed the minimum age (when the checkout asks for a tick).', 'agevow-age-verification-age-gate' ), 'type' => array( 'boolean', 'null' ), 'context' => array( 'view', 'edit' ), ), 'day' => array_merge( $part, array( 'description' => __( 'Day of birth, read and dropped (when the checkout asks for the date of birth).', 'agevow-age-verification-age-gate' ) ) ), 'month' => array_merge( $part, array( 'description' => __( 'Month of birth, read and dropped.', 'agevow-age-verification-age-gate' ) ) ), 'year' => array_merge( $part, array( 'description' => __( 'Year of birth, read and dropped.', 'agevow-age-verification-age-gate' ) ) ), 'asked' => array( 'description' => __( 'The minimum age the question on the page asked (0: none).', 'agevow-age-verification-age-gate' ), 'type' => array( 'integer', 'null' ), 'context' => array( 'view', 'edit' ), ), ); } /** * What the question's script needs: the method, the date's fields in the site's * order, the words — and the cart's minimum age when the page was made. The age * normally comes with the cart's own data (and follows every change of the cart); * this one is the question's safety net for a page whose cart data does not carry * ours (a copy the browser kept from before the question was switched on): a * shopper is never refused for a question the page did not show. The checkout page * is the shopper's own, never one cached page for all. * * @param array $settings The settings. * @param string $locale The site's language. * @param int|null $age The cart's minimum age (default: read now). * @return array */ public static function script_data( array $settings, $locale, $age = null ) { $fields = array(); foreach ( Gate::field_order( $locale ) as $part ) { $fields[] = array_merge( array( 'part' => $part ), Gate::field( $part ) ); } $cart = null === $age ? self::cart_data() : array( 'age' => (int) $age, 'remembered' => '', ); return array( 'method' => 'dob' === $settings['checkout_method'] ? 'dob' : 'confirm', 'age' => (int) $cart['age'], 'remembered' => (string) $cart['remembered'], 'fields' => $fields, 'words' => array( 'title' => __( 'Age check', 'agevow-age-verification-age-gate' ), 'legend' => __( 'Date of birth', 'agevow-age-verification-age-gate' ), /* translators: %d: the minimum age, 18 to 25. */ 'hint' => __( 'Your order holds products for customers aged %d or older. Your date of birth is used only to check your age: it is not saved.', 'agevow-age-verification-age-gate' ), /* translators: %d: the minimum age, 18 to 25. */ 'confirm' => __( 'I confirm I am %d or older', 'agevow-age-verification-age-gate' ), 'need_dob' => __( 'Please enter your date of birth to place this order.', 'agevow-age-verification-age-gate' ), /* translators: %d: the minimum age, 18 to 25. */ 'need_confirm' => __( 'Please confirm that you are %d or older to place this order.', 'agevow-age-verification-age-gate' ), ), ); } // ── the check, on the server ───────────────────────────────────────────── /** * While the Store API fills the order in from an order request (POST — never the * draft updates the form sends as the customer types): refuse it unless the * question was answered, and answered old enough. * * @param \WC_Order $order The order being placed or paid. * @param \WP_REST_Request $request The order request. * @return string pass · none (nothing to ask) * @throws \Automattic\WooCommerce\StoreApi\Exceptions\RouteException No answer, a mistake, under the age (400); * the same from a page that asked less than the order asks now (409, with the cart). */ public static function validate( $order, $request ) { self::$passed = null; if ( ! $order instanceof \WC_Order || ! $request instanceof \WP_REST_Request || 'POST' !== $request->get_method() ) { return 'none'; } $age = Checkout_Classic::order_age( $order ); if ( $age <= 0 ) { return 'none'; } // The order's own logged-in customer, remembered from an earlier check (B1). $mark = Remember::for_order( $order, $age ); if ( null !== $mark ) { self::$passed = array( 'order_id' => $order->get_id(), 'source' => 'remembered', 'method' => $mark['method'], 'min_age' => $age, 'ref' => $mark['time'], ); return 'pass'; } $method = Settings::get()['checkout_method']; $given = self::given( $request ); $answer = Checkout_Classic::judge( $method, $age, $given ); if ( 'pass' === $answer['result'] ) { self::$passed = array( 'order_id' => $order->get_id(), 'source' => self::source( $request ), 'method' => $method, 'min_age' => $age, 'ref' => null, ); return 'pass'; } if ( 'under' === $answer['result'] ) { Checkout_Classic::refused( self::source( $request ), $method, $age ); } // The page asked less than the order asks now (a product added in another tab), or // its box was ticked without saying for which age (a script from before 1.1.1): // a 409, the Store API's answer for a cart that changed under the page, sends the // cart back with the refusal, and the page draws the question for it. A script // from before 1.1.1 never says the age, so its customer is asked to reload the page. $stale = Checkout_Classic::stale( $given, $age ); $said = Checkout_Classic::message( $answer, $age ); if ( $stale && null === Checkout_Classic::asked( $given ) ) { /* translators: %d: the minimum age, 18 to 25. */ $said = sprintf( __( 'This page is out of date. Please reload it, then confirm that you are %d or older to place this order.', 'agevow-age-verification-age-gate' ), (int) $age ); } throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'agevow_age', esc_html( $said ), $stale ? 409 : 400 ); } /** * Our part of an order request: `extensions.agevow`, or nothing. * * @param \WP_REST_Request $request The order request. * @return array */ public static function given( \WP_REST_Request $request ) { $ext = $request->get_param( 'extensions' ); return is_array( $ext ) && isset( $ext[ self::NS ] ) && is_array( $ext[ self::NS ] ) ? $ext[ self::NS ] : array(); } /** * Where the order request came from: the checkout (`/checkout`), or the Store * API's pay-for-order route (`/checkout/`). * * @param \WP_REST_Request $request The order request. * @return string block · pay */ public static function source( \WP_REST_Request $request ) { return 1 === preg_match( '#/checkout/[0-9]+/?$#', (string) $request->get_route() ) ? 'pay' : 'block'; } /** * The order is complete and on its way to payment: give it its record. * * @param \WC_Order $order The order. * @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 || $order->get_id() !== self::$passed['order_id'] ) { return 0; } $check = self::$passed; self::$passed = null; return Checkout_Classic::write( $order, $check['source'], $check['method'], (int) $check['min_age'], $check['ref'] ); } }