__( 'Copied to clipboard', 'acrislip-order-receipts' ),
'failed' => __( 'Press Ctrl+C to copy', 'acrislip-order-receipts' ),
)
);
}
/**
* Whether the logged-in customer owns the order.
*
* @param WC_Order $order Order.
* @return bool
*/
private static function owns( $order ) {
$user = get_current_user_id();
return $user > 0 && $order instanceof WC_Order && (int) $order->get_customer_id() === $user;
}
/**
* Whether the current request may see document links for this order.
*
* @param WC_Order $order Order.
* @return bool
*/
public static function can_view( $order ) {
if ( ! $order instanceof WC_Order ) {
return false;
}
if ( current_user_can( 'edit_shop_orders' ) || self::owns( $order ) ) {
return true;
}
return is_order_received_page() && VPRWC_Admin::valid_order_key( $order );
}
/**
* Download URL for the current customer, including the order key for guests.
*
* @param WC_Order $order Order.
* @param string $document receipt|invoice.
* @return string
*/
public static function download_url( $order, $document = VPRWC_Receipt::DOCUMENT_RECEIPT ) {
$url = VPRWC_Admin::download_url( $order, VPRWC_Receipt::VARIANT_CUSTOMER, $document );
if ( ! self::owns( $order ) && ! current_user_can( 'edit_shop_orders' ) ) {
$url = add_query_arg( 'key', $order->get_order_key(), $url );
}
return $url;
}
/**
* Add the action to the orders table.
*
* @param array $actions Actions.
* @param WC_Order $order Order.
* @return array
*/
public static function orders_action( $actions, $order ) {
if ( self::owns( $order ) ) {
$actions['vprwc_invoice'] = array(
'url' => self::download_url( $order, VPRWC_Receipt::DOCUMENT_INVOICE ),
'name' => __( 'Invoice', 'acrislip-order-receipts' ),
);
if ( VPRWC_Receipt::is_paid( $order ) ) {
$actions['vprwc_receipt'] = array(
'url' => self::download_url( $order ),
'name' => __( 'Receipt', 'acrislip-order-receipts' ),
);
}
}
return $actions;
}
/**
* References and download buttons on My Account > View order and the thank-you page.
*
* @param WC_Order $order Order.
*/
public static function documents_box( $order ) {
if ( ! $order instanceof WC_Order ) {
return;
}
$on_account = function_exists( 'is_account_page' ) && is_account_page();
$on_thanks = function_exists( 'is_order_received_page' ) && is_order_received_page();
if ( ( ! $on_account && ! $on_thanks ) || ! self::can_view( $order ) ) {
return;
}
self::enqueue_assets();
self::render_documents_box( $order );
}
/**
* Render the customer-facing references and download buttons.
*
* @param WC_Order $order Order.
*/
public static function render_documents_box( $order ) {
$refs = VPRWC_Receipt::references( $order );
$paid = VPRWC_Receipt::is_paid( $order );
echo ' ';
echo '' . esc_html__( 'Download invoice', 'acrislip-order-receipts' ) . '';
if ( $paid ) {
echo ' ' . esc_html__( 'Download receipt', 'acrislip-order-receipts' ) . '';
}
echo '' . esc_html__( 'Payment documents', 'acrislip-order-receipts' ) . '
';
echo '
' . esc_html( $value ) . '' . VPRWC_Payment_Box::copy_button( $value, sprintf( /* translators: %s: field label */ __( 'Copy %s', 'acrislip-order-receipts' ), $label ) ) . '