PluginProbe
PostNL for WooCommerce / 3.1.6
PostNL for WooCommerce v3.1.6
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / class-wcmp-settings-callbacks.php

class-wcmp-settings-callbacks.php in PostNL for WooCommerce 3.1.6, at includes/class-wcmp-settings-callbacks.php

719 lines 26.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined('ABSPATH')) exit; // Exit if accessed directly
4
5 if ( ! class_exists('WooCommerce_PostNL_Settings_Callbacks')) :
6
7 class WooCommerce_PostNL_Settings_Callbacks {
8
9 const extra_width_for_number_input = 200;
10 const steps_number_input_fields = "0.01";
11
12 /**
13 * Section null callback.
14 * @return void.
15 */
16 public function section() { }
17
18 /**
19 * Checkbox callback.
20 * args:
21 * option_name - name of the main option
22 * id - key of the setting
23 * value - value if not 1 (optional)
24 * default - default setting (optional)
25 * description - description (optional)
26 * @return void.
27 */
28 public function checkbox($args, $disabled = "", $style = "") {
29 extract($this->normalize_settings_args($args));
30
31 // check if age_check is active and disabled moning and evening delivery
32 if (isset(WooCommerce_PostNL()->export_defaults["age_check"]) &&
33 (
34 $id == "morning_enabled" ||
35 $id == "evening_enabled" ||
36 $id == "only_recipient_enabled" ||
37 $id == "signature_enabled"
38 )){
39 $current = 0;
40 $disabled = "disabled";
41 $style = "cursor: not-allowed;";
42 $description = __("Not possible if age check is active", "woocommerce-postnl");
43
44 if ($id == "only_recipient_enabled") {
45 $current = 1;
46 $description = __("Home address only is mandatory when age check is active", "woocommerce-postnl");
47 }
48
49 if ($id == "signature_enabled") {
50 $current = 1;
51 $description = __("Signature for receipt is mandatory when age check is active", "woocommerce-postnl");
52 }
53 }
54
55 // output checkbox
56 printf(
57 '<input type="checkbox" id="%1$s" name="%2$s" value="%3$s" %4$s class="%5$s" %6$s style="%7$s"/>',
58 $id,
59 $setting_name,
60 $value,
61 checked($value, $current, false),
62 $class,
63 $disabled,
64 $style
65 );
66
67 // output description.
68 if (isset($description)) {
69 printf('<p class="description">%s</p>', $description);
70 }
71 }
72
73 /**
74 * Text input callback.
75 * args:
76 * option_name - name of the main option
77 * id - key of the setting
78 * size - size of the text input (em)
79 * default - default setting (optional)
80 * description - description (optional)
81 * type - type (optional)
82 * @return void.
83 */
84 public function text_input($args) {
85 extract($this->normalize_settings_args($args));
86 if (empty($type)) {
87 $type = 'text';
88 }
89
90 if ($type == 'number') {
91 $width = ($size) + self::extra_width_for_number_input;
92 $style = "width: {$width}px";
93 $step = self::steps_number_input_fields;
94 } else {
95 $style = '';
96 $step = '';
97 }
98
99 printf(
100 '<input type="%1$s" id="%2$s" name="%3$s" value="%4$s" size="%5$s" step="%6$s" placeholder="%7$s" class="%8$s" style="%9$s"/>',
101 $type,
102 $id,
103 $setting_name,
104 $current,
105 $size,
106 $step,
107 $placeholder,
108 $class,
109 $style
110 );
111
112 // output description.
113 if (isset($description)) {
114 printf('<p class="description">%s</p>', $description);
115 }
116 }
117
118 /**
119 * Color picker callback.
120 * args:
121 * option_name - name of the main option
122 * id - key of the setting
123 * size - size of the text input (em)
124 * default - default setting (optional)
125 * description - description (optional)
126 * @return void.
127 */
128 public function color_picker($args) {
129 extract($this->normalize_settings_args($args));
130
131 printf(
132 '<input type="text" id="%1$s" name="%2$s" value="%3$s" size="%4$s" class="wcmp-color-picker %5$s"/>',
133 $id,
134 $setting_name,
135 $current,
136 $size,
137 $class
138 );
139
140 // output description.
141 if (isset($description)) {
142 printf('<p class="description">%s</p>', $description);
143 }
144 }
145
146 /**
147 * Textarea callback.
148 * args:
149 * option_name - name of the main option
150 * id - key of the setting
151 * width - width of the text input (em)
152 * height - height of the text input (lines)
153 * default - default setting (optional)
154 * description - description (optional)
155 * @return void.
156 */
157 public function textarea($args) {
158 extract($this->normalize_settings_args($args));
159
160 printf(
161 '<textarea id="%1$s" name="%2$s" cols="%4$s" rows="%5$s" placeholder="%6$s"/>%3$s</textarea>',
162 $id,
163 $setting_name,
164 $current,
165 $width,
166 $height,
167 $placeholder
168 );
169
170 // output description.
171 if (isset($description)) {
172 printf('<p class="description">%s</p>', $description);
173 }
174 }
175
176 /**
177 * Select element callback.
178 *
179 * @param array $args Field arguments.
180 *
181 * @return string Select field.
182 */
183 public function select($args) {
184 extract($this->normalize_settings_args($args));
185
186 printf('<select id="%1$s" name="%2$s" class="%3$s">', $id, $setting_name, $class);
187
188 foreach ( $options as $key => $label ) {
189 printf('<option value="%s" %s>%s</option>', $key, selected($current, $key, false), $label);
190 }
191
192 echo '</select>';
193
194 if (isset($custom)) {
195 printf('<div class="%1$s_custom custom">', $id);
196
197 switch($custom['type']) {
198 case 'text_element_callback':
199 $this->text_input($custom['args']);
200 break;
201 case 'multiple_text_element_callback':
202 $this->multiple_text_input($custom['args']);
203 break;
204 default:
205 break;
206 }
207 echo '</div>';
208 }
209
210 // Displays option description.
211 if (isset($args['description'])) {
212 printf('<p class="description">%s</p>', $args['description']);
213 }
214 }
215
216 public function radio_button($args) {
217 extract($this->normalize_settings_args($args));
218
219 foreach ( $options as $key => $label ) {
220 printf(
221 '<input type="radio" class="radio" id="%1$s[%3$s]" name="%2$s" value="%3$s" %4$s />',
222 $id,
223 $setting_name,
224 $key,
225 checked($current, $key, false)
226 );
227 printf('<label for="%1$s[%3$s]"> %4$s</label><br>', $id, $setting_name, $key, $label);
228 }
229
230 // Displays option description.
231 if (isset($args['description'])) {
232 printf('<p class="description">%s</p>', $args['description']);
233 }
234 }
235
236 /**
237 * Multiple text element callback.
238 *
239 * @param array $args Field arguments.
240 *
241 * @return string Text input field.
242 */
243 public function multiple_text_input($args) {
244 extract($this->normalize_settings_args($args));
245
246 if ( ! empty($header)) {
247 echo "<p><strong>{$header}</strong>:</p>";
248 }
249
250 foreach ( $fields as $name => $field ) {
251 $label = $field['label'];
252 $size = $field['size'];
253 $placeholder = isset($field['placeholder']) ? $field['placeholder'] : '';
254
255 if (isset($field['label_width'])) {
256 $style = sprintf('style="display:inline-block; width:%1$s;"', $field['label_width']);
257 } else {
258 $style = '';
259 }
260
261 $suffix = isset($field['suffix']) ? $field['suffix'] : '';
262
263 // output field label
264 printf('<label for="%1$s_%2$s" %3$s>%4$s</label>', $id, $name, $style, $label);
265
266 // output field
267 $field_current = isset($current[$name]) ? $current[$name] : '';
268 printf(
269 '<input type="text" id="%1$s_%3$s" name="%2$s[%3$s]" value="%4$s" size="%5$s" placeholder="%6$s"/>%7$s<br/>',
270 $id,
271 $setting_name,
272 $name,
273 $field_current,
274 $size,
275 $placeholder,
276 $suffix
277 );
278 }
279
280 // Displays option description.
281 if (isset($args['description'])) {
282 printf('<p class="description">%s</p>', $args['description']);
283 }
284 }
285
286 public function order_status_select($args) {
287 // get list of WooCommerce statuses
288 if (version_compare(WOOCOMMERCE_VERSION, '2.2', '<')) {
289 $statuses = (array) get_terms('shop_order_status', array('hide_empty' => 0, 'orderby' => 'id'));
290 foreach ( $statuses as $status ) {
291 $order_statuses[esc_attr($status->slug)] = esc_html__($status->name, 'woocommerce');
292 }
293 } else {
294 $statuses = wc_get_order_statuses();
295 foreach ( $statuses as $status_slug => $status ) {
296 $status_slug = 'wc-' === substr($status_slug, 0, 3) ? substr($status_slug, 3) : $status_slug;
297 $order_statuses[$status_slug] = $status;
298 }
299 }
300
301 // select order status
302 $args['options'] = $order_statuses;
303 $this->select($args);
304 }
305
306 public function shipping_methods_package_types($args) {
307 extract($this->normalize_settings_args($args));
308 foreach ( $package_types as $package_type => $package_type_title ) {
309 printf('<div class="package_type_title">%s:<div>', $package_type_title);
310 $args['package_type'] = $package_type;
311 unset($args['description']);
312 $this->shipping_method_search($args);
313 }
314 // Displays option description.
315 if (isset($description)) {
316 printf('<p class="description">%s</p>', $description);
317 }
318 }
319
320 // Shipping method search callback.
321 public function shipping_method_search($args) {
322 extract($this->normalize_settings_args($args));
323
324 if (isset($package_type)) {
325 $setting_name = "{$setting_name}[{$package_type}]";
326 $current = isset($current[$package_type]) ? $current[$package_type] : '';
327 }
328
329 // get shipping methods
330 $available_shipping_methods = array();
331 $shipping_methods = WC()->shipping->load_shipping_methods();
332
333 if ($shipping_methods) {
334 foreach ( $shipping_methods as $key => $shipping_method ) {
335 // Automattic / WooCommerce Table Rate Shipping
336 if ($key == 'table_rate' && class_exists('WC_Table_Rate_Shipping')
337 && class_exists(
338 'WC_Shipping_Zones'
339 )) {
340 $zones = WC_Shipping_Zones::get_zones();
341 foreach ( $zones as $zone_data ) {
342 if (isset($zone_data['id'])) {
343 $zone_id = $zone_data['id'];
344 } else if (isset($zone_data['zone_id'])) {
345 $zone_id = $zone_data['zone_id'];
346 } else {
347 continue;
348 }
349 $zone = WC_Shipping_Zones::get_zone($zone_id);
350 $zone_methods = $zone->get_shipping_methods(false);
351 foreach ( $zone_methods as $key => $shipping_method ) {
352 if ($shipping_method->id == 'table_rate'
353 && method_exists(
354 $shipping_method,
355 'get_shipping_rates'
356 )) {
357 $zone_table_rates = $shipping_method->get_shipping_rates();
358 foreach ( $zone_table_rates as $zone_table_rate ) {
359 $rate_label = ! empty($zone_table_rate->rate_label)
360 ? $zone_table_rate->rate_label
361 : "{$shipping_method->title} ({$zone_table_rate->rate_id})";
362 $available_shipping_methods["table_rate:{$shipping_method->instance_id}:{$zone_table_rate->rate_id}"] = "{$zone->get_zone_name()} - {$rate_label}";
363 }
364 }
365 }
366 }
367 continue;
368 }
369
370 // Bolder Elements Table Rate Shipping
371 if ($key == 'betrs_shipping' && is_a($shipping_method, 'BE_Table_Rate_Method')
372 && class_exists(
373 'WC_Shipping_Zones'
374 )) {
375 $zones = WC_Shipping_Zones::get_zones();
376 foreach ( $zones as $zone_data ) {
377 if (isset($zone_data['id'])) {
378 $zone_id = $zone_data['id'];
379 } else if (isset($zone_data['zone_id'])) {
380 $zone_id = $zone_data['zone_id'];
381 } else {
382 continue;
383 }
384 $zone = WC_Shipping_Zones::get_zone($zone_id);
385 $zone_methods = $zone->get_shipping_methods(false);
386 foreach ( $zone_methods as $key => $shipping_method ) {
387 if ($shipping_method->id == 'betrs_shipping') {
388 $shipping_method_options = get_option(
389 $shipping_method->id . '_options-' . $shipping_method->instance_id
390 );
391 if (isset($shipping_method_options['settings'])) {
392 foreach ( $shipping_method_options['settings'] as $zone_table_rate ) {
393 $rate_label = ! empty($zone_table_rate['title'])
394 ? $zone_table_rate['title']
395 : "{$shipping_method->title} ({$zone_table_rate['option_id']})";
396 $available_shipping_methods["betrs_shipping_{$shipping_method->instance_id}-{$zone_table_rate['option_id']}"] = "{$zone->get_zone_name()} - {$rate_label}";
397 }
398 }
399 }
400 }
401 }
402 continue;
403 }
404 $method_title = ! empty($shipping_methods[$key]->method_title)
405 ? $shipping_methods[$key]->method_title
406 : $shipping_methods[$key]->title;
407 $available_shipping_methods[$key] = $method_title;
408
409 // split flat rate by shipping class
410 if (($key == 'flat_rate' || $key == 'legacy_flat_rate')
411 && version_compare(WOOCOMMERCE_VERSION, '2.4', '>=' )
412 ) {
413 $shipping_classes = WC()->shipping->get_shipping_classes();
414 foreach ( $shipping_classes as $shipping_class ) {
415 if ( ! isset($shipping_class->term_id)) {
416 continue;
417 }
418 $id = $shipping_class->term_id;
419 $name = esc_html("{$method_title} - {$shipping_class->name}");
420 $method_class = esc_attr($key) . ":" . $id;
421 $available_shipping_methods[$method_class] = $name;
422 }
423 }
424 }
425 }
426
427 ?>
428 <select id="<?php echo $id; ?>" name="<?php echo $setting_name; ?>[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php echo $placeholder; ?>">
429 <?php
430 $shipping_methods_selected = (array) $current;
431
432 $shipping_methods = WC()->shipping->load_shipping_methods();
433 if ($available_shipping_methods) {
434 foreach ( $available_shipping_methods as $key => $label ) {
435 echo '<option value="' . esc_attr($key) . '"' . selected(in_array($key, $shipping_methods_selected), true, false) . '>' . esc_html($label) . '</option>';
436 }
437 }
438 ?>
439 </select>
440 <?php
441 // Displays option description.
442 if (isset($description)) {
443 printf('<p class="description">%s</p>', $description);
444 }
445 }
446
447 public function enhanced_select($args) {
448 extract($this->normalize_settings_args($args));
449
450 ?>
451 <select id="<?php echo $id; ?>" name="<?php echo $setting_name; ?>[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php echo $placeholder; ?>">
452 <?php
453 foreach ( $options as $key => $title ) {
454 echo '<option value="' . esc_attr($key) . '"' . selected(! empty($current) && in_array($key, (array) $current), true, false) . '>' . esc_html($title) . '</option>';
455 }
456 ?>
457 </select>
458 <?php
459 // Displays option description.
460 if (isset($description)) {
461 printf('<p class="description">%s</p>', $description);
462 }
463 }
464
465 public function delivery_option_enable($args) {
466 extract($this->normalize_settings_args($args));
467 // checkbox (enable)
468 $cb_args = array(
469 'id' => "{$id}_enabled",
470 'class' => 'wcmp_delivery_option'
471 );
472 // number (fee)
473 $fee_args = array(
474 'id' => "{$id}_fee",
475 'type' => 'number',
476 );
477 // number (cut-off time)
478 $cutoff_time_args = array(
479 'id' => "{$id}_time",
480 'type' => 'text',
481 );
482 // textarea (description)
483 $default_delivery_text = array(
484 'id' => "{$id}_title",
485 'type' => 'text',
486 );
487
488 $this->checkbox(array_merge($args, $cb_args));
489 if (isset(WooCommerce_PostNL()->export_defaults['age_check'])){
490 if ($args['id'] == "only_recipient" || $id == "signature"){
491 $args['has_title'] = false;
492 $args['has_price'] = false;
493 }
494 }
495 ?>
496 <table class="wcmp_delivery_option_details">
497 <?php if ($args['has_title']): ?>
498 <tr>
499 <td style="min-width: 215px;"><?php _e(
500 $args['title'] . ' title',
501 'woocommerce-postnl'
502 ) ?>:
503 </td>
504 <td>&nbsp;&nbsp;&nbsp;<?php $this->text_input(
505 array_merge($args, $default_delivery_text)
506 ) ?></td>
507 </tr>
508 <?php endif; ?>
509 <?php if (isset($args['has_cutoff_time'])): ?>
510 <tr>
511 <td><?php _e('Cut-off time on Saturday', 'woocommerce-postnl') ?>:</td>
512 <td>&nbsp;&nbsp;&nbsp;<?php $this->text_input(array_merge($args, $cutoff_time_args)); ?></td>
513 </tr>
514 <?php endif; ?>
515 <?php if ($args['has_price']): ?>
516 <tr>
517 <td><?php _e('Additional fee (ex VAT, optional)', 'woocommerce-postnl') ?>:</td>
518 <td>&euro; <?php $this->text_input(array_merge($args, $fee_args)); ?></td>
519 </tr>
520 <?php endif; ?>
521 <?php if (isset($args['option_description'])): ?>
522 <tr>
523 <td colspan="2">
524 <p class="description"><?php _e($args['option_description']) ?></p>
525 </td>
526 </tr>
527 <?php endif; ?>
528 </table>
529 <?php
530 }
531
532 public function delivery_options_table($args) {
533 extract($this->normalize_settings_args($args));
534 ?>
535 <table>
536 <thead>
537 <tr>
538 <th style="width: 2.2em"><?php // _e( 'Enabled', 'woocommerce-postnl' )?></th>
539 <th><?php _e('Option', 'woocommerce-postnl') ?></th>
540 <th><?php _e('Fee (optional)', 'woocommerce-postnl') ?></th>
541 <th><?php _e('Description', 'woocommerce-postnl') ?></th>
542 </tr>
543 </thead>
544 <tbody>
545 <?php
546 foreach ($options
547
548 as $key => $title) {
549 // prepare args for input fields
550 $common_args = array(
551 'option_name' => "{$option_name}[{$key}]",
552 );
553 // checkbox (enable)
554 $cb_args = array(
555 'id' => 'enabled',
556 );
557 // number (fee)
558 $fee_args = array(
559 'id' => 'fee',
560 'type' => 'number',
561 'size' => '5',
562 );
563 // textarea (description)
564 $description_args = array(
565 'id' => 'description',
566 'width' => '50',
567 'height' => '4',
568 );
569 ?>
570 <tr>
571 <td><?php $this->checkbox(array_merge($common_args, $cb_args)); ?></td>
572 <td><?php echo $title; ?></td>
573 <td><input type="number" min="0"></td>
574 <td><input type="text"></td>
575 <?php
576 }
577 ?>
578 </tbody>
579 </table>
580 <?php
581 }
582
583 /**
584 * Wrapper function to create tabs for settings in different languages
585 */
586 public function i18n_wrap($args) {
587 extract($this->normalize_settings_args($args));
588
589 if ($languages = $this->get_languages()) {
590 printf('<div id="%s-%s-translations" class="translations">', $option_name, $id)
591 ?>
592 <ul>
593 <?php foreach ( $languages as $lang_code => $language_name ) {
594 $translation_id = "{$option_name}_{$id}_{$lang_code}";
595 printf('<li><a href="#%s">%s</a></li>', $translation_id, $language_name);
596 }
597 ?>
598 </ul>
599 <?php foreach ( $languages as $lang_code => $language_name ) {
600 $translation_id = "{$option_name}_{$id}_{$lang_code}";
601 printf('<div id="%s">', $translation_id);
602 $args['lang'] = $lang_code;
603 call_user_func(array($this, $callback), $args);
604 echo '</div>';
605 }
606 ?>
607
608 </div>
609 <?php
610 } else {
611 $args['lang'] = 'default';
612 call_user_func(array($this, $callback), $args);
613 }
614 }
615
616 public function get_languages() {
617 $wpml = class_exists('SitePress');
618
619 if ($wpml) {
620 $icl_get_languages = icl_get_languages('skip_missing=0');
621 $languages = array();
622 foreach ( $icl_get_languages as $lang => $data ) {
623 $languages[$data['language_code']] = $data['native_name'];
624 }
625 } else {
626 return false;
627 }
628
629 return $languages;
630 }
631
632 public function normalize_settings_args($args) {
633 $args['value'] = isset($args['value']) ? $args['value'] : 1;
634
635 $args['placeholder'] = isset($args['placeholder']) ? $args['placeholder'] : '';
636 $args['class'] = isset($args['class']) ? $args['class'] : '';
637
638 // get main settings array
639 $option = get_option($args['option_name']);
640
641 $args['setting_name'] = "{$args['option_name']}[{$args['id']}]";
642
643 if (isset($args['lang'])) {
644 // i18n settings name
645 $args['setting_name'] = "{$args['setting_name']}[{$args['lang']}]";
646 // copy current option value if set
647
648 if ($args['lang'] == 'default' && ! empty($option[$args['id']]) && ! isset($option[$args['id']]['default'])) {
649 // we're switching back from WPML to normal
650 // try english first
651 if (isset($option[$args['id']]['en'])) {
652 $args['current'] = $option[$args['id']]['en'];
653 } else {
654 // fallback to the first language if english not found
655 $first = array_shift($option[$args['id']]);
656 if ( ! empty($first)) {
657 $args['current'] = $first;
658 }
659 }
660 } else {
661 if (isset($option[$args['id']][$args['lang']])) {
662 $args['current'] = $option[$args['id']][$args['lang']];
663 } else if (isset($option[$args['id']]['default'])) {
664 $args['current'] = $option[$args['id']]['default'];
665 }
666 }
667 } else {
668 // copy current option value if set
669 if (isset($option[$args['id']])) {
670 $args['current'] = $option[$args['id']];
671 }
672 }
673
674 // fallback to default or empty if no value in option
675 if ( ! isset($args['current'])) {
676 $args['current'] = isset($args['default']) ? $args['default'] : '';
677 }
678
679 return $args;
680 }
681
682 /**
683 * Validate options.
684 *
685 * @param array $input options to valid.
686 *
687 * @return array validated options.
688 */
689 public function validate($input) {
690 // Create our array for storing the validated options.
691 $output = array();
692
693 if (empty($input) || ! is_array($input)) {
694 return $input;
695 }
696
697 // Loop through each of the incoming options.
698 foreach ( $input as $key => $value ) {
699 // Check to see if the current option has a value. If so, process it.
700 if (isset($input[$key])) {
701 if (is_array($input[$key])) {
702 foreach ( $input[$key] as $sub_key => $sub_value ) {
703 $output[$key][$sub_key] = $input[$key][$sub_key];
704 }
705 } else {
706 $output[$key] = $input[$key];
707 }
708 }
709 }
710
711 // Return the array processing any additional functions filtered by this action.
712 return apply_filters('woocommerce_postnl_settings_validate_input', $input, $input);
713 }
714 }
715
716 endif; // class_exists
717
718 return new WooCommerce_PostNL_Settings_Callbacks();
719