PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.2
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.2
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / Templates / Received.php

Received.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.2, at includes/Templates/Received.php

105 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Received order template.
4 *
5 * @author Paul Kilmurray <paul@kilbot.com>
6 *
7 * @see http://wcpos.com
8 * @package WooCommercePOS\Templates
9 */
10
11 namespace WCPOS\WooCommercePOS\Templates;
12
13 use Exception;
14 use WP_REST_Request;
15
16 /**
17 * Received class.
18 */
19 class Received {
20 /**
21 * Order ID.
22 *
23 * @var int
24 */
25 private $order_id;
26
27 /**
28 * Constructor.
29 *
30 * @param int $order_id The order ID.
31 */
32 public function __construct( int $order_id ) {
33 $this->order_id = $order_id;
34
35 add_filter( 'show_admin_bar', '__return_false' );
36 }
37
38 /**
39 * Fetch the order JSON via an internal REST request to the WCPOS endpoint.
40 *
41 * @param int $order_id The order ID.
42 *
43 * @return string|false JSON string or false on failure.
44 */
45 public function get_order_json( int $order_id ) {
46 // Grant POS access for this internal request so it passes both the
47 // WC REST permission check and the POS access_woocommerce_pos gate.
48 $grant_caps = function ( $allcaps ) {
49 $allcaps['access_woocommerce_pos'] = true;
50 return $allcaps;
51 };
52 add_filter( 'user_has_cap', $grant_caps );
53 add_filter( 'woocommerce_rest_check_permissions', '__return_true' );
54
55 try {
56 $request = new WP_REST_Request( 'GET', '/wcpos/v1/orders/' . $order_id );
57 $server = rest_get_server();
58 $response = $server->dispatch( $request );
59 $data = $server->response_to_data( $response, true );
60 } finally {
61 remove_filter( 'user_has_cap', $grant_caps );
62 remove_filter( 'woocommerce_rest_check_permissions', '__return_true' );
63 }
64
65 return wp_json_encode( $data );
66 }
67
68 /**
69 * Get and display the received template.
70 */
71 public function get_template(): void {
72 try {
73 $order = \wc_get_order( $this->order_id );
74
75 if ( ! $order ) {
76 wp_die( esc_html__( 'Sorry, this order is invalid.', 'woocommerce-pos' ) );
77 }
78
79 // Verify order key to prevent unauthenticated access.
80 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Order key is the auth mechanism here, matching WooCommerce core behavior.
81 $provided_key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : '';
82 if ( ! $provided_key || $provided_key !== $order->get_order_key() ) {
83 wp_die(
84 esc_html__( 'Sorry, this order cannot be viewed. The order key is missing or invalid.', 'woocommerce-pos' ),
85 /* translators: Short WCPOS UI label; keep concise. */
86 esc_html__( 'Error', 'woocommerce-pos' ),
87 array( 'response' => 403 )
88 );
89 }
90
91 $order_json = $this->get_order_json( $order->get_id() );
92 $payment_method = $order->get_payment_method();
93 $gateway_settings = woocommerce_pos_get_settings( 'payment_gateways' );
94 $status_setting = $gateway_settings['gateways'][ $payment_method ]['order_status'] ?? 'wc-completed';
95 $completed_status = 'wc-' === substr( $status_setting, 0, 3 ) ? substr( $status_setting, 3 ) : $status_setting;
96 $order_complete = 'pos-open' !== $completed_status;
97
98 include woocommerce_pos_locate_template( 'received.php' );
99 exit;
100 } catch ( Exception $e ) {
101 \wc_print_notice( $e->getMessage(), 'error' );
102 }
103 }
104 }
105