PluginProbe
PostNL for WooCommerce / 2.5.1
PostNL for WooCommerce v2.5.1
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 2.5.1, at includes/class-wcmp-settings-callbacks.php

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