PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.0.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.0.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.0.1, at classes/class-cartflows-meta-fields.php

963 lines 28.7 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_number_field( $field_data ) {
428
429 $value = $field_data['value'];
430
431 $attr = '';
432
433 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
434
435 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
436 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
437 }
438 }
439
440 $field_content = '<input type="number" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
441
442 return $this->get_field( $field_data, $field_content );
443 }
444
445 function get_hidden_field( $field_data ) {
446
447 $value = $field_data['value'];
448
449 $attr = '';
450
451 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
452
453 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
454 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
455 }
456 }
457
458 $field_content = '<input type="hidden" id="' . $field_data['name'] . '" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
459
460 return $this->get_field( $field_data, $field_content );
461 }
462
463 function get_area_field( $field_data ) {
464
465 $value = $field_data['value'];
466
467 $field_content = '<textarea name="' . $field_data['name'] . '" rows="10" cols="50">';
468 $field_content .= $value;
469 $field_content .= '</textarea>';
470
471 return $this->get_field( $field_data, $field_content );
472 }
473
474 function get_checkbox_field( $field_data ) {
475
476 $value = $field_data['value'];
477
478 $field_content = '';
479 if ( isset( $field_data['before'] ) ) {
480 $field_content .= '<span>' . $field_data['before'] . '</span>';
481 }
482 $field_content .= '<input type="hidden" name="' . $field_data['name'] . '" value="no">';
483 $field_content .= '<input type="checkbox" name="' . $field_data['name'] . '" value="yes" ' . checked( 'yes', $value, false ) . '>';
484
485 if ( isset( $field_data['after'] ) ) {
486 $field_content .= '<span>' . $field_data['after'] . '</span>';
487 }
488
489 return $this->get_field( $field_data, $field_content );
490 }
491
492 function get_font_family_field( $field_data ) {
493
494 $value = $field_data['value'];
495
496 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
497
498 $field_content = '<select class="wcf-field-font-family" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
499
500 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
501
502 $field_content .= '<optgroup label="Other System Fonts">';
503 foreach ( CartFlows_Font_Families::get_system_fonts() as $name => $variants ) {
504 $field_content .= '<option value="' . esc_attr( $name ) . '" ' . selected( $name, $value, false ) . '>' . esc_attr( $name ) . '</option>';
505 }
506 $field_content .= '</optgroup>';
507 $field_content .= '<optgroup label="Google">';
508 foreach ( CartFlows_Font_Families::get_google_fonts() as $name => $single_font ) {
509 $variants = wcf_get_prop( $single_font, '0' );
510 $category = wcf_get_prop( $single_font, '1' );
511 $font_value = '\'' . esc_attr( $name ) . '\', ' . esc_attr( $category );
512 $field_content .= '<option value="' . esc_attr( $font_value ) . '" ' . selected( $font_value, $value, false ) . '>' . esc_attr( $name ) . '</option>';
513 }
514 $field_content .= '</optgroup>';
515
516 $field_content .= '</select>';
517
518 return $this->get_field( $field_data, $field_content );
519 }
520
521 function get_font_weight_field( $field_data ) {
522
523 $value = $field_data['value'];
524
525 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
526
527 $field_content = '<select data-selected="'.esc_attr( $value ).'" class="wcf-field-font-weight" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
528
529 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
530
531 $field_content .= '</select>';
532
533 return $this->get_field( $field_data, $field_content );
534 }
535
536 function get_select_field( $field_data ) {
537
538 $value = $field_data['value'];
539 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
540
541 $field_content = '<select name="' . $field_data['name'] . '">';
542
543
544 if ( is_array( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
545
546 foreach ( $field_data['options'] as $data_key => $data_value ) {
547
548 $disabled = '';
549
550 if ( array_key_exists( $data_key, $pro_options ) ) {
551 $disabled = 'disabled ';
552 $data_value = $pro_options[ $data_key ];
553 }
554
555 $field_content .= '<option value="' . $data_key . '" ' . selected( $value, $data_key, false ) . ' ' . $disabled .'>' . $data_value . '</option>';
556 }
557 }
558
559 $field_content .= '</select>';
560
561 if ( isset( $field_data['after'] ) ) {
562 $field_content .= '<span>' . $field_data['after'] . '</span>';
563 }
564
565 return $this->get_field( $field_data, $field_content );
566 }
567
568 function get_color_picker_field( $field_data ) {
569
570 $value = $field_data['value'];
571
572 $field_content = '<input class="wcf-color-picker" type="text" name="' . $field_data['name'] . '" value="' . $value . '">';
573
574 return $this->get_field( $field_data, $field_content );
575 }
576
577 function get_product_selection_field( $field_data ) {
578
579 $value = $field_data['value'];
580
581 $multiple = '';
582
583 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
584 $multiple = ' multiple="multiple"';
585 }
586
587 $allow_clear = '';
588
589 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
590 $allow_clear = ' data-allow_clear="allow_clear"';
591 }
592
593 $field_content = '<select
594 name="' . $field_data['name'] . '[]"
595 class="wcf-product-search" ' . $multiple . $allow_clear . '
596 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
597 data-action="woocommerce_json_search_products_and_variations">';
598
599 if ( is_array( $value ) && ! empty( $value ) ) {
600
601 foreach ( $value as $data_key => $product_id ) {
602
603 $product = wc_get_product( $product_id );
604
605 // posts.
606 if ( ! empty( $product ) ) {
607 $post_title = $product->get_name() . ' (#' . $product_id . ')';
608
609 $field_content .= '<option value="' . $product_id . '" selected="selected" >' . $post_title . '</option>';
610 }
611 }
612 }
613 $field_content .= '</select>';
614
615 return $this->get_field( $field_data, $field_content );
616 }
617
618 function get_coupon_selection_field( $field_data ) {
619
620 $value = $field_data['value'];
621
622 $multiple = '';
623
624 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
625 $multiple = ' multiple="multiple"';
626 }
627
628 $allow_clear = '';
629
630 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
631 $allow_clear = ' data-allow_clear="allow_clear"';
632 }
633
634 $field_content = '<select
635 name="' . $field_data['name'] . '[]"
636 class="wc-coupon-search wcf-coupon-search" ' . $multiple . $allow_clear . '
637 data-placeholder="' . __( 'Search for a coupon&hellip;', 'cartflows' ) . '"
638 data-action="wcf_json_search_coupons">';
639
640 if ( is_array( $value ) && ! empty( $value ) ) {
641
642 $all_discount_types = wc_get_coupon_types();
643
644 foreach ( $value as $coupon_title ) {
645
646 $coupon = new WC_Coupon( $coupon_title );
647
648 $discount_type = $coupon->get_discount_type();
649
650 if ( isset( $discount_type ) && $discount_type ) {
651 $discount_type = ' ( Type: ' . $all_discount_types[ $discount_type ] . ' )';
652 }
653
654 $field_content .= '<option value="' . $coupon_title . '" selected="selected">' . $coupon_title . $discount_type . '</option>';
655 }
656 }
657
658 $field_content .= '</select>';
659
660 return $this->get_field( $field_data, $field_content );
661 }
662
663 function get_page_selection_field( $field_data ) {
664
665 $value = $field_data['value'];
666
667 $multiple = '';
668
669 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
670 $multiple = 'multiple="multiple"';
671 }
672
673 $field_content = '<select
674 name="' . $field_data['name'] . '[]"
675 class="wcf-search-pages" ' . $multiple . '"
676 data-action="wcf_json_search_pages">';
677
678 if ( is_array( $value ) && ! empty( $value ) ) {
679
680 foreach ( $value as $data_key => $data_value ) {
681
682 $field_content .= '<option value="' . $data_value . '">' . get_the_title( $data_value ) . '</option>';
683 }
684 }
685
686 $field_content .= '</select>';
687
688 return $this->get_field( $field_data, $field_content );
689 }
690
691 function get_section( $field_data ) {
692 $field_html = '<div class="wcf-field-row wcf-field-section">';
693 $field_html .= '<div class="wcf-field-section-heading" colspan="2">';
694 $field_html .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
695
696 if ( isset( $field_data['help'] ) ) {
697 $field_html .= '<i class="wcf-field-heading-help dashicons dashicons-editor-help" title="' . esc_attr( $field_data['help'] ) . '"></i>';
698 }
699 $field_html .= '</div>';
700 $field_html .= '</div>';
701 return $field_html;
702 }
703
704 function get_description_field( $field_data ) {
705
706 $field_html = '<div class="wcf-field-row wcf-field-desc ' . $field_data['name'] . '">';
707 $field_html .= '<div class="wcf-field-desc-content">';
708 $field_html .= $field_data['content'];
709 $field_html .= '</div>';
710 $field_html .= '</div>';
711
712 return $field_html;
713 }
714
715 function get_checkout_field_repeater( $field_data ) {
716
717 $value = array();
718
719 $value[0] = array(
720 'add_to' => '',
721 'type' => '',
722 'label' => '',
723 'name' => '',
724 );
725
726 $field_content = '';
727
728 $field_content .= '<div class="wcf-field-row">';
729 // $field_content .= '<div class="wcf-field-row-heading">';
730 // $field_content .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
731 // $field_content .= '</div>';
732 $field_content .= '<div class="wcf-field-row-content">';
733 $field_content .= '<div class="wcf-cpf-wrap">';
734
735 foreach ( $value as $p_key => $p_data ) {
736 $field_content .= '<div class="wcf-cpf-row" data-key="' . $p_key . '">';
737 $field_content .= '<div class="wcf-cpf-row-header">';
738 $field_content .= '<span class="wcf-cpf-row-title">Add New Custom Field</span>';
739 $field_content .= '</div>';
740
741 $field_content .= '<div class="wcf-cpf-row-standard-fields">';
742
743 /* Add To */
744 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-add_to">';
745 $field_content .= '<span class="wcf-cpf-row-setting-label">Add to</span>';
746 $field_content .= '<span class="wcf-cpf-row-setting-field">';
747 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][add_to]" class="wcf-cpf-add_to">';
748 $field_content .= '<option value="billing">Billing</option>';
749 $field_content .= '<option value="shipping">Shipping</option>';
750 $field_content .= '</select>';
751 $field_content .= '</span>';
752 $field_content .= '</div>';
753
754 /* Type */
755 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-type">';
756 $field_content .= '<span class="wcf-cpf-row-setting-label">Type</span>';
757 $field_content .= '<span class="wcf-cpf-row-setting-field">';
758 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][type]" class="wcf-cpf-type">';
759 $field_content .= '<option value="text">Text</option>';
760 $field_content .= '<option value="textarea">Textarea</option>';
761 $field_content .= '<option value="select">Select</option>';
762 $field_content .= '<option value="checkbox">Checkbox</option>';
763 $field_content .= '</select>';
764 $field_content .= '</span>';
765 $field_content .= '</div>';
766
767 /* Textarea */
768 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-options">';
769 $field_content .= '<span class="wcf-cpf-row-setting-label">Options *</span>';
770 $field_content .= '<span class="wcf-cpf-row-setting-field">';
771 $field_content .= '<textarea value="" name="wcf-checkout-custom-fields[' . $p_key . '][label]" class="wcf-cpf-options" placeholder="Enter your options separated by comma."></textarea>';
772 $field_content .= '</span>';
773 $field_content .= '</div>';
774
775 /* Label */
776 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-label">';
777 $field_content .= '<span class="wcf-cpf-row-setting-label">Label *</span>';
778 $field_content .= '<span class="wcf-cpf-row-setting-field">';
779 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][label]" class="wcf-cpf-label">';
780 $field_content .= '</span>';
781 $field_content .= '</div>';
782
783 /* Name */
784 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-name">';
785 $field_content .= '<span class="wcf-cpf-row-setting-label">Name *</span>';
786 $field_content .= '<span class="wcf-cpf-row-setting-field">';
787 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][name]" class="wcf-cpf-name">';
788 $field_content .= '</span>';
789 $field_content .= '</div>';
790
791 $field_content .= '</div>';
792 $field_content .= '</div>';
793 }
794
795 /* Add New Custom Field */
796 $field_content .= '<div class="wcf-cpf-add-row">';
797 $field_content .= '<div class="wcf-cpf-add-wrap">';
798 $field_content .= '<button class="button button-secondary wcf-cpf-add" data-name="wcf-checkout-custom-fields">Add New Field</button>';
799 $field_content .= '</div>';
800 $field_content .= '</div>';
801 /* End Add new custom field */
802
803 $field_content .= '</div>';
804 $field_content .= '</div>';
805 $field_content .= '</div>';
806
807 return $field_content;
808 }
809
810 function get_product_selection_repeater( $field_data ) {
811
812 $value = $field_data['value'];
813
814 if ( ! is_array( $value ) ) {
815
816 $value[0] = array(
817 'product' => '',
818 );
819 } else {
820
821 if ( ! isset( $value[0] ) ) {
822
823 $value[0] = array(
824 'product' => '',
825 );
826 }
827 }
828
829 $field_html = '';
830
831 $field_html .= '<script type="text/html" id="tmpl-wcf-product-repeater">';
832 $field_html .= $this->generate_product_repeater_html( '{{id}}' );
833 $field_html .= '</script>';
834
835 $field_html .= '<div class="wcf-field-row">';
836 $field_html .= '<div class="wcf-field-row-content">';
837 $field_html .= '<div class="wcf-repeatables-wrap">';
838
839 if ( is_array( $value ) ) {
840
841 foreach ( $value as $p_key => $p_data ) {
842
843 $selected_options = '';
844
845 if ( isset( $p_data['product'] ) ) {
846
847 $product = wc_get_product( $p_data['product'] );
848
849 // posts.
850 if ( ! empty( $product ) ) {
851 $post_title = $product->get_name() . ' (#' . $p_data['product'] . ')';
852
853 $selected_options = '<option value="' . $p_data['product'] . '" selected="selected" >' . $post_title . '</option>';
854 }
855 }
856
857 $field_html .= $this->generate_product_repeater_html( $p_key, $selected_options );
858 }
859 }
860
861 $field_html .= '<div class="wcf-add-repeatable-row">';
862 $field_html .= '<div class="submit wcf-add-repeatable-wrap">';
863 $field_html .= '<button class="button-primary wcf-add-repeatable" data-name="wcf-checkout-products">Add New Product</button>';
864 $field_html .= '</div>';
865 $field_html .= '</div>';
866 $field_html .= '</div>';
867 $field_html .= '</div>';
868 $field_html .= '</div>';
869
870 $field_html .= '<div class="wcf-field-row wcf-repeat-notice">';
871 $field_html .= '<p><i>The selected products will be automatically added in the cart for this checkout.</i></p>';
872 $field_html .= '</div>';
873
874 return $field_html;
875 }
876
877 function generate_product_repeater_html( $id, $options = '' ) {
878
879 $field_html = '<div class="wcf-repeatable-row" data-key="' . $id . '">';
880
881 $field_html .= '<div class="wcf-repeatable-row-standard-fields">';
882
883 /* Product Name */
884 $field_html .= '<div class="wcf-repeatable-fields wcf-sel-product">';
885 $field_html .= '<span class="wcf-repeatable-row-setting-field">';
886 $field_html .= '<select
887 name="wcf-checkout-products[' . $id . '][product]"
888 class="wcf-product-search"
889 data-allow_clear="allow_clear"
890 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
891 data-action="woocommerce_json_search_products_and_variations">';
892 $field_html .= $options;
893 $field_html .= '</select>';
894 $field_html .= '</span>';
895 $field_html .= '<span class="wcf-repeatable-row-actions">';
896 $field_html .= '<a class="wcf-remove-row wcf-repeatable-remove button" data-type="product">';
897 $field_html .= '<span class="dashicons dashicons-trash"></span>';
898 $field_html .= '<span class="wcf-repeatable-remove-button">'. __( 'Remove', 'cartflows' ).'</span>';
899 $field_html .= '</a>';
900 $field_html .= '</span>';
901 $field_html .= '</div>';
902 $field_html .= '</div>';
903 $field_html .= '</div>';
904
905 return $field_html;
906 }
907
908 function get_image_field( $field_data ) {
909
910 $value = $field_data['value'];
911
912 $attr = '';
913
914 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
915
916 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
917 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
918 }
919 }
920
921 $display_preview_box = ( isset( $value ) && '' != $value ) ? 'display:block;' : 'display:none';
922
923 $field_content = '<div id="wcf-image-preview" style="'.$display_preview_box.'">';
924 if( isset( $value ) ){
925 $field_content .= '<img src="'. $value .'" class="saved-image" name="'. $field_data['name'] .'" width="150">';
926 }
927 $field_content .= '</div>';
928 // $field_content .= '<input type="hidden" id="wcf-image-id" class="wcf-image-id" name="wcf-image-id[image-id]" value="">';
929 $field_content .= '<input type="hidden" id="wcf-image-value" class="wcf-image" name="' . $field_data['name'] . '" value="'.$value.'">';
930
931 $field_content .= '<button type="button" ' . $attr . ' class="wcf-select-image button-secondary">Select Image</button>';
932
933 $display_remove_button = ( isset( $value ) && '' != $value ) ? 'display:inline-block; margin-left: 5px;' : 'display:none';
934
935 $field_content .= '<button type="button" class="wcf-remove-image button-secondary" style="'.$display_remove_button.'">Remove Image</button>';
936
937 return $this->get_field( $field_data, $field_content );
938 }
939
940 /**
941 * Localize variables in admin
942 *
943 * @param array $vars variables.
944 */
945 function localize_vars( $vars ) {
946
947 $ajax_actions = array(
948 'wcf_add_checkout_custom_field',
949 'wcf_delete_checkout_custom_field',
950 'wcf_json_search_pages',
951 'wcf_json_search_coupons'
952 );
953
954 foreach ( $ajax_actions as $action ) {
955
956 $vars[ $action . '_nonce' ] = wp_create_nonce( str_replace( '_', '-', $action ) );
957 }
958
959 return $vars;
960 }
961 }
962 // @codingStandardsIgnoreEnd
963