self::COLUMNS, 'sortBy' => 'createdAt', 'sortOrder' => 'DESC', 'page' => $page, 'limit' => self::PER_PAGE, ); if ( isset( Page::statuses()[ $status ] ) ) { $body['status'] = array( $status ); } if ( $batch > 0 ) { $body['shipmentHeaderIds'] = array( $batch ); } $search = trim( $search ); if ( '' === $search ) { return $body; } if ( preg_match( '/^#?\d{1,9}$/', $search ) ) { // Order references are sent as "#". $body['refs'] = array( '#' . ltrim( $search, '#' ) ); } elseif ( preg_match( '/^[A-Za-z]{2,5}\d{5,}$/', $search ) || ( preg_match( '/^\d{10,}$/', $search ) && ! preg_match( '/^(\+|00|0)?1[0125]\d{8}$/', $search ) ) ) { $body['awbs'] = array( strtoupper( $search ) ); } elseif ( preg_match( '/^[\d\s+\-()]{8,}$/', $search ) ) { $body['searchType'] = 'customerPhone'; $body['searchValue'] = preg_replace( '/[^\d+]/', '', $search ); } else { $body['searchType'] = 'customerName'; $body['searchValue'] = $search; } return $body; } /** * Shipment list. * * @param AbsClient $client Client. * @return void */ private static function render_list( AbsClient $client ) { $search = Page::query( 's' ); $status = Page::query( 'status' ); $batch = absint( Page::query( 'batch' ) ); $page = Page::current_page(); echo '
'; printf( '', esc_attr( Menu::SLUG ) ); if ( $batch > 0 ) { printf( '', (int) $batch ); } printf( ' ', esc_attr( $search ), esc_attr__( 'AWB, order number, phone or name', 'abs-courier' ) ); echo ' '; if ( $batch > 0 ) { /* translators: %d: batch ID. */ echo ' ' . esc_html( sprintf( __( 'Batch %d', 'abs-courier' ), $batch ) ) . ' ' . esc_html__( 'Show all', 'abs-courier' ) . ''; } echo '
'; try { $data = $client->post( 'shipments/list', self::list_body( $search, $status, $batch, $page ) ); } catch ( AbsError $e ) { Page::error( $e->getMessage() ); return; } $rows = isset( $data['data'] ) && is_array( $data['data'] ) ? $data['data'] : array(); $has_more = ! empty( $data['hasMore'] ); $orders = Page::orders_by_awb( wp_list_pluck( $rows, 'awb' ) ); if ( empty( $rows ) ) { echo '

' . esc_html__( 'No shipments found.', 'abs-courier' ) . '

'; return; } printf( '
', esc_url( admin_url( 'admin-post.php' ) ) ); printf( '', esc_attr( PrintView::ACTION ) ); wp_nonce_field( PrintView::ACTION, '_wpnonce', false ); echo ''; foreach ( array( __( 'AWB', 'abs-courier' ), __( 'Order', 'abs-courier' ), __( 'Status', 'abs-courier' ), __( 'Consignee', 'abs-courier' ), __( 'District', 'abs-courier' ), __( 'Cash to collect', 'abs-courier' ), __( 'Created', 'abs-courier' ) ) as $heading ) { echo ''; } echo ''; foreach ( $rows as $row ) { $awb = isset( $row['awb'] ) ? (string) $row['awb'] : ''; echo ''; printf( '', esc_attr( $awb ) ); printf( '', esc_url( add_query_arg( 'awb', rawurlencode( $awb ), Menu::url( 'shipments' ) ) ), esc_html( $awb ) ); echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in order_link(). echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html( $heading ) . '
%2$s' . self::order_link( isset( $orders[ $awb ] ) ? $orders[ $awb ] : null, isset( $row['reference'] ) ? $row['reference'] : '' ) . '' . esc_html( self::status_label( $row ) ) . '' . esc_html( self::text( $row, 'consigneeName' ) ) . '
' . esc_html( self::text( $row, 'consigneePhone' ) ) . '
' . esc_html( self::places( array( self::text( $row, 'district' ), self::text( $row, 'city' ) ) ) ) . '' . esc_html( Page::money( isset( $row['cash'] ) ? $row['cash'] : null ) ) . '' . esc_html( Page::date( isset( $row['createdAt'] ) ? $row['createdAt'] : '' ) ) . '
'; echo '

'; $base = remove_query_arg( 'paged', add_query_arg( array_map( 'rawurlencode', array_filter( array( 's' => $search, 'status' => $status, 'batch' => $batch ? (string) $batch : '', ) ) ), Menu::url( 'shipments' ) ) ); Page::pagination( $base, $page, $has_more ); } /** * One shipment's details and history. * * @param AbsClient $client Client. * @param string $awb AWB. * @return void */ private static function render_detail( AbsClient $client, $awb ) { printf( '

%2$s

', esc_url( Menu::url( 'shipments' ) ), esc_html__( '← All shipments', 'abs-courier' ) ); try { $s = $client->get( 'shipments/' . rawurlencode( $awb ) ); } catch ( AbsError $e ) { Page::error( AbsError::NOT_FOUND === $e->type() ? __( 'This shipment was not found in your ABS account.', 'abs-courier' ) : $e->getMessage() ); return; } $s = is_array( $s ) ? $s : array(); $get = static function ( $key ) use ( $s ) { return isset( $s[ $key ] ) && is_scalar( $s[ $key ] ) ? (string) $s[ $key ] : ''; }; $order = Page::orders_by_awb( array( $awb ) ); /* translators: %s: AWB. */ echo '

' . esc_html( sprintf( __( 'Shipment %s', 'abs-courier' ), $awb ) ) . '

'; printf( '

%2$s

', esc_url( PrintView::url( array( $awb ) ) ), esc_html__( 'Print waybill', 'abs-courier' ) ); $fields = array( __( 'Status', 'abs-courier' ) => self::status_label( $s ), __( 'Order', 'abs-courier' ) => null, __( 'Created', 'abs-courier' ) => Page::date( $get( 'creationDate' ), false ), __( 'Expected delivery', 'abs-courier' ) => Page::date( $get( 'expectedDeliveryDate' ), false ), __( 'Delivered', 'abs-courier' ) => Page::date( $get( 'deliveryDate' ), false ), __( 'Delivery attempts', 'abs-courier' ) => $get( 'numberOfAttempts' ), __( 'Consignee', 'abs-courier' ) => trim( $get( 'customerName' ) . ' ' . $get( 'phone' ) ), __( 'Address', 'abs-courier' ) => $get( 'address' ), __( 'District', 'abs-courier' ) => self::places( array( $get( 'districtName' ), $get( 'cityName' ), $get( 'governorateName' ) ) ), __( 'Cash to collect', 'abs-courier' ) => Page::money( $get( 'cash' ) ), __( 'Cash collected', 'abs-courier' ) => Page::money( $get( 'collectedCash' ) ), __( 'Item value', 'abs-courier' ) => Page::money( $get( 'itemValue' ) ), __( 'Product', 'abs-courier' ) => $get( 'productName' ), __( 'Contents', 'abs-courier' ) => $get( 'contents' ), __( 'Pieces / weight', 'abs-courier' ) => $get( 'noOfPcs' ) . ' / ' . $get( 'weight' ) . ' kg', __( 'Special instructions', 'abs-courier' ) => $get( 'specialInstructions' ), ); echo ''; foreach ( $fields as $label => $value ) { echo ''; } $pod = $get( 'podUrl' ); if ( '' !== $pod && wp_http_validate_url( $pod ) ) { printf( '', esc_html__( 'Proof of delivery', 'abs-courier' ), esc_url( $pod ), esc_html__( 'View', 'abs-courier' ) ); } echo ''; $history = isset( $s['shipmentHistory'] ) && is_array( $s['shipmentHistory'] ) ? array_reverse( $s['shipmentHistory'] ) : array(); echo '

' . esc_html__( 'History', 'abs-courier' ) . '

'; echo ''; foreach ( $history as $event ) { echo ''; } echo '
' . esc_html__( 'Date', 'abs-courier' ) . '' . esc_html__( 'Status', 'abs-courier' ) . '
' . esc_html( Page::date( isset( $event['date'] ) ? $event['date'] : '' ) ) . '' . esc_html( self::status_label( (array) $event ) ) . '
'; } /** * A row value as text; lists (ABS returns phones as an array) are joined. * * @param array $row Row. * @param string $key Column. * @return string */ private static function text( array $row, $key ) { if ( ! isset( $row[ $key ] ) ) { return ''; } if ( is_array( $row[ $key ] ) ) { return implode( ' / ', array_filter( array_map( 'strval', array_filter( $row[ $key ], 'is_scalar' ) ) ) ); } return is_scalar( $row[ $key ] ) ? (string) $row[ $key ] : ''; } /** * Place names joined without repeats ("ElMaadi, ElMaadi, Cairo" → "ElMaadi, Cairo"). * * @param string[] $names Names, most specific first. * @return string */ private static function places( array $names ) { return implode( ', ', array_unique( array_filter( array_map( 'trim', $names ), 'strlen' ) ) ); } /** * Human status for a row or event. * * @param array $row Row with 'status' and optionally 'displayStatus'. * @return string */ private static function status_label( array $row ) { $status = isset( $row['status'] ) ? (string) $row['status'] : ''; $labels = Page::statuses(); if ( isset( $labels[ $status ] ) ) { return $labels[ $status ]; } return isset( $row['displayStatus'] ) ? (string) $row['displayStatus'] : $status; } /** * Link to the WooCommerce order, or the ABS reference as text. * * @param \WC_Order|null $order Linked order. * @param string $reference ABS reference. * @return string Escaped HTML. */ private static function order_link( $order, $reference ) { if ( $order instanceof \WC_Order ) { /* translators: %s: order number. */ return sprintf( '%2$s', esc_url( $order->get_edit_order_url() ), esc_html( sprintf( __( 'Order #%s', 'abs-courier' ), $order->get_order_number() ) ) ); } return esc_html( '' !== (string) $reference ? (string) $reference : '—' ); } }