PluginProbe
Sendy / 3.0.3
Sendy v3.0.3
3.4.6 3.4.7 trunk 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 32 releases
sendy / src / Modules / Orders / Single.php

Single.php in Sendy 3.0.3, at src/Modules/Orders/Single.php

207 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sendy\WooCommerce\Modules\Orders;
4
5 use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
6 use Sendy\WooCommerce\Plugin;
7 use Sendy\WooCommerce\Repositories\Preferences;
8 use Sendy\WooCommerce\Repositories\Shops;
9 use Sendy\WooCommerce\Utils\View;
10 use WC_Order;
11 use WP_Post;
12
13 class Single extends OrdersModule
14 {
15 public function __construct()
16 {
17 add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
18 add_action('add_meta_boxes', [$this, 'add_meta_box'], 20, 2);
19
20 add_action('wp_ajax_sendy_order_single_save_form', [$this, 'handle_create_shipment_from_form']);
21
22 add_action('woocommerce_admin_order_data_after_shipping_address', [$this, 'display_shipping_data'], 10, 1);
23
24 add_action('admin_init', [$this, 'download_label'], 10);
25 }
26
27 /**
28 * Add a meta box to the order page
29 *
30 * @param string $postType
31 * @param \WP_Post|WC_Order $postOrOrderObject
32 * @return void
33 */
34 public function add_meta_box(string $postType, $postOrOrderObject): void
35 {
36 if (! $this->init_order_object($postOrOrderObject)) {
37 return;
38 }
39
40 try {
41 $screen = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
42 ? wc_get_page_screen_id( 'shop-order' )
43 : 'shop_order';
44 } catch ( \Exception $e ) {
45 $screen = 'shop_order';
46 }
47
48 add_meta_box(
49 'woocommerce-sendy-shipment',
50 esc_html__('Sendy', 'sendy'),
51 [$this, 'meta_box_html'],
52 $screen,
53 'side',
54 'high',
55 );
56 }
57
58 /**
59 * Render the content of the meta box
60 *
61 * @param WP_Post|WC_Order $postOrOrderObject
62 * @return void
63 */
64 public function meta_box_html($postOrOrderObject): void
65 {
66 if (!$order = $this->init_order_object($postOrOrderObject)) {
67 return;
68 }
69
70 $preferences = (new Preferences())->get();
71 $shops = (new Shops())->list();
72
73 echo wp_kses(
74 View::fromTemplate('admin/meta_box/single.php')->render([
75 'order' => $order,
76 'preferences' => $preferences,
77 'shops' => $shops,
78 ]),
79 View::ALLOWED_TAGS
80 );
81 }
82
83 /**
84 * Load the assets if needed
85 *
86 * @return void
87 */
88 public function enqueue_assets(): void
89 {
90 if ($this->on_order_edit_page()) {
91 wp_enqueue_script(
92 'sendy-admin-order-single',
93 SENDY_WC_PLUGIN_DIR_URL . '/resources/js/admin-order-single.js',
94 [],
95 Plugin::VERSION,
96 true
97 );
98 }
99 }
100
101 /**
102 * Handle the submission of the form to create a shipment from the order page
103 *
104 * @return void
105 * @throws \GuzzleHttp\Exception\GuzzleException
106 */
107 public function handle_create_shipment_from_form(): void
108 {
109 try {
110 if (!isset($_REQUEST['nonce']) || !check_ajax_referer('sendy_create_shipment', 'nonce')) {
111 throw new \Exception( esc_html__( 'Nonce verification failed', 'sendy' ) );
112 }
113
114 if (!empty($_REQUEST['order_id'])) {
115 $order = wc_get_order(sanitize_key($_REQUEST['order_id']));
116
117 $this->create_shipment_from_order(
118 $order,
119 sanitize_key($_REQUEST['preference_id'] ?? ''),
120 sanitize_key($_REQUEST['shop_id'] ?? ''),
121 sanitize_key($_REQUEST['amount'] ?? '')
122 );
123
124 wp_send_json_success();
125 }
126 } catch (\Exception $e) {
127 wp_send_json_error(['message' => $e->getMessage()]);
128 }
129 }
130
131 /**
132 * Display the shipping method and chosen pick-up point on the detail page for admins
133 *
134 * @param WC_Order $order
135 * @return void
136 */
137 public function display_shipping_data(WC_Order $order): void
138 {
139 echo wp_kses(
140 View::fromTemplate('admin/single/shipping_data.php')->render(['order' => $order]),
141 View::ALLOWED_TAGS
142 );
143 }
144
145 /**
146 * Offer the label as a download to the user
147 *
148 * @return void
149 * @throws \GuzzleHttp\Exception\GuzzleException
150 */
151 public function download_label(): void
152 {
153 if (empty($_GET['sendy_download_label_nonce'])) {
154 return;
155 }
156
157 if (empty($_GET['sendy_action'])) {
158 return;
159 }
160
161 if (!wp_verify_nonce(sanitize_key($_REQUEST['sendy_download_label_nonce'] ?? ''), 'sendy_download_label')) {
162 wp_die(esc_html__('Nonce verification failed', 'sendy'));
163 }
164
165 if (sanitize_key($_REQUEST['sendy_action'] ?? '') === 'download_label') {
166 $order = wc_get_order(sanitize_key($_REQUEST['order_id'] ?? ''));
167
168 if (!$order) {
169 wp_die(esc_html__('Order could not be found', 'sendy'));
170 }
171
172 if (current_user_can('manage_woocommerce') || current_user_can('edit_shop_orders')) {
173 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'sendy'));
174 }
175
176 if (!$order->meta_exists('_sendy_shipment_id')) {
177 wp_die(esc_html__('No shipment created for order', 'sendy'));
178 }
179
180 if (get_option('sendy_mark_order_as_completed') === 'after-label-printed') {
181 $order->set_status('completed', __('Sendy: Label printed', 'sendy'));
182 $order->save();
183 }
184
185 $this->offer_labels_as_download([$order->get_meta('_sendy_shipment_id')]);
186 }
187 }
188
189 /**
190 * Determine if the user is on an order page
191 *
192 * @return bool
193 */
194 private function on_order_edit_page(): bool
195 {
196 $screen = get_current_screen();
197
198 return ($screen->id === 'woocommerce_page_wc-orders' &&
199 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
200 isset($_GET['action']) &&
201 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
202 sanitize_key($_GET['action']) === 'edit') ||
203 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
204 ($screen->id === 'shop_order' && $screen->base === 'post' && sanitize_key($_GET['action']) === 'edit');
205 }
206 }
207