$settings The settings. * @return bool */ public static function note_on( array $settings ) { return ! empty( $settings['checkout'] ) && ! empty( $settings['product_note'] ); } /** * Hook the product page's line — only while it is switched on. * * @return bool Whether anything was hooked. */ public static function init() { if ( ! self::note_on( Settings::get() ) ) { return false; } add_action( 'woocommerce_single_product_summary', array( self::class, 'print_note' ), 25 ); return true; } /** * The checkout question's stylesheet, on the checkout page only (hooked by * Checkout_Classic while the question is on). * * @return void */ public static function hook_checkout_style() { add_action( 'wp_enqueue_scripts', array( self::class, 'checkout_style' ), 20 ); } /** * Enqueue it — the date's fields side by side, 16 px text — on a checkout page (the * classic one, the block one, the pay-for-order page) whose cart or order asks the * question, and nowhere else. * * @return void */ public static function checkout_style() { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || Checkout_Classic::page_age() <= 0 ) { return; } wp_enqueue_style( 'agevow-checkout', AGEVOW_URL . 'public/css/checkout.css', array(), AGEVOW_VERSION ); } /** * The minimum age a product asks, never below the owner's; 0 when the product is * not age-restricted. * * @param int $product_id Product (or variation) id. * @param array $settings The settings. * @return int */ public static function product_age( $product_id, array $settings ) { $mark = Restrictions::effective( (int) $product_id ); if ( $mark <= 0 ) { return 0; } $base = Settings::is_age( $settings['min_age'] ) ? (int) $settings['min_age'] : Settings::AGE_MIN; return max( $base, $mark ); } /** * The minimum age a set of products asks at checkout: the highest of theirs when * any is restricted; the owner's when the checkout asks always; else 0. * * @param int[] $product_ids Products in the cart. * @param array $settings The settings. * @return int */ public static function cart_age( array $product_ids, array $settings ) { $age = 0; foreach ( $product_ids as $id ) { $age = max( $age, self::product_age( (int) $id, $settings ) ); } if ( 0 === $age && 'always' === $settings['checkout_when'] ) { $age = Settings::is_age( $settings['min_age'] ) ? (int) $settings['min_age'] : Settings::AGE_MIN; } return $age; } /** * The line on a restricted product's page. * * @return void */ public static function print_note() { global $product; $id = $product instanceof \WC_Product ? $product->get_id() : (int) get_the_ID(); if ( $id <= 0 ) { return; } $settings = Settings::get(); $age = self::product_age( $id, $settings ); if ( $age <= 0 ) { return; } echo '

' . esc_html( self::note_text( $age, $settings ) ) . '

'; } /** * The words of the line: what the checkout will ask. * * @param int $age The product's minimum age. * @param array $settings The settings. * @return string */ public static function note_text( $age, array $settings ) { if ( 'dob' === $settings['checkout_method'] ) { /* translators: %d: the minimum age, 18 to 25. */ return sprintf( __( 'For customers aged %d or older. You will be asked your date of birth at checkout.', 'agevow-age-verification-age-gate' ), (int) $age ); } /* translators: %d: the minimum age, 18 to 25. */ return sprintf( __( 'For customers aged %d or older. You will be asked to confirm your age at checkout.', 'agevow-age-verification-age-gate' ), (int) $age ); } }