$label ) { if ( 'wc_actions' === $key ) { $out[ self::COLUMN ] = __( 'ABS', 'abs-courier' ); } $out[ $key ] = $label; } if ( ! isset( $out[ self::COLUMN ] ) ) { $out[ self::COLUMN ] = __( 'ABS', 'abs-courier' ); } return $out; } /** * Renders the column. * * @param string $column Column key. * @param \WC_Order|int|null $order_or_post Order (HPOS) or post ID (legacy). * @return void */ public static function column( $column, $order_or_post ) { if ( self::COLUMN !== $column ) { return; } $order = $order_or_post instanceof \WC_Order ? $order_or_post : wc_get_order( $order_or_post ); if ( $order instanceof \WC_Order ) { echo wp_kses_post( self::badge( $order ) ); } } /** * Short HTML status for an order (used by the column and the meta box). * * @param \WC_Order $order Order. * @return string */ public static function badge( \WC_Order $order ) { $state = OrderMeta::state( $order ); $labels = array( OrderMeta::QUEUED => array( __( 'Sending…', 'abs-courier' ), '#646970' ), OrderMeta::SENDING => array( __( 'Sending…', 'abs-courier' ), '#646970' ), OrderMeta::CREATED => array( (string) $order->get_meta( OrderMeta::AWB ), '#00a32a' ), OrderMeta::FAILED => array( __( 'Failed', 'abs-courier' ), '#b32d2e' ), OrderMeta::UNKNOWN => array( __( 'Checking…', 'abs-courier' ), '#996800' ), OrderMeta::HELD => array( __( 'Needs district', 'abs-courier' ), '#996800' ), OrderMeta::CANCELED => array( __( 'Cancelled', 'abs-courier' ), '#646970' ), ); if ( ! isset( $labels[ $state ] ) ) { return '' . esc_html__( 'Not sent', 'abs-courier' ) . ''; } list( $label, $color ) = $labels[ $state ]; $html = sprintf( '%2$s', esc_attr( $color ), esc_html( $label ) ); $abs = (string) $order->get_meta( OrderMeta::STATUS ); if ( OrderMeta::CREATED === $state && '' !== $abs ) { $html .= '
' . esc_html( \ABSCourier\Shipments\Statuses::label( $abs ) ) . ''; } if ( OrderMeta::CREATED === $state && 'yes' === $order->get_meta( OrderMeta::DISTRICT_UNMATCHED ) ) { $html .= ' ⚠'; } return $html; } /** * Adds the bulk action. * * @param array $actions Actions. * @return array */ public static function bulk_actions( $actions ) { if ( current_user_can( OrderActions::CAPABILITY ) ) { $actions[ self::BULK ] = __( 'Send to ABS Courier', 'abs-courier' ); $actions[ self::PRINT ] = __( 'Print ABS waybills', 'abs-courier' ); } return $actions; } /** * Handles the bulk action: one order is sent on its own, several as one ABS batch. * * @param string $redirect_to Redirect URL. * @param string $action Action. * @param int[] $ids Order IDs. * @return string */ public static function handle_bulk( $redirect_to, $action, $ids ) { if ( ! current_user_can( OrderActions::CAPABILITY ) ) { return $redirect_to; } if ( self::PRINT === $action ) { return self::print_url( (array) $ids, $redirect_to ); } if ( self::BULK !== $action ) { return $redirect_to; } $eligible = array(); $skipped = 0; foreach ( (array) $ids as $id ) { $order = wc_get_order( $id ); if ( $order instanceof \WC_Order && '' === Eligibility::problem( $order, false ) ) { $eligible[] = $order; } else { ++$skipped; } } if ( 1 === count( $eligible ) ) { ShipmentCreator::enqueue( $eligible[0] ); } elseif ( count( $eligible ) > 1 ) { BulkCreator::enqueue( $eligible ); } $message = sprintf( /* translators: %d: number of orders. */ _n( '%d order is being sent to ABS Courier.', '%d orders are being sent to ABS Courier.', count( $eligible ), 'abs-courier' ), count( $eligible ) ); if ( $skipped > 0 ) { $message .= ' ' . sprintf( /* translators: %d: number of orders. */ _n( '%d order was skipped (already sent, not in EGP, outside Egypt, or nothing to ship).', '%d orders were skipped (already sent, not in EGP, outside Egypt, or nothing to ship).', $skipped, 'abs-courier' ), $skipped ); } Notice::flash( empty( $eligible ) ? 'warning' : 'success', $message ); return $redirect_to; } /** * Print URL for the selected orders' AWBs, or the list with a notice when none have one. * * @param int[] $ids Order IDs. * @param string $redirect_to List URL. * @return string */ private static function print_url( array $ids, $redirect_to ) { $awbs = array(); foreach ( $ids as $id ) { $order = wc_get_order( $id ); if ( $order instanceof \WC_Order && OrderMeta::CREATED === OrderMeta::state( $order ) ) { $awbs[] = (string) $order->get_meta( OrderMeta::AWB ); } } if ( empty( $awbs ) ) { Notice::flash( 'warning', __( 'None of the selected orders has an ABS shipment to print.', 'abs-courier' ) ); return $redirect_to; } return PrintView::url( array_slice( $awbs, 0, PrintView::MAX_AWBS ) ); } /** * Filter options. * * @return array */ private static function filter_options() { return array( '' => __( 'All ABS states', 'abs-courier' ), self::NOT_SENT => __( 'Not sent to ABS', 'abs-courier' ), OrderMeta::CREATED => __( 'ABS shipment created', 'abs-courier' ), OrderMeta::FAILED => __( 'ABS failed', 'abs-courier' ), OrderMeta::HELD => __( 'ABS needs district', 'abs-courier' ), OrderMeta::UNKNOWN => __( 'ABS checking', 'abs-courier' ), OrderMeta::CANCELED => __( 'ABS cancelled', 'abs-courier' ), ); } /** * Selected filter value. * * @return string */ private static function current_filter() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filter. $value = isset( $_GET[ self::FILTER ] ) ? sanitize_key( wp_unslash( $_GET[ self::FILTER ] ) ) : ''; return array_key_exists( $value, self::filter_options() ) ? $value : ''; } /** * Prints the filter dropdown. * * @return void */ private static function filter_dropdown() { printf( ''; } /** * Filter dropdown on the HPOS screen. * * @param string $order_type Order type. * @param string $which 'top' or 'bottom'. * @return void */ public static function filter_dropdown_hpos( $order_type, $which ) { if ( 'shop_order' === $order_type && 'top' === $which ) { self::filter_dropdown(); } } /** * Filter dropdown on the legacy screen. * * @param string $post_type Post type. * @return void */ public static function filter_dropdown_legacy( $post_type ) { if ( 'shop_order' === $post_type ) { self::filter_dropdown(); } } /** * Meta query for the selected filter, or null. * * @return array|null */ private static function meta_query() { $value = self::current_filter(); if ( '' === $value ) { return null; } if ( self::NOT_SENT === $value ) { return array( 'key' => OrderMeta::STATE, 'compare' => 'NOT EXISTS', ); } return array( 'key' => OrderMeta::STATE, 'value' => $value, ); } /** * Applies the filter on the HPOS screen. * * @param array $args Order query args. * @return array */ public static function filter_query_hpos( $args ) { $clause = self::meta_query(); if ( null !== $clause ) { $args['meta_query'] = isset( $args['meta_query'] ) && is_array( $args['meta_query'] ) ? $args['meta_query'] : array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args['meta_query'][] = $clause; } return $args; } /** * Applies the filter on the legacy screen. * * @param array $vars Query vars. * @return array */ public static function filter_query_legacy( $vars ) { global $typenow; $clause = self::meta_query(); if ( null !== $clause && 'shop_order' === $typenow ) { $vars['meta_query'] = isset( $vars['meta_query'] ) && is_array( $vars['meta_query'] ) ? $vars['meta_query'] : array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $vars['meta_query'][] = $clause; } return $vars; } }