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

941 lines 27.8 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 $label = sanitize_text_field( wp_unslash( $_POST['label'] ) );
164 $name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
165
166 if ( '' !== $name ) {
167
168 $fields = Cartflows_Helper::get_checkout_fields( $add_to, $post_id );
169 $field_keys = array_keys($fields);
170
171 $name = $add_to . '_' . sanitize_key( $name );
172 if( in_array($name, $field_keys) ) {
173 $name = $name . '_' . rand( 0000, 9999 );
174 }
175
176 $field_data = array(
177 'type' => $type,
178 'label' => $label,
179 'placeholder' => '',
180 'options' => array(),
181 'class' => array( 'form-row-wide' ),
182 'label_class' => array(),
183 'required' => true,
184 'custom' => true,
185 );
186
187 Cartflows_Helper::add_checkout_field( $add_to, $name, $field_data, $post_id );
188
189 $key = sanitize_key( $name );
190 $name = 'wcf-' . $key;
191
192 $field_args = array(
193 'label' => $label,
194 'name' => $name,
195 'value' => 'yes',
196 'after' => 'Enable',
197 );
198
199 $field_args['after_html'] = '<span class="wcf-cpf-actions" data-type="billing" data-key="' . $key . '"> | ';
200 $field_args['after_html'] .= '<a class="wcf-cpf-action-remove">' . __( 'Remove', 'cartflows' ) . '</a>';
201 $field_args['after_html'] .= '</span>';
202
203 $field_markup = wcf()->meta->get_checkbox_field( $field_args );
204
205 if( 'billing' === $add_to ) {
206 $add_to_class = 'wcf-cb-fields';
207 } else if( 'shipping' === $add_to ) {
208 $add_to_class = 'wcf-sb-fields';
209 }
210
211 $data = array(
212 'field_data' => $field_data,
213 'field_args' => $field_args,
214 'add_to_class' => $add_to_class,
215 'markup' => $field_markup,
216 );
217
218 wp_send_json( $data );
219 }
220
221 wp_send_json( false );
222
223 }
224
225 /**
226 * [delete_checkout_custom_field description]
227 *
228 * @hook wcf_delete_checkout_custom_field
229 * @return [type] [description]
230 */
231 public function delete_checkout_custom_field() {
232
233 check_ajax_referer( 'wcf-delete-checkout-custom-field', 'security' );
234
235 $post_id = intval( $_POST['post_id'] );
236 $type = sanitize_text_field( wp_unslash( $_POST['type'] ) );
237 $key = sanitize_text_field( wp_unslash( $_POST['key'] ) );
238
239 if ( '' !== $key ) {
240
241 Cartflows_Helper::delete_checkout_field( $type, $key, $post_id );
242
243 wp_send_json( true );
244
245 }
246
247 wp_send_json( false );
248
249 }
250
251 /**
252 * Function to search coupons
253 */
254 public function json_search_pages() {
255
256 check_ajax_referer( 'wcf-json-search-pages', 'security' );
257
258 $term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
259
260 if ( empty( $term ) ) {
261 die( 'not found' );
262 }
263
264 $search_string = $term;
265 $data = array();
266 $result = array();
267
268 add_filter( 'posts_search', array( $this, 'search_only_titles' ), 10, 2 );
269
270 $query = new WP_Query(
271 array(
272 's' => $search_string,
273 'post_type' => 'page',
274 'posts_per_page' => - 1,
275 )
276 );
277
278 if ( $query->have_posts() ) {
279 while ( $query->have_posts() ) {
280 $query->the_post();
281 $title = get_the_title();
282 $title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : '';
283 $id = get_the_id();
284 $data[] = array(
285 'id' => $id,
286 'text' => $title,
287 );
288 }
289 }
290
291 if ( is_array( $data ) && ! empty( $data ) ) {
292 $result[] = array(
293 'text' => '',
294 'children' => $data,
295 );
296 }
297
298 wp_reset_postdata();
299
300 // return the result in json.
301 wp_send_json( $result );
302 }
303
304 public function search_only_titles( $search, $wp_query ) {
305 if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
306 global $wpdb;
307
308 $q = $wp_query->query_vars;
309 $n = ! empty( $q['exact'] ) ? '' : '%';
310
311 $search = array();
312
313 foreach ( (array) $q['search_terms'] as $term ) {
314 $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
315 }
316
317 if ( ! is_user_logged_in() ) {
318 $search[] = "$wpdb->posts.post_password = ''";
319 }
320
321 $search = ' AND ' . implode( ' AND ', $search );
322 }
323
324 return $search;
325 }
326
327 function get_field( $field_data, $field_content ) {
328
329 $label = isset( $field_data['label'] ) ? $field_data['label'] : '';
330 $help = isset( $field_data['help'] ) ? $field_data['help'] : '';
331 $after_html = isset( $field_data['after_html'] ) ? $field_data['after_html'] : '';
332
333 $name_class = 'field-' . $field_data['name'];
334
335 $field_html = '<div class="wcf-field-row ' . $name_class . '">';
336
337 if( ! empty( $label ) || ! empty( $help ) ) {
338 $field_html .= '<div class="wcf-field-row-heading">';
339 if( ! empty( $label ) ) {
340 $field_html .= '<label>' . esc_html( $label ) . '</label>';
341 }
342 if ( ! empty( $help ) ) {
343 $field_html .= '<i class="wcf-field-heading-help dashicons dashicons-editor-help">';
344 // $field_html .= '<span class="wcf-tooltip" data-tooltip= "'. esc_attr( $help ) .'"></span>';
345 $field_html .= '</i>';
346 $field_html .= '<span class="wcf-tooltip-text">';
347 $field_html .= $help;
348 $field_html .= '</span>';
349 }
350 $field_html .= '</div>';
351 }
352
353 $field_html .= '<div class="wcf-field-row-content">';
354 $field_html .= $field_content;
355
356 if ( ! empty( $after_html ) ) {
357 $field_html .= $after_html;
358 }
359
360 $field_html .= '</div>';
361 $field_html .= '</div>';
362
363 return $field_html;
364 }
365
366 function get_text_field( $field_data ) {
367
368 $value = $field_data['value'];
369
370 $attr = '';
371
372 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
373
374 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
375 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
376 }
377 }
378
379 $field_content = '<input type="text" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
380
381 return $this->get_field( $field_data, $field_content );
382 }
383
384 function get_shortcode_field( $field_data ) {
385
386 $attr = '';
387
388 $attr_fields = array(
389 'readonly' => 'readonly',
390 'onfocus' => 'this.select()',
391 'onmouseup' => 'return false',
392 );
393
394 if ( $attr_fields && is_array( $attr_fields ) ) {
395
396 foreach ( $attr_fields as $attr_key => $attr_value ) {
397 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
398 }
399 }
400
401 $field_content = '<input type="text" name="' . $field_data['name'] . '" value="' . $field_data['content'] . '" ' . $attr . '>';
402
403 return $this->get_field( $field_data, $field_content );
404 }
405
406 function get_display_field( $field_data ) {
407
408 $field_content = $field_data['content'];
409
410 return $this->get_field( $field_data, $field_content );
411 }
412
413 function get_number_field( $field_data ) {
414
415 $value = $field_data['value'];
416
417 $attr = '';
418
419 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
420
421 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
422 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
423 }
424 }
425
426 $field_content = '<input type="number" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
427
428 return $this->get_field( $field_data, $field_content );
429 }
430
431 function get_hidden_field( $field_data ) {
432
433 $value = $field_data['value'];
434
435 $attr = '';
436
437 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
438
439 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
440 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
441 }
442 }
443
444 $field_content = '<input type="hidden" id="' . $field_data['name'] . '" name="' . $field_data['name'] . '" value="' . $value . '" ' . $attr . '>';
445
446 return $this->get_field( $field_data, $field_content );
447 }
448
449 function get_area_field( $field_data ) {
450
451 $value = $field_data['value'];
452
453 $field_content = '<textarea name="' . $field_data['name'] . '" rows="10" cols="50">';
454 $field_content .= $value;
455 $field_content .= '</textarea>';
456
457 return $this->get_field( $field_data, $field_content );
458 }
459
460 function get_checkbox_field( $field_data ) {
461
462 $value = $field_data['value'];
463
464 $field_content = '';
465 if ( isset( $field_data['before'] ) ) {
466 $field_content .= '<span>' . $field_data['before'] . '</span>';
467 }
468 $field_content .= '<input type="hidden" name="' . $field_data['name'] . '" value="no">';
469 $field_content .= '<input type="checkbox" name="' . $field_data['name'] . '" value="yes" ' . checked( 'yes', $value, false ) . '>';
470
471 if ( isset( $field_data['after'] ) ) {
472 $field_content .= '<span>' . $field_data['after'] . '</span>';
473 }
474
475 return $this->get_field( $field_data, $field_content );
476 }
477
478 function get_font_family_field( $field_data ) {
479
480 $value = $field_data['value'];
481
482 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
483
484 $field_content = '<select class="wcf-field-font-family" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
485
486 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
487
488 $field_content .= '<optgroup label="Other System Fonts">';
489 foreach ( CartFlows_Font_Families::get_system_fonts() as $name => $variants ) {
490 $field_content .= '<option value="' . esc_attr( $name ) . '" ' . selected( $name, $value, false ) . '>' . esc_attr( $name ) . '</option>';
491 }
492 $field_content .= '</optgroup>';
493 $field_content .= '<optgroup label="Google">';
494 foreach ( CartFlows_Font_Families::get_google_fonts() as $name => $single_font ) {
495 $variants = wcf_get_prop( $single_font, '0' );
496 $category = wcf_get_prop( $single_font, '1' );
497 $font_value = '\'' . esc_attr( $name ) . '\', ' . esc_attr( $category );
498 $field_content .= '<option value="' . esc_attr( $font_value ) . '" ' . selected( $font_value, $value, false ) . '>' . esc_attr( $name ) . '</option>';
499 }
500 $field_content .= '</optgroup>';
501
502 $field_content .= '</select>';
503
504 return $this->get_field( $field_data, $field_content );
505 }
506
507 function get_font_weight_field( $field_data ) {
508
509 $value = $field_data['value'];
510
511 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
512
513 $field_content = '<select data-selected="'.esc_attr( $value ).'" class="wcf-field-font-weight" data-for="' . $field_data['for'] . '" name="' . $field_data['name'] . '">';
514
515 $field_content .= '<option value="" ' . selected( '', $value, false ) . '>Default</option>';
516
517 $field_content .= '</select>';
518
519 return $this->get_field( $field_data, $field_content );
520 }
521
522 function get_select_field( $field_data ) {
523
524 $value = $field_data['value'];
525 $pro_options = isset( $field_data['pro-options'] ) ? $field_data['pro-options'] : array();
526
527 $field_content = '<select name="' . $field_data['name'] . '">';
528
529
530 if ( is_array( $field_data['options'] ) && ! empty( $field_data['options'] ) ) {
531
532 foreach ( $field_data['options'] as $data_key => $data_value ) {
533
534 $disabled = '';
535
536 if ( array_key_exists( $data_key, $pro_options ) ) {
537 $disabled = 'disabled ';
538 $data_value = $pro_options[ $data_key ];
539 }
540
541 $field_content .= '<option value="' . $data_key . '" ' . selected( $value, $data_key, false ) . ' ' . $disabled .'>' . $data_value . '</option>';
542 }
543 }
544
545 $field_content .= '</select>';
546
547 if ( isset( $field_data['after'] ) ) {
548 $field_content .= '<span>' . $field_data['after'] . '</span>';
549 }
550
551 return $this->get_field( $field_data, $field_content );
552 }
553
554 function get_color_picker_field( $field_data ) {
555
556 $value = $field_data['value'];
557
558 $field_content = '<input class="wcf-color-picker" type="text" name="' . $field_data['name'] . '" value="' . $value . '">';
559
560 return $this->get_field( $field_data, $field_content );
561 }
562
563 function get_product_selection_field( $field_data ) {
564
565 $value = $field_data['value'];
566
567 $multiple = '';
568
569 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
570 $multiple = ' multiple="multiple"';
571 }
572
573 $allow_clear = '';
574
575 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
576 $allow_clear = ' data-allow_clear="allow_clear"';
577 }
578
579 $field_content = '<select
580 name="' . $field_data['name'] . '[]"
581 class="wcf-product-search" ' . $multiple . $allow_clear . '
582 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
583 data-action="woocommerce_json_search_products_and_variations">';
584
585 if ( is_array( $value ) && ! empty( $value ) ) {
586
587 foreach ( $value as $data_key => $product_id ) {
588
589 $product = wc_get_product( $product_id );
590
591 // posts.
592 if ( ! empty( $product ) ) {
593 $post_title = $product->get_name() . ' (#' . $product_id . ')';
594
595 $field_content .= '<option value="' . $product_id . '" selected="selected" >' . $post_title . '</option>';
596 }
597 }
598 }
599 $field_content .= '</select>';
600
601 return $this->get_field( $field_data, $field_content );
602 }
603
604 function get_coupon_selection_field( $field_data ) {
605
606 $value = $field_data['value'];
607
608 $multiple = '';
609
610 if ( isset( $field_data['multiple'] ) && $field_data['multiple'] ) {
611 $multiple = ' multiple="multiple"';
612 }
613
614 $allow_clear = '';
615
616 if ( isset( $field_data['allow_clear'] ) && $field_data['allow_clear'] ) {
617 $allow_clear = ' data-allow_clear="allow_clear"';
618 }
619
620 $field_content = '<select
621 name="' . $field_data['name'] . '[]"
622 class="wc-coupon-search wcf-coupon-search" ' . $multiple . $allow_clear . '
623 data-placeholder="' . __( 'Search for a coupon&hellip;', 'cartflows' ) . '"
624 data-action="wcf_json_search_coupons">';
625
626 if ( is_array( $value ) && ! empty( $value ) ) {
627
628 $all_discount_types = wc_get_coupon_types();
629
630 foreach ( $value as $coupon_title ) {
631
632 $coupon = new WC_Coupon( $coupon_title );
633
634 $discount_type = $coupon->get_discount_type();
635
636 if ( isset( $discount_type ) && $discount_type ) {
637 $discount_type = ' ( Type: ' . $all_discount_types[ $discount_type ] . ' )';
638 }
639
640 $field_content .= '<option value="' . $coupon_title . '" selected="selected">' . $coupon_title . $discount_type . '</option>';
641 }
642 }
643
644 $field_content .= '</select>';
645
646 return $this->get_field( $field_data, $field_content );
647 }
648
649 function get_page_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 $field_content = '<select
660 name="' . $field_data['name'] . '[]"
661 class="wcf-search-pages" ' . $multiple . '"
662 data-action="wcf_json_search_pages">';
663
664 if ( is_array( $value ) && ! empty( $value ) ) {
665
666 foreach ( $value as $data_key => $data_value ) {
667
668 $field_content .= '<option value="' . $data_value . '">' . get_the_title( $data_value ) . '</option>';
669 }
670 }
671
672 $field_content .= '</select>';
673
674 return $this->get_field( $field_data, $field_content );
675 }
676
677 function get_section( $field_data ) {
678 $field_html = '<div class="wcf-field-row wcf-field-section">';
679 $field_html .= '<div class="wcf-field-section-heading" colspan="2">';
680 $field_html .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
681
682 if ( isset( $field_data['help'] ) ) {
683 $field_html .= '<i class="wcf-field-heading-help dashicons dashicons-editor-help" title="' . esc_attr( $field_data['help'] ) . '"></i>';
684 }
685 $field_html .= '</div>';
686 $field_html .= '</div>';
687 return $field_html;
688 }
689
690 function get_description_field( $field_data ) {
691
692 $field_html = '<div class="wcf-field-row wcf-field-desc ' . $field_data['name'] . '">';
693 $field_html .= '<div class="wcf-field-desc-content">';
694 $field_html .= $field_data['content'];
695 $field_html .= '</div>';
696 $field_html .= '</div>';
697
698 return $field_html;
699 }
700
701 function get_checkout_field_repeater( $field_data ) {
702
703 $value = array();
704
705 $value[0] = array(
706 'add_to' => '',
707 'type' => '',
708 'label' => '',
709 'name' => '',
710 );
711
712 $field_content = '';
713
714 $field_content .= '<div class="wcf-field-row">';
715 // $field_content .= '<div class="wcf-field-row-heading">';
716 // $field_content .= '<label>' . esc_html( $field_data['label'] ) . '</label>';
717 // $field_content .= '</div>';
718 $field_content .= '<div class="wcf-field-row-content">';
719 $field_content .= '<div class="wcf-cpf-wrap">';
720
721 foreach ( $value as $p_key => $p_data ) {
722 $field_content .= '<div class="wcf-cpf-row" data-key="' . $p_key . '">';
723 $field_content .= '<div class="wcf-cpf-row-header">';
724 $field_content .= '<span class="wcf-cpf-row-title">Add New Custom Field</span>';
725 $field_content .= '</div>';
726
727 $field_content .= '<div class="wcf-cpf-row-standard-fields">';
728
729 /* Add To */
730 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-add_to">';
731 $field_content .= '<span class="wcf-cpf-row-setting-label">Add to</span>';
732 $field_content .= '<span class="wcf-cpf-row-setting-field">';
733 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][add_to]" class="wcf-cpf-add_to">';
734 $field_content .= '<option value="billing">Billing</option>';
735 $field_content .= '<option value="shipping">Shipping</option>';
736 $field_content .= '</select>';
737 $field_content .= '</span>';
738 $field_content .= '</div>';
739
740 /* Type */
741 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-type">';
742 $field_content .= '<span class="wcf-cpf-row-setting-label">Type</span>';
743 $field_content .= '<span class="wcf-cpf-row-setting-field">';
744 $field_content .= '<select name="wcf-checkout-custom-fields[' . $p_key . '][type]" class="wcf-cpf-type">';
745 $field_content .= '<option value="text">Text</option>';
746 $field_content .= '<option value="textarea">Textarea</option>';
747 $field_content .= '<option value="select">Select</option>';
748 $field_content .= '<option value="checkbox">Checkbox</option>';
749 $field_content .= '</select>';
750 $field_content .= '</span>';
751 $field_content .= '</div>';
752
753 /* Label */
754 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-label">';
755 $field_content .= '<span class="wcf-cpf-row-setting-label">Label *</span>';
756 $field_content .= '<span class="wcf-cpf-row-setting-field">';
757 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][label]" class="wcf-cpf-label">';
758 $field_content .= '</span>';
759 $field_content .= '</div>';
760
761 /* Name */
762 $field_content .= '<div class="wcf-cpf-fields wcf-cpf-name">';
763 $field_content .= '<span class="wcf-cpf-row-setting-label">Name *</span>';
764 $field_content .= '<span class="wcf-cpf-row-setting-field">';
765 $field_content .= '<input type="text" value="" name="wcf-checkout-custom-fields[' . $p_key . '][name]" class="wcf-cpf-name">';
766 $field_content .= '</span>';
767 $field_content .= '</div>';
768
769 $field_content .= '</div>';
770 $field_content .= '</div>';
771 }
772
773 /* Add New Custom Field */
774 $field_content .= '<div class="wcf-cpf-add-row">';
775 $field_content .= '<div class="wcf-cpf-add-wrap">';
776 $field_content .= '<button class="button button-secondary wcf-cpf-add" data-name="wcf-checkout-custom-fields">Add New Field</button>';
777 $field_content .= '</div>';
778 $field_content .= '</div>';
779 /* End Add new custom field */
780
781 $field_content .= '</div>';
782 $field_content .= '</div>';
783 $field_content .= '</div>';
784
785 return $field_content;
786 }
787
788 function get_product_selection_repeater( $field_data ) {
789
790 $value = $field_data['value'];
791
792 if ( ! is_array( $value ) ) {
793
794 $value[0] = array(
795 'product' => '',
796 );
797 } else {
798
799 if ( ! isset( $value[0] ) ) {
800
801 $value[0] = array(
802 'product' => '',
803 );
804 }
805 }
806
807 $field_html = '';
808
809 $field_html .= '<script type="text/html" id="tmpl-wcf-product-repeater">';
810 $field_html .= $this->generate_product_repeater_html( '{{id}}' );
811 $field_html .= '</script>';
812
813 $field_html .= '<div class="wcf-field-row">';
814 $field_html .= '<div class="wcf-field-row-content">';
815 $field_html .= '<div class="wcf-repeatables-wrap">';
816
817 if ( is_array( $value ) ) {
818
819 foreach ( $value as $p_key => $p_data ) {
820
821 $selected_options = '';
822
823 if ( isset( $p_data['product'] ) ) {
824
825 $product = wc_get_product( $p_data['product'] );
826
827 // posts.
828 if ( ! empty( $product ) ) {
829 $post_title = $product->get_name() . ' (#' . $p_data['product'] . ')';
830
831 $selected_options = '<option value="' . $p_data['product'] . '" selected="selected" >' . $post_title . '</option>';
832 }
833 }
834
835 $field_html .= $this->generate_product_repeater_html( $p_key, $selected_options );
836 }
837 }
838
839 $field_html .= '<div class="wcf-add-repeatable-row">';
840 $field_html .= '<div class="submit wcf-add-repeatable-wrap">';
841 $field_html .= '<button class="button-primary wcf-add-repeatable" data-name="wcf-checkout-products">Add New Product</button>';
842 $field_html .= '</div>';
843 $field_html .= '</div>';
844 $field_html .= '</div>';
845 $field_html .= '</div>';
846 $field_html .= '</div>';
847
848 $field_html .= '<div class="wcf-field-row wcf-repeat-notice">';
849 $field_html .= '<p><i>The selected products will be automatically added in the cart for this checkout.</i></p>';
850 $field_html .= '</div>';
851
852 return $field_html;
853 }
854
855 function generate_product_repeater_html( $id, $options = '' ) {
856
857 $field_html = '<div class="wcf-repeatable-row" data-key="' . $id . '">';
858
859 $field_html .= '<div class="wcf-repeatable-row-standard-fields">';
860
861 /* Product Name */
862 $field_html .= '<div class="wcf-repeatable-fields wcf-sel-product">';
863 $field_html .= '<span class="wcf-repeatable-row-setting-field">';
864 $field_html .= '<select
865 name="wcf-checkout-products[' . $id . '][product]"
866 class="wcf-product-search"
867 data-allow_clear="allow_clear"
868 data-placeholder="' . __( 'Search for a product&hellip;', 'cartflows' ) . '"
869 data-action="woocommerce_json_search_products_and_variations">';
870 $field_html .= $options;
871 $field_html .= '</select>';
872 $field_html .= '</span>';
873 $field_html .= '<span class="wcf-repeatable-row-actions">';
874 $field_html .= '<a class="wcf-remove-row wcf-repeatable-remove button" data-type="product">';
875 $field_html .= '<span class="dashicons dashicons-trash"></span>';
876 $field_html .= '<span class="wcf-repeatable-remove-button">'. __( 'Remove', 'cartflows' ).'</span>';
877 $field_html .= '</a>';
878 $field_html .= '</span>';
879 $field_html .= '</div>';
880 $field_html .= '</div>';
881 $field_html .= '</div>';
882
883 return $field_html;
884 }
885
886 function get_image_field( $field_data ) {
887
888 $value = $field_data['value'];
889
890 $attr = '';
891
892 if ( isset( $field_data['attr'] ) && is_array( $field_data['attr'] ) ) {
893
894 foreach ( $field_data['attr'] as $attr_key => $attr_value ) {
895 $attr .= ' ' . $attr_key . '="' . $attr_value . '"';
896 }
897 }
898
899 $display_preview_box = ( isset( $value ) && '' != $value ) ? 'display:block;' : 'display:none';
900
901 $field_content = '<div id="wcf-image-preview" style="'.$display_preview_box.'">';
902 if( isset( $value ) ){
903 $field_content .= '<img src="'. $value .'" class="saved-image" name="'. $field_data['name'] .'" width="150">';
904 }
905 $field_content .= '</div>';
906 // $field_content .= '<input type="hidden" id="wcf-image-id" class="wcf-image-id" name="wcf-image-id[image-id]" value="">';
907 $field_content .= '<input type="hidden" id="wcf-image-value" class="wcf-image" name="' . $field_data['name'] . '" value="'.$value.'">';
908
909 $field_content .= '<button type="button" ' . $attr . ' class="wcf-select-image button-secondary">Select Image</button>';
910
911 $display_remove_button = ( isset( $value ) && '' != $value ) ? 'display:inline-block; margin-left: 5px;' : 'display:none';
912
913 $field_content .= '<button type="button" class="wcf-remove-image button-secondary" style="'.$display_remove_button.'">Remove Image</button>';
914
915 return $this->get_field( $field_data, $field_content );
916 }
917
918 /**
919 * Localize variables in admin
920 *
921 * @param array $vars variables.
922 */
923 function localize_vars( $vars ) {
924
925 $ajax_actions = array(
926 'wcf_add_checkout_custom_field',
927 'wcf_delete_checkout_custom_field',
928 'wcf_json_search_pages',
929 'wcf_json_search_coupons'
930 );
931
932 foreach ( $ajax_actions as $action ) {
933
934 $vars[ $action . '_nonce' ] = wp_create_nonce( str_replace( '_', '-', $action ) );
935 }
936
937 return $vars;
938 }
939 }
940 // @codingStandardsIgnoreEnd
941