PluginProbe
Sendy / 3.4.5
Sendy v3.4.5
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 / lib / Modules / Orders / Single.php

Single.php in Sendy 3.4.5, at lib/Modules/Orders/Single.php

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