This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-keendelivery-settings.php
362 lines (274 loc) · 9.63 KB
/
class-keendelivery-settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // If accessed directly, then deny
/**
* Class KeenDelivery_Settings
*/
class KeenDelivery_Settings {
/**
* @var
*/
private $options;
/**
* KeenDelivery_Settings constructor.
*/
public function __construct() {
// add admin setting page
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
/**
* Add KeenDelivery options page
*/
public function add_plugin_page() {
// This page will be under "Settings"
add_options_page(
'KeenDelivery instellingen',
'KeenDelivery',
'manage_options',
'keendelivery-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page() {
$this->options = get_option( 'keendelivery_option_name' );
?>
<div class="wrap">
<h2>Instellingen KeenDelivery</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'keendelivery_option_group' );
do_settings_sections( 'keendelivery-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
*
*/
public function admin_init() {
register_setting(
'keendelivery_option_group', // Option group
'keendelivery_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'keendelivery_setting_section_authorisation', // ID
'API-authorisatiegegevens', // Title
array( $this, 'print_section_authorisation_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_section(
'keendelivery_setting_section_printer_methods', // ID
'Printerinstellingen', // Title
array( $this, 'print_section_printer_methods_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_section(
'keendelivery_setting_section_printer_label', // ID
'Labelformaat', // Title
array( $this, 'print_section_printer_label_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_section(
'keendelivery_setting_section_shipping_methods', // ID
'Verzendmethodes', // Title
array( $this, 'print_section_shipping_methods_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_section(
'keendelivery_setting_section_auto_tracktrace', // ID
'Track&Trace-mail', // Title
array( $this, 'print_section_shipping_methods_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_section(
'keendelivery_setting_section_status', // ID
'API-status', // Title
array( $this, 'print_section_status_info' ), // Callback
'keendelivery-setting-admin' // Page
);
add_settings_field(
'jet_token',
'API-sleutel',
array( $this, 'jet_token_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_authorisation'
);
add_settings_field(
'jet_printer_methods',
'Welke type labelprinter moet standaard ingesteld staan?',
array( $this, 'jet_printer_methods_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_printer_methods'
);
add_settings_field(
'jet_printer_methods',
'Geef het labelformaat op:',
array( $this, 'jet_printer_label_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_printer_label'
);
add_settings_field(
'jet_shipping_methods',
'Welke verzendmethodes moeten naar KeenDelivery verstuurd worden?',
array( $this, 'jet_shipping_methods_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_shipping_methods'
);
add_settings_field(
'jet_shipping_methods',
'Automatisch track&trace-mail versturen',
array( $this, 'jet_shipping_tracktrace_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_auto_tracktrace'
);
add_settings_field(
'jet_status',
'Status',
array( $this, 'jet_status_callback' ),
'keendelivery-setting-admin',
'keendelivery_setting_section_status'
);
}
/**
* @param $input
*
* @return array
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['jet_token'] ) ) {
$postfix = '';
if ( substr( $input['jet_token'], - 1 ) == ' ' ) {
$postfix = ' ';
}
$new_input['jet_token'] = sanitize_text_field( $input['jet_token'] ) . $postfix;
}
$new_input['jet_printer_methods'] = ( isset( $input['jet_printer_methods'] ) ) ? sanitize_text_field( $input['jet_printer_methods'] ) : 'pdf';
$new_input['jet_printer_label'] = ( isset( $input['jet_printer_label'] ) ) ? sanitize_text_field( $input['jet_printer_label'] ) : 'default';
$new_input['jet_shipping_methods'] = isset( $input['jet_shipping_methods'] ) ? $input['jet_shipping_methods'] : array();
$new_input['jet_auto_tracktrace'] = ( isset( $input['jet_auto_tracktrace'] ) && $input['jet_auto_tracktrace'] == 1 ) ? 1 : 0;
$new_input['jet_status'] = ( isset( $input['jet_status'] ) && $input['jet_status'] == 'live' ) ? 'live' : 'test';
return $new_input;
}
/**
*
*/
public function print_section_authorisation_info() {
//
}
/**
*
*/
public function print_section_printer_methods_info() {
//
}
/**
*
*/
public function print_section_printer_label_info() {
//
}
/**
*
*/
public function print_section_shipping_methods_info() {
//
}
public function print_section_shipping_auto_tracktrace() {
//
}
/**
*
*/
public function keendelivery_setting_section_printer_label() {
}
/**
*
*/
public function print_section_status_info() {
//
}
/**
*
*/
public function jet_token_callback() {
printf(
'<input type="text" id="jet_token" name="keendelivery_option_name[jet_token]" value="%s" style="width:100%%">',
isset( $this->options['jet_token'] ) ? $this->options['jet_token'] : ''
);
}
/**
*
*/
public function jet_printer_methods_callback() {
echo '<select multiple name="keendelivery_option_name[jet_printer_methods]" id="jet_printer_methods" style="width:100%">';
echo ' <option value="pdf" ' . ( ( isset( $this->options['jet_printer_methods'] ) && $this->options['jet_printer_methods'] == 'pdf' ) ? 'selected="selected"' : '' ) . '>PDF</option>';
echo ' <option value="bat" ' . ( ( isset( $this->options['jet_printer_methods'] ) && $this->options['jet_printer_methods'] == 'bat' ) ? 'selected="selected"' : '' ) . '>BAT (werkt alleen met DHL)</option>';
echo ' <option value="zpl" ' . ( ( isset( $this->options['jet_printer_methods'] ) && $this->options['jet_printer_methods'] == 'zpl' ) ? 'selected="selected"' : '' ) . '>ZPL (werkt alleen met DHL)</option>';
echo '</select>';
}
/**
*
*/
public function jet_printer_label_callback() {
echo '<select multiple name="keendelivery_option_name[jet_printer_label]" id="jet_printer_label" style="width:100%">';
echo ' <option value="DEFAULT" ' . ( ( isset( $this->options['jet_printer_label'] ) && $this->options['jet_printer_label'] == 'DEFAULT' ) ? 'selected="selected"' : '' ) . '>1x A6 label</option>';
echo ' <option value="4XA6" ' . ( ( isset( $this->options['jet_printer_label'] ) && $this->options['jet_printer_label'] == '4XA6' ) ? 'selected="selected"' : '' ) . '>4x A6 labels per A4 pagina</option>';
echo ' <option value="A5" ' . ( ( isset( $this->options['jet_printer_label'] ) && $this->options['jet_printer_label'] == 'A5' ) ? 'selected="selected"' : '' ) . '>1x A5 label (alleen PostNL)</option>';
echo ' <option value="2XA5" ' . ( ( isset( $this->options['jet_printer_label'] ) && $this->options['jet_printer_label'] == '2XA5' ) ? 'selected="selected"' : '' ) . '>2x A5 labels per A4 pagina (alleen PostNL)</option>';
echo '</select>';
}
/**
*
*/
public function jet_shipping_methods_callback() {
$shipping_methods = WC()->shipping->load_shipping_methods();
$active_methods = array();
foreach ( $shipping_methods as $id => $shipping_method ) {
if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {
$method = array( 'title' => $shipping_method->method_title, 'id' => $shipping_method->id );
array_push( $active_methods, $method );
}
}
if ( count( $active_methods ) > 0 ) {
echo '<select multiple name="keendelivery_option_name[jet_shipping_methods][]" id="jet_shipping_methods" style="width:100%">';
foreach ( $active_methods as $method ) {
echo '<option value="' . esc_attr( $method['id'] ) . '" ' . ( isset( $this->options['jet_shipping_methods'] ) && in_array( $method['id'], $this->options['jet_shipping_methods'] ) ? 'selected="selected"' : '' ) . '>' . esc_html( $method['title'] ) . '</option>';
}
echo '</select>';
} else {
echo 'Geen actieve verzend methodes gevonden';
}
}
/**
*
*/
public function jet_shipping_tracktrace_callback() {
$status = ( isset( $this->options['jet_auto_tracktrace'] ) && $this->options['jet_auto_tracktrace'] == 1 ) ? 1 : 0;
print ( '<select name="keendelivery_option_name[jet_auto_tracktrace]" id="jet_auto_tracktrace" style="width:100%">
<option value="1" ' . ( $status == 1 ? ' selected' : '' ) . '>Ja, automatisch</option>
<option value="0" ' . ( $status == 0 ? ' selected' : '' ) . '>Nee, handmatig</option>
</select>' );
}
/**
*
*/
public function jet_status_callback() {
$status = ( isset( $this->options['jet_status'] ) && $this->options['jet_status'] == 'live' ) ? 'live' : 'test';
print ( '<select name="keendelivery_option_name[jet_status]" id="jet_status" style="width:100%">
<option value="test" ' . ( $status == 'test' ? ' selected' : '' ) . '>Test-status</option>
<option value="live" ' . ( $status == 'live' ? ' selected' : '' ) . '>Live-status</option>
</select>' );
}
}
if ( is_admin() ) {
$keendelivery_settings = new KeenDelivery_Settings();
}