, and HTML does not allow a form inside a form. * So the panel shows ', esc_attr( $form_id ), $primary ? ' button-primary' : '', esc_html( $label ) ); } /** Prints the POST forms collected by post_button(). Nothing if none. */ public static function print_post_forms() { foreach ( self::$post_forms as $form_id => $f ) { echo ''; } } /* ---------- order panel ---------- */ public static function add_box() { foreach ( array( 'shop_order', 'woocommerce_page_wc-orders' ) as $screen ) { add_meta_box( 'ahcouriers-delivery', 'AfterHoursCouriers', array( __CLASS__, 'render_box' ), $screen, 'side', 'high' ); } } public static function render_box( $post_or_order ) { $order = $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order ) { return; } $item = self::ahcouriers_item( $order ); if ( ! $item ) { echo '

This order does not use AfterHoursCouriers delivery.

'; return; } $job = $order->get_meta( '_ahcouriers_job_reference' ); $status = $order->get_meta( '_ahcouriers_status' ); $track = $order->get_meta( '_ahcouriers_tracking_link' ); $link = function ( $action, $extra = array() ) use ( $order ) { return wp_nonce_url( add_query_arg( array_merge( array( 'action' => $action, 'order_id' => $order->get_id() ), $extra ), admin_url( 'admin-post.php' ) ), $action . '_' . $order->get_id() ); }; echo '

' . esc_html( AHCouriers_Logic::option_label( $item->get_meta( '_ahcouriers_option' ) ) ) . ', '; echo esc_html( AHCouriers_Logic::short_date( $item->get_meta( '_ahcouriers_date' ) ) ); // [CHANGE-2026-11-13-4] Shows the shopper's "Deliver from" time. echo esc_html( ', from ' . AHCouriers_Logic::time_label( $item->get_meta( '_ahcouriers_time' ) ) ); // [CHANGE-2026-11-14-10] Wrapped in esc_html() (Andrew 14 Nov 2026). WHY: WordPress.org review expects every // output escaped, even fixed text. WHAT BREAKS IF REMOVED: possible review rejection; display unchanged. echo esc_html( 'bh' === $item->get_meta( '_ahcouriers_mode' ) ? ' (business-hours collection)' : ' (after-hours collection)' ); echo '

'; if ( ! $job ) { // [CHANGE-2026-11-18-3] POST button (was a GET link). echo '

' . self::post_button( $order->get_id(), 'ahcouriers_send', 'Send to AfterHoursCouriers', true ) . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- post_button() escapes every part. $err = $order->get_meta( '_ahcouriers_last_error' ); if ( $err ) { // Only offered after a failed send, so the day is never changed by accident. echo '

' . esc_html( $err ) . '

'; // [CHANGE-2026-11-18-3] POST button (was a GET link). echo '

' . self::post_button( $order->get_id(), 'ahcouriers_send', 'Send for next available day', false, array( 'next' => 1 ) ) . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- post_button() escapes every part. } return; } echo '

Job: ' . esc_html( $job ) . '
Status: ' . esc_html( $status ? $status : 'booked' ) . '

'; if ( $track ) { echo '

Tracking link

'; } // [CHANGE-2026-11-18-3] POST button (was a GET link). echo '

' . self::post_button( $order->get_id(), 'ahcouriers_refresh', 'Refresh status' ) . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- post_button() escapes every part. if ( 'delivered' === $status && 'yes' === $order->get_meta( '_ahcouriers_pod' ) ) { echo '

Download proof of delivery

'; } } /* ---------- sending ---------- */ /** * Builds and sends the booking. $use_next = true books the next available * day instead of the checkout day (merchant's explicit choice). * @return array { ok:bool, msg:string } */ public static function send( $order, $use_next = false ) { $item = self::ahcouriers_item( $order ); if ( ! $item ) { return array( 'ok' => false, 'msg' => 'This order does not use AfterHoursCouriers delivery.' ); } if ( $order->get_meta( '_ahcouriers_job_reference' ) ) { return array( 'ok' => true, 'msg' => 'Already booked: ' . $order->get_meta( '_ahcouriers_job_reference' ) ); } $option = (string) $item->get_meta( '_ahcouriers_option' ); $date = (string) $item->get_meta( '_ahcouriers_date' ); $mode = (string) $item->get_meta( '_ahcouriers_mode' ); $time = (string) $item->get_meta( '_ahcouriers_time' ); $availability = AHCouriers_API::availability( true ); if ( ! $availability ) { return self::fail( $order, 'Could not check availability with AfterHoursCouriers. Try again shortly.' ); } if ( $use_next ) { $slot = AHCouriers_Logic::pick_slot( $availability, AHCouriers_Settings::yes( 'collect_bh' ), AHCouriers_Settings::yes( 'collect_ah' ), AHCouriers_Settings::yes( 'collect_weekends' ) ); if ( ! $slot ) { return self::fail( $order, 'No available day matches your collection settings.' ); } // [CHANGE-2026-11-13-4] Keep the shopper's "Deliver from" time on // the next day - never move it silently. If that time (with the // same option) isn't offered on the next day, nothing is sent. $next = AHCouriers_Logic::choose_time( $slot, $time ); if ( $next['time'] !== $time || ! in_array( $option, $next['options'], true ) ) { return self::fail( $order, sprintf( '%s from %s is not available on %s. Nothing was sent. Contact your customer and AfterHoursCouriers.', AHCouriers_Logic::option_label( $option ), AHCouriers_Logic::time_label( $time ), AHCouriers_Logic::short_date( $slot['date'] ) ) ); } $date = $next['date']; $mode = $next['mode']; $time = $next['time']; } elseif ( ! AHCouriers_Logic::slot_still_valid( $availability, $date, $mode, $time, $option ) ) { return self::fail( $order, sprintf( 'The delivery day chosen at checkout (%s) is no longer available. Nothing was sent. Use "Send for next available day", and let your customer know.', AHCouriers_Logic::short_date( $date ) ) ); } $ship = $order->get_address( 'shipping' ); $dest = AHCouriers_Logic::address_line( $ship ); $name = trim( $order->get_shipping_first_name() . ' ' . $order->get_shipping_last_name() ); $phone = method_exists( $order, 'get_shipping_phone' ) && $order->get_shipping_phone() ? $order->get_shipping_phone() : $order->get_billing_phone(); if ( '' === $dest ) { return self::fail( $order, 'The order has no complete Australian shipping address.' ); } if ( '' === trim( (string) $phone ) ) { return self::fail( $order, 'The order has no phone number for the recipient.' ); } $lines = array(); foreach ( $order->get_items() as $line ) { $p = $line->get_product(); if ( ! $p || ! $p->needs_shipping() ) { continue; } $lines[] = array( 'weight' => $p->get_weight(), 'length' => $p->get_length(), 'width' => $p->get_width(), 'height' => $p->get_height(), 'qty' => $line->get_quantity(), ); } $parcel = AHCouriers_Logic::parcel( $lines, get_option( 'woocommerce_weight_unit', 'kg' ), get_option( 'woocommerce_dimension_unit', 'cm' ), array( 'weightKg' => AHCouriers_Settings::get( 'default_weight_kg' ), 'lengthMm' => AHCouriers_Settings::get( 'default_length_mm' ), 'widthMm' => AHCouriers_Settings::get( 'default_width_mm' ), 'heightMm' => AHCouriers_Settings::get( 'default_height_mm' ), ) ); $collection = array( 'address' => AHCouriers_Settings::get( 'collection_address' ), 'contactName' => AHCouriers_Settings::get( 'collection_name' ), 'contactPhone' => AHCouriers_Settings::get( 'collection_phone' ), 'date' => $date, 'businessHours' => 'bh' === $mode, ); if ( 'bh' === $mode ) { $collection['preferredDeliveryTime'] = $time; } else { $collection['time'] = $time; } $request = array( 'orderReference' => (string) $order->get_order_number(), 'collection' => $collection, 'dropoffs' => array( array( 'address' => $dest, 'contactName' => '' !== $name ? $name : 'Customer', 'contactPhone' => (string) $phone, ), ), 'serviceTier' => AHCouriers_Settings::get( 'service_tier' ), 'deliveryOption' => $option, 'weightKg' => $parcel['weightKg'], 'lengthMm' => $parcel['lengthMm'], 'widthMm' => $parcel['widthMm'], 'heightMm' => $parcel['heightMm'], 'itemCount' => $parcel['itemCount'], 'podRequired' => AHCouriers_Settings::yes( 'pod_required' ), 'tollsRequired' => false, 'dangerousGoods' => false, 'notes' => trim( 'Order ' . $order->get_order_number() . '. ' . (string) $order->get_customer_note() ), ); $r = AHCouriers_API::book( $request ); if ( null !== $r['error'] || empty( $r['body']['jobReference'] ) ) { $msg = $r['error'] ? $r['error'] : 'No job reference returned.'; if ( 502 === $r['code'] && false !== stripos( $msg, 'outcome unknown' ) ) { $msg .= ' (Do not resend - contact AfterHoursCouriers.)'; } return self::fail( $order, 'Not booked: ' . $msg ); } // [CHANGE-2026-11-14-11] Job reference from the API sanitised before it is saved or shown (Andrew 14 Nov 2026). // WHY: WordPress.org rule "sanitise on input" includes data from external services. // WHAT BREAKS IF REMOVED: possible review rejection; stored value unchanged for normal references (AHC-STD-260036). $job_ref = sanitize_text_field( $r['body']['jobReference'] ); $order->update_meta_data( '_ahcouriers_job_reference', $job_ref ); $order->update_meta_data( '_ahcouriers_status', 'booked' ); $order->update_meta_data( '_ahcouriers_booked_date', $date ); $order->update_meta_data( '_ahcouriers_pod', AHCouriers_Settings::yes( 'pod_required' ) ? 'yes' : 'no' ); $order->delete_meta_data( '_ahcouriers_last_error' ); $order->add_order_note( sprintf( 'AfterHoursCouriers booked: %s - %s, %s. Invoiced to your account: $%s.', $job_ref, // [CHANGE-2026-11-14-11] was $r['body']['jobReference'] AHCouriers_Logic::option_label( $option ), AHCouriers_Logic::short_date( $date ), number_format( (float) ( $r['body']['total'] ?? 0 ), 2 ) ) ); $order->save(); return array( 'ok' => true, 'msg' => 'Booked ' . $job_ref /* [CHANGE-2026-11-14-11] */ . ' for ' . AHCouriers_Logic::short_date( $date ) . '.' ); } private static function fail( $order, $msg ) { // [CHANGE-2026-11-14-11] Error text can contain the API's own message - sanitised before it is saved // to the order and its notes. WHAT BREAKS IF REMOVED: possible review rejection; plain messages unchanged. $msg = sanitize_text_field( $msg ); $order->update_meta_data( '_ahcouriers_last_error', $msg ); $order->add_order_note( 'AfterHoursCouriers: ' . $msg ); $order->save(); AHCouriers_API::log( 'Order ' . $order->get_order_number() . ': ' . $msg ); return array( 'ok' => false, 'msg' => $msg ); } public static function handle_send() { // [CHANGE-2026-11-18-3] POST only (was $_GET). Nonce checked before use. $order_id = absint( wp_unslash( $_POST['order_id'] ?? 0 ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce checked on the next line; the id is part of the nonce action. check_admin_referer( 'ahcouriers_send_' . $order_id ); if ( ! self::can_edit() ) { wp_die( 'Not allowed.' ); } $order = wc_get_order( $order_id ); if ( ! $order ) { wp_die( 'Order not found.' ); } $res = self::send( $order, ! empty( $_POST['next'] ) ); // [CHANGE-2026-11-18-3] was $_GET self::back( $order_id, $res['msg'], $res['ok'] ? 'success' : 'error' ); } /* ---------- automatic sending ---------- */ public static function maybe_queue_auto( $order_id ) { if ( 'auto' !== AHCouriers_Settings::get( 'send_mode' ) ) { return; } $order = wc_get_order( $order_id ); if ( ! $order || ! self::ahcouriers_item( $order ) || $order->get_meta( '_ahcouriers_job_reference' ) ) { return; } if ( function_exists( 'as_enqueue_async_action' ) ) { as_enqueue_async_action( 'ahcouriers_auto_send', array( 'order_id' => $order_id ), 'afterhourscouriers' ); } else { self::auto_send( $order_id ); } } public static function auto_send( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $res = self::send( $order, false ); if ( ! $res['ok'] ) { wp_mail( get_option( 'admin_email' ), 'AfterHoursCouriers: order ' . $order->get_order_number() . ' was not sent', $res['msg'] . "\n\nOpen the order to send it: " . $order->get_edit_order_url() ); } } /* ---------- status ---------- */ public static function refresh( $order ) { $job = $order->get_meta( '_ahcouriers_job_reference' ); if ( ! $job ) { return array( 'ok' => false, 'msg' => 'Not booked yet.' ); } $r = AHCouriers_API::status( $job ); if ( null !== $r['error'] || empty( $r['body']['status'] ) ) { return array( 'ok' => false, 'msg' => 'Status check failed: ' . ( $r['error'] ? $r['error'] : 'no status' ) ); } $new = sanitize_key( $r['body']['status'] ); $old = $order->get_meta( '_ahcouriers_status' ); if ( ! empty( $r['body']['trackingLink'] ) ) { $order->update_meta_data( '_ahcouriers_tracking_link', esc_url_raw( $r['body']['trackingLink'] ) ); } if ( $new !== $old ) { $order->update_meta_data( '_ahcouriers_status', $new ); $note = 'AfterHoursCouriers status: ' . $new; if ( 'delivered' === $new && ! empty( $r['body']['receivedBy'] ) ) { $note .= ' (received by ' . sanitize_text_field( $r['body']['receivedBy'] ) . ')'; } $order->add_order_note( $note ); } $order->save(); return array( 'ok' => true, 'msg' => 'Status: ' . $new ); } public static function handle_refresh() { // [CHANGE-2026-11-18-3] POST only (was $_GET). Nonce checked before use. $order_id = absint( wp_unslash( $_POST['order_id'] ?? 0 ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce checked on the next line; the id is part of the nonce action. check_admin_referer( 'ahcouriers_refresh_' . $order_id ); if ( ! self::can_edit() ) { wp_die( 'Not allowed.' ); } $order = wc_get_order( $order_id ); if ( ! $order ) { wp_die( 'Order not found.' ); } $res = self::refresh( $order ); self::back( $order_id, $res['msg'], $res['ok'] ? 'success' : 'error' ); } /** Hourly: refresh up to 50 booked orders that aren't finished. */ public static function sync_statuses() { $orders = wc_get_orders( array( 'limit' => 50, // [CHANGE-2026-11-14-4] Marker comment only (Plugin Check warning, 14 Nov 2026). WHY: this lookup // is the hourly background refresh, limited to 50 orders that have an unfinished AHC job. // WHAT BREAKS IF REMOVED: Plugin Check / WordPress.org review shows a slow-query warning here. 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- hourly background refresh, max 50 orders, only orders with an unfinished AfterHoursCouriers job. array( 'key' => '_ahcouriers_job_reference', 'compare' => 'EXISTS', ), array( 'key' => '_ahcouriers_status', 'value' => self::FINAL_STATUSES, 'compare' => 'NOT IN', ), ), ) ); foreach ( $orders as $order ) { self::refresh( $order ); } } /* ---------- proof of delivery ---------- */ public static function handle_pod() { $order_id = absint( $_GET['order_id'] ?? 0 ); check_admin_referer( 'ahcouriers_pod_' . $order_id ); if ( ! self::can_edit() ) { wp_die( 'Not allowed.' ); } $order = wc_get_order( $order_id ); $job = $order ? $order->get_meta( '_ahcouriers_job_reference' ) : ''; if ( ! $job ) { wp_die( 'This order has not been booked with AfterHoursCouriers.' ); } $r = AHCouriers_API::pod( $job ); if ( ! $r['ok'] ) { self::back( $order_id, 'Proof of delivery: ' . $r['error'], 'error' ); } nocache_headers(); header( 'Content-Type: application/pdf' ); header( 'Content-Disposition: attachment; filename="POD-' . sanitize_file_name( $job ) . '.pdf"' ); echo $r['pdf']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- binary PDF. exit; } /* ---------- checkout: AHC line [CHANGE-2026-11-12-9] ---------- */ // [CHANGE-2026-11-14-6] Wording "Same Day After Hours Delivery" (Andrew 14 Nov 2026): "evening" was wrong for weekend daytime slots. const TAGLINE = 'Same Day After Hours Delivery by AfterHoursCouriers, Melbourne.'; /** * [CHANGE-2026-11-14-2] Block-checkout text only (approved by Andrew 14 Nov 2026). * WHY: on the block checkout a saved address collapses into a summary card * that hides "Deliver from" (WooCommerce's address card shows only name, * address and phone - checked in WC 11.3 source). This tells shoppers where * to change the time. Classic checkout keeps TAGLINE (no "Deliver from" there). * WHAT BREAKS IF REMOVED: returning shoppers never find "Deliver from". */ const TAGLINE_BLOCK = 'Same Day After Hours Delivery by AfterHoursCouriers, Melbourne. To choose a later delivery time, click Edit on your shipping address.'; /** * Classic cart/checkout: one row under the shipping options, shown only * when AHC rates are on offer. * What breaks if removed: shoppers don't see who delivers. */ public static function tagline_row() { if ( ! function_exists( 'WC' ) || ! WC()->shipping() ) { return; } foreach ( (array) WC()->shipping()->get_packages() as $pkg ) { foreach ( (array) ( $pkg['rates'] ?? array() ) as $rate ) { if ( 'ahcouriers_evening' === $rate->get_method_id() ) { echo '' . esc_html( self::TAGLINE ) . ''; return; } } } } /** * Block checkout: WooCommerce 9.2+ shows a rate's "delivery time" text * after its price ("$46.97 — ..."); plain text only. Set on AHC rates. * Older WooCommerce has no set_delivery_time() - then nothing is added. * FRAMEWORK NOTE (declared before use): checked against WooCommerce * source (WC_Shipping_Rate @since 9.2.0; block checkout * render-package-rate-option.tsx renders rate.delivery_time). * What breaks if removed: block-checkout shoppers don't see who delivers. */ public static function tag_rate( $rate, $args, $method ) { if ( is_object( $method ) && 'ahcouriers_evening' === $method->id && method_exists( $rate, 'set_delivery_time' ) ) { $rate->set_delivery_time( self::TAGLINE_BLOCK ); // [CHANGE-2026-11-14-2] was self::TAGLINE } return $rate; } /* ---------- order screen [CHANGE-2026-11-12-10] ---------- */ /** * The _ahcouriers_ keys are working data for the plugin, not for people. Hiding * only affects display; they stay on the order and are still read when * sending. "Delivery day" stays visible on purpose. * What breaks if removed: merchants see _ahcouriers_option, _ahcouriers_date, etc. */ public static function hide_meta( $keys ) { return array_merge( (array) $keys, array( '_ahcouriers_option', '_ahcouriers_date', '_ahcouriers_mode', '_ahcouriers_time', '_ahcouriers_quote_total' ) ); } /* ---------- checkout: over 100 km ---------- */ /** * Shown on the classic cart/checkout when the address is over 100 km. * FRAMEWORK NOTE: the WooCommerce Checkout BLOCK does not run these * hooks; there the shopper sees WooCommerce's own "no shipping options" * text instead. To be checked on the test shop. */ public static function over_limit_row() { if ( ! function_exists( 'WC' ) || ! WC()->session || ! WC()->session->get( 'ahcouriers_over_limit' ) ) { return; } echo '' . esc_html__( 'Same Day After Hours Delivery', 'afterhourscouriers-delivery' ) /* [CHANGE-2026-11-14-6] */ . '' . esc_html__( "Same Day After Hours Delivery isn't available to this address online. Longer distances are available on request - please contact us.", 'afterhourscouriers-delivery' ) . ''; } /** * [CHANGE-2026-11-14-5] Block checkout: over-100 km message (approved by Andrew 14 Nov 2026). * WHY: over 100 km our options disappear and the block checkout only shows * WooCommerce's generic "verify the address is correct or try a different * address" - misleading. Agreed rule: the shopper is told to contact the store. * FRAMEWORK NOTE (declared before use, checked in WooCommerce 11.3 source, * StoreApi/Utilities/CartController.php): 'woocommerce_store_api_cart_errors' * passes a WP_Error; errors added are returned with the cart and shown by the * block checkout, AND they block placing the order. So the message is added * ONLY when our quote said "over 100 km" (session flag set in * calculate_shipping) AND no carrier offers any shipping option - a shop with * other carriers can still check out with them. * WHAT BREAKS IF REMOVED: over-100 km shoppers see only the generic message. * * @param WP_Error $errors Cart errors. * @param WC_Cart $cart Cart. */ public static function over_limit_cart_error( $errors, $cart ) { if ( ! function_exists( 'WC' ) || ! WC()->session || ! WC()->session->get( 'ahcouriers_over_limit' ) ) { return; } if ( ! is_object( $cart ) || ! $cart->needs_shipping() || ! WC()->shipping() ) { return; } foreach ( (array) WC()->shipping()->get_packages() as $pkg ) { if ( ! empty( $pkg['rates'] ) ) { return; // another carrier (or option) is available - don't block checkout. } } $errors->add( 'ahcouriers_over_limit', esc_html__( "Same Day After Hours Delivery isn't available to this address online. Longer distances are available on request - please contact us.", 'afterhourscouriers-delivery' ) ); } }