'11:00 – 13:00',
'SLOT_1_3' => '13:00 – 15:00',
'SLOT_3_5' => '15:00 – 17:00',
);
/** Vehicle type sent with every pickup, as the Shopify app does (the vehicle list is not available to API keys). */
const VEHICLE_TYPE_ID = 1;
/**
* Hooks the handler.
*
* @return void
*/
public static function register() {
add_action( 'admin_post_' . self::ACTION, array( __CLASS__, 'create' ) );
}
/**
* Renders the tab.
*
* @return void
*/
public static function render() {
$client = Page::client();
if ( null === $client ) {
return;
}
$view = 'history' === Page::query( 'view' ) ? 'history' : 'upcoming';
$base = add_query_arg( 'view', $view, Menu::url( 'pickups' ) );
echo '
';
foreach ( array(
'upcoming' => __( 'Upcoming', 'abs-courier' ),
'history' => __( 'History', 'abs-courier' ),
) as $key => $label ) {
printf( '- %3$s%4$s
', esc_url( add_query_arg( 'view', $key, Menu::url( 'pickups' ) ) ), $key === $view ? ' class="current"' : '', esc_html( $label ), 'upcoming' === $key ? ' | ' : '' );
}
echo '
';
if ( 'upcoming' === $view && 'new' === Page::query( 'form' ) ) {
self::render_form( $client );
return;
}
if ( 'upcoming' === $view ) {
printf( '%2$s
', esc_url( add_query_arg( 'form', 'new', $base ) ), esc_html__( 'Request a pickup', 'abs-courier' ) );
}
$page = Page::current_page();
try {
$data = $client->get(
'pickup/' . $view,
array(
'page' => $page,
'limit' => self::PER_PAGE,
'lang' => 'en',
)
);
} catch ( AbsError $e ) {
// ABS answers 404 when there are no upcoming pickups.
if ( AbsError::NOT_FOUND !== $e->type() ) {
Page::error( $e->getMessage() );
return;
}
$data = array();
}
$rows = isset( $data['data'] ) && is_array( $data['data'] ) ? $data['data'] : array();
$total = isset( $data['total'] ) ? (int) $data['total'] : count( $rows );
if ( empty( $rows ) ) {
echo '' . esc_html( 'upcoming' === $view ? __( 'No upcoming pickups.', 'abs-courier' ) : __( 'No past pickups.', 'abs-courier' ) ) . '
';
return;
}
echo '';
foreach ( array( __( 'Pickup', 'abs-courier' ), __( 'Status', 'abs-courier' ), __( 'Pickup date', 'abs-courier' ), __( 'From', 'abs-courier' ), __( 'ABS branch', 'abs-courier' ), __( 'Shipments', 'abs-courier' ), __( 'Contact', 'abs-courier' ), __( 'Requested', 'abs-courier' ) ) as $heading ) {
echo '| ' . esc_html( $heading ) . ' | ';
}
echo '
';
foreach ( $rows as $row ) {
$v = static function ( $key ) use ( $row ) {
return isset( $row[ $key ] ) && is_scalar( $row[ $key ] ) ? (string) $row[ $key ] : '';
};
$count = '' !== $v( 'actualAWBs' ) ? $v( 'actualAWBs' ) . ' / ' . $v( 'noOfAWBs' ) : $v( 'noOfAWBs' );
echo '';
echo '' . esc_html( $v( 'id' ) ) . ' ' . esc_html( $v( 'vehicleType' ) ) . ' | ';
echo '' . esc_html( self::status_label( $v( 'status' ) ) ) . ' | ';
echo '' . esc_html( Page::date( $v( 'availablePickupDate' ), false ) ) . ' ' . esc_html( $v( 'slotLabel' ) ) . ' | ';
echo '' . esc_html( $v( 'fromLocation' ) ) . ' | ';
echo '' . esc_html( $v( 'toBranch' ) ) . ' | ';
echo '' . esc_html( $count ) . ' | ';
echo '' . esc_html( $v( 'contactName' ) ) . ' ' . esc_html( $v( 'contactPhones' ) ) . ' | ';
echo '' . esc_html( Page::date( $v( 'creationDate' ), false ) ) . ' ' . esc_html( $v( 'createdBy' ) ) . ' | ';
echo '
';
}
echo '
';
$pages = max( 1, (int) ceil( $total / self::PER_PAGE ) );
Page::pagination( $base, $page, $page < $pages, $pages );
}
/**
* Pickup request form.
*
* @param AbsClient $client Client.
* @return void
*/
private static function render_form( AbsClient $client ) {
try {
$locations = $client->get( 'shared/dropdown/pickup-locations', array( 'lang' => 'en' ) );
$pending = self::pickupable_shipments( $client );
} catch ( AbsError $e ) {
Page::error( $e->getMessage() );
return;
}
$locations = is_array( $locations ) ? $locations : array();
if ( empty( $locations ) ) {
Page::error( __( 'Your ABS account has no pickup locations. Add one in the ABS Courier portal first.', 'abs-courier' ) );
return;
}
echo '' . esc_html__( 'Request a pickup', 'abs-courier' ) . '
';
printf( '', esc_html__( 'Request pickup', 'abs-courier' ), esc_url( Menu::url( 'pickups' ) ), esc_html__( 'Cancel', 'abs-courier' ) );
}
/**
* Shipments still in NEW (not yet handed over), newest first, up to 200.
*
* @param AbsClient $client Client.
* @return array Rows with awb, reference, consigneeName.
* @throws AbsError On failure.
*/
private static function pickupable_shipments( AbsClient $client ) {
$data = $client->post(
'shipments/list',
array(
'status' => array( 'NEW' ),
'columns' => array( 'awb', 'reference', 'consigneeName', 'createdAt' ),
'sortBy' => 'createdAt',
'sortOrder' => 'DESC',
'page' => 1,
'limit' => 200,
)
);
$rows = isset( $data['data'] ) && is_array( $data['data'] ) ? $data['data'] : array();
return array_values(
array_filter(
$rows,
static function ( $row ) {
return ! empty( $row['awb'] );
}
)
);
}
/**
* Validates the submitted form into an ABS pickup, or returns an error message.
*
* @param array $input Submitted values (unslashed).
* @param string $today Today's date (Y-m-d) in the site timezone.
* @return array|string Pickup object, or an error message.
*/
public static function build_pickup( array $input, $today ) {
$date = isset( $input['date'] ) ? (string) $input['date'] : '';
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date ) || $date < $today ) {
return __( 'Choose a pickup date from today onwards.', 'abs-courier' );
}
$slot = isset( $input['slot'] ) ? (string) $input['slot'] : '';
if ( ! isset( self::SLOTS[ $slot ] ) ) {
return __( 'Choose a time slot.', 'abs-courier' );
}
$location = isset( $input['location'] ) ? absint( $input['location'] ) : 0;
if ( $location <= 0 ) {
return __( 'Choose a pickup location.', 'abs-courier' );
}
$pickup = array(
'availablePickupDate' => $date,
'slot' => $slot,
'fromLocationId' => $location,
'vehicleTypeId' => self::VEHICLE_TYPE_ID,
);
$awbs = isset( $input['awbs'] ) && is_array( $input['awbs'] ) ? array_values( array_unique( array_filter( array_map( 'sanitize_text_field', array_map( 'strval', $input['awbs'] ) ) ) ) ) : array();
$count = isset( $input['count'] ) ? absint( $input['count'] ) : 0;
if ( ! empty( $awbs ) ) {
$pickup['awbs'] = array_slice( $awbs, 0, 500 );
} elseif ( $count >= 1 && $count <= 500 ) {
$pickup['noOfAWBs'] = $count;
} else {
return __( 'Choose the shipments to hand over, or enter the number of parcels.', 'abs-courier' );
}
$notes = isset( $input['notes'] ) ? trim( sanitize_textarea_field( (string) $input['notes'] ) ) : '';
if ( '' !== $notes ) {
$pickup['notes'] = mb_substr( $notes, 0, 500, 'UTF-8' );
}
return $pickup;
}
/**
* Handles the form.
*
* @return void
*/
public static function create() {
if ( ! current_user_can( Menu::CAPABILITY ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'abs-courier' ), '', array( 'response' => 403 ) );
}
check_admin_referer( self::ACTION );
$pickup = self::build_pickup( wp_unslash( $_POST ), wp_date( 'Y-m-d' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Every field is validated in build_pickup().
if ( is_string( $pickup ) ) {
self::back( 'error', $pickup, true );
}
try {
( new AbsClient( Credentials::get() ) )->post( 'create-pickup', array( 'pickups' => array( $pickup ) ) );
} catch ( AbsError $e ) {
self::back( 'error', $e->getMessage(), true );
}
/* translators: 1: date, 2: time slot. */
self::back( 'success', sprintf( __( 'Pickup requested for %1$s, %2$s.', 'abs-courier' ), Page::date( $pickup['availablePickupDate'], false ), self::SLOTS[ $pickup['slot'] ] ), false );
}
/**
* Human pickup status.
*
* @param string $status Status.
* @return string
*/
private static function status_label( $status ) {
$labels = array(
'REQUESTED' => __( 'Requested', 'abs-courier' ),
'IN_PROGRESS' => __( 'In progress', 'abs-courier' ),
'COMPLETED' => __( 'Completed', 'abs-courier' ),
'CANCELED' => __( 'Cancelled', 'abs-courier' ),
);
return isset( $labels[ $status ] ) ? $labels[ $status ] : $status;
}
/**
* Redirects back to the pickups tab with a notice.
*
* @param string $type Notice type.
* @param string $message Message.
* @param bool $to_form Return to the form.
* @return void
*/
private static function back( $type, $message, $to_form ) {
Notice::flash( $type, $message );
wp_safe_redirect( $to_form ? add_query_arg( 'form', 'new', Menu::url( 'pickups' ) ) : Menu::url( 'pickups' ) );
exit;
}
}