PluginProbe
Sendy / 3.4.7
Sendy v3.4.7
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 / Admin / Settings.php

Settings.php in Sendy 3.4.7, at lib/Modules/Admin/Settings.php

272 lines 8.8 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\Admin;
4
5 use Sendy\Api\ApiException;
6 use Sendy\WooCommerce\ApiClientFactory;
7 use Sendy\WooCommerce\Enums\ProcessingMethod;
8 use Sendy\WooCommerce\Modules\Admin\Fields\Checkbox;
9 use Sendy\WooCommerce\Modules\Admin\Fields\Dropdown;
10 use Sendy\WooCommerce\Plugin;
11 use Sendy\WooCommerce\Repositories\Shops;
12 use Sendy\WooCommerce\Utils\View;
13
14 class Settings
15 {
16 public function __construct()
17 {
18 if (! is_admin()) {
19 return;
20 }
21
22 add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
23
24 add_action('admin_menu', [$this, 'register_menu']);
25 add_action('admin_init', [$this, 'register_settings']);
26 add_action('admin_init', [$this, 'logout_action']);
27 }
28
29 public function enqueue_assets(): void
30 {
31 wp_enqueue_script(
32 'sendy-admin-settings',
33 SENDY_WC_PLUGIN_DIR_URL . '/resources/js/admin-settings.js',
34 [],
35 Plugin::VERSION,
36 true,
37 );
38 }
39
40 /**
41 * Add the settings page for the module as a submenu item for the WooCommerce menu
42 */
43 public function register_menu(): void
44 {
45 add_submenu_page(
46 'woocommerce',
47 __('Sendy', 'sendy'),
48 __('Sendy', 'sendy'),
49 'manage_woocommerce',
50 'sendy',
51 [$this, 'render_settings_page'],
52 );
53 }
54
55 /**
56 * Render the settings page
57 */
58 public function render_settings_page(): void
59 {
60 $name = null;
61
62 if (sendy_is_authenticated()) {
63 try {
64 $result = ApiClientFactory::buildConnectionUsingTokens()->me->get();
65
66 $name = $result['name'];
67 } catch (ApiException $e) {
68 // When this action fails, it will be because the access token is invalid. The best option is to null
69 // the access token option and restart the authentication process from the start. Only resetting the
70 // access token is sufficient as it will not generate multiple instances of the WooCommerce integration
71 // in the Sendy application
72 update_option('sendy_access_token', null, false);
73 }
74 }
75
76 echo wp_kses(
77 View::fromTemplate('admin/settings.php')->render(['name' => $name]),
78 View::ALLOWED_TAGS,
79 );
80 }
81
82 public function register_settings(): void
83 {
84 $slug = 'sendy';
85
86 // Save with the same capability the page is shown with; options.php requires 'manage_options' otherwise.
87 add_filter('option_page_capability_sendy_general_settings', fn() => 'manage_woocommerce');
88
89 add_settings_section('sendy_section_id', '', '', $slug);
90
91 register_setting('sendy_general_settings', 'sendy_import_weight', fn($value) => $value === 'true');
92 register_setting('sendy_general_settings', 'sendy_import_products', fn($value) => $value === 'true');
93
94 register_setting('sendy_general_settings', 'sendy_mark_order_as_completed', [
95 'sanitize_callback' => function ($value): string {
96 $possibleValues = [
97 'manually',
98 'after-shipment-created',
99 'after-label-printed',
100 'after-shipment-delivered',
101 ];
102
103 if (! in_array($value, $possibleValues)) {
104 return 'manually';
105 }
106
107 return $value;
108 },
109 ]);
110
111 register_setting('sendy_general_settings', 'sendy_processing_method', [
112 'sanitize_callback' => function ($value): string {
113 if (! in_array($value, ProcessingMethod::cases())) {
114 return ProcessingMethod::WooCommerce;
115 }
116
117 return $value;
118 },
119 ]);
120
121 register_setting('sendy_general_settings', 'sendy_processable_order_status', [
122 'sanitize_callback' => function ($value): string {
123 $orderStatuses = wc_get_order_statuses();
124
125 $possibleValues = [];
126
127 foreach ($orderStatuses as $status => $label) {
128 // The 'wc-' prefix is stripped as it is intended only for WooCommerce internally.
129 $possibleValues[] = str_replace('wc-', '', $status);
130 }
131
132 if (! in_array($value, $possibleValues)) {
133 return $possibleValues[0];
134 }
135
136 return $value;
137 },
138 ]);
139
140 register_setting('sendy_general_settings', 'sendy_default_shop', fn($value) => $value);
141
142 add_settings_field(
143 'sendy_processing_method',
144 __('Processing method', 'sendy'),
145 [$this, 'render_processing_method_dropdown'],
146 $slug,
147 'sendy_section_id',
148 );
149
150 $hidden = get_option('sendy_processing_method') !== ProcessingMethod::Sendy ? 'hidden' : '';
151
152 add_settings_field(
153 'sendy_processable_order_status',
154 __('Send order to Sendy when status changed to', 'sendy'),
155 [$this, 'render_processable_order_status_dropdown'],
156 $slug,
157 'sendy_section_id',
158 ['class' => "sendy-processing-method-field {$hidden}"],
159 );
160
161 add_settings_field(
162 'sendy_default_shop',
163 __('Shop to use when orders are sent to Sendy', 'sendy'),
164 [$this, 'render_default_shop_dropdown'],
165 $slug,
166 'sendy_section_id',
167 ['class' => "sendy-processing-method-field {$hidden}"],
168 );
169
170 add_settings_field(
171 'sendy_import_weight',
172 __('Import weight', 'sendy'),
173 [$this, 'render_import_weight_field'],
174 $slug,
175 'sendy_section_id',
176 );
177
178 add_settings_field(
179 'sendy_import_products',
180 __('Import products', 'sendy'),
181 [$this, 'render_import_products_field'],
182 $slug,
183 'sendy_section_id',
184 );
185
186 add_settings_field(
187 'sendy_mark_order_as_completed',
188 __('Mark order as completed', 'sendy'),
189 [$this, 'render_status_after_shipping_field'],
190 $slug,
191 'sendy_section_id',
192 );
193 }
194
195 public function render_import_weight_field(): void
196 {
197 (new Checkbox('sendy_import_weight', __('Import weight when creating a shipment', 'sendy')))->render();
198 }
199
200 public function render_import_products_field(): void
201 {
202 (new Checkbox('sendy_import_products', __('Send products to Sendy when creating a shipment', 'sendy')))->render();
203 }
204
205 public function render_status_after_shipping_field(): void
206 {
207 $options = [
208 'manually' => __('Manually', 'sendy'),
209 'after-shipment-created' => __('After the shipment is created', 'sendy'),
210 ];
211
212 if (get_option('sendy_processing_method') === ProcessingMethod::WooCommerce) {
213 $options['after-label-printed'] = __('After the label is printed', 'sendy');
214 }
215
216 if (get_option('sendy_processing_method') === ProcessingMethod::Sendy) {
217 $options['after-shipment-delivered'] = __('After the shipment is delivered', 'sendy');
218 }
219
220 (new Dropdown('sendy_mark_order_as_completed'))->render(['options' => $options]);
221 }
222
223 public function render_processing_method_dropdown(): void
224 {
225 $options = ProcessingMethod::casesWithDescription();
226
227 (new Dropdown('sendy_processing_method'))->render(['options' => $options]);
228 }
229
230 public function render_processable_order_status_dropdown(): void
231 {
232 $orderStatuses = wc_get_order_statuses();
233
234 $options = [];
235
236 foreach ($orderStatuses as $status => $label) {
237 // The 'wc-' prefix is stripped as it is intended only for WooCommerce internally.
238 $status = str_replace('wc-', '', $status);
239
240 $options[$status] = $label;
241 }
242
243 (new Dropdown('sendy_processable_order_status'))->render(['options' => $options]);
244 }
245
246 public function render_default_shop_dropdown(): void
247 {
248 $options = (new Shops())->list();
249
250 (new Dropdown('sendy_default_shop'))->render(['options' => $options]);
251 }
252
253 public function logout_action(): void
254 {
255 if (isset($_GET['sendy_logout'])) {
256 if (! current_user_can('manage_woocommerce')) {
257 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'sendy'), 403);
258 }
259
260 if (! wp_verify_nonce($_GET['_wpnonce'] ?? '', 'sendy_logout')) {
261 wp_die(esc_html__('Nonce verification failed.', 'sendy'), 401);
262 }
263
264 update_option('sendy_access_token', null, false);
265
266 wp_safe_redirect(admin_url('admin.php?page=sendy'));
267 exit;
268 }
269 }
270
271 }
272