id, $ids, true );
}
/**
* Styles and copy script (bundled, no external assets).
*/
public static function assets() {
if ( ! self::is_order_screen() ) {
return;
}
wp_enqueue_style( 'vprwc-admin', VPRWC_URL . 'assets/css/vprwc-admin.css', array(), VPRWC_VERSION );
wp_enqueue_script( 'vprwc-admin', VPRWC_URL . 'assets/js/vprwc-admin.js', array(), VPRWC_VERSION, true );
wp_localize_script(
'vprwc-admin',
'vprwcAdmin',
array(
'copied' => __( 'Copied to clipboard', 'acrislip-order-receipts' ),
'failed' => __( 'Press Ctrl+C to copy', 'acrislip-order-receipts' ),
)
);
}
/**
* Register the box high in the main column.
*/
public static function add() {
$screens = array( 'shop_order' );
if ( function_exists( 'wc_get_page_screen_id' ) ) {
$screens[] = wc_get_page_screen_id( 'shop-order' );
}
foreach ( array_unique( $screens ) as $screen ) {
add_meta_box( 'vprwc-payment', __( 'Payment', 'acrislip-order-receipts' ), array( __CLASS__, 'render' ), $screen, 'normal', 'high' );
}
}
/**
* Copy button markup.
*
* @param string $value Value.
* @param string $label Accessible label.
* @return string
*/
public static function copy_button( $value, $label ) {
return '';
}
/**
* Provider mark (initials on the provider color).
*
* @param array $s Summary.
* @return string
*/
public static function provider_mark( array $s ) {
$words = preg_split( '/\s+/', trim( $s['provider_name'] ) );
$initials = strtoupper( substr( (string) $words[0], 0, 1 ) . ( isset( $words[1] ) ? substr( $words[1], 0, 1 ) : substr( (string) $words[0], 1, 1 ) ) );
return '' . esc_html( $initials ) . '';
}
/**
* Render the box.
*
* @param WP_Post|WC_Order $post_or_order Post or order.
*/
public static function render( $post_or_order ) {
$order = $post_or_order instanceof WP_Post ? wc_get_order( $post_or_order->ID ) : $post_or_order;
if ( ! $order instanceof WC_Order || ! current_user_can( 'edit_shop_orders' ) ) {
return;
}
$s = VPRWC_Overview::summary( $order, true );
if ( $s['transaction_reference'] && ! $order->get_meta( '_vprwc_transaction_reference' ) ) {
VPRWC_Overview::remember_reference( $order, $s['transaction_reference'] );
}
$paid = VPRWC_Receipt::is_paid( $order );
$share = '';
if ( $paid ) {
VPRWC_Numbering::get_or_assign( $order );
$share = apply_filters( 'vprwc_receipt_share_url', '', $order );
}
echo '
';
// Header: provider, amount, status.
echo '
';
echo '
' . self::provider_mark( $s ) . '
' . esc_html( $s['provider_name'] ) . '
' . esc_html( $s['method_title'] ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- provider_mark() escapes.
echo '
' . esc_html( $s['amount'] ) . '';
echo '' . esc_html( $s['status_label'] ) . '';
if ( $s['environment'] ) {
echo '' . esc_html( 'test' === $s['environment'] ? __( 'Test mode', 'acrislip-order-receipts' ) : __( 'Live', 'acrislip-order-receipts' ) ) . '';
}
echo '
';
// References, full width and never truncated: this is what customers call about.
$crefs = VPRWC_Receipt::references( $order );
$refs = array_filter(
array(
__( 'Transaction reference', 'acrislip-order-receipts' ) => $s['transaction_reference'],
__( 'Customer reference', 'acrislip-order-receipts' ) => $crefs['customer_reference'],
__( 'Payment customer reference', 'acrislip-order-receipts' ) => $crefs['payment_customer_reference'],
__( 'Provider reference', 'acrislip-order-receipts' ) => $s['provider_reference'],
)
);
if ( $refs ) {
echo '
';
foreach ( $refs as $label => $value ) {
/* translators: %s: field label */
echo '
' . esc_html( $label ) . '' . esc_html( $value ) . '' . self::copy_button( $value, sprintf( __( 'Copy %s', 'acrislip-order-receipts' ), strtolower( $label ) ) ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- copy_button() escapes.
}
echo '
';
} else {
echo '
' . esc_html__( 'Transaction reference', 'acrislip-order-receipts' ) . '' . esc_html__( 'No reference recorded yet', 'acrislip-order-receipts' ) . '
';
}
echo '
';
if ( 'card' === $s['payment_type'] || $s['card'] ) {
self::row( __( 'Card', 'acrislip-order-receipts' ), $s['card'] );
}
if ( 'bank' === $s['payment_type'] ) {
self::row( __( 'Bank', 'acrislip-order-receipts' ), $s['bank_name'] );
self::row( __( 'Account', 'acrislip-order-receipts' ), trim( $s['account_masked'] . ( $s['account_name'] ? ' · ' . $s['account_name'] : '' ) ) );
self::row( __( 'Transfer reference', 'acrislip-order-receipts' ), $s['bank_reference'], true, true );
}
self::row( __( 'Channel', 'acrislip-order-receipts' ), $s['channel'] );
/* translators: %s: timezone name */
self::row( sprintf( __( 'Paid (%s)', 'acrislip-order-receipts' ), wp_timezone_string() ), $s['paid_at'] ? $s['paid_at'] : '—' );
self::row( __( 'Gateway fee', 'acrislip-order-receipts' ), $s['fee'], false, false, true );
self::row( __( 'Net to you', 'acrislip-order-receipts' ), $s['net'], false, false, true );
self::row( __( 'Refunded', 'acrislip-order-receipts' ), $s['refunds'] );
echo '
';
echo '
';
}
/**
* Render one row.
*
* @param string $label Label.
* @param string $value Value.
* @param bool $copy Copy button.
* @param bool $mono Monospace.
* @param bool $admin Admin-only marker.
*/
private static function row( $label, $value, $copy = false, $mono = false, $admin = false ) {
if ( '' === (string) $value ) {
return;
}
echo '' . esc_html( $label );
if ( $admin ) {
echo ' ' . esc_html__( 'staff only', 'acrislip-order-receipts' ) . '';
}
echo '' . esc_html( $value ) . '';
if ( $copy ) {
/* translators: %s: field label */
echo self::copy_button( $value, sprintf( __( 'Copy %s', 'acrislip-order-receipts' ), strtolower( $label ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- copy_button() escapes.
}
echo '';
}
/**
* Add the column after the order status.
*
* @param array $columns Columns.
* @return array
*/
public static function columns( $columns ) {
$out = array();
foreach ( $columns as $key => $label ) {
$out[ $key ] = $label;
if ( 'order_status' === $key ) {
$out['vprwc_reference'] = __( 'Reference', 'acrislip-order-receipts' );
}
}
if ( ! isset( $out['vprwc_reference'] ) ) {
$out['vprwc_reference'] = __( 'Reference', 'acrislip-order-receipts' );
}
return $out;
}
/**
* Legacy column.
*
* @param string $column Column.
* @param int $post_id Post id.
*/
public static function column_legacy( $column, $post_id ) {
if ( 'vprwc_reference' === $column ) {
self::column_hpos( $column, wc_get_order( $post_id ) );
}
}
/**
* HPOS column.
*
* @param string $column Column.
* @param WC_Order $order Order.
*/
public static function column_hpos( $column, $order ) {
if ( 'vprwc_reference' !== $column || ! $order instanceof WC_Order ) {
return;
}
$s = VPRWC_Overview::summary( $order );
if ( '' === $s['transaction_reference'] ) {
echo '—';
return;
}
if ( ! $order->get_meta( '_vprwc_transaction_reference' ) ) {
VPRWC_Overview::remember_reference( $order, $s['transaction_reference'] );
}
echo '' . self::provider_mark( $s ) . '' . esc_html( $s['transaction_reference'] ) . '' . self::copy_button( $s['transaction_reference'], __( 'Copy reference', 'acrislip-order-receipts' ) ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in helpers.
}
/**
* Search the reference meta keys from the orders list search box.
*
* @param array $fields Meta keys.
* @return array
*/
public static function search_fields( $fields ) {
return array_values( array_unique( array_merge( (array) $fields, VPRWC_Overview::reference_meta_keys() ) ) );
}
}