'vprwc_download', 'order_id' => $order->get_id(), 'variant' => $variant, 'document' => $document, ), admin_url( 'admin-post.php' ) ), 'vprwc_download_' . $order->get_id() . '_' . $variant . '_' . $document ); } /** * Signed email URL. * * @param WC_Order $order Order. * @param string $document receipt|invoice. * @return string */ public static function email_url( $order, $document = VPRWC_Receipt::DOCUMENT_RECEIPT ) { $document = VPRWC_Receipt::DOCUMENT_INVOICE === $document ? VPRWC_Receipt::DOCUMENT_INVOICE : VPRWC_Receipt::DOCUMENT_RECEIPT; return wp_nonce_url( add_query_arg( array( 'action' => 'vprwc_email', 'order_id' => $order->get_id(), 'document' => $document, ), admin_url( 'admin-post.php' ) ), 'vprwc_email_' . $order->get_id() . '_' . $document ); } /** * Whether the current user may download this variant of this order's document. * * @param WC_Order $order Order. * @param string $variant Variant. * @param string $document receipt|invoice. * @return bool */ public static function can_download( $order, $variant, $document = VPRWC_Receipt::DOCUMENT_RECEIPT ) { if ( VPRWC_Receipt::VARIANT_COMPLIANCE === $variant && ! apply_filters( 'vprwc_allow_compliance_copy', false, $order ) ) { return false; } if ( current_user_can( 'edit_shop_orders' ) ) { return true; } if ( VPRWC_Receipt::VARIANT_CUSTOMER !== $variant ) { return false; } $allowed = false; $user = get_current_user_id(); if ( $user > 0 && (int) $order->get_customer_id() === $user ) { $allowed = true; } elseif ( self::valid_order_key( $order ) ) { $allowed = true; } if ( ! $allowed ) { return false; } if ( VPRWC_Receipt::DOCUMENT_INVOICE === $document ) { return true; } return VPRWC_Receipt::is_paid( $order ); } /** * Whether the request carries this order's key (guest thank-you / order-received). * * @param WC_Order $order Order. * @return bool */ public static function valid_order_key( $order ) { if ( ! $order instanceof WC_Order || empty( $_GET['key'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Order key is the capability secret. return false; } $key = wc_clean( wp_unslash( $_GET['key'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized return is_string( $key ) && '' !== $key && hash_equals( (string) $order->get_order_key(), $key ); } /** * Stream a receipt or invoice PDF. */ public static function handle_download() { $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified below. $requested = ( isset( $_GET['variant'] ) && VPRWC_Receipt::VARIANT_COMPLIANCE === sanitize_key( wp_unslash( $_GET['variant'] ) ) ) ? VPRWC_Receipt::VARIANT_COMPLIANCE : VPRWC_Receipt::VARIANT_CUSTOMER; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $variant = apply_filters( 'vprwc_requested_receipt_variant', $requested, isset( $order_id ) ? $order_id : 0 ); $document = ( isset( $_GET['document'] ) && VPRWC_Receipt::DOCUMENT_INVOICE === sanitize_key( wp_unslash( $_GET['document'] ) ) ) ? VPRWC_Receipt::DOCUMENT_INVOICE : VPRWC_Receipt::DOCUMENT_RECEIPT; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( VPRWC_Receipt::DOCUMENT_INVOICE === $document ) { $variant = VPRWC_Receipt::VARIANT_CUSTOMER; } check_admin_referer( 'vprwc_download_' . $order_id . '_' . $variant . '_' . $document ); $order = wc_get_order( $order_id ); if ( ! $order instanceof WC_Order || ! self::can_download( $order, $variant, $document ) ) { wp_die( esc_html__( 'You do not have permission to download this document.', 'acrislip-order-receipts' ), '', array( 'response' => 403 ) ); } if ( VPRWC_Receipt::DOCUMENT_RECEIPT === $document && ! VPRWC_Receipt::is_paid( $order ) ) { wp_die( esc_html__( 'Payment not confirmed by the gateway', 'acrislip-order-receipts' ), '', array( 'response' => 400 ) ); } try { $receipt = VPRWC_Receipt::build( $order, $variant, $document ); $bytes = VPRWC_Renderer::pdf( $receipt ); } catch ( \Throwable $e ) { wc_get_logger()->error( 'PDF failed for order ' . $order_id . ': ' . $e->getMessage(), array( 'source' => 'acrislip-order-receipts' ) ); wp_die( esc_html__( 'The document could not be created. Please try again or contact the shop.', 'acrislip-order-receipts' ), '', array( 'response' => 500 ) ); } nocache_headers(); header( 'Content-Type: application/pdf' ); header( 'Content-Disposition: attachment; filename="' . VPRWC_Renderer::filename( $receipt ) . '"' ); header( 'Content-Length: ' . strlen( $bytes ) ); header( 'X-Content-Type-Options: nosniff' ); echo $bytes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Binary PDF. exit; } /** * Email the receipt or invoice from the meta box button. */ public static function handle_email() { $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified below. $document = ( isset( $_GET['document'] ) && VPRWC_Receipt::DOCUMENT_INVOICE === sanitize_key( wp_unslash( $_GET['document'] ) ) ) ? VPRWC_Receipt::DOCUMENT_INVOICE : VPRWC_Receipt::DOCUMENT_RECEIPT; // phpcs:ignore WordPress.Security.NonceVerification.Recommended check_admin_referer( 'vprwc_email_' . $order_id . '_' . $document ); if ( ! current_user_can( 'edit_shop_orders' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'acrislip-order-receipts' ), '', array( 'response' => 403 ) ); } $order = wc_get_order( $order_id ); if ( VPRWC_Receipt::DOCUMENT_INVOICE === $document ) { $ok = $order instanceof WC_Order && VPRWC_Emails::send_invoice( $order ); $arg = 'vprwc_invoice_sent'; } else { $ok = $order instanceof WC_Order && VPRWC_Emails::send( $order ); $arg = 'vprwc_sent'; } $back = wp_get_referer(); $back = $back ? $back : ( $order ? $order->get_edit_order_url() : admin_url() ); wp_safe_redirect( add_query_arg( $arg, $ok ? '1' : '0', $back ) ); exit; } /** * Notice after emailing. Only on order screens, and only after the * corresponding email action redirected back with a query flag. */ public static function notices() { if ( ! current_user_can( 'edit_shop_orders' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $screens = array( 'edit-shop_order', 'shop_order', 'woocommerce_page_wc-orders' ); if ( function_exists( 'wc_get_page_screen_id' ) ) { $screens[] = wc_get_page_screen_id( 'shop-order' ); } if ( ! $screen || ! in_array( $screen->id, array_unique( $screens ), true ) ) { return; } if ( isset( $_GET['vprwc_sent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. $ok = '1' === sanitize_key( wp_unslash( $_GET['vprwc_sent'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended printf( '

%2$s

', esc_attr( $ok ? 'success' : 'error' ), esc_html( $ok ? __( 'Payment receipt emailed to the customer.', 'acrislip-order-receipts' ) : __( 'The receipt could not be emailed. Check that the order is paid and has a valid billing email.', 'acrislip-order-receipts' ) ) ); } if ( isset( $_GET['vprwc_invoice_sent'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. $ok = '1' === sanitize_key( wp_unslash( $_GET['vprwc_invoice_sent'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended printf( '

%2$s

', esc_attr( $ok ? 'success' : 'error' ), esc_html( $ok ? __( 'Invoice emailed to the customer.', 'acrislip-order-receipts' ) : __( 'The invoice could not be emailed. Check that the order has a valid billing email.', 'acrislip-order-receipts' ) ) ); } } /** * Register the meta box on both the HPOS and legacy order screens. */ public static function add_meta_box() { $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-receipt', __( 'Acrislip Order Receipts', 'acrislip-order-receipts' ), array( __CLASS__, 'render_meta_box' ), $screen, 'side', 'default' ); } } /** * Meta box body. * * @param WP_Post|WC_Order $post_or_order Post or order. */ public static function render_meta_box( $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; } $invoice_no = VPRWC_Numbering::get_invoice( $order ); if ( $invoice_no ) { /* translators: %s: invoice number */ echo '

' . esc_html( sprintf( __( 'Invoice number: %s', 'acrislip-order-receipts' ), $invoice_no ) ) . '

'; } echo '

' . esc_html__( 'Download invoice (PDF)', 'acrislip-order-receipts' ) . '

'; echo '

' . esc_html__( 'Email invoice to customer', 'acrislip-order-receipts' ) . '

'; if ( ! VPRWC_Receipt::is_paid( $order ) ) { $reserved = VPRWC_Numbering::get( $order ); if ( $reserved ) { echo '

' . esc_html( sprintf( /* translators: %s: reserved receipt number */ __( 'Receipt number %s is reserved, but the receipt is not available to download.', 'acrislip-order-receipts' ), $reserved ) ) . '

'; } echo '

' . esc_html__( 'Payment not confirmed by the gateway. A receipt is issued only after the payment provider confirms the payment. Changing the order status by hand is not enough for this payment method.', 'acrislip-order-receipts' ) . '

'; return; } $number = VPRWC_Numbering::get( $order ); if ( $number ) { /* translators: %s: receipt number */ echo '

' . esc_html( sprintf( __( 'Receipt number: %s', 'acrislip-order-receipts' ), $number ) ) . '

'; } echo '

' . esc_html__( 'Download receipt (PDF)', 'acrislip-order-receipts' ) . '

'; echo '

' . esc_html__( 'Email receipt to customer', 'acrislip-order-receipts' ) . '

'; /** * Extra document actions on the order screen, such as a compliance copy. * * @param WC_Order $order Order. */ do_action( 'vprwc_admin_document_actions', $order ); } /** * Orders list row action. * * @param array $actions Actions. * @param WC_Order $order Order. * @return array */ public static function row_action( $actions, $order ) { if ( $order instanceof WC_Order && current_user_can( 'edit_shop_orders' ) ) { $actions['vprwc_invoice'] = array( 'url' => self::download_url( $order, VPRWC_Receipt::VARIANT_CUSTOMER, VPRWC_Receipt::DOCUMENT_INVOICE ), 'name' => __( 'Invoice', 'acrislip-order-receipts' ), 'action' => 'vprwc_invoice', ); if ( VPRWC_Receipt::is_paid( $order ) ) { $actions['vprwc_receipt'] = array( 'url' => self::download_url( $order ), 'name' => __( 'Payment receipt', 'acrislip-order-receipts' ), 'action' => 'vprwc_receipt', ); } } return $actions; } /** * Entry in the order's "Order actions" dropdown. * * @param array $actions Actions. * @param WC_Order|null $order Order (WooCommerce 8.1+). * @return array */ public static function order_actions( $actions, $order = null ) { $actions['vprwc_send_invoice'] = __( 'Email invoice to customer', 'acrislip-order-receipts' ); if ( ! $order instanceof WC_Order || VPRWC_Receipt::is_paid( $order ) ) { $actions['vprwc_send_receipt'] = __( 'Email payment receipt to customer', 'acrislip-order-receipts' ); } return $actions; } /** * Run the dropdown action. * * @param WC_Order $order Order. */ public static function order_action_send( $order ) { if ( current_user_can( 'edit_shop_orders' ) && ! VPRWC_Emails::send( $order ) ) { $order->add_order_note( __( 'Payment receipt was not emailed: the order is not paid or has no valid billing email.', 'acrislip-order-receipts' ) ); } } /** * Run the email invoice dropdown action. * * @param WC_Order $order Order. */ public static function order_action_send_invoice( $order ) { if ( current_user_can( 'edit_shop_orders' ) && ! VPRWC_Emails::send_invoice( $order ) ) { $order->add_order_note( __( 'Invoice was not emailed: the order has no valid billing email.', 'acrislip-order-receipts' ) ); } } /** * Icon for the orders list row actions, on the legacy and HPOS order lists only. * * @param string $hook_suffix Current admin page. */ public static function enqueue_order_action_styles( $hook_suffix ) { unset( $hook_suffix ); $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, array( 'edit-shop_order', 'woocommerce_page_wc-orders' ), true ) ) { return; } wp_register_style( 'vprwc-order-actions', false, array(), VPRWC_VERSION ); wp_enqueue_style( 'vprwc-order-actions' ); wp_add_inline_style( 'vprwc-order-actions', '.wc-action-button-vprwc_receipt::after{font-family:dashicons!important;content:"\f495"!important;}.wc-action-button-vprwc_invoice::after{font-family:dashicons!important;content:"\f498"!important;}' ); } }