MC4WP_Subscribe_Request

The MailChimp for WordPress Lite MC4WP Subscribe Request class.

Defined (1)

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

/includes/class-subscribe-request.php  
  1. class MC4WP_Subscribe_Request extends MC4WP_Request { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. private $list_fields_map = array(); 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. private $unmapped_fields = array(); 
  12.  
  13. /** 
  14. * @var array 
  15. */ 
  16. private $global_fields = array(); 
  17.  
  18. /** 
  19. * Prepare data for MailChimp API request 
  20. * @return bool 
  21. */ 
  22. public function prepare() { 
  23. $this->guess_fields(); 
  24. $mapped = $this->map_data(); 
  25. return $mapped; 
  26.  
  27. /** 
  28. * Try to guess the values of various fields, if not given. 
  29. */ 
  30. protected function guess_fields() { 
  31. // add some data to the posted data, like FNAME and LNAME 
  32. $this->user_data = MC4WP_Tools::guess_merge_vars( $this->user_data ); 
  33.  
  34. /** 
  35. * Maps the received data to MailChimp lists 
  36. * @return array 
  37. */ 
  38. protected function map_data() { 
  39.  
  40. $mapper = new MC4WP_Field_Mapper( $this->user_data, $this->get_lists() ); 
  41.  
  42. if( $mapper->success ) { 
  43. $this->list_fields_map = $mapper->get_list_fields_map(); 
  44. $this->global_fields = $mapper->get_global_fields(); 
  45. $this->unmapped_fields = $mapper->get_unmapped_fields(); 
  46. } else { 
  47. $this->message_type = $mapper->get_error_code(); 
  48.  
  49. return $mapper->success; 
  50.  
  51. /** 
  52. * @return bool 
  53. */ 
  54. public function process() { 
  55. $api = mc4wp_get_api(); 
  56.  
  57. do_action( 'mc4wp_before_subscribe', $this->user_data['EMAIL'], $this->user_data, 0 ); 
  58.  
  59. $result = false; 
  60. $email_type = $this->get_email_type(); 
  61.  
  62. // loop through selected lists 
  63. foreach ( $this->list_fields_map as $list_id => $list_field_data ) { 
  64.  
  65. // allow plugins to alter merge vars for each individual list 
  66. $list_merge_vars = $this->get_list_merge_vars( $list_id, $list_field_data ); 
  67.  
  68. // send a subscribe request to MailChimp for each list 
  69. $result = $api->subscribe( $list_id, $this->user_data['EMAIL'], $list_merge_vars, $email_type, $this->form->settings['double_optin'], $this->form->settings['update_existing'], $this->form->settings['replace_interests'], $this->form->settings['send_welcome'] ); 
  70. do_action( 'mc4wp_subscribe', $this->user_data['EMAIL'], $list_id, $list_merge_vars, $result, 'form', 'form', 0 ); 
  71.  
  72. do_action( 'mc4wp_after_subscribe', $this->user_data['EMAIL'], $this->user_data, 0, $result ); 
  73.  
  74. // did we succeed in subscribing with the parsed data? 
  75. if( ! $result ) { 
  76. // don't grab mailchimp error if status code is "already_subscribed" 
  77. if( $api->get_error_code() === 214 ) { 
  78. $this->message_type = 'already_subscribed'; 
  79. } else { 
  80. $this->message_type = 'error'; 
  81. $this->mailchimp_error = $api->get_error_message(); 
  82. } else { 
  83. $this->message_type = 'subscribed'; 
  84.  
  85. // store user email in a cookie 
  86. MC4WP_Tools::remember_email( $this->user_data['EMAIL'] ); 
  87.  
  88. $this->success = $result; 
  89.  
  90. return $result; 
  91.  
  92.  
  93. /** 
  94. * Adds global fields like OPTIN_IP, MC_LANGUAGE, OPTIN_DATE, etc to the list of user-submitted field data. 
  95. * @param string $list_id 
  96. * @param array $list_field_data 
  97. * @return array 
  98. */ 
  99. protected function get_list_merge_vars( $list_id, $list_field_data ) { 
  100.  
  101. $merge_vars = array(); 
  102.  
  103. // add OPTIN_IP, we do this here as the user shouldn't be allowed to set this 
  104. $merge_vars['OPTIN_IP'] = MC4WP_tools::get_client_ip(); 
  105.  
  106. // make sure MC_LANGUAGE matches the requested format. Useful when getting the language from WPML etc. 
  107. if( isset( $this->global_fields['MC_LANGUAGE'] ) ) { 
  108. $merge_vars['MC_LANGUAGE'] = strtolower( substr( $this->global_fields['MC_LANGUAGE'], 0, 2 ) ); 
  109.  
  110. $merge_vars = array_merge( $merge_vars, $list_field_data ); 
  111.  
  112. /** 
  113. * @filter `mc4wp_merge_vars` 
  114. * @expects array 
  115. * @param int $form_id 
  116. * @param string $list_id 
  117. * Can be used to filter the merge variables sent to a given list 
  118. */ 
  119. $merge_vars = apply_filters( 'mc4wp_merge_vars', $merge_vars, 0, $list_id ); 
  120.  
  121. return (array) $merge_vars; 
  122.  
  123.  
  124. /** 
  125. * Gets the email_type 
  126. * @return string The email type to use for subscription coming from this form 
  127. */ 
  128. protected function get_email_type( ) { 
  129.  
  130. $email_type = 'html'; 
  131.  
  132. // get email type from form 
  133. if( isset( $this->user_data['_MC4WP_EMAIL_TYPE'] ) ) { 
  134. $email_type = sanitize_text_field( $this->user_data['_MC4WP_EMAIL_TYPE'] ); 
  135.  
  136. // allow plugins to override this email type 
  137. $email_type = apply_filters( 'mc4wp_email_type', $email_type ); 
  138.  
  139. return (string) $email_type; 
  140.