PluginProbe
Integration for Mailchimp and Contact Form 7, WPForms, Elementor, Ninja Forms / trunk
Integration for Mailchimp and Contact Form 7, WPForms, Elementor, Ninja Forms vtrunk
1.0 1.0.1 1.0.4 1.0.5 1.0.6 1.0.7 1.1.2 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 trunk
cf7-mailchimp / cf7-mailchimp.php

cf7-mailchimp.php in Integration for Mailchimp and Contact Form 7, WPForms, Elementor, Ninja Forms trunk, at cf7-mailchimp.php

2,048 lines 62.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WP Contact Form Mailchimp
4 * Description: Integrates Contact Form 7, <a href="https://wordpress.org/plugins/contact-form-entries/">Contact Form Entries Plugin</a> and many other forms with Mailchimp allowing form submissions to be automatically sent to your Mailchimp account
5 * Version: 1.2.3
6 * Author URI: https://www.crmperks.com
7 * Plugin URI: https://www.crmperks.com/plugins/contact-form-plugins/contact-form-mailchimp-plugin/
8 * Author: CRM Perks.
9 * Text Domain: contact-form-mailchimp
10 * Domain Path: /languages/
11 */
12 // Exit if accessed directly
13 if( !defined( 'ABSPATH' ) ) exit;
14
15
16 if( !class_exists( 'vxcf_mailchimp' ) ):
17
18
19 class vxcf_mailchimp {
20
21
22 public $url = "https://www.crmperks.com";
23
24 public $crm_name = "mailchimp";
25 public $id = "vxcf_mailchimp";
26 public $domain = "vxcf-mailchimp";
27 public $version = "1.2.3";
28 public $update_id = "6000001";
29 public $min_cf_version = "1.0";
30 public $type = "vxcf_mailchimp";
31 public $fields = null;
32 public $data = null;
33
34 private $filter_condition;
35 private $plugin_dir= "";
36 private $temp;
37 private $crm_arr= false;
38 public $notice_js= false;
39 public static $title='Contact Form Mailchimp Plugin';
40 public static $path = '';
41 public static $slug = "";
42 public static $debug_html = '';
43 public static $save_key='';
44 public static $lic_msg = "";
45 public static $db_version='';
46 public static $vx_plugins;
47 public static $note;
48 public static $feeds_res;
49 public static $cf_status='';
50 public static $cf_status_msg='';
51 public static $tooltips;
52 public static $entry_created=false;
53 public static $plugin;
54 public static $api_timeout;
55 public static $is_pr;
56
57 public function instance(){
58 add_action( 'plugins_loaded', array( $this, 'setup_main' ) );
59 register_deactivation_hook(__FILE__,array($this,'deactivate'));
60 register_activation_hook(__FILE__,(array($this,'activate')));
61
62
63
64 }
65 /**
66 * Plugin starting point. Will load appropriate files
67 *
68 */
69 public function init(){
70
71 /* self::$cf_status= $this->cf_status();
72 if(self::$cf_status !== 1){
73 add_action( 'admin_notices', array( $this, 'install_cf_notice' ) );
74 add_action( 'after_plugin_row_'.self::$slug, array( $this, 'install_cf_notice_plugin_row' ) );
75 return;
76 } */
77
78 $pro_file=self::$path . 'wp/crmperks-notices.php';
79 if(file_exists($pro_file)){
80 include_once($pro_file);
81 }else{
82 //plugin api
83 $this->plugin_api(true);
84 self::$is_pr=true;
85 $pro_file=self::$path . 'pro/add-ons.php';
86 if(file_exists($pro_file)){
87 include_once($pro_file);
88 } }
89
90 require_once(self::$path . "includes/crmperks-cf.php");
91 require_once(self::$path . "includes/plugin-pages.php");
92
93
94 }
95
96 /**
97 * create tables and roles
98 *
99 */
100 public function setup_main(){
101
102
103 add_action('cfx_form_submitted', array($this, 'entry_created_crmperks'),10,3);
104 add_action('vxcf_entry_created', array($this, 'entry_created'),10,3);
105 add_action('vx_contact_created', array($this, 'entry_created_contacts'),10,3);
106 add_action('vx_callcenter_entry_created', array($this, 'entry_created_callcenter'),10,3);
107
108 add_filter('wpcf7_before_send_mail', array($this, 'create_entry_cf'),99);
109 //formidable
110 add_action('frm_after_create_entry', array($this, 'create_entry_fd'), 99, 2);
111 add_action('ninja_forms_after_submission', array($this, 'create_entry_na'),99);
112 add_action( 'wpforms_process_entry_save',array(&$this,'create_entry_wp'), 99, 4 );
113 //elemntor form
114 add_action( 'elementor_pro/forms/new_record', array($this,'create_entry_el'), 99 );
115
116 if(is_admin()){
117 self::$path=$this->get_base_path();
118 add_action('init', array($this,'init'));
119 //loading translations
120 load_plugin_textdomain('contact-form-mailchimp-crm', FALSE, $this->plugin_dir_name(). '/languages/' );
121 $this->maybe_install(true);
122 }
123
124 }
125 public function maybe_install($version_check=false){
126
127 if(current_user_can( 'manage_options' )){
128 self::$db_version=get_option($this->type."_version");
129 $do_install=false;
130 if($version_check == false){
131 $do_install=true;
132 }else if(self::$db_version != $this->version){
133 $do_install=true;
134 }
135 if($do_install){
136 $data=$this->get_data_object();
137 $data->update_table();
138 update_option($this->type."_version", $this->version);
139 //add post permissions
140 require_once(self::$path . "includes/install.php");
141 $install=new vxcf_mailchimp_install();
142 $install->create_roles();
143 }
144 }
145 }
146 public function form_submitted($form){
147
148 //entries plugin exists , do not use this hook
149 if(class_exists('vxcf_form')){ return; }
150 $form_id=$form->id();
151 $submission = WPCF7_Submission::get_instance();
152
153 $lead = $submission->uploaded_files();
154 if(!is_array($lead)){ $lead=array(); }
155 $form_title=$form->title();
156 $tags=array();
157
158 if(method_exists('WPCF7_ShortcodeManager','get_instance') || method_exists('WPCF7_FormTagsManager','get_instance')){
159
160 $form_text=get_post_meta($form_id ,'_form',true);
161
162 if(method_exists('WPCF7_FormTagsManager','get_instance')){
163 $manager=WPCF7_FormTagsManager::get_instance();
164 $contents=$manager->scan($form_text);
165 $tags=$manager->get_scanned_tags();
166
167 }else if(method_exists('WPCF7_ShortcodeManager','get_instance')){ //
168 $manager = WPCF7_ShortcodeManager::get_instance();
169 $contents=$manager->do_shortcode($form_text);
170 $tags=$manager->get_scanned_tags();
171 } }
172
173
174 if(is_array($tags)){
175 foreach($tags as $k=>$v){
176 if(!empty($v['name'])){
177 $name=$v['name'];
178 $val=$submission->get_posted_data($name);
179 if(!isset($lead[$name])){ $lead[$name]=$val; }
180
181 } }
182 }
183 //var_dump($lead);
184 $form_arr=array('id'=>'cf_'.$form_id,'name'=>$form_title,'fields'=>$tags);
185 $this->entry_created($lead,'0',$form_arr);
186
187 }
188 public function create_entry_cf($form){
189 if(class_exists('vxcf_form')){ return; }
190 $form_id=$form->id();
191
192 $submission = WPCF7_Submission::get_instance();
193 $uploaded_files = $submission->uploaded_files();
194
195 $form_title=$form->title();
196 $tags=$this->get_form_fields('cf_'.$form_id);
197 $post_data=$submission->get_posted_data();
198 //var_dump($post_data); die();
199 $lead=array();
200 if(is_array($post_data)){
201 foreach($post_data as $k=>$val){
202 if(in_array($k,array('vx_width','vx_height','vx_url','g-recaptcha-response'))){ continue; }
203 if(isset($tags[$k])){
204 $v=$tags[$k]; //$v is empty for non form fields
205 }
206 $name=$k; //$v['name'] //if empty then $v is old
207 //var_dump($v);
208 if(isset($uploaded_files[$name])){
209 $val=$uploaded_files[$name];
210 }
211
212 if( !empty($val) && isset($v['basetype']) && $v['basetype'] == 'mfile' && function_exists('dnd_get_upload_dir') ){
213 $dir=dnd_get_upload_dir();
214 $f_arr=array();
215 foreach($val as $file){
216 $file_name=explode('/',$file);
217 if(count($file_name)>1){
218 $f_arr[]=$dir['upload_url'].'/'.end($file_name);
219 }
220 }
221 $val=$f_arr;
222 }
223 if(!isset($uploaded_files[$name])){
224 $val=wp_unslash($val);
225 }
226 $lead[$k]=$val;
227 }
228 }
229 //var_dump($lead,$post_data); die('-----------');
230 $form_arr=array('id'=>'cf_'.$form_id,'name'=>$form_title,'fields'=>$tags);
231 $this->entry_created($lead,'0',$form_arr);
232
233 }
234 public function create_entry_na($data){
235 if(class_exists('vxcf_form')){ return; }
236 $form_id=$data['form_id'];
237
238
239 if(empty($data['form_id'])){
240 return;
241 }
242
243 $form_title=$data['settings']['title'];
244 $lead=$upload_files=array();
245 if(!empty($data['fields'])){
246 foreach($data['fields'] as $v){
247 $field_id=$v['id'];
248 if(!empty($v['value'])){
249 if($v['type'] == 'file_upload'){
250 $upload_files[$field_id]=$v['value'];
251 }else{
252 $lead[$field_id]=$v['value'];
253 }
254 }
255 }
256
257 if(is_array($upload_files)){
258 foreach($upload_files as $k=>$v){
259 $lead[$k]=$v;
260 }
261 }
262 $form_arr=array('id'=>'na_'.$form_id,'name'=>$form_title,'fields'=>$data['fields']);
263 $this->entry_created($lead,'0',$form_arr);
264
265 }
266 }
267
268 public function create_entry_wp($fields, $entry, $form_id, $form_data){
269 if(class_exists('vxcf_form')){ return; }
270
271
272 $upload_files=$lead=array();
273 if(!empty($fields)){
274 foreach($fields as $v){
275 if($v['type'] == 'file-upload'){
276 $v['value']=array_map('trim',explode("\n",$v['value']));
277 $upload_files[$v['id']]=$v['value'];
278 }else{
279 $val=$v['value'];
280 if(in_array($v['type'],array('payment-select','payment-multiple'))){
281 $val=$v['amount'];
282 }else if($v['type'] == 'checkbox'){
283 $val=array_map('trim',explode("\n",$val));
284 }
285 $lead[$v['id']]=$val;
286 } }
287
288
289 if(is_array($upload_files)){
290 foreach($upload_files as $k=>$v){
291 $lead[$k]=$v;
292 } }
293
294 $form_arr=array('id'=>'wp_'.$form_data['id'],'name'=>'WP Forms','fields'=>$form_data['fields']);
295 if(!empty($form_data['fields']['settings']['form_title'])){
296 $form_arr['name']=$form_data['fields']['settings']['form_title'];
297 }
298 $id=isset($entry['id']) ? $entry['id'] : '0';
299 $this->entry_created($lead,$id,$form_arr);
300 }
301 //var_dump($fields); die();
302 }
303 public function create_entry_el( $record){
304 if(class_exists('vxcf_form')){ return; }
305 $data=$record->get_formatted_data();
306 $form_id_p=$this->post('form_id');
307 $post_id_p=$this->post('post_id');
308
309 $form_id=$form_id_p.'_'.$post_id_p;
310
311 $fields=$this->get_form_fields('el_'.$form_id);
312 $upload_files=$lead=array();
313 if(!empty($fields)){
314 foreach($fields as $v){
315 if(isset($data[$v['label']])){
316 $val=$data[$v['label']];
317 if($v['type'] == 'upload'){
318 $upload_files[$v['id']]=$val;
319 }else{
320
321 if(in_array($v['type'],array('checkbox','multiselect'))){
322 $val=array_map('trim',explode(',',$val));
323 }
324 $lead[$v['id']]=$val;
325 } } }
326
327 if(is_array($upload_files)){
328 foreach($upload_files as $k=>$v){
329 $lead[$k]=$v;
330 } }
331 //var_dump($lead,$data); die();
332 $form_arr=array('id'=>'el_'.$form_id,'name'=>'Elementor Forms','fields'=>$fields);
333 $this->entry_created($lead,'0',$form_arr);
334
335 }
336 //var_dump($fields); die();
337 }
338 public function create_entry_fd($entry_id,$form_id){
339 if(class_exists('vxcf_form')){ return; }
340
341 $fields=$this->get_form_fields('fd_'.$form_id);
342 global $wpdb;
343 $table=$wpdb->prefix.'frm_item_metas';
344 $sql=$wpdb->prepare("Select * from $table where item_id=%d",$entry_id);
345 $entry=$wpdb->get_results($sql,ARRAY_A);
346 $detail=array();
347 if(is_array($entry) && count($entry)>0){
348 foreach($entry as $v){
349 $detail[$v['field_id']]=$v['meta_value'];
350 }
351 }
352 //var_dump($tags); die();
353 $lead=array();
354 if(is_array($fields)){
355 $uploaded_files_form=array();
356 foreach($fields as $k=>$v){
357
358 $name=$v['name'];
359 if(isset($detail[$name])){
360 $val=$detail[$name];
361 if($v['type'] == 'file'){
362 $val= wp_get_attachment_url($val) ;
363 $base_url=get_site_url();
364 $val=str_replace($base_url,trim(ABSPATH,'/'),$val);
365 $uploaded_files_form[$name]=$val;
366 }
367 $lead[$name]=$detail[$name];
368 }
369 }
370 //
371
372 if(is_array($uploaded_files_form)){
373 foreach($uploaded_files_form as $k=>$v){
374 $lead[$k]=$v;
375 }
376 }
377 }
378 global $wpdb;
379 $table=$wpdb->prefix.'frm_forms';
380 $sql=$wpdb->prepare("Select name from $table where id=%d",$form_id);
381 $form_name=$wpdb->get_var($sql);
382 $form_arr=array('id'=>'fd_'.$form_id,'name'=>$form_name,'fields'=>$fields);
383 $this->entry_created($lead,'0',$form_arr);
384 }
385 public function plugin_api($start_instance=false){
386 if(empty(self::$path)){ self::$path=$this->get_base_path(); }
387 $api_file=self::$path . "pro/plugin-api.php";
388 if(!class_exists('vxcf_plugin_api') && file_exists( $api_file)){
389 require_once( $api_file );
390 }
391 if(class_exists('vxcf_plugin_api')){
392 $slug=$this->get_slug();
393 $settings_link=$this->link_to_settings();
394 $is_plugin_page=$this->is_crm_page();
395 self::$plugin=new vxcf_plugin_api($this->id,$this->version,$this->type,$this->domain,$this->update_id,self::$title,$slug,self::$path,$settings_link,$is_plugin_page);
396 if($start_instance){
397 self::$plugin->instance();
398 }
399 } }
400 /**
401 * contact form entry created
402 *
403 * @param mixed $entry
404 * @param mixed $form
405 */
406 public function entry_created($entry,$entry_id,$form){
407
408 self::$entry_created=true;
409
410 if($this->do_actions()){
411 do_action('vx_addons_save_entry',$entry_id,$entry,'cf',$form);
412 }
413
414 $entry['__vx_id']=$entry_id;
415 $this->push($entry,$form,'',false);
416 }
417 public function entry_created_crmperks($entry_id,$entry,$form){
418 self::$entry_created=true;
419 if($this->do_actions()){
420 do_action('vx_addons_save_entry',$entry_id,$entry,'vf',$form);
421 }
422
423 $form['id']='vf_'.$form['id'];
424 $form['cfx_type']='vf';
425 $entry['__vx_id']=$entry_id;
426 $this->push($entry,$form,'',false);
427 }
428 public function entry_created_contacts($entry,$entry_id,$form){
429
430 if($this->do_actions()){
431 do_action('vx_addons_save_entry',$entry_id,$entry,'cc',$form);
432 }
433
434 $entry['__vx_id']=$entry_id;
435 $this->push($entry,$form,'',false);
436 }
437 public function entry_created_callcenter($entry,$entry_id,$form){
438 $this->push($entry,$form,'',false);
439 }
440
441 /**
442 * Install Contact Form Notice
443 *
444 */
445 public function install_cf_notice(){
446 $message=self::$cf_status_msg;
447 if(!empty($message)){
448 $this->display_msg('admin',$message,'gravity');
449 $this->notice_js=true;
450
451 }
452 }
453 public function submit(){
454 $entry='{"your-name":"Your Name (required)","your-email":"admin@localhost.com","your-subject":"subject test","your-message":"sdfsdfsdfsdf","your-country":["India"],"your-sports":["Tennis"],"your-fruit":["Banana"],"your-browser":"Safari","your-file":""}';
455 $form='{"id":69,"name":"Contact form 1"}';
456 $entry=json_decode($entry,true);
457 $form=json_decode($form,true);
458 apply_filters('vx_addons_save_entry',false,$entry_id,$entry,'cf',$form);
459 $entry['__vx_id']="1";
460 $p=$this->push($entry,$form);
461 var_dump($p); die();
462 }
463
464 /**
465 * Install Contact Form Notice (plugin row)
466 *
467 */
468 public function install_cf_notice_plugin_row(){
469 $message=self::$cf_status_msg;
470 if(!empty($message)){
471 $this->display_msg('',$message,'gravity');
472 }
473 }
474 /**
475 * display admin notice
476 *
477 * @param mixed $type
478 * @param mixed $message
479 * @param mixed $id
480 */
481 public function display_msg($type,$message,$id=""){
482 //exp
483 global $wp_version;
484 $ver=floatval($wp_version);
485 if($type == "admin"){
486 if($ver<4.2){
487 ?>
488 <div class="error vx_notice notice" data-id="<?php echo esc_attr($id) ?>"><p style="display: table"><span style="display: table-cell; width: 98%"><span class="dashicons dashicons-megaphone"></span> <b><?php esc_html_e('Contact Form Mailchimp Plugin','contact-form-mailchimp-crm') ?>. </b><?php echo wp_kses_post($message);?> </span>
489 <span style="display: table-cell; padding-left: 10px; vertical-align: middle;"><a href="#" class="notice-dismiss" title="<?php esc_html_e('Dismiss Notice','contact-form-mailchimp-crm') ?>">dismiss</a></span> </p></div>
490 <?php
491 }else{
492 ?>
493 <div class="error vx_notice notice below-h2 is-dismissible" data-id="<?php echo esc_attr($id) ?>"><p><span class="dashicons dashicons-megaphone"></span> <b><?php esc_html_e('Contact Form Mailchimp Plugin','contact-form-mailchimp-crm') ?>. </b> <?php echo wp_kses_post($message);?> </p>
494 </div>
495 <?php
496 }
497 }else{
498 ?>
499 <tr class="plugin-update-tr"><td colspan="5" class="plugin-update">
500 <style type="text/css"> .vx_msg a{color: #fff; text-decoration: underline;} .vx_msg a:hover{color: #eee} </style>
501 <div style="background-color: rgba(224, 224, 224, 0.5); padding: 9px; margin: 0px 10px 10px 28px "><div style="background-color: #d54d21; padding: 5px 10px; color: #fff" class="vx_msg"> <span class="dashicons dashicons-info"></span> <?php echo wp_kses_post($message) ?>
502 </div></div></td></tr>
503 <?php
504 }
505 }
506 /**
507 * admin_screen_message function.
508 *
509 * @param mixed $message
510 * @param mixed $level
511 */
512 public function screen_msg( $message, $level = 'updated') {
513 echo '<div class="'. esc_attr( $level ) .' fade notice below-h2 is-dismissible"><p>';
514 echo wp_kses_post($message) ;
515 echo '</p></div>';
516 }
517
518 /**
519 * Contact Form status
520 *
521 */
522 public function cf_status() {
523
524 $installed = 0;
525 if(!class_exists('vxcf_form')) {
526 if(file_exists(WP_PLUGIN_DIR.'/contact-form-entries-pro/contact-form-entries-pro.php')) {
527 $installed=2;
528 }
529 }else{
530 $installed=1;
531 if(!$this->is_cf_supported()){
532 $installed=3;
533 }
534 }
535 if($installed !=1){
536 if($installed === 0){ // not found
537 $message = sprintf(__("%sContact Form Entries%s is required. %s it today!%s", 'contact-form-mailchimp-crm'), "<a href='https://www.crmperks.com/'>", "</a>", "<a href='https://www.crmperks.com/'>", "</a>");
538 }else if($installed === 2){ // not active
539 $message = sprintf(__('Contact Form Entries is installed but not active. %sActivate Contact Form Entries%s to use the Contact Form Mailchimp Plugin','contact-form-mailchimp-crm'), '<strong><a href="'.wp_nonce_url(admin_url('plugins.php?action=activate&plugin=contact-form-entries-pro/contact-form-entries-pro.php'), 'activate-plugin_contact-form-entries-pro/contact-form-entries-pro.php').'">', '</a></strong>');
540 } else if($installed === 3){ // not supported
541 $message = sprintf(__("A higher version of %sContact Form Entries%s is required. %sPurchase it today!%s", 'contact-form-mailchimp-crm'), "<a href='https://www.crmperks.com/'>", "</a>", "<a href='https://www.crmperks.com/'>", "</a>");
542 }
543 self::$cf_status_msg=$message;
544 }
545 return $installed;
546 }
547
548
549 /**
550 * Returns true if the current page is an Feed pages. Returns false if not
551 *
552 * @param mixed $page
553 */
554 public function is_crm_page($page=""){
555 if(empty($page)) {
556 $page = $this->post("page");
557 }
558 return $page == $this->id;
559 }
560
561
562 /**
563 * form fields
564 *
565 * @param mixed $form_id
566 */
567 public function get_form_fields($form_id){
568 $fields=array();
569
570 $fields=apply_filters('vx_add_crm_form_fields',$fields,$form_id);
571 if(empty($fields)){
572 global $vxcf_form;
573
574 if(is_object($vxcf_form) && method_exists($vxcf_form,'get_form_fields')){
575 $fields=$vxcf_form->get_form_fields($form_id);
576 }else{
577
578 $form_arr=explode('_',$form_id);
579 $type=$id='';
580 $fields = array();
581 if(isset($form_arr[0])){
582 $type=$form_arr[0];
583 }
584 if(isset($form_arr[1])){
585 $id=$form_arr[1];
586 }
587 switch($type){
588 case'cf':
589 if(method_exists('WPCF7_ShortcodeManager','get_instance') || method_exists('WPCF7_FormTagsManager','get_instance')){
590 $id=substr($form_id,3);
591 $form_text=get_post_meta($id,'_form',true);
592
593 if(method_exists('WPCF7_FormTagsManager','get_instance')){
594 $manager=WPCF7_FormTagsManager::get_instance();
595 $contents=$manager->scan($form_text);
596 $tags=$manager->get_scanned_tags();
597
598 }else if(method_exists('WPCF7_ShortcodeManager','get_instance')){ //
599 $manager = WPCF7_ShortcodeManager::get_instance();
600 $contents=$manager->do_shortcode($form_text);
601 $tags=$manager->get_scanned_tags();
602 }
603
604 if(is_array($tags)){
605 foreach($tags as $tag){
606 if(is_object($tag)){ $tag=(array)$tag; }
607 if(!empty($tag['name'])){
608 $id=str_replace(' ','',$tag['name']);
609 $tag['label']=ucwords(str_replace(array('-','_')," ",$tag['name']));
610 $tag['type_']=$tag['type'];
611 $tag['type']=$tag['basetype'];
612 $tag['req']=strpos($tag['type'],'*') !==false ? 'true' : '';
613 if($tag['type'] == 'select' && !empty($tag['options']) && array_search('multiple',$tag['options'])!== false){
614 $tag['type']='multiselect';
615 }
616 $fields[$id]=$tag;
617 }
618 }
619 }
620 }
621 break;
622 case'na':
623 if(class_exists('Ninja_Forms')){
624
625 $form_fields = Ninja_Forms()->form( $id )->get_fields(); //var_dump($form_fields); die('----------');
626 foreach ($form_fields as $obj) {
627 $field=array();
628 if( is_object( $obj ) ) {
629 $field = $obj->get_settings();
630 $field['id']= $obj->get_id();
631 }
632
633 $arr=array('name'=>$field['id']);
634 $type=$field['type'];
635 if($type == 'textbox'){ $type='text'; }
636 if($type == 'starrating'){ $type='text'; }
637 if($type == 'file_upload'){ $type='file'; }
638 if(in_array($type,array('spam','confirm','submit','repeater','save','html','hr')) ){ continue; } //|| !isset($field['required']) // it is not set for hidden fields that is why removed it
639 if($type == 'checkbox'){
640 $arr['values']=array(array('text'=>$field['label'],'value'=>'1'));
641 }
642 if(in_array($type,array('listmultiselect','listcheckbox','listradio','listselect'))){
643 $type=ltrim($type,'list');
644 $vals=array();
645 if(!empty($field['options'])){
646 foreach($field['options'] as $v){
647 $vals[]=array('text'=>$v['label'],'value'=>$v['value']);
648 }
649 }
650 $arr['values']=$vals;
651 }
652
653 $arr['type']=$type;
654 $arr['label']=$field['label'];
655 $arr['req']=!empty($field['required']) ? 'true' : 'false';
656 $fields[$field['id']]=$arr;
657 }
658 }
659 break;
660 case'el':
661 if(isset($form_arr[2])){
662 $post_id=$form_arr[2];
663 $forms=get_post_meta($post_id,'_elementor_data',true);
664 $forms=json_decode($forms,true);
665 if(!empty($forms)){
666 $form=self::find_el_form($forms,$id);
667
668 if(!empty($form['form_fields'])){
669 foreach($form['form_fields'] as $tag){
670 if(!empty($tag['custom_id']) ){
671 if(empty($tag['field_type'])){ $tag['field_type']=$tag['custom_id']; }
672 if(!in_array($tag['field_type'],array('html','step','honeypot','recaptcha','recaptcha_v3'))){
673 $field=array('id'=>$tag['custom_id']);
674 $field['name']=$tag['custom_id'];
675 $field['label']=$tag['field_label'];
676 $field['type']=$tag['field_type'];
677 $field['req']=!empty($tag['required']) ? 'true' : '';
678 if(!empty($tag['allow_multiple']) ){
679 $field['type']='multiselect';
680 }
681 if($field['type'] == 'acceptance'){
682 $field['type']='checkbox';
683 $field['values']=array(array('label'=>$tag['acceptance_text'],'value'=>'on'));
684 }
685 if($field['type'] == 'upload'){
686 $field['type']='file';
687 }
688 if(!empty($tag['field_options'])){
689 $opts_array=explode("\n",$tag['field_options']);
690 $ops=array();
691 foreach($opts_array as $v){
692 $v_arr=explode('|',$v);
693 if(!isset($v_arr[1])){ $v_arr[1]=$v_arr[0]; }
694 $ops[]=array('label'=>$v_arr[0],'value'=>$v_arr[1]);
695 }
696 $field['values']=$ops;
697 }
698 $fields[$tag['custom_id']]=$field;
699 } }
700 }
701 }
702 }
703
704 }
705 break;
706 case'fd':
707 global $wpdb;
708 $table=$wpdb->prefix.'frm_fields';
709 $sql=$wpdb->prepare("Select * from $table where form_id=%d",$id);
710 $fields_arr=$wpdb->get_results($sql,ARRAY_A);
711 if(count($fields_arr)>0){
712 foreach($fields_arr as $field){
713 $field['label']=$field['name'];
714 $field['name']=$field['id'];
715 if(!empty($field['options'])){
716 $field['values']=maybe_unserialize($field['options']);
717 }
718 $fields[$field['id']]=$field;
719 }
720 }
721 break;
722 case'wp':
723 if(function_exists('wpforms') && method_exists(wpforms()->form,'get')){
724 $forms_arr=wpforms()->form->get( $id );
725 if(!empty($forms_arr)){
726 $form=json_decode($forms_arr->post_content,true);
727 $fields=array();
728 foreach($form['fields'] as $v){
729 $type=$v['type'];
730 if($type == 'name'){ $type='text'; }
731 if($type == 'payment-select'){ $type='select'; }
732 if($type == 'payment-multiple'){ $type='radio'; }
733 if($type == 'payment-single'){ $type='text'; }
734 if($type == 'file-upload'){ $type='file'; }
735 if($type == 'date-time'){ $type='date'; }
736 if($type == 'address'){ $type='textarea'; }
737 if($type == 'phone'){ $type='tel'; }
738 $label=isset($v['label']) ? $v['label'] : $type;
739 // if(in_array($type,array('text','textarea','email','number','hidden','select','checkbox','radio','url','password','tel','date','file','number-slider'))){
740 $field=array('id'=>$v['id'],'name'=>$v['id'],'label'=>$label,'type'=>$type);
741 $field['req']=!empty($v['required']) ? true : false;
742 if(in_array($type,array('radio','checkbox','select'))){
743 $is_val=false;
744 if(in_array($v['type'],array('payment-select','payment-multiple'))){ $is_val=true; }
745 $choices=array();
746 if(!empty($v['choices'])){
747 foreach($v['choices'] as $c){
748 $c_val=$is_val ? $c['value'] : $c['label'];
749 $choices[]=array('text'=>$c['label'],'value'=>$c_val);
750 }
751 }
752 $field['values']=$choices;
753 }
754 $fields[$v['id']]=$field;
755 // }
756
757 }
758 } } //var_dump($form['fields']);
759 break;
760 }
761 }
762 }
763 return $fields;
764
765
766 }
767 public static function find_el_form($var,$key=''){
768
769 if(is_array($var) && isset($var[0]) ){
770 foreach($var as $v){
771 if (!empty($v['elements']) && is_array( $v['elements'] ) ) {
772 $se=self::find_el_form($v['elements'],$key);
773 if(!empty($se)){ return $se; }
774 }
775 if($v['id'] == $key){ // var_dump($v); echo '----<hr>';
776 return $v['settings'];
777 }
778 }
779
780 }
781 }
782 public static function find_el_forms($var,&$forms=array()){
783
784 if(is_array($var) && isset($var[0]) ){
785 foreach($var as $v){
786 if(isset($v['widgetType']) && $v['widgetType'] == 'form'){
787 $forms[]= $v;
788 }
789 if (!empty($v['elements']) && is_array( $v['elements'] ) ) {
790 self::find_el_forms($v['elements'],$forms);
791 }
792
793 }
794
795 }
796 return $forms;
797 }
798
799 public function get_form($form_id){
800
801 $title='Contacts Addon';
802 if($form_id !='vx_contacts'){
803 global $vxcf_form;
804 if(method_exists($vxcf_form,'get_forms')){
805 $forms=$vxcf_form->get_forms();
806 if(is_array($forms) && count($forms)>0){
807 foreach($forms as $key=>$vals){
808 $found=false;
809 if(!empty($vals['forms']) && is_array($vals['forms'])){
810 foreach($vals['forms'] as $k=>$v){
811 $f_id=$key.'_'.$k;
812 if($f_id == $form_id){
813 $found=true;
814 if(!empty($v)){
815 $title=$v;
816 }
817 break;
818 }
819 }
820 }
821 if($found){
822 break;
823 }
824 }
825 }
826 }
827 }
828 $form=array('id'=>$form_id,'title'=>$title,'name'=>$title);
829
830 return $form;
831 }
832
833
834 /**
835 * settings link
836 *
837 * @param mixed $escaped
838 */
839 public function link_to_settings( $tab='' ) {
840 $q=array('page'=>$this->id);
841 if(!empty($tab)){
842 $q['tab']=$tab;
843 }
844 $url = admin_url('admin.php?'.http_build_query($q));
845
846 return $url;
847 }
848
849
850 /**
851 * Get CRM info
852 *
853 */
854 public function get_info($id){
855 $data=$this->get_data_object();
856 $info=$data->get_account($id);
857 $data=array(); $meta=$info_arr=array();
858 if(is_array($info)){
859 if(!empty($info['data'])){
860 $info['data']=trim($info['data']);
861 if(strpos($info['data'],'{') !== 0){
862 $info['data']=$this->de_crypt($info['data']);
863 }
864 $info_arr=json_decode($info['data'],true);
865 if(!is_array($info_arr)){
866 $info_arr=array();
867 }
868 }
869
870 $info_arr['time']=$info['time'];
871 $info_arr['id']=$info['id'];
872 $info['data']=$info_arr;
873 if(!empty($info['meta'])){
874 $meta=json_decode($info['meta'],true);
875 }
876 $info['meta']=is_array($meta) ? $meta : array();
877
878 }
879 return $info;
880 }
881 /**
882 * update account
883 *
884 * @param mixed $data
885 * @param mixed $id
886 */
887 public function update_info($data,$id) {
888
889 if(empty($id)){
890 return;
891 }
892
893 $time = current_time( 'mysql' ,1);
894
895 $sql=array('updated'=>$time);
896 if(is_array($data)){
897
898
899 if(isset($data['meta'])){
900 $sql['meta']= json_encode($data['meta']);
901 }
902 if( isset($data['data']) && is_array($data['data'])){
903 $_data=$this->get_data_object();
904 $acount=$_data->get_account($id);
905 if(array_key_exists('time' , $data['data']) && empty($data['data']['time'])){
906 $sql['time']= $time;
907 $sql['status']= '2';
908 }
909 if(isset($data['data']['class'])){
910 $sql['status']= $data['data']['class'] == 'updated' ? '1' : '2';
911 }
912 if(isset($data['data']['meta'])){
913 unset($data['data']['meta']);
914 }
915 if(isset($data['data']['status'])){
916 unset($data['data']['status']);
917 }
918 if(isset($data['data']['name'])){
919 $sql['name']=$data['data']['name'];
920 // unset($data['data']['name']);
921 }else if(isset($_GET['id'])){
922 $sql['name']="Account #".$this->post('id');
923 }
924
925 $enc_str=json_encode($data['data']);
926 //$enc_str=$this->en_crypt($enc_str);
927 $sql['data']=$enc_str;
928 }
929 }
930
931 $data=$this->get_data_object();
932 $result = $data->update_info_data($sql,$id);
933
934
935 return $result;
936 }
937
938 /**
939 * contact form field values, modify check boxes etc
940 *
941 * @param mixed $entry
942 * @param mixed $form
943 * @param mixed $gf_field_id
944 * @param mixed $crm_field_id
945 * @param mixed $custom
946 */
947 public function verify_field_val($entry,$field_id,$sf_id=''){
948 $value=false;
949
950 if(isset($entry[$field_id])){
951 $value=$entry[$field_id];
952 if(is_array($value) && isset($value['value'])){
953 $value=$value['value'];
954 }
955 if(!is_array($value)){
956 // $value=maybe_unserialize($value);
957 }
958
959 }
960 $fields=$this->form_fields;
961 $type=isset($fields[$field_id]['type']) ? $fields[$field_id]['type'] : '';
962 if( $type == 'file' && !empty($value)){
963 if(class_exists('vxcf_form')){
964 $upload=vxcf_form::get_upload_dir();
965 $temp_files=array();
966 if(!is_array($value)){ $value=array($value); }
967 foreach($value as $f){
968 if(filter_var($f,FILTER_VALIDATE_URL) === false){
969 if(strpos($sf_id,'vx_list_files') !== false){
970 $f=$upload['dir'].$f;
971 }else{
972 $f=$upload['url'].$f; //url , dir
973 } }
974 $temp_files[]=$f;
975 } $value=$temp_files;
976 }
977 $value=trim(implode(' ',$value));
978 }else if( is_array($value) && count($value) == 1 ){
979 $value=trim(implode(' ',$value));
980 }
981 return $value;
982 }
983 /**
984 * filter enteries
985 *
986 * @param mixed $feed
987 * @param mixed $entry
988 * @param mixed $form
989 */
990 public function check_filter($feed,$entry){
991 $filters=$this->post('filters',$feed);
992 $final=$this->filter_condition=null;
993 if(is_array($filters)){
994 $time=current_time('timestamp');
995 foreach($filters as $filter_s){
996 $check=null; $and=null;
997 if(is_array($filter_s)){
998 foreach($filter_s as $filter){
999 $field=$filter['field'];
1000 $fval=$filter['value'];
1001 $val=$this->verify_field_val($entry,$field);
1002 if(is_array($val)){ $val=trim(implode(' ',$val)); }
1003 switch($filter['op']){
1004 case"is": $check=$fval == $val; break;
1005 case"is_not": $check=$fval != $val; break;
1006 case"contains": $check=strpos($val,$fval) !==false; break;
1007 case"not_contains": $check=strpos($val,$fval) ===false; break;
1008 case"is_in": $check=strpos($fval,$val) !==false; break;
1009 case"not_in": $check=strpos($fval,$val) ===false; break;
1010 case"starts": $check=strpos($val,$fval) === 0; break;
1011 case"not_starts": $check=strpos($val,$fval) !== 0; break;
1012 case"ends": $check=(strpos($val,$fval)+strlen($fval)) == strlen($val); break;
1013 case"not_ends": $check=(strpos($val,$fval)+strlen($fval)) != strlen($val); break;
1014 case"less": $check=(float)$val<(float)$fval; break;
1015 case"greater": $check=(float)$val>(float)$fval; break;
1016 case"less_date": $check=strtotime($val,$time) < strtotime($fval,$time); break;
1017 case"greater_date": $check=strtotime($val,$time) > strtotime($fval,$time); break;
1018 case"equal_date": $check=strtotime($val,$time) == strtotime($fval,$time); break;
1019 case"empty": $check=$val == ""; break;
1020 case"not_empty": $check=$val != ""; break;
1021 }
1022 $and_c[]=array("check"=>$check,"field_val"=>$fval,"input"=>$val,"field"=>$field,"op"=>$filter['op']);
1023 if($check !== null){
1024 if($and !== null){
1025 $and=$and && $check;
1026 }else{
1027 $and=$check;
1028 }
1029 }
1030 } //end and loop filter
1031 }
1032 if($and !== null){
1033 if($final !== null){
1034 $final=$final || $and;
1035 }else{
1036 $final=$and;
1037 }
1038 }
1039 $this->filter_condition[]=$and_c;
1040 } // end or loop
1041 }
1042 return $final === null ? true : $final;
1043 }
1044
1045 public function get_all_objects(){
1046 $option=get_option($this->id.'_meta',array());
1047 $objects=array();
1048 if(!empty($option['objects'])){
1049 $objects=$option['objects'];
1050 }
1051 return $objects;
1052 }
1053 /**
1054 * Logs page row
1055 *
1056 * @param mixed $feed
1057 */
1058 public function verify_log($feed,$objects=''){
1059 $link="N/A"; $desc=__("Added to ",'contact-form-mailchimp-crm');
1060 $status_imgs=array("1"=>"created","2"=>"updated","4"=>"filtered",'5'=>'deleted');
1061 $feed['status_img']=isset($status_imgs[$feed["status"]]) ? $status_imgs[$feed["status"]] : 'failed';
1062
1063 if(isset($objects[$feed['object']])){
1064 $feed['object']=$objects[$feed['object']];
1065 }
1066
1067 if(!empty($feed['crm_id'])&& !empty($feed['object']) && !empty($feed['status'])){
1068
1069 $link=$feed['crm_id'];
1070
1071 if($feed['status'] == 1 && $feed['event'] == 'add_note'){
1072 // $desc="Note Added to ";
1073 }
1074 if($feed['status'] == 2){
1075 $desc="Updated to ";
1076 }
1077 $desc.=$feed['object'];
1078 if($feed['status'] == 5){
1079 $desc=sprintf(__("Deleted from %s",'contact-form-mailchimp-crm'),$feed['object']);
1080 }
1081 }else if($feed['status'] == 4){
1082 $desc=sprintf(__("Filtered from %s",'contact-form-mailchimp-crm'),$feed['object']);
1083 }else{
1084 $desc= !empty($feed['error']) ? $feed['error'] : esc_html__("Unknown Error",'contact-form-mailchimp-crm');
1085 }
1086 $title=__("Failed",'contact-form-mailchimp-crm');
1087 if($feed['status'] == 1){
1088 $title=__("Created",'contact-form-mailchimp-crm');
1089 }else if($feed['status'] == 2){
1090 $title=__("Updated",'contact-form-mailchimp-crm');
1091 }else if($feed['status'] == 4){
1092 $title=__("Filtered",'contact-form-mailchimp-crm');
1093 }else if($feed['status'] == 5){
1094 $title=__("Deleted",'contact-form-mailchimp-crm');
1095 }
1096 $feed['_crm_id']= !empty($feed['crm_id']) ? $feed['crm_id'] : "N/A";
1097 $feed['a_link']=$link;
1098 $feed['desc']=$desc;
1099 $feed['title']=$title;
1100 return $feed;
1101 }
1102 /**
1103 * get address components
1104 *
1105 * @param mixed $entry
1106 * @param mixed $field_id
1107 * @param mixed $type
1108 */
1109 private function get_address($entry, $field_id,$type=""){
1110 $street_value = str_replace(" ", " ", trim($entry[$field_id . ".1"]));
1111 $street2_value = str_replace(" ", " ", trim($entry[$field_id . ".2"]));
1112 $city_value = str_replace(" ", " ", trim($entry[$field_id . ".3"]));
1113 $state_value = str_replace(" ", " ", trim($entry[$field_id . ".4"]));
1114 $zip_value = trim($entry[$field_id . ".5"]);
1115 if(method_exists('GF_Field_Address','get_country_code')){
1116 $field_c=new GF_Field_Address();
1117 $country_value=$field_c->get_country_code(trim($entry[$field_id . ".6"]));
1118 }else{
1119 $country_value = GFCommon::get_country_code(trim($entry[$field_id . ".6"]));
1120 }
1121 $country =trim($entry[$field_id . ".6"]);
1122 $address = $street_value;
1123 $address .= !empty($address) && !empty($street2_value) ? " $street2_value" : $street2_value;
1124 if($type =="json"){
1125 $arr=array("street"=>$address,"city"=>$city_value,"state"=>$state_value,"zip"=>$zip_value,"country"=>$country);
1126 return json_encode($arr);
1127 }
1128 $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? " $city_value" : $city_value;
1129 $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value;
1130 $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value;
1131 $address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value;
1132
1133 return $address;
1134 }
1135
1136 /**
1137 * Add checkbox to entry info - option to send entry to crm
1138 *
1139 * @param mixed $form_id
1140 * @param mixed $lead
1141 */
1142 public function entry_info_send_checkbox( $form_id, $lead ) {
1143
1144 // If this entry's form isn't connected to crm, don't show the checkbox
1145 if(!$this->show_send_to_crm_button() ) { return; }
1146
1147 // If this is not the Edit screen, get outta here.
1148 if(empty($_POST["screen_mode"]) || $_POST["screen_mode"] === 'view') { return; }
1149
1150 if(!current_user_can($this->id."_send_to_crm")){return; }
1151
1152 if( apply_filters( $this->id.'_show_manual_export_button', true ) ) {
1153 printf('<input type="checkbox" name="'.esc_attr($this->id).'_update" id="'.esc_attr($this->id).'_update" value="1" /><label for="'.esc_attr($this->id).'_update" title="%s">%s</label><br /><br />', esc_html__('Create or update this entry in Mailchimp. The fields will be mapped according to the form feed settings.', 'contact-form-mailchimp-crm'), esc_html__('Send to Mailchimp', 'contact-form-mailchimp-crm'));
1154 } else {
1155 echo '<input type="hidden" name="'.esc_attr($this->id).'_update" id="'.esc_attr($this->id).'_update" value="1" />';
1156 }
1157 }
1158 /**
1159 * Add button to entry info - option to send entry to crm
1160 *
1161 * @param mixed $button
1162 */
1163 public function entry_info_send_button( $button = '' ) {
1164 // If this entry's form isn't connected to crm, don't show the button
1165 if(!$this->show_send_to_crm_button()) { return $button; }
1166 if(!current_user_can($this->id."_send_to_crm")){return; }
1167 // Is this the view or the edit screen?
1168 $mode = empty($_POST["screen_mode"]) ? "view" : $this->post("screen_mode");
1169 if($mode === 'view') {
1170 $margin="";
1171 if(defined("vx_btn")){
1172 $margin="margin-top: 5px;";
1173 }else{define('vx_btn','true');}
1174 $button.= '<input type="submit" class="button button-large button-secondary alignright" name="'.esc_attr($this->id).'_send" style="margin-left:5px; '.$margin.'" title="'.__('Create or update this entry in Mailchimp. The fields will be mapped according to the form feed settings.','contact-form-mailchimp-crm').'" value="'.__('Send to Mailchimp', 'contact-form-mailchimp-crm').'" onclick="jQuery(\'#action\').val(\'send_to_crm\')" />';
1175 //logs button
1176
1177 $entry_id=$this->post('lid');
1178 $form_id = rgget('id');
1179 if(empty($entry_id)){
1180 $entry_id=$this->get_entry_id($form_id);
1181 }
1182 $log_url=admin_url( 'admin.php?page=gf_edit_forms&view=settings&subview='.$this->id.'&tab=log&id='.$this->post('id').'&entry_id='.$entry_id);
1183 $button.= '<a class="button button-large button-secondary alignright" style="margin-left:5px; margin-top:5px; " title="'.__('Go to Mailchimp Logs','contact-form-mailchimp-crm').'" href="'.esc_url($log_url).'">'.__('Mailchimp Logs','contact-form-mailchimp-crm').'</a>';
1184
1185 }
1186 return $button;
1187 }
1188 /**
1189 * Whether to show the Entry "Send to CRM" button or not
1190 *
1191 * If the entry's form has been mapped to CRM feed, show the Send to CRM button. Otherwise, don't.
1192 *
1193 * @return boolean True: Show the button; False: don't show the button.
1194 */
1195 private function show_send_to_crm_button() {
1196
1197 $form_id = rgget('id');
1198
1199 return $this->has_feed($form_id);
1200 }
1201 /**
1202 * Does the current form have a feed assigned to it?
1203 * @param INT $form_id Form ID
1204 * @return boolean
1205 */
1206 function has_feed($form_id) {
1207 $data=$this->get_data_object();
1208 $feeds = $data->get_feed_by_form( $form_id , true);
1209
1210 return !empty($feeds);
1211 }
1212
1213
1214
1215 /**
1216 * if contact form installed and supported
1217 *
1218 */
1219 private function is_cf_supported(){
1220 if(class_exists("vxcf_form")){
1221 global $vxcf_form;
1222 $version='1.0';
1223 if($vxcf_form->version){
1224 $version=$vxcf_form->version;
1225 }
1226 $is_correct_version = version_compare($version, $this->min_cf_version, ">=");
1227 return $is_correct_version;
1228 }
1229 else{
1230 return false;
1231 }
1232 }
1233 /**
1234 * uninstall plugin
1235 *
1236 */
1237 public function uninstall(){
1238 //droping all tables
1239 require_once(self::$path . "includes/install.php");
1240 $install=new vxcf_mailchimp_install();
1241 do_action('uninstall_vx_plugin_'.$install->id);
1242 $install->remove_data();
1243 $install->remove_roles();
1244 }
1245 /**
1246 * email validation
1247 *
1248 * @param mixed $email
1249 */
1250 public function is_valid_email($email){
1251 if(function_exists('filter_var')){
1252 if(filter_var($email, FILTER_VALIDATE_EMAIL)){
1253 return true;
1254 }
1255 }else{
1256 if(strpos($email,"@")>1){
1257 return true;
1258 }
1259 }
1260 return false;
1261 }
1262 /**
1263 * deactivate
1264 *
1265 * @param mixed $action
1266 */
1267 public function deactivate($action="deactivate"){
1268 do_action('plugin_status_'.$this->type,$action);
1269 }
1270 /**
1271 * activate plugin
1272 *
1273 */
1274 public function activate(){
1275 $this->plugin_api(true);
1276 $this->maybe_install();
1277 do_action('plugin_status_'.$this->type,'activate');
1278 }
1279 /**
1280 * Send CURL Request
1281 *
1282 * @param mixed $body
1283 * @param mixed $path
1284 * @param mixed $method
1285 */
1286 public function request($path="",$method='POST',$body="",$head=array()) {
1287
1288 $args = array(
1289 'body' => $body,
1290 'headers'=> $head,
1291 'method' => strtoupper($method), // GET, POST, PUT, DELETE, etc.
1292 'sslverify' => false,
1293 'timeout' => 20,
1294 );
1295
1296 $response = wp_remote_request($path, $args);
1297
1298 if(is_wp_error($response)) {
1299 $this->error_msg= $response->get_error_message();
1300 return false;
1301 } else if(isset($response['response']['code']) && $response['response']['code'] != 200 && $response['response']['code'] != 404) {
1302 $this->error_msg = strip_tags($response['body']);
1303 return false;
1304 } else if(!$response) {
1305 return false;
1306 }
1307 $result=wp_remote_retrieve_body($response);
1308 return $result;
1309 }
1310
1311 /**
1312 * Formates User Informations and submitted form to string
1313 * This string is sent to email and mailchimp
1314 * @param array $info User informations
1315 * @param bool $is_html If HTML needed or not
1316 * @return string formated string
1317 */
1318 public function format_user_info($info,$is_html=false){
1319 $str=""; $file="";
1320 if($is_html){
1321 self::$path=$this->get_base_path();
1322 if(file_exists(self::$path."templates/email.php")){
1323 ob_start();
1324 include_once(self::$path."templates/email.php");
1325 $file= ob_get_contents(); // data is now in here
1326 ob_end_clean();
1327 }
1328 if(trim($file) == "")
1329 $is_html=false;
1330 }
1331 if(isset($info['info']) && is_array($info['info'])){
1332 if($is_html){
1333 if(isset($info['info_title'])){
1334 $str.='<tr><td style="font-family: Helvetica, Arial, sans-serif;background-color: #C35050; height: 36px; color: #fff; font-size: 24px; padding: 0px 10px">'.$info['info_title'].'</td></tr>'."\n";
1335 }
1336 if(is_array($info['info']) && count($info['info'])>0){
1337 $str.='<tr><td style="padding: 10px;"><table border="0" cellpadding="0" cellspacing="0" width="100%;"><tbody>';
1338 foreach($info['info'] as $f_k=>$f_val){
1339 $str.='<tr><td style="padding-top: 10px;color: #303030;font-family: Helvetica;font-size: 13px;line-height: 150%;text-align: right; font-weight: bold; width: 28%; padding-right: 10px;">'.$f_k.'</td><td style="padding-top: 10px;color: #303030;font-family: Helvetica;font-size: 13px;line-height: 150%;text-align: left; word-break:break-all;">'.$f_val.'</td></tr>'."\n";
1340 }
1341 $str.="</table></td></tr>";
1342 }
1343 }else{
1344 if(isset($info['title']))
1345 $str.="\n".$info['title']."\n";
1346 foreach($info['info'] as $f_k=>$f_val){
1347 $str.=$f_k." : ".$f_val."\n";
1348 }
1349 }
1350 }
1351 if($is_html){
1352 $str=str_replace(array("{title}","{msg}","{sf_contents}"),array($info['title'],$info['msg'],$str),$file);
1353 }
1354 return $str;
1355 }
1356
1357
1358 /**
1359 * if plugin user is valid
1360 *
1361 * @param mixed $update
1362 */
1363
1364 public function is_valid_user($update){
1365 return is_array($update) && isset($update['user']['user']) && $update['user']['user']!=""&& isset($update['user']['expires']);
1366 }
1367 public function post($key, $arr="") {
1368 if($arr!=""){
1369 return isset($arr[$key]) ? self::clean($arr[$key]) : "";
1370 }
1371 return isset($_REQUEST[$key]) ? self::clean($_REQUEST[$key]) : "";
1372 }
1373 public static function clean($var){
1374 if ( is_array( $var ) ) {
1375 return array_map( array('self','clean'), $var );
1376 } else {
1377 return sanitize_text_field(wp_unslash($var));
1378 }
1379 }
1380 public static function get_key(){
1381 $k='Wezj%+l-x.4fNzx%hJ]FORKT5Ay1w,iczS=DZrp~H+ve2@1YnS;;g?_VTTWX~-|t';
1382 if(defined('AUTH_KEY')){
1383 $k=AUTH_KEY;
1384 }
1385 return substr($k,0,30);
1386 }
1387 /**
1388 * check if other version of this plugin exists
1389 *
1390 */
1391 public function other_plugin_version(){
1392 $status=0;
1393 if(class_exists('vxcf_mailchimp_wp')){
1394 $status=1;
1395 }else if( file_exists(WP_PLUGIN_DIR.'/contact-form-mailchimp-crm/contact-form-mailchimp-crm.php')) {
1396 $status=2;
1397 }
1398 return $status;
1399 }
1400 /**
1401 * Get time Offset
1402 *
1403 */
1404 public function time_offset(){
1405 $offset = (int) get_option('gmt_offset');
1406 return $offset*3600;
1407 }
1408 /**
1409 * Decrypts Values
1410 * @param array $info Mailchimp encrypted API info
1411 * @return array API settings
1412 */
1413 public static function de_crypt($info){
1414 $info=trim($info);
1415 if($info == "")
1416 return '';
1417 $str=base64_decode($info);
1418 $key=self::get_key();
1419 $decrypted_string='';
1420 if(function_exists("openssl_encrypt") && strpos($str,':')!==false ) {
1421 $method='AES-256-CBC';
1422 $arr = explode(':', $str);
1423 if(isset($arr[1]) && $arr[1]!=""){
1424 $decrypted_string=openssl_decrypt($arr[0],$method,$key,false, base64_decode($arr[1]));
1425 }
1426 }else{
1427 $decrypted_string=$str;
1428 }
1429 return $decrypted_string;
1430 }
1431 /**
1432 * Encrypts Values
1433 * @param string $str
1434 * @return string Encrypted Value
1435 */
1436 public static function en_crypt($str){
1437 $str=trim($str);
1438 if($str == "")
1439 return '';
1440 $key=self::get_key();
1441 if(function_exists("openssl_encrypt")) {
1442 $method='AES-256-CBC';
1443 $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
1444 $enc_str=openssl_encrypt($str,$method, $key,false,$iv);
1445 $enc_str.=":".base64_encode($iv);
1446 }else{
1447 $enc_str=$str;
1448 }
1449 $enc_str=base64_encode($enc_str);
1450 return $enc_str;
1451 }
1452
1453 /**
1454 * Get variable from array
1455 *
1456 * @param mixed $key
1457 * @param mixed $key2
1458 * @param mixed $arr
1459 */
1460 public function post2($key,$key2, $arr="") {
1461 if(is_array($arr)){
1462 return isset($arr[$key][$key2]) ? self::clean($arr[$key][$key2]) : "";
1463 }
1464 return isset($_REQUEST[$key][$key2]) ? self::clean($_REQUEST[$key][$key2]) : "";
1465 }
1466 /**
1467 * Get variable from array
1468 *
1469 * @param mixed $key
1470 * @param mixed $key2
1471 * @param mixed $arr
1472 */
1473 public function post3($key,$key2,$key3, $arr="") {
1474 if(is_array($arr)){
1475 return isset($arr[$key][$key2][$key3]) ? self::clean($arr[$key][$key2][$key3]) : "";
1476 }
1477 return isset($_REQUEST[$key][$key2][$key3]) ? self::clean($_REQUEST[$key][$key2][$key3]) : "";
1478 }
1479 /**
1480 * get base url
1481 *
1482 */
1483 public function get_base_url(){
1484 return plugin_dir_url(__FILE__);
1485 }
1486 /**
1487 * get plugin direcotry name
1488 *
1489 */
1490 public function plugin_dir_name(){
1491 if(!empty($this->plugin_dir)){
1492 return $this->plugin_dir;
1493 }
1494 if(empty(self::$path)){
1495 self::$path=$this->get_base_path();
1496 }
1497 $this->plugin_dir=basename(self::$path);
1498 return $this->plugin_dir;
1499 }
1500 /**
1501 * get plugin slug
1502 *
1503 */
1504 public function get_slug(){
1505 return plugin_basename(__FILE__);
1506 }
1507 public function do_actions(){
1508 if(!is_object(self::$plugin) ){ $this->plugin_api(); }
1509 if(is_object(self::$plugin) && method_exists(self::$plugin,'valid_addons')){
1510 return self::$plugin->valid_addons();
1511 }
1512
1513 return false;
1514 }
1515 /**
1516 * Returns the physical path of the plugin's root folder
1517 *
1518 */
1519 public function get_base_path(){
1520 return plugin_dir_path(__FILE__);
1521 }
1522
1523 /**
1524 * get api object
1525 *
1526 * @param mixed $settings
1527 * @return vxg_api_mailchimp
1528 */
1529 public function get_api($crm=""){
1530 $api = false;
1531 $api_class=$this->id."_api";
1532 if(!class_exists($api_class))
1533 require_once(self::$path."api/api.php");
1534
1535 $api = new $api_class($crm);
1536 return $api;
1537 }
1538 /**
1539 * get contact form entry
1540 *
1541 */
1542 public function get_cf_entry($entry_id,$type=''){
1543 $entry=array();
1544
1545 if($type == 'addon'){
1546
1547 if( class_exists( 'vxcf_crm' ) && !empty( $entry_id ) ) {
1548 $entry = vxcf_crm::get_entry( $entry_id );
1549 $entry['__vx_id']=$entry_id;
1550 $entry['form_id']='vx_contacts';
1551 }
1552 }
1553 else{
1554 if( class_exists( 'vxcf_form' ) && !empty( $entry_id ) ) {
1555 $entry = vxcf_form::get_entry( $entry_id );
1556 $entry['__vx_id']=$entry_id;
1557 }
1558 }
1559 return $entry;
1560 }
1561 /**
1562 * get contact form entry
1563 *
1564 */
1565 public function get_cf_entry_detail($entry_id){
1566 $entry=array();
1567 // Fetch entry (use new GF API from version 1.8)
1568 if( class_exists( 'vxcf_form' ) && !empty( $entry_id ) ) {
1569 $entry = vxcf_form::get_entry_detail( $entry_id );
1570 }
1571 return $entry;
1572 }
1573 /**
1574 * get data object
1575 *
1576 */
1577 public function get_data_object(){
1578 require_once(self::$path . "includes/data.php");
1579 if(!is_object($this->data))
1580 $this->data=new vxcf_mailchimp_data();
1581 return $this->data;
1582 }
1583
1584
1585 /**
1586 * push form data to crm
1587 *
1588 * @param mixed $entry
1589 * @param mixed $form
1590 * @param mixed $is_admin
1591 */
1592 public function push($entry, $form,$event="",$is_admin=true,$log=""){
1593
1594 $data_db=$this->get_data_object();
1595 $log_id=''; $feeds_meta=array();
1596 if(!empty($log)){
1597 if(isset($log['id'])){
1598 $log_id=$log['id'];
1599 }
1600 $log_feed=$data_db->get_feed($log['feed_id']);
1601
1602 if(!empty($log_feed)){
1603 $feeds_meta=array($log_feed);
1604 }
1605 }else{
1606 //get feeds of a form
1607 $feeds=$data_db->get_feed_by_form($form['id'],true);
1608
1609 if(is_array($feeds) && count($feeds)>0){
1610 $e=1000; $k=1;
1611 foreach($feeds as $feed){
1612 $data=isset($feed['data']) ? json_decode($feed['data'],true) : array();
1613 $meta=isset($feed['meta']) ? json_decode($feed['meta'],true) : array();
1614 $feed['meta']=$meta;
1615 $feed['data']=$data;
1616 if(!empty($data['organisation_check']) || !empty($data['opportunity_check']) || !empty($data['contact_check'])){
1617 $feeds_meta[$e++]=$feed;
1618 }else{
1619 $feeds_meta[$k++]=$feed;
1620 }
1621 }
1622 ksort($feeds_meta);
1623 }
1624
1625 }
1626
1627 $entry_id=$this->post('__vx_id',$entry);
1628
1629 $type='cf';
1630 if($form['id'] == 'vx_contacts'){
1631 $type='cc';
1632 }
1633
1634 if(empty($form['__vx_addons']) && ($event == '' || $event == 'update' || $event == 'submit')){
1635 $entry=apply_filters('vx_crm_post_fields',$entry,$entry_id,$type,$form);
1636 }
1637 if(isset($form['id'])){
1638 $entry['_vx_form_id']=$form['id'];
1639 }
1640 if( isset($form['name'])){
1641 $entry['_vx_form_name']=$form['name'];
1642 }else if( isset($form['title'])){
1643 $entry['_vx_form_name']=$form['title'];
1644 }
1645
1646 $local_time=current_time( 'mysql');
1647 $entry['_vx_created']=isset($entry['__vx_entry']['created']) ? $entry['__vx_entry']['created'] : $local_time;
1648 $entry['_vx_updated']=isset($entry['__vx_entry']['updated']) ? $entry['__vx_entry']['updated'] : $local_time;
1649
1650 if(!empty($entry['_vx_created'])){
1651 $entry['_vx_created']=date('m/d/Y h:i A',strtotime($entry['_vx_created']));
1652 }
1653 if(!empty($entry['_vx_updated'])){
1654 $entry['_vx_updated']=date('m/d/Y h:i A',strtotime($entry['_vx_updated']));
1655 }
1656 if(!empty($entry['__vx_entry'])){
1657 $entry['_vx_url']=$entry['__vx_entry']['url'];
1658 }
1659
1660 $screen_msg_class="updated"; $notice=""; $log_link='';
1661 if(is_array($feeds_meta) && count($feeds_meta)>0){
1662 foreach($feeds_meta as $feed){
1663 $temp=array();
1664 $force_send=false;
1665 $post_comment=true;
1666 $screen_msg="";
1667 $parent_id=0;
1668 if(isset($entry['__vx_parent_id'])){
1669 $parent_id=$entry['__vx_parent_id'];
1670 }
1671 $object=$this->post('object',$feed);
1672 if(empty($object)){
1673 continue;
1674 }
1675 //
1676 $data=$meta=array();
1677
1678 if(is_array($feed)){
1679 if(isset($feed['data']) && is_array($feed['data'])){
1680 $data=$feed['data'];
1681 $feed=array_merge($feed,$data);
1682 }
1683 //
1684 if(isset($feed['meta']) && is_array($feed['meta'])){
1685 $meta=$feed['meta'];
1686 $feed=array_merge($feed,$meta);
1687 }
1688 }
1689 //
1690
1691
1692 if( in_array($event,array('restore','update','delete','add_note','delete_note'))){
1693 $is_admin=true;
1694 $search_object=$object;
1695 if(in_array($event,array('add_note','delete_note')) && !empty($log)){
1696
1697 if($event == 'add_note'){
1698 $note=json_decode($log['data'],true);
1699 if(!empty($note['title']['value'])){
1700 self::$note=array('id'=>$log['parent_id']);
1701 self::$note['title']=$note['title']['value'];
1702 self::$note['body']=$note['body']['value'];
1703 }
1704 }
1705 }
1706 if($event == 'delete_note' && !empty(self::$note)){
1707 $parent_id=self::$note['id'];
1708 }
1709
1710 if(in_array($event,array('delete_note','add_note'))){
1711 $order_notes=$this->post('entry_notes',$feed); //if notes sync not enabled in feed return
1712 if( empty($order_notes)){
1713 continue;
1714 }
1715 //change main object to Note
1716 $feed['related_object']=$object;
1717 $object=$feed['object']='Note';
1718 }
1719 if($event == 'delete_note'){
1720 //when deleting note search note object
1721 $search_object='Note';
1722 }
1723 $_data=$this->get_data_object();
1724 $feed_log=$_data->get_feed_log($feed['id'],$entry_id,$search_object,$parent_id);
1725 //var_dump($feed_log); die();
1726 if($event == 'restore' && $feed_log['status'] != 5) { // only allow successfully deleted records
1727 continue;
1728 }
1729 if( in_array($event,array('update','delete') ) && !in_array($feed_log['status'],array(1,2) ) ){ // only allow successfully sent records
1730 continue;
1731 }
1732 if(empty($feed_log['crm_id']) || empty($feed_log['object']) || $feed_log['object'] != $search_object){
1733
1734 continue;
1735 }
1736 if($event !='restore'){
1737 $feed['crm_id']=$feed_log['crm_id'];
1738 unset($feed['primary_key']);
1739 }
1740 $feed['event']=$event;
1741 // add note and save related extra info
1742 if( $event == 'add_note' && !empty(self::$note)){
1743 $temp=array('title'=>array('value'=>self::$note['title']),'body'=>array('value'=>self::$note['body']),'parent_id'=>array('value'=> $feed['crm_id']),'object'=>array('value'=> $search_object));
1744 $parent_id=self::$note['id'];
1745 $feed['note_object_link']=$feed_log['crm_id'];
1746 }
1747 // delete not and save extra info
1748 if( $event == 'delete_note'){
1749 $feed_log_arr= json_decode($feed_log['extra'],true);
1750 if(isset($feed_log_arr['note_object_link'])){
1751 $feed['note_object_link']=$feed_log_arr['note_object_link'];
1752 }
1753 $temp=array('ParentId'=>array('value'=> $feed['crm_id']));
1754 }
1755 //delete object
1756 if( $event == 'delete'){
1757 $temp=array('Id'=>array('value'=> $feed['crm_id']));
1758 }
1759 //
1760 if(!in_array($event , array('update','restore') )){
1761 //do not apply filters when adding note , deleting note , entry etc
1762 $force_send=true;
1763 }
1764 //do not post comment in al other cases
1765 $post_comment=false;
1766
1767 }
1768 // var_dump(self::$note,$object,$feed['note_object'],$feed['object'],$feed['crm_id'],$feed['event'],$temp,$force_send);
1769 //continue;
1770 // die();
1771 if(isset($entry['__vx_data'])){
1772 $force_send=true;
1773 $temp=$entry['__vx_data'];
1774 }
1775
1776 if(!$force_send && isset($data['map']) && is_array($data['map']) && count($data['map'])>0){
1777
1778 $custom= isset($meta['fields']) && is_array($meta['fields']) ? $meta['fields'] : array();
1779 if(empty($this->form_fields)){
1780 $this->form_fields=$this->get_form_fields($form['id']);
1781 }
1782 foreach($data['map'] as $k=>$v){
1783 ///
1784 $value='';
1785 if(!empty($v)){ //if value not empty
1786 if($this->post('type',$v) == "value"){ //custom value
1787 $value=trim($this->post('value',$v));
1788 //starts with { and ends } , any char in brackets except {
1789 preg_match_all('/\{[^\{]+\}/',$value,$matches);
1790 if(!empty($matches[0])){
1791 $vals=array();
1792 foreach($matches[0] as $m){
1793 $m=trim($m,'{}');
1794 $vals['{'.$m.'}']=$this->verify_field_val($entry,$m);
1795 }
1796 $value=str_replace(array_keys($vals),array_values($vals),$value);
1797 }
1798
1799 }else{ //general field
1800 $field=$this->post('field',$v);
1801
1802 if($field !=""){
1803 $value=$this->verify_field_val($entry,$field);
1804 if($field == '_vx_form_id' && isset($form['id'])){
1805 $value=$form['id'];
1806 }else if($field == '_vx_form_name' && isset($form['title'])){
1807 $value=$form['title'];
1808 }
1809 }}
1810 if(!empty($value)){
1811 if(isset($custom[$k])){
1812
1813 $temp[$k]=array("value"=>$value,"label"=>$custom[$k]['label']);
1814 }
1815 }
1816 }
1817 }
1818
1819 //add note
1820 if(!empty($data['note_check']) && !empty($data['note_fields']) && is_array($data['note_fields'])){
1821 $entry_note=''; $entry_note_title='';
1822 foreach($data['note_fields'] as $e_note){
1823 $value=$this->verify_field_val($entry,$e_note);
1824 if(!empty($value)){
1825 if(!empty($entry_note)){
1826 $entry_note.="\n";
1827 }
1828 $entry_note.=$value;
1829 }
1830 if(empty($entry_note_title)){
1831 $entry_note_title=substr($entry_note,0,100);
1832 }
1833 }
1834 if(!empty($entry_note)){
1835 $feed['__vx_entry_note']=array('title'=>$entry_note_title,'body'=>$entry_note);
1836 }
1837
1838 }
1839
1840 }
1841
1842 $no_filter=true;
1843 //not submitted by admin
1844 if( !$is_admin && $this->post('manual_export',$data) == "1"){ //if manual export is yes
1845 continue;
1846 }
1847 if(isset($_REQUEST['bulk_action']) && $_REQUEST['bulk_action'] =="send_to_crm_bulk_force" && !empty($log_id)){
1848 $force_send=true;
1849 }
1850 if(!$force_send && $this->post('optin_enabled',$data) == "1"){ //apply filters if not sending by force and optin is enabled
1851 $no_filter=$this->check_filter($data,$entry);
1852 $res=array("status"=>"4","extra"=>array("filter"=>$this->filter_condition),"data"=>$temp);
1853 }
1854 $account=$this->post('account',$feed);
1855
1856 $info=$this->get_info($account);
1857
1858 if(!$no_filter && !empty($data['un_sub'])){
1859 $no_filter=true;
1860 if(empty($feed['primary_key'])){ $feed['primary_key']='email_address'; }
1861 $feed['vx_unsub']='1';
1862 }
1863
1864 if($no_filter){ //get $res if no filter , other wise use filtered $res
1865 $api=$this->get_api($info);
1866 $res_temp=$api->push_object($feed['object'],$temp,$feed);
1867 if(!empty($res_temp)){ $res=$res_temp; }
1868 }
1869
1870 $feed_id=$this->post('id',$feed);
1871 self::$feeds_res[$feed_id]=$res;
1872 $status=$res['status']; $error=""; $id="";
1873 if($this->post('id',$res)!=""){
1874 $id=$res['id'];
1875 $action=$this->post('action',$res);
1876 if($action == "Added"){
1877 if(empty($res['link'])){
1878 $msg=sprintf(__('Successfully Added to Mailchimp (%s) with ID # %s .', 'contact-form-mailchimp-crm'),$feed['object'],$res['id']);
1879 }else{
1880 $msg=sprintf(__('Successfully Added to Mailchimp (%s) with ID # %s . View entry at %s', 'contact-form-mailchimp-crm'),$feed['object'],$res['id'],$res['link']);
1881 }
1882 $screen_msg=__( 'Entry added in Mailchimp', 'contact-form-mailchimp-crm');
1883 }else{
1884 if(empty($res['link'])){
1885 $msg=sprintf(__('Successfully Updated to Mailchimp (%s) with ID # %s . View entry at %s', 'contact-form-mailchimp-crm'),$feed['object'],$res['id'],$res['link']);
1886 }else{
1887 $msg=sprintf(__('Successfully Updated to Mailchimp (%s) with ID # %s .', 'contact-form-mailchimp-crm'),$feed['object'],$res['id']);
1888 }
1889 if($event == 'delete'){
1890 $screen_msg=__( 'Entry deleted from Mailchimp', 'contact-form-mailchimp-crm');
1891 }else{
1892 $screen_msg=__( 'Entry updated in Mailchimp', 'contact-form-mailchimp-crm');
1893 }
1894 }
1895
1896
1897 }else if($this->post('status',$res) == 4){
1898 $screen_msg=$msg=__( 'Entry filtered', 'contact-form-mailchimp-crm');
1899 }else{
1900 $status=0; $screen_msg_class="error";
1901 $screen_msg=__('Errors when adding to Mailchimp. Entry not sent! Check the Entry Notes below for more details.' , 'contact-form-mailchimp-crm' );
1902 if($log_id!=""){
1903 //message for bulk actions in logs
1904 $screen_msg=__('Errors when adding to Mailchimp. Entry not sent' , 'contact-form-mailchimp-crm' );
1905 }
1906 $msg=sprintf(__('Error while creating %s', 'contact-form-mailchimp-crm'),$feed['object']);
1907 if($this->post('error',$res)!=""){
1908 $error= is_array($res['error']) ? json_encode($res['error']) : $res['error'];
1909 $msg.=" ($error)";
1910
1911 $_REQUEST['VXGMailchimpError']=$msg; //front end form error for admin only
1912 }
1913 if(!$is_admin){
1914 $info['msg']=$msg;
1915 $this->send_error_email($info,$entry,$form);
1916 }
1917
1918 }
1919
1920 //insert log
1921 $arr=array("object"=>$feed["object"],"form_id"=>$form['id'],"status"=>$status,"entry_id"=>$entry_id,"crm_id"=>$id,"meta"=>$error,"time"=>date('Y-m-d H:i:s'),"data"=>$temp,"response"=>$this->post('response',$res),"extra"=>$this->post('extra',$res),"feed_id"=>$this->post('id',$feed),'parent_id'=>$parent_id,'event'=>$event,"link"=>$this->post('link',$res));
1922 $settings=get_option($this->type.'_settings',array());
1923 if($this->post('disable_log',$settings) !="yes"){
1924 $insert_id=$data_db->insert_log($arr,$log_id);
1925 }
1926 $log_link='';
1927 if(!empty($insert_id)){ //
1928 $log_url=admin_url( 'admin.php?page='.$this->id.'&tab=logs&log_id='.$insert_id);
1929 $log_link=' <a href="'.esc_url($log_url).'" class="vx_log_link" data-id="'.$insert_id.'">'.__('View Detail','contact-form-mailchimp-crm')."</a>";
1930 $screen_msg.=$log_link;
1931 }
1932 if($post_comment){
1933 //insert entry comment
1934 //$this->add_note($entry["id"], $msg);
1935 }
1936 if($notice!=""){
1937 $notice.='<br/>';
1938 }
1939 if(isset($info['objects'][$object])){
1940 $notice.='<b>'.$info['objects'][$object].': </b>';
1941 }
1942 $notice.=$screen_msg;
1943
1944 }
1945 }
1946 return array("msg"=>$notice,"class"=>$screen_msg_class);
1947 }
1948
1949 /**
1950 * Send error email
1951 *
1952 * @param mixed $info
1953 * @param mixed $entry
1954 * @param mixed $form
1955 */
1956 public function send_error_email($info,$entry,$form){
1957 if(!empty($info['data']['error_email'])){
1958 $subject="Error While Posting to Mailchimp";
1959 $entry_link=add_query_arg(array('page' => 'vxcf_leads','tab'=>'entries', 'id' => $entry['__vx_id']), admin_url('admin.php'));
1960 $page_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
1961
1962 $detail=array(
1963 "Time"=>date('d/M/y H:i:s',current_time('timestamp')),
1964 "Page URL"=>'<a href="'.$page_url.'" style="word-break:break-all;">'.$page_url.'</a>',
1965 "Entry ID"=>'<a href="'.$entry_link.'" target="_blank" style="word-break:break-all;">'.$entry_link.'</a>'
1966 );
1967 if(isset($form['title'])){
1968 $detail["Form Name"]=$form['title'];
1969 $detail["Form Id"]=$form['id'];
1970 }
1971 $email_info=array("msg"=>$info['msg'],"title"=>__('Mailchimp','contact-form-mailchimp-crm')." Error","info_title"=>"More Detail","info"=>$detail);
1972 $email_body=$this->format_user_info($email_info,true);
1973
1974 $error_emails=explode(",",$info['data']['error_email']);
1975 $headers = array('Content-Type: text/html; charset=UTF-8');
1976 foreach($error_emails as $email)
1977 wp_mail(trim($email),$subject, $email_body,$headers);
1978
1979 }
1980
1981 }
1982
1983
1984 /**
1985 * Get Objects from local options or from mailchimp
1986 *
1987 * @param mixed $check_option
1988 * @return array
1989 */
1990 public function get_objects($info="",$refresh=false){
1991
1992
1993
1994 $objects=array();
1995 if(!empty($info)){
1996 $meta=$this->post('meta',$info);
1997
1998 }else{
1999 $meta=get_option($this->id.'_meta',array());
2000 }
2001 if(empty($meta['objects'])){
2002 $refresh=true;
2003 }else{
2004 $objects=$meta['objects'];
2005 }
2006
2007 //get objects from mailchimp
2008 if($refresh && !empty($info)){
2009 $api=$this->get_api($info);
2010 $objects=$api->get_crm_objects();
2011
2012 if(is_array($objects)){
2013 $option=get_option($this->id.'_meta',array());
2014 $meta_objects=$objects;
2015 if(!empty($option['objects']) && is_array($option['objects'])){
2016 $meta_objects=array_merge($option['objects'],$objects);
2017 }
2018
2019 $option['objects']=$meta_objects;
2020
2021 update_option($this->id.'_meta',$option); //save objects for logs search option
2022 $meta["objects"]=$objects;
2023 $this->update_info(array("meta"=>$meta),$info['id']);
2024 }
2025 }
2026
2027 return $objects;
2028 }
2029 /**
2030 * check if user conected to crm
2031 *
2032 * @param mixed $settings
2033 */
2034 public function api_is_valid($info="") {
2035
2036 if(isset($info['data']['class']) && is_array($info['data']) && $info['data']['class'] =='updated'){
2037 return true;
2038 }else{
2039 return false;}
2040 }
2041 }
2042
2043 $vxcf_mailchimp=new vxcf_mailchimp();
2044 $vxcf_mailchimp->instance();
2045 $vx_cf['vxcf_mailchimp']='vxcf_mailchimp';
2046 endif;
2047
2048