· 2 years ago · Feb 13, 2023, 04:40 PM
1<?php
2/*
3Plugin Name: WP SendFox
4Plugin URI: https://wordpress.org/plugins/wp-sendfox/
5Description: Capture emails and add them to your SendFox list via comments, registration, WooCommerce checkout, Gutenberg page or Divi Builder page. Export your WP users and WooCommerce customers to your list.
6Author: BogdanFix
7Author URI: https://bogdanfix.com/
8Version: 1.2.0
9Text Domain: sf4wp
10Domain Path: /lang
11License: GNU General Public License v3.0
12License URI: http://www.gnu.org/licenses/gpl-3.0.html
13WC requires at least: 3.0.0
14WC tested up to: 7.2.0
15*/
16
17define( 'GB_SF4WP_NAME', 'SendFox for WordPress' );
18define( 'GB_SF4WP_VER', '1.2.0' );
19define( 'GB_SF4WP_ID', 'wp-sendfox' );
20
21define( 'GB_SF4WP_CORE_FILE', __FILE__ );
22
23define( 'GB_SF4WP_USERS_PER_STEP', 5 );
24define( 'GB_SF4WP_STEP_TIMEOUT', 250 );
25
26$checkbox_label_text='subscribe to our newsletter';
27
28/**
29 * Init and hook most of the subscribe forms
30 *
31 * @since 1.0.0
32 */
33
34function gb_sf4wp_init()
35{
36 // comment form
37
38 add_action( 'comment_form_after_fields', 'gb_sf4wp_comment_form' );
39
40 // registration form
41
42 add_action( 'register_form', 'gb_sf4wp_registration_form' );
43
44 // checkout form
45
46 $options = get_option( 'gb_sf4wp_options' );
47
48 if( !empty( $options['woocommerce-checkout'] ) )
49 {
50 $form = $options['woocommerce-checkout'];
51
52 if( empty( $form['position'] ) )
53 {
54 $form['position'] = 'after_notes';
55 }
56
57 if( !empty( $form['position'] ) )
58 {
59 // if( $form['position'] == 'after_email' )
60
61 if( $form['position'] == 'after_billing' )
62 {
63 add_action( 'woocommerce_after_checkout_billing_form', 'gb_sf4wp_wc_checkout_form' );
64 }
65 elseif( $form['position'] == 'after_shipping' )
66 {
67 add_action( 'woocommerce_after_checkout_shipping_form', 'gb_sf4wp_wc_checkout_form' );
68 }
69 elseif( $form['position'] == 'after_customer' )
70 {
71 add_action( 'woocommerce_checkout_after_customer_details', 'gb_sf4wp_wc_checkout_form' );
72 }
73 elseif( $form['position'] == 'before_submit' )
74 {
75 add_action( 'woocommerce_review_order_before_submit', 'gb_sf4wp_wc_checkout_form' );
76 }
77 elseif( $form['position'] == 'after_notes' )
78 {
79 add_action( 'woocommerce_after_order_notes', 'gb_sf4wp_wc_checkout_form' );
80 }
81 }
82 }
83
84
85 if (function_exists('pll__')) {
86 $GLOBALS['checkbox_label_text']=pll__('subscribe to our newsletter');
87 }
88
89}
90add_action( 'init', 'gb_sf4wp_init' );
91
92/**
93 * Load plugin's textdomain
94 *
95 * @since 1.0.0
96 */
97
98function gb_sf4wp_load_textdomain()
99{
100 load_plugin_textdomain( 'sf4wp', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
101}
102add_action( 'plugins_loaded', 'gb_sf4wp_load_textdomain' );
103
104/**
105 * Registers plugin's admin page
106 *
107 * @since 1.0.0
108 */
109
110function gb_sf4wp_add_page()
111{
112 add_menu_page(
113 GB_SF4WP_NAME,
114 'SendFox',
115 'manage_options',
116 GB_SF4WP_ID,
117 'gb_sf4wp_do_page',
118 plugins_url( 'assets/img/sendfox-icon.svg', __FILE__ ),
119 99
120 );
121}
122add_action( 'admin_menu', 'gb_sf4wp_add_page' );
123
124/**
125 * Displays plugin's admin page
126 *
127 * @since 1.0.0
128 */
129
130function gb_sf4wp_do_page()
131{
132 if( !current_user_can( 'manage_options' ) )
133 {
134 wp_die( __( 'Oops, you can\'t access this page.', 'sf4wp' ) );
135 }
136
137 include_once 'wp-sendfox-admin.php';
138}
139
140/**
141 * Registers plugins admin page options
142 *
143 * @param string $option_group A settings group name. Must exist prior to the register_setting call.
144 * This must match the group name in settings_fields()
145 * @param string $option_name The name of an option to sanitize and save.
146 *
147 * @since 1.0.0
148 */
149
150function gb_sf4wp_admin_init()
151{
152 register_setting( 'gb_sf4wp_options', 'gb_sf4wp_options' );
153}
154add_action( 'admin_init', 'gb_sf4wp_admin_init' );
155
156/**
157 * Redirect to the settings page on the first activation
158 *
159 * @since 1.0.0
160 */
161
162function gb_sf4wp_plugin_install( $plugin )
163{
164 if( $plugin == plugin_basename( __FILE__ ) )
165 {
166 $options = get_option( 'gb_sf4wp_options' );
167
168 if( empty( $options ) )
169 {
170 if( wp_redirect( admin_url( 'admin.php?page=' . GB_SF4WP_ID ) ) )
171 {
172 exit;
173 }
174 }
175 }
176}
177add_action( 'activated_plugin', 'gb_sf4wp_plugin_install' );
178
179/**
180 * API Request: Get lists
181 *
182 * @since 1.0.0
183 */
184
185function gb_sf4wp_get_lists()
186{
187 $response = array();
188
189 $lists_cache = get_site_transient( 'gb_sf4wp_api_lists' );
190
191 if( $lists_cache === FALSE )
192 {
193 $response = gb_sf4wp_api_request( 'lists' );
194
195 $response = gb_sf4wp_api_response( $response );
196
197 $response_data = array();
198
199 // process multiple pages
200
201 if(
202 $response['status'] === 'success' &&
203 !empty( $response['result'] ) &&
204 !empty( $response['result']['data'] ) &&
205 !empty( $response['result']['total'] )
206 )
207 {
208 // preserve first page of lists
209
210 $response_data = $response['result']['data'];
211
212 // count total pages
213
214 $lists_total = absint( $response['result']['total'] );
215 $list_per_page = absint( $response['result']['per_page'] );
216
217 $pagination_needed = absint( $lists_total / $list_per_page ) + 1;
218
219 if( $pagination_needed >= 2 )
220 {
221 // request pages >=2 and merge lists
222
223 $response_pages = array();
224
225 for( $i = 2; $i <= $pagination_needed; $i++ )
226 {
227 $response_pages = gb_sf4wp_api_request( 'lists?page=' . $i );
228
229 $response_pages = gb_sf4wp_api_response( $response_pages );
230
231 if(
232 $response_pages['status'] === 'success' &&
233 !empty( $response_pages['result'] ) &&
234 !empty( $response_pages['result']['data'] )
235 )
236 {
237 $response_data = array_merge( $response_data, $response_pages['result']['data'] );
238 }
239 }
240 }
241
242 // update data in the final response
243
244 $response['result']['data'] = $response_data;
245 }
246
247 // save cache to transient
248
249 set_site_transient( 'gb_sf4wp_api_lists', $response, 86400 );
250 }
251 else
252 {
253 $response = $lists_cache;
254 }
255
256 return $response;
257}
258
259/**
260 * API Request: Add contact
261 *
262 * @since 1.0.0
263 */
264
265function gb_sf4wp_add_contact( $contact = array() )
266{
267 $response = gb_sf4wp_api_request( 'contacts', $contact, 'POST' );
268
269 return gb_sf4wp_api_response( $response );
270}
271
272/**
273 * API Request Wrapper
274 *
275 * @since 1.0.0
276 */
277
278function gb_sf4wp_api_request( $endpoint = 'me', $data = array(), $method = 'GET' )
279{
280 $result = FALSE;
281
282 $base = 'https://api.sendfox.com/';
283
284 $options = get_option( 'gb_sf4wp_options' );
285
286 if( empty( $options['api_key'] ) )
287 {
288 $result = array(
289 'status' => 'error',
290 'error' => 'empty_api_key',
291 'error_text' => __( 'API Key is not set.', 'sf4wp' ),
292 );
293
294 return $result;
295 }
296
297 // access for 3rd parties
298
299 $args = apply_filters( 'gb_sf4wp_request_args', $data, $endpoint, $method );
300
301 // prepare request args
302
303 $args = array(
304 'body' => $args,
305 );
306
307 $args['headers'] = array(
308 'Authorization' => 'Bearer ' . $options['api_key'],
309 );
310
311 $args['method'] = $method;
312 $args['timeout'] = 30;
313
314 // make request
315
316 $result = wp_remote_request( $base . $endpoint, $args );
317
318 gb_sf4wp_log(
319 array(
320 '_time' => date( 'H:i:s d.m.Y' ),
321 'event' => 'API_REQUEST',
322 'endpoint' => $base . $endpoint,
323 'args' => $args,
324 'response_raw' => $result,
325 )
326 );
327
328 if(
329 !is_wp_error( $result ) &&
330 ( $result['response']['code'] == 200 || $result['response']['code'] == 201 )
331 )
332 {
333 $result = wp_remote_retrieve_body( $result );
334
335 $result = json_decode( $result, TRUE );
336
337 if( !empty( $result ) )
338 {
339 $result = array(
340 'status' => 'success',
341 'result' => $result,
342 );
343 }
344 else
345 {
346 $result = array(
347 'status' => 'error',
348 'error' => 'json_parse_error',
349 'error_text' => __( 'JSON Parse', 'sf4wp' ),
350 );
351 }
352 }
353 else // if WP_Error happened
354 {
355 if( is_object( $result ) )
356 {
357 $result = array(
358 'status' => 'error',
359 'error' => 'request_error',
360 'error_text' => $result->get_error_message(),
361 );
362 }
363 else
364 {
365 $result = wp_remote_retrieve_body( $result );
366
367 $result = array(
368 'status' => 'error',
369 'error' => 'request_error',
370 'error_text' => $result,
371 );
372 }
373 }
374
375 return $result;
376}
377
378/**
379 * API Response Wrapper
380 *
381 * @since 1.0.0
382 */
383
384function gb_sf4wp_api_response( $response = array() )
385{
386 $result = array(
387 'status' => 'error',
388 'error' => 'status_error',
389 'error_text' => __( 'Error: Response Status', 'sf4wp' ),
390 );
391
392 if( !empty( $response['status'] ) )
393 {
394 $result = $response;
395 }
396
397 return $result;
398}
399
400/**
401 * Display menu icon CSS in admin header
402 *
403 * @since 1.0.0
404 */
405
406function gb_sf4wp_admin_header()
407{
408 echo '<style type="text/css">
409 #adminmenu .toplevel_page_wp-sendfox .wp-menu-image img { width: 18px; padding: 6px 0 0 0; }
410 #adminmenu .toplevel_page_wp-sendfox.current .wp-menu-image img { opacity: 1; }
411 </style>';
412}
413add_action( 'admin_head', 'gb_sf4wp_admin_header' );
414
415/**
416 * Pre-update plugin settings filter
417 *
418 * @since 1.0.0
419 */
420
421function gb_sf4wp_pre_update_option( $new_value = '', $old_value = '' )
422{
423 if( !empty( $old_value ) )
424 {
425 // update existing settings with new value (only changed ones)
426
427 foreach( $old_value as $k => $v )
428 {
429 if( isset( $new_value[ $k ] ) )
430 {
431 $old_value[ $k ] = $new_value[ $k ];
432 }
433 }
434
435 // add new ones, that don't exist in existing settings yet
436
437 foreach( $new_value as $k => $v )
438 {
439 if( ! isset( $old_value[ $k ] ) )
440 {
441 $old_value[ $k ] = $new_value[ $k ];
442 }
443 }
444 }
445 else
446 {
447 // if old value doesn't exist, just save the new value (ex. first install)
448
449 $old_value = $new_value;
450 }
451
452 return $old_value;
453}
454add_filter( 'pre_update_option_gb_sf4wp_options', 'gb_sf4wp_pre_update_option', 10, 2 );
455
456/**
457 * Comment Form: add checkbox to comment form
458 *
459 * @since 1.0.0
460 */
461
462function gb_sf4wp_comment_form( $post_id )
463{
464 $options = get_option( 'gb_sf4wp_options' );
465
466 if( !empty( $options['wp-comment-form'] ) )
467 {
468 $form = $options['wp-comment-form'];
469
470 if( !empty( $form['enabled'] ) )
471 {
472 if( !empty( $form['implicit'] ) )
473 {
474 $form['implicit'] = 'style="display: none !important;"';
475
476 $form['prechecked'] = 1;
477 }
478 else
479 {
480 $form['implicit'] = '';
481 }
482
483 if( !empty( $form['prechecked'] ) )
484 {
485 $form['prechecked'] = 'checked="checked"';
486 }
487 else
488 {
489 $form['prechecked'] = '';
490 }
491
492 if( empty( $form['label'] ) )
493 {
494 $form['label'] = $GLOBALS['checkbox_label_text'];
495 }
496 else
497 {
498 $form['label'] = strip_tags( $form['label'], '<strong><em><a>' );
499 }
500
501 if( !empty( $form['css'] ) )
502 {
503 echo '<style type="text/css">.comment-form-sf4wp-subscribe { display: block; clear: both; float: none; margin: 1em 0; padding: 0; }</style>';
504 }
505
506 echo '<p class="comment-form-sf4wp-subscribe" ' . $form['implicit'] . '>
507 <label for="sf4wp-subscribe">' .
508 '<input type="checkbox" id="sf4wp-subscribe" name="sf4wp-subscribe" value="1" ' . $form['prechecked'] . ' /> ' .
509 $form['label'] .
510 '</label>
511 </p>';
512 }
513 }
514}
515
516/**
517 * Comment Form: process checkbox
518 *
519 * @since 1.0.0
520 */
521
522function gb_sf4wp_comment_post( $comment_id, $comment_approved )
523{
524 if( !empty( $_POST['sf4wp-subscribe'] ) )
525 {
526 $comment = get_comment( $comment_id );
527
528 if( !empty( $comment ) && !empty( $comment->comment_author_email ) )
529 {
530 $options = get_option( 'gb_sf4wp_options' );
531
532 // either process email from auto-approved comment or
533 // from manually approved one, when status transition happens
534
535 if( $comment_approved == 0 )
536 {
537 // mark comment data to be sent to SendFox only after approval
538
539 if( !empty( $options['wp-comment-form']['approved_check'] ) )
540 {
541 add_comment_meta( $comment_id, 'sf4wp_send', 1, TRUE );
542
543 return;
544 }
545 else
546 {
547 return;
548 }
549 }
550
551 // if comment data is submitted instantly
552
553 if(
554 !empty( $options['wp-comment-form'] ) &&
555 !empty( $options['wp-comment-form']['list'] )
556 )
557 {
558 $contact = array(
559 'email' => $comment->comment_author_email,
560 'lists' => array(
561 intval( $options['wp-comment-form']['list'] )
562 ),
563 );
564
565 if( !empty( $comment->comment_author ) )
566 {
567 $contact['first_name'] = $comment->comment_author;
568 }
569
570 $contact = apply_filters( 'sf4wp_before_add_contact', $contact, 'comment', $comment );
571
572 $result = gb_sf4wp_add_contact( $contact );
573 }
574 }
575 }
576}
577add_action( 'comment_post', 'gb_sf4wp_comment_post', 10, 2 );
578
579/**
580 * Comment Form: process manual comment approval
581 *
582 * @since 1.1.0
583 */
584
585function gb_sf4wp_comment_approved( $new_status, $old_status, $comment )
586{
587 if( $old_status === $new_status )
588 {
589 return;
590 }
591
592 if( $new_status !== 'approved' )
593 {
594 return;
595 }
596
597 if(
598 !empty( $comment ) &&
599 !empty( $comment->comment_ID ) &&
600 !empty( $comment->comment_author_email )
601 )
602 {
603 $options = get_option( 'gb_sf4wp_options' );
604
605 // if comment data is marked to be sent to SendFox after approval
606
607 if( get_comment_meta( $comment->comment_ID, 'sf4wp_send', TRUE ) )
608 {
609 if(
610 !empty( $options['wp-comment-form'] ) &&
611 !empty( $options['wp-comment-form']['list'] )
612 )
613 {
614 $contact = array(
615 'email' => $comment->comment_author_email,
616 'lists' => array(
617 intval( $options['wp-comment-form']['list'] ),
618 ),
619 );
620
621 if( !empty( $comment->comment_author ) )
622 {
623 $contact['first_name'] = $comment->comment_author;
624 }
625
626 $contact = apply_filters( 'sf4wp_before_add_contact', $contact, 'comment', $comment );
627
628 $result = gb_sf4wp_add_contact( $contact );
629 }
630 }
631 }
632}
633add_action( 'transition_comment_status', 'gb_sf4wp_comment_approved', 10, 3 );
634
635/**
636 * Registration Form: add checkbox to registration form
637 *
638 * @since 1.0.0
639 */
640
641function gb_sf4wp_registration_form()
642{
643 $options = get_option( 'gb_sf4wp_options' );
644
645 if( !empty( $options['wp-registration-form'] ) )
646 {
647 $form = $options['wp-registration-form'];
648
649 if( !empty( $form['enabled'] ) )
650 {
651 if( !empty( $form['implicit'] ) )
652 {
653 $form['implicit'] = 'style="display: none !important;"';
654
655 $form['prechecked'] = 1;
656 }
657 else
658 {
659 $form['implicit'] = '';
660 }
661
662 if( !empty( $form['prechecked'] ) )
663 {
664 $form['prechecked'] = 'checked="checked"';
665 }
666 else
667 {
668 $form['prechecked'] = '';
669 }
670
671 if( empty( $form['label'] ) )
672 {
673 $form['label'] = $GLOBALS['checkbox_label_text'];
674 }
675 else
676 {
677 $form['label'] = strip_tags( $form['label'], '<strong><em><a>' );
678 }
679
680 if( !empty( $form['css'] ) )
681 {
682 echo '<style type="text/css">
683 #login form p.registration-form-sf4wp-subscribe { display: block; clear: both; float: none; margin: 0.5em 0; padding: 0; }
684 #login form p.registration-form-sf4wp-subscribe label { font-size: 13px; }
685 </style>';
686 }
687
688 echo '<p class="registration-form-sf4wp-subscribe" ' . $form['implicit'] . '>
689 <label for="sf4wp-subscribe">' .
690 '<input type="checkbox" id="sf4wp-subscribe" name="sf4wp-subscribe" value="1" ' . $form['prechecked'] . ' /> ' .
691 $form['label'] .
692 '</label>
693 </p>';
694 }
695 }
696}
697
698/**
699 * Registration Form: process checkbox
700 *
701 * @since 1.0.0
702 */
703
704function gb_sf4wp_user_register( $user_id )
705{
706 if( !empty( $_POST['sf4wp-subscribe'] ) )
707 {
708 $user = get_user_by( 'id', $user_id );
709
710 if( !empty( $user ) && !empty( $user->user_email ) )
711 {
712 $options = get_option( 'gb_sf4wp_options' );
713
714 if(
715 !empty( $options['wp-registration-form'] ) &&
716 !empty( $options['wp-registration-form']['list'] )
717 )
718 {
719 $contact = array(
720 'email' => $user->user_email,
721 'lists' => array(
722 intval( $options['wp-registration-form']['list'] )
723 ),
724 );
725
726 if( !empty( $user->user_nicename ) )
727 {
728 $contact['first_name'] = $user->user_nicename;
729 }
730
731 $contact = apply_filters( 'sf4wp_before_add_contact', $contact, 'registration', $user );
732
733 $result = gb_sf4wp_add_contact( $contact );
734 }
735 }
736 }
737}
738add_action( 'user_register', 'gb_sf4wp_user_register' );
739
740/**
741 * WooCommerce Checkout: add checkbox to checkout form
742 *
743 * @since 1.0.0
744 */
745
746function gb_sf4wp_wc_checkout_form()
747{
748 $options = get_option( 'gb_sf4wp_options' );
749
750 if( !empty( $options['woocommerce-checkout'] ) )
751 {
752 $form = $options['woocommerce-checkout'];
753
754 if( !empty( $form['enabled'] ) )
755 {
756 if( !empty( $form['implicit'] ) )
757 {
758 $form['implicit'] = ' style="display: none !important;"';
759
760 $form['prechecked'] = 1;
761 }
762 else
763 {
764 $form['implicit'] = '';
765 }
766
767 if( !empty( $form['prechecked'] ) )
768 {
769 $form['prechecked'] = 'checked="checked"';
770 }
771 else
772 {
773 $form['prechecked'] = '';
774 }
775
776 if( empty( $form['label'] ) )
777 {
778 $form['label'] = $GLOBALS['checkbox_label_text'];
779 }
780 else
781 {
782 $form['label'] = strip_tags( $form['label'], '<strong><em><a>' );
783 }
784
785 if( !empty( $form['position'] ) && $form['position'] == 'after_customer' )
786 {
787 $clear_css = 'clear: none;';
788 }
789 else
790 {
791 $clear_css = 'clear: both;';
792 }
793
794 if( !empty( $form['css'] ) )
795 {
796 echo '<style type="text/css">
797 .wc-checkout-sf4wp-subscribe { display: block; ' . $clear_css . ' float: none; margin: 1em 0; padding: 0; }
798 .wc-checkout-sf4wp-subscribe input[type="checkbox"] { margin-right: 0.3342343017em; }
799 </style>';
800 }
801
802 echo '<p class="wc-checkout-sf4wp-subscribe" ' . $form['implicit'] . '>
803 <label for="sf4wp-subscribe">' .
804 '<input type="checkbox" id="sf4wp-subscribe" name="sf4wp-subscribe" value="1" ' . $form['prechecked'] . ' /> ' .
805 $form['label'] .
806 '</label>
807 </p>';
808 }
809 }
810}
811
812/**
813 * WooCommerce Checkout: process checkbox
814 *
815 * @since 1.0.0
816 */
817
818function gb_sf4wp_order_processed( $order_id )
819{
820 if( !empty( $_POST['sf4wp-subscribe'] ) )
821 {
822 if( !empty( $_POST['billing_email'] ) )
823 {
824 $options = get_option( 'gb_sf4wp_options' );
825
826 if(
827 !empty( $options['woocommerce-checkout'] ) &&
828 !empty( $options['woocommerce-checkout']['list'] )
829 )
830 {
831 $contact = array(
832 'email' => sanitize_email( $_POST['billing_email'] ),
833 'lists' => array(
834 intval( $options['woocommerce-checkout']['list'] )
835 ),
836 );
837
838 if( !empty( $_POST['billing_first_name'] ) )
839 {
840 $contact['first_name'] = sanitize_text_field( $_POST['billing_first_name'] );
841 }
842
843 if( !empty( $_POST['billing_last_name'] ) )
844 {
845 $contact['last_name'] = sanitize_text_field( $_POST['billing_last_name'] );
846 }
847
848 $contact = apply_filters( 'sf4wp_before_add_contact', $contact, 'wc-checkout', $order_id );
849
850 $result = gb_sf4wp_add_contact( $contact );
851 }
852 }
853 }
854}
855add_action( 'woocommerce_checkout_order_processed', 'gb_sf4wp_order_processed' );
856
857/**
858 * Process synchronization
859 *
860 * @since 1.0.0
861 */
862
863function gb_sf4wp_process_sync()
864{
865 $result = array( 'result' => 'error' );
866
867 if(
868 !empty( $_POST['stage'] ) &&
869 !empty( $_POST['list'] ) &&
870 !empty( $_POST['mode'] )
871 )
872 {
873 $stage = intval( $_POST['stage'] );
874 $list = intval( $_POST['list'] );
875 $mode = sanitize_text_field( $_POST['mode'] );
876
877 if( $stage === 1 )
878 {
879 // count total emails
880
881 if( $mode == 'wp-users' )
882 {
883 $users = count_users();
884
885 if( !empty( $users['total_users'] ) )
886 {
887 $result['result'] = 'success';
888 $result['stage'] = 1;
889 $result['total'] = intval( $users['total_users'] );
890 $result['total_steps'] = ceil( $users['total_users'] / GB_SF4WP_USERS_PER_STEP );
891 }
892 else
893 {
894 $response['error_text'] = 'count users error';
895 }
896 }
897 elseif( $mode == 'wc-customers' )
898 {
899 global $wpdb;
900
901 $users = $wpdb->get_var(
902
903 "SELECT COUNT(DISTINCT meta_value)
904 FROM {$wpdb->postmeta}
905 WHERE meta_key = '_billing_email' AND meta_value <> '' ;"
906
907 );
908
909 if( !empty( $users ) )
910 {
911 $result['result'] = 'success';
912 $result['stage'] = 1;
913 $result['total'] = intval( $users );
914 $result['total_steps'] = ceil( $users / GB_SF4WP_USERS_PER_STEP );
915 }
916 else
917 {
918 $response['error_text'] = 'count customers error';
919 }
920 }
921 else
922 {
923 $response['error_text'] = 'mode error';
924 }
925 }
926 elseif( $stage === 2 )
927 {
928 $step = 0;
929 $total_steps = 0;
930
931 if( isset( $_POST['step'] ) )
932 {
933 $step = intval( $_POST['step'] );
934 }
935
936 if( isset( $_POST['total_steps'] ) )
937 {
938 $total_steps = intval( $_POST['total_steps'] );
939 }
940
941 // pull emails and subscribe
942
943 if( $mode == 'wp-users' )
944 {
945 $users = get_users(
946 array(
947 'fields' => array( 'ID', 'user_email' ),
948 'number' => GB_SF4WP_USERS_PER_STEP,
949 'paged' => $step,
950 )
951 );
952
953 if( !empty( $users ) )
954 {
955 $contact = $contact_info = array();
956
957 $import_success = $import_fail = array();
958
959 $import_success_count = $import_fail_count = 0;
960
961 foreach( $users as $u )
962 {
963 $u_data = get_userdata( $u->ID );
964
965 $contact = array(
966 'email' => $u->user_email,
967 'first_name' => ( !empty( $u_data->first_name ) ? $u_data->first_name : '' ),
968 'last_name' => ( !empty( $u_data->last_name ) ? $u_data->last_name : '' ),
969 'lists' => array(
970 $list
971 ),
972 );
973
974 $contact = apply_filters( 'sf4wp_before_export_contact', $contact, $mode );
975
976 $request = gb_sf4wp_add_contact( $contact );
977
978 if(
979 !empty( $request['status'] ) &&
980 $request['status'] === 'success' &&
981
982 !empty( $request['result'] ) &&
983 !empty( $request['result']['id'] ) &&
984 empty( $request['result']['invalid_at'] )
985 )
986 {
987 ++$import_success_count;
988 }
989 else
990 {
991 ++$import_fail_count;
992
993 $import_fail[] = $contact['email'];
994 }
995 }
996
997 $result['result'] = 'success';
998 $result['stage'] = 2;
999 $result['import_success_count'] = $import_success_count;
1000 $result['import_fail_count'] = $import_fail_count;
1001 $result['import_fail_emails'] = $import_fail;
1002 }
1003 else
1004 {
1005 $response['error_text'] = 'get users error';
1006 }
1007 }
1008 elseif( $mode == 'wc-customers' )
1009 {
1010 global $wpdb;
1011
1012 $offset = intval( GB_SF4WP_USERS_PER_STEP * ( $step - 1 ) );
1013 $limit = intval( GB_SF4WP_USERS_PER_STEP );
1014
1015 $customers = $wpdb->get_results(
1016
1017 "SELECT DISTINCT meta_value as billing_email
1018 FROM {$wpdb->postmeta}
1019 WHERE meta_key = '_billing_email' AND meta_value <> ''
1020 LIMIT {$offset}, {$limit};"
1021 );
1022
1023 if( !empty( $customers ) )
1024 {
1025 $contact = array();
1026
1027 $import_success = $import_fail = array();
1028
1029 $import_success_count = $import_fail_count = 0;
1030
1031 foreach( $customers as $c )
1032 {
1033 $contact = array(
1034 'email' => $c->billing_email,
1035 // 'first_name' => ( !empty( $c->first_name ) ? $c->first_name : '' ),
1036 // 'last_name' => ( !empty( $c->last_name ) ? $c->last_name : '' ),
1037 'lists' => array(
1038 $list
1039 ),
1040 );
1041
1042 $contact = apply_filters( 'sf4wp_before_export_contact', $contact, $mode );
1043
1044 $request = gb_sf4wp_add_contact( $contact );
1045
1046 if(
1047 !empty( $request['status'] ) &&
1048 $request['status'] === 'success' &&
1049
1050 !empty( $request['result'] ) &&
1051 !empty( $request['result']['id'] ) &&
1052 empty( $request['result']['invalid_at'] )
1053 )
1054 {
1055 ++$import_success_count;
1056 }
1057 else
1058 {
1059 ++$import_fail_count;
1060
1061 $import_fail[] = $contact['email'];
1062 }
1063 }
1064
1065 $result['result'] = 'success';
1066 $result['stage'] = 2;
1067 $result['import_success_count'] = $import_success_count;
1068 $result['import_fail_count'] = $import_fail_count;
1069 $result['import_fail_emails'] = $import_fail;
1070 }
1071 else
1072 {
1073 $response['error_text'] = 'get customers error';
1074 }
1075 }
1076 else
1077 {
1078 $response['error_text'] = 'mode error';
1079 }
1080 }
1081 else
1082 {
1083 $response['error_text'] = 'stage error';
1084 }
1085 }
1086 else
1087 {
1088 $response['error_text'] = 'request error';
1089 }
1090
1091 echo json_encode( $result );
1092
1093 wp_die();
1094}
1095add_action( 'wp_ajax_sf4wp_process_sync', 'gb_sf4wp_process_sync' );
1096
1097/**
1098 * Integration: Replace pluggable Divi Builder's method to
1099 * register 3rd-party components
1100 *
1101 * @since 1.1.0
1102 */
1103
1104if ( ! function_exists( 'et_core_get_third_party_components' ) ):
1105
1106function et_core_get_third_party_components( $group = '' )
1107{
1108 $third_party_components = apply_filters( 'et_core_get_third_party_components', array(), $group );
1109
1110 return $third_party_components;
1111}
1112
1113endif;
1114
1115/**
1116 * Integration: Add SendFox email provider to Divi Builder opt-ins
1117 *
1118 * @since 1.1.0
1119 */
1120
1121function gb_sf4wp_add_divi_provider( $third_party_components, $group )
1122{
1123 if( class_exists( 'ET_Core_API_Email_Provider' ) )
1124 {
1125 if( $group === 'api' || $group === 'api/email' )
1126 {
1127 $options = get_option( 'gb_sf4wp_options' );
1128
1129 if( !empty( $options['divi'] ) && !empty( $options['divi']['enabled'] ) )
1130 {
1131 require_once plugin_dir_path( __FILE__ ) . 'includes/divi-builder/divi-email-provider.php';
1132
1133 $third_party_components['sendfox'] = new ET_Core_API_Email_SendFox( 'builder', 'default', '' );
1134 }
1135 }
1136 }
1137
1138 return $third_party_components;
1139}
1140add_filter( 'et_core_get_third_party_components', 'gb_sf4wp_add_divi_provider', 1, 2 );
1141
1142/**
1143 * Integration: Add SendFox email provider to Gutenberg Email Optin
1144 *
1145 * @since 1.0.0
1146 */
1147
1148function gb_sf4wp_add_gutenberg_email_optin()
1149{
1150 if( function_exists( 'has_blocks' ) )
1151 {
1152 $options = get_option( 'gb_sf4wp_options' );
1153
1154 if( !empty( $options['gutenberg'] ) && !empty( $options['gutenberg']['enabled'] ) )
1155 {
1156 require_once plugin_dir_path( __FILE__ ) . 'includes/gutenberg/gutenberg-email-optin.php';
1157 }
1158 }
1159}
1160add_action( 'after_setup_theme', 'gb_sf4wp_add_gutenberg_email_optin' );
1161
1162/**
1163 * Helper: Simple logging function, useful for debugging
1164 *
1165 * @since 1.0.0
1166 */
1167
1168function gb_sf4wp_log( $data = array(), $file = 'debug.log', $force = FALSE )
1169{
1170 if( empty( $file ) )
1171 {
1172 $file = 'debug.log';
1173 }
1174
1175 if( !empty( $data ) )
1176 {
1177 $options = get_option( 'gb_sf4wp_options' );
1178
1179 if( empty( $options['enable_log'] ) && $force === FALSE )
1180 {
1181 return;
1182 }
1183
1184 if( empty( $data['_time'] ) )
1185 {
1186 $data[ '_time' ] = date( 'H:i:s d.m.y' );
1187 }
1188
1189 $data = json_encode( $data );
1190
1191 // remove api_key from logs
1192
1193 if( !empty( $options['api_key'] ) )
1194 {
1195 $data = str_replace( $options['api_key'], '###_API_KEY_REMOVED_###', $data );
1196 }
1197
1198 $data = $data . PHP_EOL . PHP_EOL;
1199
1200 return file_put_contents( dirname( __FILE__ ) . '/' . $file, $data, FILE_APPEND );
1201 }
1202}
1203
1204