$action, 'order_id' => $order_id, self::NONCE => wp_create_nonce( $action . '_' . $order_id ), ), admin_url( 'admin-post.php' ) ); } /** * Sends (or re-checks) an order now and reports the result. * * @return void */ public static function send() { $order = self::order( self::SEND ); $state = OrderMeta::state( $order ); self::apply_posted_district( $order ); if ( OrderMeta::UNKNOWN !== $state ) { $problem = Eligibility::problem( $order, false ); if ( '' !== $problem ) { self::back( $order, 'error', $problem ); } OrderMeta::set_state( $order, OrderMeta::QUEUED ); } ShipmentCreator::run( $order->get_id() ); $order = wc_get_order( $order->get_id() ); switch ( OrderMeta::state( $order ) ) { case OrderMeta::CREATED: /* translators: %s: AWB. */ self::back( $order, 'success', sprintf( __( 'ABS shipment created. AWB: %s', 'abs-courier' ), $order->get_meta( OrderMeta::AWB ) ) ); break; case OrderMeta::HELD: self::back( $order, 'warning', __( 'The delivery district could not be matched. Choose it below, then send again.', 'abs-courier' ) ); break; case OrderMeta::QUEUED: case OrderMeta::UNKNOWN: self::back( $order, 'warning', (string) $order->get_meta( OrderMeta::ERROR ) ); break; default: self::back( $order, 'error', (string) $order->get_meta( OrderMeta::ERROR ) ); } } /** * Cancels the order's shipment. * * @return void */ public static function cancel() { $order = self::order( self::CANCEL ); if ( ShipmentCanceller::run( $order->get_id() ) ) { self::back( $order, 'success', __( 'The ABS shipment was cancelled.', 'abs-courier' ) ); } $order = wc_get_order( $order->get_id() ); self::back( $order, 'error', (string) $order->get_meta( OrderMeta::ERROR ) ); } /** * Checks the shipment's status with ABS now. * * @return void */ public static function refresh() { $order = self::order( self::REFRESH ); try { $status = StatusSync::refresh( $order ); } catch ( AbsError $e ) { self::back( $order, 'error', $e->getMessage() ); } if ( '' === $status ) { self::back( $order, 'warning', __( 'ABS Courier did not return a status for this shipment.', 'abs-courier' ) ); } /* translators: %s: status. */ self::back( $order, 'success', sprintf( __( 'Shipment status: %s', 'abs-courier' ), Statuses::label( $status ) ) ); } /** * Saves the merchant's district choice. * * @return void */ public static function district() { $order = self::order( self::DISTRICT ); if ( ! self::apply_posted_district( $order, true ) ) { self::back( $order, 'error', __( 'Choose a district from the list.', 'abs-courier' ) ); } self::back( $order, 'success', __( 'District saved.', 'abs-courier' ) ); } /** * Saves the district chosen in the box, when one was posted with the button. * * @param \WC_Order $order Order. * @param bool $required Whether "match automatically" (0) counts as invalid. * @return bool True when a valid district (or, if not required, no choice) was posted. */ private static function apply_posted_district( \WC_Order $order, $required = false ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in order(). if ( ! isset( $_POST['abs_courier_district_id'] ) ) { return ! $required; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in order(). $id = absint( $_POST['abs_courier_district_id'] ); if ( 0 === $id ) { // "Match automatically from the address": forget any earlier manual choice. if ( ! $required ) { $order->delete_meta_data( OrderMeta::DISTRICT_ID ); $order->save(); } return ! $required; } $valid = DistrictMatcher::UNRESOLVED_DISTRICT_ID === $id; if ( ! $valid ) { try { $valid = in_array( $id, array_column( DistrictDirectory::all(), 'id' ), true ); } catch ( AbsError $e ) { self::back( $order, 'error', __( 'The district list could not be loaded from ABS Courier. Try again shortly.', 'abs-courier' ) ); } } if ( ! $valid ) { return false; } $order->update_meta_data( OrderMeta::DISTRICT_ID, $id ); $order->delete_meta_data( OrderMeta::DISTRICT_UNMATCHED ); $order->save(); return true; } /** * Loads the order after checking capability and nonce. * * @param string $action Action. * @return \WC_Order */ private static function order( $action ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is checked below. $order_id = isset( $_REQUEST['order_id'] ) ? absint( $_REQUEST['order_id'] ) : 0; if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'abs-courier' ), '', array( 'response' => 403 ) ); } check_admin_referer( $action . '_' . $order_id, self::NONCE ); $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { wp_die( esc_html__( 'Order not found.', 'abs-courier' ), '', array( 'response' => 404 ) ); } if ( ! current_user_can( 'edit_shop_order', $order_id ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce meta capability for one order. wp_die( esc_html__( 'You do not have permission to do this.', 'abs-courier' ), '', array( 'response' => 403 ) ); } return $order; } /** * Returns to the order screen with a notice. * * @param \WC_Order $order Order. * @param string $type Notice type. * @param string $message Message. * @return void */ private static function back( \WC_Order $order, $type, $message ) { Notice::flash( $type, '' !== $message ? $message : __( 'Something went wrong. Check the order notes for details.', 'abs-courier' ) ); wp_safe_redirect( $order->get_edit_order_url() ); exit; } }