PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.1
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / classes / class-cartflows-meta-fields.php

class-cartflows-meta-fields.php in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.1, at classes/class-cartflows-meta-fields.php

994 lines 30.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // @codingStandardsIgnoreStart
3 /**
4 * Meta Fields.
5 *
6 * @package CartFlows
7 */
8
9 /**
10 * Class Cartflows_Meta_Fields.
11 */
12 class Cartflows_Meta_Fields {
13
14 /**
15 * Instance
16 *
17 * @var $instance
18 */
19 private static $instance;
20
21 /**
22 * Initiator
23 */
24 public static function get_instance() {
25 if ( ! isset( self::$instance ) ) {
26 self::$instance = new self;
27 }
28
29 return self::$instance;
30 }
31
32 /**
33 * Constructor
34 */
35 public function __construct() {
36
37 /* Add Scripts */
38 add_action( 'admin_enqueue_scripts', array( $this, 'admin_meta_scripts' ), 20 );
39
40 add_action( 'wp_ajax_wcf_json_search_coupons', array( $this, 'json_search_coupons' ) );
41
42 add_action( 'wp_ajax_wcf_add_checkout_custom_field', array( $this, 'add_checkout_custom_field' ) );
43 add_action( 'wp_ajax_wcf_delete_checkout_custom_field', array( $this, 'delete_checkout_custom_field' ) );
44
45 add_action( 'wp_ajax_wcf_json_search_pages', array( $this, 'json_search_pages' ) );
46
47 add_filter( 'cartflows_admin_js_localize', array( $this, 'localize_vars' ) );
48 }
49
50 public function admin_meta_scripts() {
51
52 global $pagenow;
53 global $post;
54
55 $screen = get_current_screen();
56
57 if (
58 ( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) &&
59 wcf()->utils->is_step_post_type( $screen->post_type )
60 ) {
61
62 wp_enqueue_style( 'woocommerce_admin_styles' );
63
64 wp_enqueue_script( 'select2' );
65 wp_enqueue_script( 'wc-enhanced-select' );
66
67 wp_enqueue_script(
68 'wcf-admin-meta',
69 CARTFLOWS_URL . 'admin/meta-assets/js/admin-edit.js',
70 array( 'jquery', 'select2', 'wp-color-picker' ),
71 CARTFLOWS_VER,
72 true
73 );
74
75 wp_enqueue_style( 'wcf-admin-meta', CARTFLOWS_URL . 'admin/meta-assets/css/admin-edit.css', array( 'wp-color-picker' ), CARTFLOWS_VER );
76 wp_style_add_data( 'wcf-admin-meta', 'rtl', 'replace' );
77
78 $localize = array(
79 'ajax_url' => admin_url( 'admin-ajax.php' ),
80 'google_fonts' => CartFlows_Font_Families::get_google_fonts(),
81 'system_fonts' => CartFlows_Font_Families::get_system_fonts(),
82 'font_weights' => array(
83 '100' => __( 'Thin 100', 'cartflows' ),
84 '200' => __( 'Extra-Light 200', 'cartflows' ),
85 '300' => __( 'Light 300', 'cartflows' ),
86 '400' => __( 'Normal 400', 'cartflows' ),
87 '500' => __( 'Medium 500', 'cartflows' ),
88 '600' => __( 'Semi-Bold 600', 'cartflows' ),
89 '700' => __( 'Bold 700', 'cartflows' ),
90 '800' => __( 'Extra-Bold 800', 'cartflows' ),
91 '900' => __( 'Ultra-Bold 900', 'cartflows' ),
92 )
93 );
94
95 wp_localize_script( 'jquery', 'wcf', apply_filters( 'wcf_js_localize', $localize ) );
96
97 do_action( 'cartflows_admin_meta_scripts' );
98 }
99 }
100
101 /**
102 * Function to search coupons
103 */
104 public function json_search_coupons() {
105
106 check_admin_referer( 'wcf-json-search-coupons', 'security' );
107
108 global $wpdb;
109
110 $term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
111
112 if ( empty( $term ) ) {
113 die();
114 }
115
116 $posts = wp_cache_get( 'wcf_search_coupons', 'wcf_funnel_Cart' );
117
118 if ( false === $posts ) {
119 $posts = $wpdb->get_results( // phpcs:ignore
120 $wpdb->prepare(
121 "SELECT *
122 FROM {$wpdb->prefix}posts
123 WHERE post_type = %s
124 AND post_title LIKE %s
125 AND post_status = %s",
126 'shop_coupon',
127 $wpdb->esc_like( $term ) . '%',
128 'publish'
129 )
130 );
131 wp_cache_set( 'wcf_search_coupons', $posts, 'wcf_funnel_Cart' );
132 }
133
134 $coupons_found = array();
135 $all_discount_types = wc_get_coupon_types();
136
137 if ( $posts ) {
138 foreach ( $posts as $post ) {
139
140 $discount_type = get_post_meta( $post->ID, 'discount_type', true );
141
142 if ( ! empty( $all_discount_types[ $discount_type ] ) ) {
143 $coupons_found[ get_the_title( $post->ID ) ] = get_the_title( $post->ID ) . ' (Type: ' . $all_discount_types[ $discount_type ] . ')';
144 }
145 }
146 }
147
148 wp_send_json( $coupons_found );
149 }
150
151 /**
152 * [add_checkout_custom_field description]
153 *
154 * @hook wcf_add_checkout_custom_field
155 */
156 public function add_checkout_custom_field() {
157
158 check_ajax_referer( 'wcf-add-checkout-custom-field', 'security' );
159
160 $post_id = intval( $_POST['post_id'] );
161 $add_to = sanitize_text_field( wp_unslash( $_POST['add_to'] ) );
162 $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
163 $options = sanitize_text_field( wp_unslash( $_POST['options'] ) );
164 $label = sanitize_text_field( wp_unslash( $_POST['label'] ) );
165 $name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
166
167 if ( '' !== $name ) {
168
169 $fields = Cartflows_Helper::get_checkout_fields( $add_to, $post_id );
170 $field_keys = array_keys($fields);
171
172 $name = $add_to . '_' . sanitize_key( $name );
173 if( in_array($name, $field_keys) ) {
174 $name = $name . '_' . rand( 0000, 9999 );
175 }
176
177 $field_data = array(
178 'type' => $type,
179 'label' => $label,
180 'placeholder' => '',
181 'class' => array( 'form-row-wide' ),
182 'label_class' => array(),
183 'required' => true,
184 'custom' => true,
185 );
186
187 if ( 'select' === $type ) {
188
189 $options = explode( ',', $options );
190 $field_data['options'] = array();
191
192 if ( is_array( $options ) && ! empty( $options ) ) {
193
194 foreach ( $options as $key => $value ) {
195
196 $field_data['options'][ $value ] = $value;
197 }
198 }
199 }
200
201 Cartflows_Helper::add_checkout_field( $add_to, $name, $field_data, $post_id );
202
203 $key = sanitize_key( $name );
204 $name = 'wcf-' . $key;
205
206 $field_args = array(
207 'label' => $label,
208 'name' => $name,
209 'value' => 'yes',
210 'after' => 'Enable',
211 );
212
213 $field_args['after_html'] = '<span class="wcf-cpf-actions" data-type="billing" data-key="' . $key . '"> | ';
214 $field_args['after_html'] .= '<a class="wcf-cpf-action-remove">' . __( 'Remove', 'cartflows' ) . '</a>';
215 $field_args['after_html'] .= '</span>';
216
217 $field_markup = wcf()->meta->get_checkbox_field( $field_args );
218
219 if( 'billing' === $add_to ) {
220 $add_to_class = 'wcf-cb-fields';
221 } else if( 'shipping' === $add_to ) {
222 $add_to_class = 'wcf-sb-fields';
223 }
224
225 $data = array(
226 'field_data' => $field_data,
227 'field_args' => $field_args,
228 'add_to_class' => $add_to_class,
229 'markup' => $field_markup,
230 );
231
232 wp_send_json( $data );
233 }
234
235 wp_send_json( false );
236
237 }
238
239 /**
240 * [delete_checkout_custom_field description]
241 *
242 * @hook wcf_delete_checkout_custom_field
243 * @return [type] [description]
244 */
245 public function delete_checkout_custom_field() {
246
247 check_ajax_referer( 'wcf-delete-checkout-custom-field', 'security' );
248
249 $post_id = intval( $_POST['post_id'] );
250 $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
251 $key = sanitize_text_field( wp_unslash( $_POST['key'] ) );
252
253 if ( '' !== $key ) {
254
255 Cartflows_Helper::delete_checkout_field( $type, $key, $post_id );
256
257 wp_send_json( true );
258
259 }
260
261 wp_send_json( false );
262
263 }
264
265 /**
266 * Function to search coupons
267 */
268 public function json_search_pages() {
269
270 check_ajax_referer( 'wcf-json-search-pages', 'security' );
271
272 $term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
273
274 if ( empty( $term ) ) {
275 die( 'not found' );
276 }
277
278 $search_string = $term;
279 $data = array();
280 $result = array();
281
282 add_filter( 'posts_search', array( $this, 'search_only_titles' ), 10, 2 );
283
284 $query = new WP_Query(
285 array(
286 's' => $search_string,
287 'post_type' => 'page',
288 'posts_per_page' => - 1,
289 )
290 );
291
292 if ( $query->have_posts() ) {
293 while ( $query->have_posts() ) {
294 $query->the_post();
295 $title = get_the_title();
296 $title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : '';
297 $id = get_the_id();
298 $data[] = array(
299 'id' => $id,
300 'text' => $title,
301 );
302 }
303 }
304
305 if ( is_array( $data ) && ! empty( $data ) ) {
306 $result[] = array(
307 'text' => '',
308 'children' => $data,
309 );
310 }
311
312 wp_reset_postdata();
313
314 // return the result in json.
315 wp_send_json( $result );
316 }
317
318 public function search_only_titles( $search, $wp_query ) {
319 if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
320 global $wpdb;
321
322 $q = $wp_query->query_vars;
323 $n = ! empty( $q['exact'] ) ? '' : '%';
324
325 $search = array();
326
327 foreach ( (array) $q['search_terms'] as $term ) {
328 $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
329 }
330
331 if ( ! is_user_logged_in() ) {
332 $search[] = "$wpdb->posts.post_password = ''";
333 }
334
335 $search = ' AND ' . implode( ' AND ', $search );
336 }
337
338 return $search;
339 }
340
341 function get_field( $field_data, $field_content ) {
342
343 $label = isset( $field_data['label'] ) ? $field_data['label'] : '';
344 $help = isset( $field_data['help'] ) ? $field_data['help'] : '';
345 $after_html = isset( $field_data['after_html'] ) ? $field_data['after_html'] : '';
346
347 $name_class = 'field-' . $field_data['name'];
348
349 $field_html = '<div class="wcf-field-row ' . $name_class . '">';
350
351 if( ! empty( $label ) || ! empty( $help ) ) {
352 $field_html .= '<div class="wcf-field-row-heading">';
353 if( ! empty( $label ) ) {
354 $field_html .= '<label>' . esc_html( $label ) . '</label>';
355 }
356 if ( ! empty( $help ) ) {
357 $field_html .= '<i class="wcf-field-heading-help dashicons dashicons-editor-help">';
358 // $field_html .= '<span class="wcf-tooltip" data-tooltip= "'. esc_attr( $help ) .'"></span>';
359 $field_html .= '</i>';
360 $field_html .= '<span class="wcf-tooltip-text">';
361 $field_html .= $help;
362 $field_html .= '</span>';
363 }
364 $field_html .= '</div>';
365 }
366
367 $field_html .= '<div class="wcf-field-row-content">';
368 $field_html .= $field_content;
369
370 if ( ! empty( $after_html ) ) {
371 $field_html .= $after_html;
372 }
373
374 $field_html .= '</div>';
375 $field_html .= '</div>';
376
377 return $field_html;
378 }
379
380 function get_text_field( $field_data ) {
381
382 $value = $field_data['value'];
383
384 $attr = '';
385
386 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
387
388 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
389 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
390 }
391 }
392
393 $field_content = '<input type="text" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
394
395 return $this->get_field( $field_data, $field_content );
396 }
397
398 function get_shortcode_field( $field_data ) {
399
400 $attr = '';
401
402 $attr_fields = array(
403 'readonly' => 'readonly',
404 'onfocus' => 'this.select()',
405 'onmouseup' => 'return false',
406 );
407
408 if ( $attr_fields && is_array( $attr_fields ) ) {
409
410 foreach ( $attr_fields as $attr_key => $attr_value ) {
411 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
412 }
413 }
414
415 $field_content = '<input type="text" name="' . $field_data['name'] . '" value="' . $field_data['content'] . '" ' . $attr . '>';
416
417 return $this->get_field( $field_data, $field_content );
418 }
419
420 function get_display_field( $field_data ) {
421
422 $field_content = $field_data['content'];
423
424 return $this->get_field( $field_data, $field_content );
425 }
426
427 function get_hr_line_field( $field_data ) {
428
429 $field_data = array(
430 'name' => 'wcf-hr-line',
431 'content' => '<hr>'
432 );
433
434 $field_content = $field_data['content'];
435
436 return $this->get_field( $field_data, $field_content );
437 }
438
439 function get_number_field( $field_data ) {
440
441 $value = $field_data['value'];
442
443 $attr = '';
444
445 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
446
447 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
448 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
449 }
450 }
451
452 $field_content = '<input type="number" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
453
454 return $this->get_field( $field_data, $field_content );
455 }
456
457 function get_hidden_field( $field_data ) {
458
459 $value = $field_data['value'];
460
461 $attr = '';
462
463 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
464
465 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
466 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
467 }
468 }
469
470 $field_content = '<input type="hidden" id="' . $field_data['name'] . '" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
471
472 return $this->get_field( $field_data, $field_content );
473 }
474
475 function get_area_field( $field_data ) {
476
477 $value = $field_data['value'];
478
479 $field_content = '<textarea name="' . $field_data['name'] . '" rows="10" cols="50">';
480 $field_content .= $value;
481 $field_content .= '</textarea>';
482
483 return $this->get_field( $field_data, $field_content );
484 }
485
486 function get_checkbox_field( $field_data ) {
487
488 $value = $field_data['value'];
489
490 $field_content = '';
491 if ( isset( $field_data['before'] ) ) {
492 $field_content .= '<span>' . $field_data['before'] . '</span>';
493 }
494 $field_content .= '<input type="hidden" name="' . $field_data['name'] . '" value="no">';
495 $field_content .= '<input type="checkbox" name="' . $field_data['name'] . '" value="yes" ' . checked( 'yes', $value, false ) . '>';
496
497 if ( isset( $field_data['after'] ) ) {
498 $field_content .= '<span>' . $field_data['after'] . '</span>';
499 }
500
501 return $this->get_field( $field_data, $field_content );
502 }
503
504 function get_radio_field( $field_data ) {
505
506 $value = $field_data['value'];
507 $field_content = '';
508
509 if ( is_array( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
510
511 foreach ( $field_data['options'] as $data_key => $data_value ) {
512
513 $field_content .= '<div class="wcf-radio-option">';
514 $field_content .= '<input type="radio" name="' . $field_data['name'] . '" value="' . $data_key . '" ' . checked( $data_key, $value, false ) . '>';
515 $field_content .= $data_value;
516 $field_content .= '</div>';
517 }
518 }
519
520 return $this->get_field( $field_data, $field_content );
521 }
522
523 function get_font_family_field( $field_data ) {
524
525 $value = $field_data['value'];
526
527 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
528
529 $field_content = '<select class="wcf-field-font-family" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
530
531 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
532
533 $field_content .= '<optgroup label="Other System Fonts">';
534 foreach ( CartFlows_Font_Families::get_system_fonts() as $name => $variants ) {
535 $field_content .= '<option value="' . esc_attr( $name ) . '" ' . selected( $name, $value, false ) . '>' . esc_attr( $name ) . '</option>';
536 }
537 $field_content .= '</optgroup>';
538 $field_content .= '<optgroup label="Google">';
539 foreach ( CartFlows_Font_Families::get_google_fonts() as $name => $single_font ) {
540 $variants = wcf_get_prop( $single_font, '0' );
541 $category = wcf_get_prop( $single_font, '1' );
542 $font_value = '\'' . esc_attr( $name ) . '\', ' . esc_attr( $category );
543 $field_content .= '<option value="' . esc_attr( $font_value ) . '" ' . selected( $font_value, $value, false ) . '>' . esc_attr( $name ) . '</option>';
544 }
545 $field_content .= '</optgroup>';
546
547 $field_content .= '</select>';
548
549 return $this->get_field( $field_data, $field_content );
550 }
551
552 function get_font_weight_field( $field_data ) {
553
554 $value = $field_data['value'];
555
556 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
557
558 $field_content = '<select data-selected="'.esc_attr( $value ).'" class="wcf-field-font-weight" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
559
560 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
561
562 $field_content .= '</select>';
563
564 return $this->get_field( $field_data, $field_content );
565 }
566
567 function get_select_field( $field_data ) {
568
569 $value = $field_data['value'];
570 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
571
572 $field_content = '<select name="' . $field_data['name'] . '">';
573
574
575 if ( is_array( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
576
577 foreach ( $field_data['options'] as $data_key => $data_value ) {
578
579 $disabled = '';
580
581 if ( array_key_exists( $data_key, $pro_options ) ) {
582 $disabled = 'disabled ';
583 $data_value = $pro_options[ $data_key ];
584 }
585
586 $field_content .= '<option value="' . $data_key . '" ' . selected( $value, $data_key, false ) . ' ' . $disabled .'>' . $data_value . '</option>';
587 }
588 }
589
590 $field_content .= '</select>';
591
592 if ( isset( $field_data['after'] ) ) {
593 $field_content .= '<span>' . $field_data['after'] . '</span>';
594 }
595
596 return $this->get_field( $field_data, $field_content );
597 }
598
599 function get_color_picker_field( $field_data ) {
600
601 $value = $field_data['value'];
602
603 $field_content = '<input class="wcf-color-picker" type="text" name="' . $field_data['name'] . '" value="' . $value . '">';
604
605 return $this->get_field( $field_data, $field_content );
606 }
607
608 function get_product_selection_field( $field_data ) {
609
610 $value = $field_data['value'];
611
612 $multiple = '';
613
614 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
615 $multiple = ' multiple="multiple"';
616 }
617
618 $allow_clear = '';
619
620 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
621 $allow_clear = ' data-allow_clear="allow_clear"';
622 }
623
624 $field_content = '<select
625 name="' . $field_data['name'] . '[]"
626 class="wcf-product-search" ' . $multiple . $allow_clear . '
627 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
628 data-action="woocommerce_json_search_products_and_variations">';
629
630 if ( is_array( $value ) && ! empty( $value ) ) {
631
632 foreach ( $value as $data_key => $product_id ) {
633
634 $product = wc_get_product( $product_id );
635
636 // posts.
637 if ( ! empty( $product ) ) {
638 $post_title = $product->get_name() . ' (#' . $product_id . ')';
639
640 $field_content .= '<option value="' . $product_id . '" selected="selected" >' . $post_title . '</option>';
641 }
642 }
643 }
644 $field_content .= '</select>';
645
646 return $this->get_field( $field_data, $field_content );
647 }
648
649 function get_coupon_selection_field( $field_data ) {
650
651 $value = $field_data['value'];
652
653 $multiple = '';
654
655 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
656 $multiple = ' multiple="multiple"';
657 }
658
659 $allow_clear = '';
660
661 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
662 $allow_clear = ' data-allow_clear="allow_clear"';
663 }
664
665 $field_content = '<select
666 name="' . $field_data['name'] . '[]"
667 class="wc-coupon-search wcf-coupon-search" ' . $multiple . $allow_clear . '
668 data-placeholder="' . __( 'Search for a coupon&hellip;', 'cartflows' ) . '"
669 data-action="wcf_json_search_coupons">';
670
671 if ( is_array( $value ) && ! empty( $value ) ) {
672
673 $all_discount_types = wc_get_coupon_types();
674
675 foreach ( $value as $coupon_title ) {
676
677 $coupon = new WC_Coupon( $coupon_title );
678
679 $discount_type = $coupon->get_discount_type();
680
681 if ( isset( $discount_type ) && $discount_type ) {
682 $discount_type = ' ( Type: ' . $all_discount_types[ $discount_type ] . ' )';
683 }
684
685 $field_content .= '<option value="' . $coupon_title . '" selected="selected">' . $coupon_title . $discount_type . '</option>';
686 }
687 }
688
689 $field_content .= '</select>';
690
691 return $this->get_field( $field_data, $field_content );
692 }
693
694 function get_page_selection_field( $field_data ) {
695
696 $value = $field_data['value'];
697
698 $multiple = '';
699
700 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
701 $multiple = 'multiple="multiple"';
702 }
703
704 $field_content = '<select
705 name="' . $field_data['name'] . '[]"
706 class="wcf-search-pages" ' . $multiple . '"
707 data-action="wcf_json_search_pages">';
708
709 if ( is_array( $value ) && ! empty( $value ) ) {
710
711 foreach ( $value as $data_key => $data_value ) {
712
713 $field_content .= '<option value="' . $data_value . '">' . get_the_title( $data_value ) . '</option>';
714 }
715 }
716
717 $field_content .= '</select>';
718
719 return $this->get_field( $field_data, $field_content );
720 }
721
722 function get_section( $field_data ) {
723 $field_html = '<div class="wcf-field-row wcf-field-section">';
724 $field_html .= '<div class="wcf-field-section-heading" colspan="2">';
725 $field_html .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
726
727 if ( isset( $field_data['help'] ) ) {
728 $field_html .= '<i class="wcf-field-heading-help dashicons dashicons-editor-help" title="' . esc_attr( $field_data['help'] ) . '"></i>';
729 }
730 $field_html .= '</div>';
731 $field_html .= '</div>';
732 return $field_html;
733 }
734
735 function get_description_field( $field_data ) {
736
737 $field_html = '<div class="wcf-field-row wcf-field-desc ' . $field_data['name'] . '">';
738 $field_html .= '<div class="wcf-field-desc-content">';
739 $field_html .= $field_data['content'];
740 $field_html .= '</div>';
741 $field_html .= '</div>';
742
743 return $field_html;
744 }
745
746 function get_checkout_field_repeater( $field_data ) {
747
748 $value = array();
749
750 $value[0] = array(
751 'add_to' => '',
752 'type' => '',
753 'label' => '',
754 'name' => '',
755 );
756
757 $field_content = '';
758
759 $field_content .= '<div class="wcf-field-row">';
760 // $field_content .= '<div class="wcf-field-row-heading">';
761 // $field_content .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
762 // $field_content .= '</div>';
763 $field_content .= '<div class="wcf-field-row-content">';
764 $field_content .= '<div class="wcf-cpf-wrap">';
765
766 foreach ( $value as $p_key => $p_data ) {
767 $field_content .= '<div class="wcf-cpf-row" data-key="' . $p_key . '">';
768 $field_content .= '<div class="wcf-cpf-row-header">';
769 $field_content .= '<span class="wcf-cpf-row-title">Add New Custom Field</span>';
770 $field_content .= '</div>';
771
772 $field_content .= '<div class="wcf-cpf-row-standard-fields">';
773
774 /* Add To */
775 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-add_to">';
776 $field_content .= '<span class="wcf-cpf-row-setting-label">Add to</span>';
777 $field_content .= '<span class="wcf-cpf-row-setting-field">';
778 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][add_to]" class="wcf-cpf-add_to">';
779 $field_content .= '<option value="billing">Billing</option>';
780 $field_content .= '<option value="shipping">Shipping</option>';
781 $field_content .= '</select>';
782 $field_content .= '</span>';
783 $field_content .= '</div>';
784
785 /* Type */
786 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-type">';
787 $field_content .= '<span class="wcf-cpf-row-setting-label">Type</span>';
788 $field_content .= '<span class="wcf-cpf-row-setting-field">';
789 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][type]" class="wcf-cpf-type">';
790 $field_content .= '<option value="text">Text</option>';
791 $field_content .= '<option value="textarea">Textarea</option>';
792 $field_content .= '<option value="select">Select</option>';
793 $field_content .= '<option value="checkbox">Checkbox</option>';
794 $field_content .= '</select>';
795 $field_content .= '</span>';
796 $field_content .= '</div>';
797
798 /* Textarea */
799 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-options">';
800 $field_content .= '<span class="wcf-cpf-row-setting-label">Options *</span>';
801 $field_content .= '<span class="wcf-cpf-row-setting-field">';
802 $field_content .= '<textarea value="" name="wcf-checkout-custom-fields[' . $p_key . '][label]" class="wcf-cpf-options" placeholder="Enter your options separated by comma."></textarea>';
803 $field_content .= '</span>';
804 $field_content .= '</div>';
805
806 /* Label */
807 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-label">';
808 $field_content .= '<span class="wcf-cpf-row-setting-label">Label *</span>';
809 $field_content .= '<span class="wcf-cpf-row-setting-field">';
810 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][label]" class="wcf-cpf-label">';
811 $field_content .= '</span>';
812 $field_content .= '</div>';
813
814 /* Name */
815 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-name">';
816 $field_content .= '<span class="wcf-cpf-row-setting-label">Name *</span>';
817 $field_content .= '<span class="wcf-cpf-row-setting-field">';
818 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][name]" class="wcf-cpf-name">';
819 $field_content .= '</span>';
820 $field_content .= '</div>';
821
822 $field_content .= '</div>';
823 $field_content .= '</div>';
824 }
825
826 /* Add New Custom Field */
827 $field_content .= '<div class="wcf-cpf-add-row">';
828 $field_content .= '<div class="wcf-cpf-add-wrap">';
829 $field_content .= '<button class="button button-secondary wcf-cpf-add" data-name="wcf-checkout-custom-fields">Add New Field</button>';
830 $field_content .= '</div>';
831 $field_content .= '</div>';
832 /* End Add new custom field */
833
834 $field_content .= '</div>';
835 $field_content .= '</div>';
836 $field_content .= '</div>';
837
838 return $field_content;
839 }
840
841 function get_product_selection_repeater( $field_data ) {
842
843 $value = $field_data['value'];
844
845 if ( ! is_array( $value ) ) {
846
847 $value[0] = array(
848 'product' => '',
849 );
850 } else {
851
852 if ( ! isset( $value[0] ) ) {
853
854 $value[0] = array(
855 'product' => '',
856 );
857 }
858 }
859
860 $field_html = '';
861
862 $field_html .= '<script type="text/html" id="tmpl-wcf-product-repeater">';
863 $field_html .= $this->generate_product_repeater_html( '{{id}}' );
864 $field_html .= '</script>';
865
866 $field_html .= '<div class="wcf-field-row">';
867 $field_html .= '<div class="wcf-field-row-content">';
868 $field_html .= '<div class="wcf-repeatables-wrap">';
869
870 if ( is_array( $value ) ) {
871
872 foreach ( $value as $p_key => $p_data ) {
873
874 $selected_options = '';
875
876 if ( isset( $p_data['product'] ) ) {
877
878 $product = wc_get_product( $p_data['product'] );
879
880 // posts.
881 if ( ! empty( $product ) ) {
882 $post_title = $product->get_name() . ' (#' . $p_data['product'] . ')';
883
884 $selected_options = '<option value="' . $p_data['product'] . '" selected="selected" >' . $post_title . '</option>';
885 }
886 }
887
888 $field_html .= $this->generate_product_repeater_html( $p_key, $selected_options );
889 }
890 }
891
892 $field_html .= '<div class="wcf-add-repeatable-row">';
893 $field_html .= '<div class="submit wcf-add-repeatable-wrap">';
894 $field_html .= '<button class="button-primary wcf-add-repeatable" data-name="wcf-checkout-products">Add New Product</button>';
895 $field_html .= '</div>';
896 $field_html .= '</div>';
897 $field_html .= '</div>';
898 $field_html .= '</div>';
899 $field_html .= '</div>';
900
901 $field_html .= '<div class="wcf-field-row wcf-repeat-notice">';
902 /*$field_html .= '<p><i>The selected products will be automatically added in the cart for this checkout.</i></p>';*/
903 $field_html .= '</div>';
904
905 return $field_html;
906 }
907
908 function generate_product_repeater_html( $id, $options = '' ) {
909
910 $field_html = '<div class="wcf-repeatable-row" data-key="' . $id . '">';
911
912 $field_html .= '<div class="wcf-repeatable-row-standard-fields">';
913
914 /* Product Name */
915 $field_html .= '<div class="wcf-repeatable-fields wcf-sel-product">';
916 $field_html .= '<span class="wcf-repeatable-row-setting-field">';
917 $field_html .= '<select
918 name="wcf-checkout-products[' . $id . '][product]"
919 class="wcf-product-search"
920 data-allow_clear="allow_clear"
921 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
922 data-action="woocommerce_json_search_products_and_variations">';
923 $field_html .= $options;
924 $field_html .= '</select>';
925 $field_html .= '</span>';
926 $field_html .= '<span class="wcf-repeatable-row-actions">';
927 $field_html .= '<a class="wcf-remove-row wcf-repeatable-remove button" data-type="product">';
928 $field_html .= '<span class="dashicons dashicons-trash"></span>';
929 $field_html .= '<span class="wcf-repeatable-remove-button">'. __( 'Remove', 'cartflows' ).'</span>';
930 $field_html .= '</a>';
931 $field_html .= '</span>';
932 $field_html .= '</div>';
933 $field_html .= '</div>';
934 $field_html .= '</div>';
935
936 return $field_html;
937 }
938
939 function get_image_field( $field_data ) {
940
941 $value = $field_data['value'];
942
943 $attr = '';
944
945 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
946
947 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
948 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
949 }
950 }
951
952 $display_preview_box = ( isset( $value ) && '' != $value ) ? 'display:block;' : 'display:none';
953
954 $field_content = '<div id="wcf-image-preview" style="'.$display_preview_box.'">';
955 if( isset( $value ) ){
956 $field_content .= '<img src="'. $value .'" class="saved-image" name="'. $field_data['name'] .'" width="150">';
957 }
958 $field_content .= '</div>';
959 // $field_content .= '<input type="hidden" id="wcf-image-id" class="wcf-image-id" name="wcf-image-id[image-id]" value="">';
960 $field_content .= '<input type="hidden" id="wcf-image-value" class="wcf-image" name="' . $field_data['name'] . '" value="'.$value.'">';
961
962 $field_content .= '<button type="button" ' . $attr . ' class="wcf-select-image button-secondary">Select Image</button>';
963
964 $display_remove_button = ( isset( $value ) && '' != $value ) ? 'display:inline-block; margin-left: 5px;' : 'display:none';
965
966 $field_content .= '<button type="button" class="wcf-remove-image button-secondary" style="'.$display_remove_button.'">Remove Image</button>';
967
968 return $this->get_field( $field_data, $field_content );
969 }
970
971 /**
972 * Localize variables in admin
973 *
974 * @param array $vars variables.
975 */
976 function localize_vars( $vars ) {
977
978 $ajax_actions = array(
979 'wcf_add_checkout_custom_field',
980 'wcf_delete_checkout_custom_field',
981 'wcf_json_search_pages',
982 'wcf_json_search_coupons'
983 );
984
985 foreach ( $ajax_actions as $action ) {
986
987 $vars[ $action . '_nonce' ] = wp_create_nonce( str_replace( '_', '-', $action ) );
988 }
989
990 return $vars;
991 }
992 }
993 // @codingStandardsIgnoreEnd
994