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 / BulkActions.php

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

217 lines 7.1 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 Sendy\WooCommerce\Plugin;
6 use Sendy\WooCommerce\Repositories\Preferences;
7 use Sendy\WooCommerce\Repositories\Shops;
8 use Sendy\WooCommerce\Utils\View;
9
10 class BulkActions extends OrdersModule
11 {
12 public function __construct()
13 {
14 add_filter('bulk_actions-woocommerce_page_wc-orders', [$this, 'add_bulk_actions'], 10, 1);
15 add_filter('handle_bulk_actions-woocommerce_page_wc-orders', [$this, 'handle_bulk_action_create_shipments'], 10, 3);
16 add_filter('handle_bulk_actions-woocommerce_page_wc-orders', [$this, 'handle_bulk_action_print_labels'], 10, 3);
17
18 add_filter('bulk_actions-edit-shop_order', [$this, 'add_bulk_actions'], 10, 1);
19 add_filter('handle_bulk_actions-edit-shop_order', [$this, 'handle_bulk_action_create_shipments'], 10, 3);
20 add_filter('handle_bulk_actions-edit-shop_order', [$this, 'handle_bulk_action_print_labels'], 10, 3);
21
22 add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
23
24 add_action('admin_footer', [$this, 'modal_create_shipments']);
25 }
26
27 /**
28 * Add the bulk actions to the dropdown menu
29 *
30 * @param array<string,string> $bulkActions
31 * @return array<string,string>
32 */
33 public function add_bulk_actions(array $bulkActions): array
34 {
35 $bulkActions['sendy_create_shipments'] = esc_html__('Sendy - Create shipments', 'sendy');
36 $bulkActions['sendy_print_labels'] = esc_html__('Sendy - Print labels', 'sendy');
37
38 return $bulkActions;
39 }
40
41 /**
42 * Create the shipments with the selected preference
43 *
44 * @param string $redirect
45 * @param string $action
46 * @param array $objectIds
47 * @return string
48 */
49 public function handle_bulk_action_create_shipments(string $redirect, string $action, array $objectIds): string
50 {
51 if ($action !== 'sendy_create_shipments') {
52 return $redirect;
53 }
54
55 if (!isset($_REQUEST['sendy_bulk_modal_nonce']) || ! wp_verify_nonce(sanitize_key($_REQUEST['sendy_bulk_modal_nonce']), 'sendy_bulk_modal')) {
56 wp_die('Nonce verification failed');
57 }
58
59 foreach ($objectIds as $id) {
60 $this->create_shipment_from_order(
61 wc_get_order($id),
62 sanitize_key($_REQUEST['sendy_preference_id'] ?? ''),
63 sanitize_key($_REQUEST['sendy_shop_id'] ?? ''),
64 sanitize_key($_REQUEST['sendy_amount'] ?? '')
65 );
66 }
67
68 update_option('sendy_previously_used_preference_id', sanitize_key($_REQUEST['sendy_preference_id'] ?? ''));
69 update_option('sendy_previously_used_shop_id', sanitize_key($_REQUEST['sendy_shop_id'] ?? ''));
70 update_option('sendy_previously_used_amount', sanitize_key($_REQUEST['sendy_amount'] ?? ''));
71
72 return $redirect;
73 }
74
75 /**
76 * Handle the print labels bulk action
77 *
78 * @param string $redirect
79 * @param string $action
80 * @param array $objectIds
81 * @return string|void
82 */
83 public function handle_bulk_action_print_labels(string $redirect, string $action, array $objectIds)
84 {
85 if ($action !== 'sendy_print_labels' || empty($objectIds)) {
86 return $redirect;
87 }
88
89 $shipmentIds = [];
90
91 foreach ($objectIds as $objectId) {
92 $order = wc_get_order($objectId);
93
94 if ($order->meta_exists('_sendy_shipment_id')) {
95 $shipmentIds[] = $order->get_meta('_sendy_shipment_id');
96
97 if (get_option('sendy_mark_order_as_completed') === 'after-label-printed') {
98 $order->set_status('completed', __('Sendy: Label printed', 'sendy'));
99 $order->save();
100 }
101 }
102 }
103
104 if (count($shipmentIds) == 0) {
105 sendy_flash_admin_notice('notice', __('Non of the selected orders have any labels', 'sendy'));
106
107 return $redirect;
108 }
109
110 $this->offer_labels_as_download($shipmentIds);
111 }
112
113 /**
114 * Load the assets when needed
115 *
116 * @return void
117 */
118 public function enqueue_assets(): void
119 {
120 if ($this->on_orders_list_page()) {
121 wp_enqueue_style('thickbox');
122 wp_enqueue_script('thickbox');
123
124 wp_enqueue_script(
125 'sendy-admin-order-bulk',
126 SENDY_WC_PLUGIN_DIR_URL . '/resources/js/admin-order-bulk.js',
127 [],
128 Plugin::VERSION,
129 true
130 );
131
132 wp_enqueue_style(
133 'sendy-admin-order-bulk',
134 SENDY_WC_PLUGIN_DIR_URL . '/resources/css/modal.css',
135 [],
136 Plugin::VERSION,
137 );
138 }
139 }
140
141 /**
142 * Add a modal to the overview page
143 *
144 * @return void
145 * @throws \GuzzleHttp\Exception\GuzzleException
146 * @throws \Sendy\Api\ApiException
147 */
148 public function modal_create_shipments(): void
149 {
150 if ($this->on_orders_list_page()) {
151 $preferences = (new Preferences())->get();
152 $shops = (new Shops())->list();
153
154 echo wp_kses(
155 View::fromTemplate('admin/modals/create-shipment.php')->render([
156 'fields' => $this->create_shipment_fields($shops, $preferences)
157 ]),
158 View::ALLOWED_TAGS
159 );
160 }
161 }
162
163 /**
164 * Return the fields needed for the modal to create a shipment
165 *
166 * @param array $shops
167 * @param array $preferences
168 * @return array<int,array<string,mixed>>
169 */
170 private function create_shipment_fields(array $shops, array $preferences): array
171 {
172 $fields = [];
173
174 $fields[] = [
175 'id' => 'sendy_shop_id',
176 'type' => 'select',
177 'label' => __('Shop', 'sendy'),
178 'description' => __('The shipments will be created with the selected shop', 'sendy'),
179 'value' => get_option('sendy_previously_used_shop_id'),
180 'options' => $shops,
181 ];
182
183 $fields[] = [
184 'id' => 'sendy_preference_id',
185 'type' => 'select',
186 'label' => __('Select preference', 'sendy'),
187 'description' => __('The shipments will be created with the preference you select here', 'sendy'),
188 'value' => get_option('sendy_previously_used_preference_id'),
189 'options' => $preferences,
190 ];
191
192 $fields[] = [
193 'id' => 'sendy_amount',
194 'type' => 'number',
195 'label' => __('Amount of packages', 'sendy'),
196 'value' => get_option('sendy_previously_used_amount', 1),
197 ];
198
199 return $fields;
200 }
201
202 /**
203 * Determine whether the user is on an orders overview page
204 *
205 * @return bool
206 */
207 private function on_orders_list_page(): bool
208 {
209 $screen = get_current_screen();
210
211 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
212 return ($screen->id === 'woocommerce_page_wc-orders' && !isset($_GET['action'])) ||
213 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
214 ($screen->id === 'edit-shop_order' && $screen->base === 'edit' && !isset($_GET['action']));
215 }
216 }
217