'text', 'label' => __( 'Date of birth', '1account-age-verification' ), 'placeholder' => 'DD/MM/YYYY', 'required' => true, 'class' => array( 'form-row-wide' ), 'priority' => 120, 'clear' => true, 'custom_attributes' => array( 'inputmode' => 'numeric', 'maxlength' => '10', 'autocomplete' => 'bday', 'pattern' => '(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[0-9]{4}', 'title' => __( 'Enter the date as DD/MM/YYYY.', '1account-age-verification' ), ), ); } public function classic_validate() { if ( ! self::requested( OrderProductTags::cart_has_tobacco() ) ) { return; } $raw = self::posted_dob(); if ( '' === $raw ) { return; } if ( '' === AgeCheck::valid_checkout_dob( $raw, self::today() ) ) { wc_add_notice( __( 'Enter a valid date of birth as DD/MM/YYYY.', '1account-age-verification' ), 'error' ); } } /** * @param \WC_Order $order Order being created. */ public function classic_save( $order ) { if ( ! self::requested( OrderProductTags::cart_has_tobacco() ) || ! is_object( $order ) || ! method_exists( $order, 'update_meta_data' ) ) { return; } $dob = AgeCheck::valid_checkout_dob( self::posted_dob(), self::today() ); if ( '' === $dob ) { return; } $order->update_meta_data( self::ORDER_META, $dob ); } public function register_block_field() { if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { return; } $always = self::option_enabled( self::ALWAYS_OPTION ); $tobacco = self::option_enabled( self::TOBACCO_OPTION ); $project = trim( (string) get_option( self::TOBACCO_PROJECT_OPTION, '' ) ); if ( ! $always && ( ! $tobacco || '' === $project ) ) { return; } $field = array( 'id' => self::BLOCK_FIELD_ID, 'label' => __( 'Date of birth', '1account-age-verification' ), 'location' => 'contact', 'type' => 'text', 'attributes' => array( 'pattern' => '(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/[0-9]{4}', 'title' => __( 'Enter the date as DD/MM/YYYY.', '1account-age-verification' ), 'autocomplete' => 'bday', 'maxLength' => '10', ), 'validate_callback' => array( $this, 'validate_block_field' ), ); if ( $always ) { $field['required'] = true; $field['hidden'] = false; } else { $schema = AgeCheck::tobacco_checkout_field_schema( OrderProductTags::tobacco_catalog_ids() ); if ( null === $schema ) { return; } $field['required'] = $schema['required']; $field['hidden'] = $schema['hidden']; } woocommerce_register_additional_checkout_field( $field ); } /** * @param string $field_value Posted date. * @return \WP_Error|void */ public function validate_block_field( $field_value ) { $required = self::requested( OrderProductTags::cart_has_tobacco() ); $dob = AgeCheck::valid_checkout_dob( $field_value, self::today() ); if ( '' !== $dob || ( ! $required && '' === trim( (string) $field_value ) ) ) { return; } return new \WP_Error( 'oneacc_invalid_dob', __( 'Enter a valid date of birth as DD/MM/YYYY.', '1account-age-verification' ) ); } /** * Copy the Checkout block value onto billing_dob, which the thank-you payload reads. * * @param string $key Additional field id. * @param mixed $value Posted value. * @param string $group billing, shipping, or other. * @param mixed $wc_object Customer or order receiving the value. */ public function copy_block_value_to_order( $key, $value, $group, $wc_object ) { if ( self::BLOCK_FIELD_ID !== $key || ! $wc_object instanceof \WC_Order || ! method_exists( $wc_object, 'update_meta_data' ) ) { return; } $dob = AgeCheck::valid_checkout_dob( $value, self::today() ); if ( '' === $dob ) { return; } $wc_object->update_meta_data( self::ORDER_META, $dob ); } public function enqueue_mask() { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_order_received_page() ) { return; } $plugin_file = dirname( __DIR__, 2 ) . '/one-account-age-verification.php'; $script = dirname( __DIR__, 2 ) . '/assets/checkout-dob.js'; $style = dirname( __DIR__, 2 ) . '/assets/checkout-dob.css'; if ( ! is_readable( $script ) ) { return; } wp_enqueue_style( 'oneacc-checkout-dob', plugins_url( 'assets/checkout-dob.css', $plugin_file ), array(), is_readable( $style ) ? (string) filemtime( $style ) : false ); wp_enqueue_script( 'oneacc-checkout-dob', plugins_url( 'assets/checkout-dob.js', $plugin_file ), array(), (string) filemtime( $script ), true ); } private static function posted_dob() { // WooCommerce verifies this before classic checkout creates the order. $nonce = isset( $_POST['woocommerce-process-checkout-nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce-process-checkout-nonce'] ) ) : ''; if ( '' === $nonce && isset( $_POST['_wpnonce'] ) ) { $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ); } if ( ! wp_verify_nonce( $nonce, 'woocommerce-process_checkout' ) ) { return ''; } if ( ! isset( $_POST['billing_dob'] ) ) { return ''; } return sanitize_text_field( wp_unslash( $_POST['billing_dob'] ) ); } private static function option_enabled( $option ) { return 'yes' === get_option( $option, 'no' ); } public static function today() { if ( function_exists( 'wp_date' ) ) { return wp_date( 'Y-m-d' ); } return gmdate( 'Y-m-d' ); } }