/includes/class-subscribe-request.php

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