epm_mailchimp_submit_to_list

Add Ajax Handler - this is the function that handles the submission of the form.

Description

epm_mailchimp_submit_to_list(); 

Usage

  1. if ( !function_exists( 'epm_mailchimp_submit_to_list' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-peasy-mailchimp-ajax-form/includes/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = epm_mailchimp_submit_to_list(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/ajax.php  
  1. function epm_mailchimp_submit_to_list() { 
  2.  
  3. global $epm_options; 
  4.  
  5. //get data from our ajax() call 
  6. $epm_list_id = $_POST['epm_list_id']; 
  7. if(epm_get_option('display_name_fields')): 
  8. $epm_name = $_POST['epm_firstname']; 
  9. $epm_lastname = $_POST['epm_lastname']; 
  10. endif; 
  11. $epm_email = $_POST['epm_email']; 
  12. $epm_enable_validation = apply_filters( 'epm_filter_validation', 'enabled' ); //filter to disable/enable default validation messages 
  13. $epm_enable_success = apply_filters( 'epm_filter_success', 'enabled' ); //filter to disable/enable default success messages 
  14.  
  15. //showerrorif fields are empty and validation is enabled 
  16. if($epm_enable_validation == 'enabled') { 
  17.  
  18. // first name and last name not filled and name fields are enabled 
  19. if(empty($epm_name) && epm_get_option('display_name_fields')) { 
  20. echo '<div class="epm-message epm-error message error"><p>'.__('Please fill in first name and last name fields.'.$epm_options['display_name_fields'], 'easy-peasy-mailchimp').'</p></div>'; 
  21. // email field is empty and is not an email 
  22. if(empty($epm_email) && !is_email( $epm_email )) { 
  23. echo '<div class="epm-message epm-error message error"><p>'.__('Please add a correct email address.'.$epm_options['display_name_fields'], 'easy-peasy-mailchimp').'</p></div>'; 
  24. // email field is not empty and is not an email 
  25. if(!empty($epm_email) && !is_email( $epm_email )) { 
  26. echo '<div class="epm-message epm-error message error"><p>'.__('The email address seems to be wrong.', 'easy-peasy-mailchimp').'</p></div>'; 
  27.  
  28.  
  29. //show success if enabled and form is correctly filled 
  30. if($epm_enable_success == 'enabled') { 
  31.  
  32. if(epm_get_option('display_name_fields') && !empty($epm_name) && !empty($epm_lastname) && !empty($epm_email) && is_email( $epm_email ) ) { 
  33. echo '<div class="epm-message epm-success message success"><p>'.__('Thank you for signing up to the newsletter.', 'easy-peasy-mailchimp').'</p></div>'; 
  34.  
  35. if(!epm_get_option('display_name_fields') && !empty($epm_email) && is_email( $epm_email )) { 
  36. echo '<div class="epm-message epm-success message success"><p>'.__('Thank you for signing up to the newsletter.', 'easy-peasy-mailchimp').'</p></div>'; 
  37.  
  38.  
  39. //proceed with submission to themailchimpapi 
  40. if(epm_get_option('display_name_fields') && !empty($epm_name) && !empty($epm_lastname) && !empty($epm_email) && is_email( $epm_email ) || !epm_get_option('display_name_fields') && !empty($epm_email) && is_email( $epm_email ) ) { 
  41.  
  42. $MailChimp = new \Drewm\MailChimp( $epm_options['mailchimp_api_key'] ); 
  43. $result = $MailChimp->call('lists/subscribe', array( 
  44. 'id' => $epm_options['mailchimp_list_id'],  
  45. 'email' => array('email'=> $epm_email),  
  46. 'merge_vars' => (epm_get_option('display_name_fields') ? array('FNAME'=>$epm_name, 'LNAME'=>$epm_lastname) : array()),  
  47. 'double_optin' => (epm_get_option('enable_double_optin') ? true : false),  
  48. 'update_existing' => true,  
  49. 'replace_interests' => false,  
  50. 'send_welcome' => (epm_get_option('send_welcome_message') ? true : false),  
  51. )); 
  52.  
  53.  
  54.  
  55. // Return String 
  56. die(); 
  57.