label. * * @return array */ public static function statuses() { return \ABSCourier\Shipments\Statuses::labels(); } /** * Client for the connected account, or null after printing a "connect first" message. * * @return AbsClient|null */ public static function client() { $api_key = Credentials::get(); $state = Connection::state_saved(); if ( '' === $api_key || Connection::CONNECTED !== $state['status'] ) { printf( '

%1$s %3$s

', esc_html__( 'Connect your ABS Courier account to see this page.', 'abs-courier' ), esc_url( Menu::url( 'settings' ) ), esc_html__( 'Go to settings', 'abs-courier' ) ); return null; } return new AbsClient( $api_key ); } /** * Prints an error box. * * @param string $message Message. * @return void */ public static function error( $message ) { printf( '

%s

', esc_html( $message ) ); } /** * Formats an ABS date or timestamp in the site's timezone and format. * * @param string $value ISO date/time from ABS. * @param bool $time Include the time. * @return string */ public static function date( $value, $time = true ) { $ts = is_string( $value ) && '' !== $value ? strtotime( $value ) : false; if ( false === $ts ) { return '—'; } $format = get_option( 'date_format' ) . ( $time ? ' ' . get_option( 'time_format' ) : '' ); return wp_date( $format, $ts ); } /** * Formats a money amount in EGP. * * @param mixed $amount Amount. * @return string */ public static function money( $amount ) { return is_numeric( $amount ) ? number_format_i18n( (float) $amount, 2 ) . ' ' . __( 'EGP', 'abs-courier' ) : '—'; } /** * Prints previous/next (and optional page count) links. * * @param string $base URL without the page argument. * @param int $page Current page. * @param bool $has_more Whether a next page exists. * @param int|null $pages Total pages when known. * @return void */ public static function pagination( $base, $page, $has_more, $pages = null ) { if ( 1 === $page && ! $has_more ) { return; } echo '
'; if ( null !== $pages ) { /* translators: 1: current page, 2: total pages. */ echo '' . esc_html( sprintf( __( 'Page %1$d of %2$d', 'abs-courier' ), $page, $pages ) ) . ' '; } if ( $page > 1 ) { printf( '%2$s ', esc_url( add_query_arg( 'paged', $page - 1, $base ) ), esc_html__( '‹ Previous', 'abs-courier' ) ); } if ( $has_more ) { printf( '%2$s', esc_url( add_query_arg( 'paged', $page + 1, $base ) ), esc_html__( 'Next ›', 'abs-courier' ) ); } echo '
'; } /** * Current page number from the query string. * * @return int */ public static function current_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only paging. return isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; } /** * A sanitized query-string value. * * @param string $key Name. * @return string */ public static function query( $key ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only filters. return isset( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; } /** * Orders linked to AWBs, as awb => order (one query). * * @param string[] $awbs AWBs. * @return array */ public static function orders_by_awb( array $awbs ) { $awbs = array_values( array_filter( array_map( 'strval', $awbs ) ) ); if ( empty( $awbs ) ) { return array(); } if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) { $orders = wc_get_orders( array( 'limit' => count( $awbs ), 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => \ABSCourier\Orders\OrderMeta::AWB, 'value' => $awbs, 'compare' => 'IN', ), ), ) ); } else { // The legacy (posts) order store ignores meta_query and would return unrelated // orders; it does support a single meta_key/meta_value pair. $orders = array(); foreach ( $awbs as $awb ) { $orders = array_merge( $orders, wc_get_orders( array( 'limit' => 1, 'meta_key' => \ABSCourier\Orders\OrderMeta::AWB, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => $awb, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ) ); } } $map = array(); foreach ( $orders as $order ) { $map[ (string) $order->get_meta( \ABSCourier\Orders\OrderMeta::AWB ) ] = $order; } return $map; } }