'; echo esc_html__( '1account Age Verification requires WooCommerce. Install and activate WooCommerce, then try again.', '1account-age-verification' ); echo '

'; } add_action( 'plugins_loaded', 'oneacc_boot' ); function oneacc_push_api_script_url() { $host = oneacc_api_host(); // Production is served at www.1account.net. The QA certificate is only // for 1accountqa.net, so www.1accountqa.net fails the TLS handshake. $web_host = ( '1accountqa.net' === $host ) ? $host : 'www.' . $host; return 'https://' . $web_host . '/pushApi/index.js'; } /** * Order-status rows used when a merchant has not saved their own mapping. * Left value is the WooCommerce status name. Right value is the age-check result. */ function oneacc_default_order_status_settings() { return \Oneacc\AgeCheck::default_status_settings(); } function oneacc_get_order_status_settings() { return \Oneacc\AgeCheck::status_settings( get_option( 'wc_oneacc_order_status_settings' ) ); } function oneacc_order_is_completed( $order ) { return is_object( $order ) && is_callable( array( $order, 'get_status' ) ) && \Oneacc\AgeCheck::is_completed_status( $order->get_status() ); } /** * Store URL sent on a new incomplete age-check record. No trailing slash and no /shop/ suffix. */ function oneacc_av_store_url() { return untrailingslashit( site_url() ); } /** * Store URL for an update. Uses the value saved at create time when the order has one. * * @param WC_Order|object|null $order Order the incomplete record belongs to. */ function oneacc_av_store_url_for_order( $order ) { $saved = ''; if ( is_object( $order ) && is_callable( array( $order, 'get_meta' ) ) ) { $saved = $order->get_meta( '_oneaccount_av_store_url' ); } if ( is_string( $saved ) && '' !== $saved ) { return $saved; } return oneacc_av_store_url(); } /** * POST or PUT JSON through the WordPress HTTP API. * * @param string $url Request URL. * @param string $method POST or PUT. * @param string $body JSON request body. * @return array{body: string, http_code: int, error: string} */ function oneacc_remote_json( $url, $method, $body ) { $response = wp_remote_request( $url, array( 'method' => $method, 'timeout' => oneacc_incomplete_av_timeout(), 'redirection' => 10, 'httpversion' => '1.1', 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => $body, ) ); if ( is_wp_error( $response ) ) { return array( 'body' => '', 'http_code' => 0, 'error' => $response->get_error_message(), ); } return array( 'body' => (string) wp_remote_retrieve_body( $response ), 'http_code' => (int) wp_remote_retrieve_response_code( $response ), 'error' => '', ); } function oneacc_render_order_av_status($order) { $value = strtoupper( str_replace( ' ', '_', (string) $order->get_meta( 'av_order_tag_key' ) ) ); $legacy = array( 'AV_PASS' => 'AV_SUCCESS', 'AV_FAIL' => 'AV_FAILED', 'AV_SUCCESS' => 'AV_SUCCESS', 'AV_FAILED' => 'AV_FAILED', 'NO_UK_USER' => 'NON_UK_USER', ); if ( isset( $legacy[ $value ] ) ) { $value = $legacy[ $value ]; } // Shown only. The 1account backend writes av_order_tag_key; the order screen must not. echo '

Order AV status:
'; echo esc_html( $value !== '' ? $value : '—' ); echo '

'; } function oneacc_thankyou_maybe_create_record($order_id){ $order = wc_get_order($order_id); if ( ! $order || oneacc_order_is_completed( $order ) ) { return; } if ( \Oneacc\AgeCheck::thankyou_should_create_record( $order->get_payment_method(), $order->is_paid() ) ) { oneacc_create_incomplete_av_record( $order_id, null, true ); } } function oneacc_register_custom_order_statuses() { $serialized_option = oneacc_get_order_status_settings(); if ( ! is_array( $serialized_option ) ) { return; } foreach ( \Oneacc\AgeCheck::statuses_to_register( $serialized_option ) as $wc_slug => $status_name ) { // WooCommerce, or another plugin, already defined this slug. if ( function_exists( 'get_post_status_object' ) && get_post_status_object( 'wc-' . $wc_slug ) ) { continue; } // Merchant-defined names are not catalog strings. register_post_status() // only needs the array shape _n_noop() would return. $count_label = str_replace( '%', '%%', $status_name ) . ' (%s)'; register_post_status( 'wc-' . $wc_slug, array( 'label' => $status_name, 'public' => true, 'show_in_admin_status_list' => true, 'show_in_admin_all_list' => true, 'exclude_from_search' => false, 'label_count' => array( 'singular' => $count_label, 'plural' => $count_label, 'context' => null, 'domain' => null, ), )); } } function oneacc_add_custom_order_statuses($order_statuses) { $new_order_statuses = array(); $serialized_option = oneacc_get_order_status_settings(); $custom_statuses = is_array( $serialized_option ) ? \Oneacc\AgeCheck::statuses_to_register( $serialized_option ) : array(); foreach ($order_statuses as $key => $status) { $new_order_statuses[$key] = $status; if ( 'wc-on-hold' !== $key ) { continue; } $new_order_statuses[ 'wc-' . \Oneacc\AgeCheck::AV_PENDING_STATUS ] = __( 'AV Pending', '1account-age-verification' ); foreach ( $custom_statuses as $wc_slug => $status_name ) { $status_key = 'wc-' . $wc_slug; if ( isset( $order_statuses[ $status_key ] ) ) { continue; } $new_order_statuses[ $status_key ] = $status_name; } } return $new_order_statuses; } function oneacc_register_av_pending_order_status() { register_post_status( 'wc-' . \Oneacc\AgeCheck::AV_PENDING_STATUS, array( 'label' => __( 'AV Pending', '1account-age-verification' ), 'public' => true, 'show_in_admin_status_list' => true, 'show_in_admin_all_list' => true, 'exclude_from_search' => false, /* translators: %s: number of orders in the AV Pending status. */ 'label_count' => _n_noop( 'AV Pending (%s)', 'AV Pending (%s)', '1account-age-verification' ), ) ); } function oneacc_add_push_api_script_attributes($tag, $handle) { // Check if the script handle matches 'one-account-push-api' if ($handle === 'one-account-push-api') { // Add custom attributes to the script tag $tag = str_replace('info( $line, array( 'source' => 'oneacc-order-update' ) ); } function oneacc_update_order_from_admin($order_id, $order_or_post = null, $update = null) { $nonce = isset( $_POST['woocommerce_meta_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['woocommerce_meta_nonce'] ) ) : ''; $nonce_ok = wp_verify_nonce( $nonce, 'woocommerce_save_data' ); $can_edit = current_user_can( 'edit_shop_order', $order_id ); if ( ! $nonce_ok || ! $can_edit ) { return; } $posted_status = isset( $_POST['order_status'] ) ? sanitize_text_field( wp_unslash( $_POST['order_status'] ) ) : ''; oneacc_order_update_log( 'save hook fired', array( 'order_id' => $order_id, 'posted_status' => $posted_status, 'hook' => current_filter(), ) ); if ( '' === $posted_status ) { oneacc_order_update_log( 'stopped before update', array( 'reason' => 'order status missing' ) ); return; } $order = $order_or_post instanceof WC_Order ? $order_or_post : wc_get_order($order_id); if (!$order) { oneacc_order_update_log( 'stopped before update', array( 'reason' => 'order not found', 'order_id' => $order_id ) ); return; } static $handled = array(); if (isset($handled[$order->get_id()])) { oneacc_order_update_log( 'stopped before update', array( 'reason' => 'already handled this save', 'order_id' => $order->get_id() ) ); return; } $handled[$order->get_id()] = true; $plugin_status = (string) $order->get_meta( \Oneacc\AgeCheck::PLUGIN_STATUS_META ); if ( '' !== $plugin_status && ! \Oneacc\AgeCheck::same_status( $posted_status, $plugin_status ) ) { $order->delete_meta_data( \Oneacc\AgeCheck::PLUGIN_STATUS_META ); $order->save(); } $original_status = isset( $_POST['original_post_status'] ) ? sanitize_text_field( wp_unslash( $_POST['original_post_status'] ) ) : ''; if ( oneacc_order_is_completed( $order ) || \Oneacc\AgeCheck::is_completed_status( $original_status ) ) { oneacc_order_update_log( 'stopped before update', array( 'reason' => 'completed orders are left unchanged', 'order_id' => $order->get_id(), 'current_status' => $order->get_status(), 'posted_status' => $posted_status, 'original_status' => $original_status, ) ); return; } if ( \Oneacc\AgeCheck::is_completed_status( $posted_status ) ) { oneacc_order_update_log( 'stopped before update', array( 'reason' => 'completing an order stops reminders when the status changes', 'order_id' => $order->get_id(), 'posted_status' => $posted_status, ) ); return; } $order_status = $posted_status; $serialized_option = oneacc_get_order_status_settings(); $change_order_status = array(); if (isset($serialized_option['oneacc_status']) && isset($serialized_option['one_acc_user_status']) && is_array($serialized_option)) { $order_statuses = $serialized_option['oneacc_status']; $user_statuses = $serialized_option['one_acc_user_status']; $status_part = strtolower( $order_status ); if ( 0 === strpos( $status_part, 'wc-' ) ) { $status_part = substr( $status_part, 3 ); } $change_order_status = \Oneacc\AgeCheck::matched_results( $order_status, $serialized_option ); oneacc_order_update_log( 'compared posted status to mapping', array( 'order_id' => $order->get_id(), 'posted_status' => $order_status, 'status_part' => $status_part, 'woo_statuses' => $order_statuses, 'result_statuses'=> $user_statuses, 'matched' => $change_order_status, ) ); } else { oneacc_order_update_log( 'mapping missing', array( 'order_id' => $order->get_id() ) ); } if (!empty($change_order_status)) { oneacc_update_incomplete_av( $order->get_id(), $change_order_status[0] ); $order->save(); } else { oneacc_order_update_log( 'update not sent', array( 'order_id' => $order->get_id(), 'reason' => 'posted status did not match a mapping row', ) ); } } /** * Fires only when the status changes to Completed, including the orders list and bulk actions. * Saving an order that is already Completed does not fire it. */ function oneacc_stop_reminders_for_completed_order( $order_id, $order = null ) { oneacc_update_incomplete_av( $order_id, \Oneacc\AgeCheck::COMPLETED_REMINDER_STATUS ); } /** * POST the incomplete age-check record once the order is paid. * payment_complete and the paid-status hooks share one request, so only the first of those posts. * A failed post does not save the flag. The thank-you page passes $retry so it can try once more. * * @param int $order_id * @param WC_Order|null $order Unused. Status hooks pass the order as the second argument. * @param bool $retry True when the thank-you page is repeating a failed attempt. */ function oneacc_create_incomplete_av_record( $order_id, $order = null, $retry = false ) { unset( $order ); $order_id = absint( $order_id ); if ( ! $order_id ) { return; } static $succeeded = array(); if ( isset( $succeeded[ $order_id ] ) ) { return; } static $attempted = array(); if ( isset( $attempted[ $order_id ] ) && ! $retry ) { return; } $attempted[ $order_id ] = true; $order = wc_get_order( $order_id ); if ( ! $order || oneacc_store_skipped_av_tag( $order, true ) || oneacc_order_is_completed( $order ) ) { return; } if ( 'yes' === (string) $order->get_meta( '_oneaccount_incomplete_av_created' ) ) { return; } $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; if ( ! \Oneacc\AgeCheck::should_start_age_check( $order->get_billing_country(), $skip_av, oneacc_order_is_completed( $order ), $order->get_meta( 'av_order_tag_key' ) ) ) { return; } $items = array(); foreach ( $order->get_items() as $item ) { $product = $item->get_product(); if ( ! $product ) { continue; } $items[] = \Oneacc\AgeCheck::incomplete_item( $product->get_name(), $product->get_short_description() ); } $store_url = oneacc_av_store_url(); $order_array = \Oneacc\AgeCheck::incomplete_create_payload( $order->get_billing_email(), $order->get_billing_phone(), $order->get_id(), $items, $order->get_checkout_order_received_url(), $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), $store_url ); $order_json = json_encode( $order_array ); $url = oneacc_api_url( 'platforms/woomagento/incompleteAV/create' ); oneacc_order_update_log( 'incomplete AV create started', array( 'order_id' => $order->get_id(), 'url' => $url, ) ); $result = oneacc_remote_json( $url, 'POST', $order_json ); $http_code = $result['http_code']; $body = $result['body']; $created = ( $http_code >= 200 && $http_code < 300 ) || false !== strpos( $body, 'ALREADY_PROCEED' ); oneacc_order_update_log( $created ? 'sent incomplete AV create' : 'incomplete AV create failed', array( 'order_id' => $order->get_id(), 'url' => $url, 'http_code' => $http_code, 'error' => $result['error'], 'response' => $body, ) ); if ( ! $created ) { return; } $succeeded[ $order_id ] = true; $order->update_meta_data( '_oneaccount_incomplete_av_created', 'yes' ); $order->update_meta_data( '_oneaccount_av_store_url', $order_array['storeUrl'] ); $order->save(); } function oneacc_update_incomplete_av( $order_id, $result_status ) { $arr = \Oneacc\AgeCheck::incomplete_update_payload( $order_id, $result_status, oneacc_av_store_url_for_order( wc_get_order( $order_id ) ) ); $order_json = json_encode( $arr ); $url = oneacc_api_url( 'platforms/woomagento/incompleteAV/update' ); $result = oneacc_remote_json( $url, 'PUT', $order_json ); oneacc_order_update_log( 'sent incomplete AV update', array( 'url' => $url, 'body' => $arr, 'http_code' => $result['http_code'], 'error' => $result['error'], 'response' => $result['body'], ) ); } /** * Card and wallet payments. A UK order that still needs a check waits in AV Pending * instead of Processing or Completed. A non-UK order uses the NON_UK_USER row. * * @param string $status * @param int $order_id * @param WC_Order|null $order */ function oneacc_payment_complete_order_status( $status, $order_id, $order ) { if ( ! $order instanceof WC_Order ) { $order = wc_get_order( $order_id ); } if ( ! $order ) { return $status; } $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; $next = \Oneacc\AgeCheck::status_for_paid_order( $status, $order->get_billing_country(), $skip_av, oneacc_order_is_completed( $order ), $order->get_meta( 'av_order_tag_key' ), oneacc_get_order_status_settings(), true ); if ( ! \Oneacc\AgeCheck::is_av_pending_status( $next ) && ! \Oneacc\AgeCheck::same_status( $next, $status ) ) { $order->update_meta_data( 'av_order_tag_key', 'NON_UK_USER' ); $order->update_meta_data( \Oneacc\AgeCheck::PLUGIN_STATUS_META, $next ); } oneacc_store_skipped_av_tag( $order, false ); return $next; } /** * Store AV_SKIPPED when every product is tagged SKIP_AV and none is TOBACCO. * Payment keeps the WooCommerce status it already chose. This only labels the order * so the merchant can see the check was not missed. * * @param WC_Order $order * @param bool $save True when the caller is not about to save the order. * @return bool True when this order skips the check. */ function oneacc_store_skipped_av_tag( $order, $save ) { if ( ! $order instanceof WC_Order ) { return false; } $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; $tag = \Oneacc\AgeCheck::skipped_av_tag( $skip_av, $order->get_meta( 'av_order_tag_key' ) ); if ( '' !== $tag ) { $order->update_meta_data( 'av_order_tag_key', $tag ); if ( $save ) { $order->save(); } } return $skip_av; } /** * A gateway that sets Processing itself, rather than payment_complete, still waits for the check. * The meta stops a pass, or a merchant move back to Processing, from being sent to AV Pending again. * * @param int $order_id */ function oneacc_move_processing_order_to_av_pending( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || 'yes' === (string) $order->get_meta( \Oneacc\AgeCheck::AV_PENDING_SET_META ) ) { return; } if ( oneacc_store_skipped_av_tag( $order, true ) ) { return; } $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; if ( ! \Oneacc\AgeCheck::should_start_age_check( $order->get_billing_country(), $skip_av, oneacc_order_is_completed( $order ), $order->get_meta( 'av_order_tag_key' ) ) ) { return; } $order->update_status( \Oneacc\AgeCheck::AV_PENDING_STATUS ); } /** * First entry to AV Pending records the waiting tag and posts the verification record. * * @param int $order_id * @param WC_Order|null $order */ function oneacc_on_av_pending( $order_id, $order = null ) { if ( ! $order instanceof WC_Order ) { $order = wc_get_order( $order_id ); } if ( ! $order ) { return; } if ( 'yes' !== (string) $order->get_meta( \Oneacc\AgeCheck::AV_PENDING_SET_META ) ) { $order->update_meta_data( 'av_order_tag_key', 'AV_PENDING' ); $order->update_meta_data( \Oneacc\AgeCheck::AV_PENDING_SET_META, 'yes' ); $order->update_meta_data( \Oneacc\AgeCheck::PLUGIN_STATUS_META, \Oneacc\AgeCheck::AV_PENDING_STATUS ); $order->save(); } oneacc_create_incomplete_av_record( $order_id ); } /** * Cash on delivery does not call payment_complete. Leave WooCommerce's COD status in place * long enough for it to reduce stock, then apply the NON_UK_USER row. * * @param int $order_id */ function oneacc_apply_cod_non_uk_status( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || 'cod' !== $order->get_payment_method() ) { return; } if ( 'yes' === (string) $order->get_meta( '_oneaccount_non_uk_applied' ) ) { return; } if ( \Oneacc\AgeCheck::is_status_locked( $order->get_status() ) || \Oneacc\AgeCheck::is_av_pending_status( $order->get_status() ) ) { return; } $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; if ( $skip_av || 'GB' === strtoupper( (string) $order->get_billing_country() ) ) { return; } $mapped = \Oneacc\AgeCheck::mapped_status_for_result( 'non_uk_user', oneacc_get_order_status_settings() ); if ( '' === $mapped ) { return; } $order->update_meta_data( 'av_order_tag_key', 'NON_UK_USER' ); $order->update_meta_data( \Oneacc\AgeCheck::PLUGIN_STATUS_META, $mapped ); $order->update_meta_data( '_oneaccount_non_uk_applied', 'yes' ); $order->save(); if ( ! \Oneacc\AgeCheck::same_status( $order->get_status(), $mapped ) ) { $order->update_status( $mapped ); } } /** * WooCommerce sends the new-order email for Pending, Failed, or Cancelled moving to Processing. * AV Pending is none of those, so send that same email when payment lands here. * * @param int $order_id * @param string $from * @param string $to * @param WC_Order|null $order */ function oneacc_send_new_order_email_for_av_pending( $order_id, $from, $to, $order ) { if ( \Oneacc\AgeCheck::AV_PENDING_STATUS !== $to ) { return; } if ( ! in_array( $from, array( 'pending', 'failed', 'cancelled', 'checkout-draft' ), true ) ) { return; } oneacc_trigger_order_email( 'WC_Email_New_Order', $order_id, $order ); } /** * A pass moves AV Pending to Processing. WooCommerce does not email that transition. * * @param int $order_id * @param WC_Order|null $order */ function oneacc_send_customer_processing_email( $order_id, $order = null ) { oneacc_trigger_order_email( 'WC_Email_Customer_Processing_Order', $order_id, $order ); } /** * @param string $class_name Email class WooCommerce registers. * @param int $order_id * @param WC_Order|null $order */ function oneacc_trigger_order_email( $class_name, $order_id, $order ) { if ( ! function_exists( 'WC' ) ) { return; } $mailer = WC()->mailer(); if ( ! is_object( $mailer ) || ! is_callable( array( $mailer, 'get_emails' ) ) ) { return; } foreach ( $mailer->get_emails() as $email ) { if ( $email instanceof $class_name && is_callable( array( $email, 'trigger' ) ) ) { $email->trigger( $order_id, $order instanceof WC_Order ? $order : false ); return; } } } function oneacc_cod_payment_order_status($order_status, $order) { $billing_country = $order->get_billing_country(); $product_tags = \Oneacc\Product\OrderProductTags::from_order( $order ); $skip_av = $product_tags['all_no_av'] && ! $product_tags['has_tobacco']; $start_check = \Oneacc\AgeCheck::should_start_age_check( $billing_country, $skip_av, oneacc_order_is_completed( $order ), $order->get_meta( 'av_order_tag_key' ) ); if ( $start_check ) { oneacc_create_incomplete_av_record( $order->get_id() ); } oneacc_store_skipped_av_tag( $order, false ); return \Oneacc\AgeCheck::status_for_paid_order( $order_status, $billing_country, $skip_av, oneacc_order_is_completed( $order ), $order->get_meta( 'av_order_tag_key' ), oneacc_get_order_status_settings(), false ); } /** * WooCommerce hooks. Registered only after WooCommerce itself has loaded. */ function oneacc_register_hooks() { add_action( 'woocommerce_admin_order_data_after_order_details', 'oneacc_render_order_av_status', 10, 1 ); add_action( 'woocommerce_thankyou', 'oneacc_thankyou_maybe_create_record' ); add_action( 'init', 'oneacc_register_custom_order_statuses' ); add_action( 'init', 'oneacc_register_av_pending_order_status' ); add_filter( 'wc_order_statuses', 'oneacc_add_custom_order_statuses' ); add_filter( 'script_loader_tag', 'oneacc_add_push_api_script_attributes', 10, 2 ); add_action( 'save_post_shop_order', 'oneacc_update_order_from_admin', 10, 3 ); add_action( 'woocommerce_process_shop_order_meta', 'oneacc_update_order_from_admin', 10, 2 ); add_action( 'woocommerce_order_status_completed', 'oneacc_stop_reminders_for_completed_order', 10, 2 ); add_action( 'woocommerce_payment_complete', 'oneacc_create_incomplete_av_record' ); add_action( 'woocommerce_order_status_processing', 'oneacc_move_processing_order_to_av_pending', 5, 1 ); add_action( 'woocommerce_order_status_processing', 'oneacc_create_incomplete_av_record' ); add_action( 'woocommerce_order_status_completed', 'oneacc_create_incomplete_av_record' ); add_action( 'woocommerce_order_status_av-pending', 'oneacc_on_av_pending', 10, 2 ); add_action( 'woocommerce_order_status_processing', 'oneacc_apply_cod_non_uk_status', 20, 1 ); add_action( 'woocommerce_order_status_on-hold', 'oneacc_apply_cod_non_uk_status', 20, 1 ); add_filter( 'woocommerce_payment_complete_order_status', 'oneacc_payment_complete_order_status', 10, 3 ); add_action( 'woocommerce_order_status_changed', 'oneacc_send_new_order_email_for_av_pending', 10, 4 ); add_action( 'woocommerce_order_status_av-pending_to_processing', 'oneacc_send_customer_processing_email', 10, 2 ); add_action( 'woocommerce_order_status_av-pending', 'wc_maybe_reduce_stock_levels' ); add_filter( 'woocommerce_cod_process_payment_order_status', 'oneacc_cod_payment_order_status', 10, 2 ); } // Activation includes this file after plugins_loaded has already run. if ( did_action( 'plugins_loaded' ) ) { oneacc_boot(); }