MC4WP_Field_Mapper

The MailChimp for WordPress Lite MC4WP Field Mapper class.

Defined (1)

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

/includes/class-field-mapper.php  
  1. class MC4WP_Field_Mapper { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. protected $form_data = array(); 
  7.  
  8. /** 
  9. * @var array 
  10. */ 
  11. protected $lists = array(); 
  12.  
  13. /** 
  14. * @var MC4WP_MailChimp 
  15. */ 
  16. protected $mailchimp; 
  17.  
  18. /** 
  19. * @var array|bool 
  20. */ 
  21. protected $list_fields_map; 
  22.  
  23. /** 
  24. * @var array 
  25. */ 
  26. protected $global_fields = array(); 
  27.  
  28. /** 
  29. * @var array 
  30. */ 
  31. protected $mapped_fields = array( 'EMAIL' ); 
  32.  
  33. /** 
  34. * @var array 
  35. */ 
  36. protected $unmapped_fields = array(); 
  37.  
  38. /** 
  39. * @var bool 
  40. */ 
  41. public $success = false; 
  42.  
  43. /** 
  44. * @var string 
  45. */ 
  46. protected $error_code = ''; 
  47.  
  48. /** 
  49. * @param array $form_data 
  50. * @param array $lists 
  51. */ 
  52. public function __construct( array $form_data, array $lists ) { 
  53. $this->form_data = $form_data; 
  54. $this->lists = $lists; 
  55.  
  56. $this->mailchimp = new MC4WP_MailChimp(); 
  57.  
  58. $this->list_fields_map = $this->map_lists_fields(); 
  59.  
  60. // only proceed if successful 
  61. if( $this->list_fields_map ) { 
  62. $this->success = true; 
  63. $this->global_fields = $this->map_global_fields(); 
  64. $this->unmapped_fields = $this->find_unmapped_fields(); 
  65.  
  66. public function get_list_fields_map() { 
  67. return $this->list_fields_map; 
  68.  
  69. public function get_global_fields() { 
  70. return $this->global_fields; 
  71.  
  72. public function get_mapped_fields() { 
  73. return $this->mapped_fields; 
  74.  
  75. public function get_unmapped_fields() { 
  76. return $this->unmapped_fields; 
  77.  
  78. public function get_error_code() { 
  79. return $this->error_code; 
  80.  
  81. /** 
  82. * @return array|bool 
  83. */ 
  84. public function map_lists_fields() { 
  85.  
  86. $map = array(); 
  87.  
  88. // loop through selected lists 
  89. foreach( $this->lists as $list_id ) { 
  90.  
  91. $list = $this->mailchimp->get_list( $list_id, false, true ); 
  92.  
  93. // skip this list if it's unexisting 
  94. if( ! is_object( $list ) || ! isset( $list->merge_vars ) ) { 
  95. continue; 
  96.  
  97. // generate map for this given list 
  98. $list_map = $this->map_data_to_list( $list ); 
  99. if( $list_map === false ) { 
  100. return false; 
  101.  
  102. $map[ $list_id ] = $list_map; 
  103.  
  104. return $map; 
  105.  
  106. /** 
  107. * @return array 
  108. */ 
  109. public function find_unmapped_fields() { 
  110.  
  111. $unmapped_fields = array(); 
  112.  
  113. // is there still unmapped data left? 
  114. $total_fields_mapped = count( $this->mapped_fields ) + count( $this->global_fields ); 
  115.  
  116. if( $total_fields_mapped < count( $this->form_data ) ) { 
  117. foreach( $this->form_data as $field_key => $field_value ) { 
  118.  
  119. if( $this->is_internal_var( $field_key ) ) { 
  120. continue; 
  121.  
  122. if( ! in_array( $field_key, $this->mapped_fields ) ) { 
  123. $unmapped_fields[ $field_key ] = $field_value; 
  124.  
  125. return $unmapped_fields; 
  126.  
  127. /** 
  128. * @return array 
  129. */ 
  130. private function map_global_fields() { 
  131. $global_fields = array(); 
  132.  
  133. // map global fields 
  134. $global_field_names = array( 'MC_LOCATION', 'MC_NOTES', 'MC_LANGUAGE' ); 
  135. foreach( $global_field_names as $field_name ) { 
  136. if( isset( $this->form_data[ $field_name ] ) ) { 
  137. $global_fields[ $field_name ] = $this->form_data[ $field_name ]; 
  138.  
  139. return $global_fields; 
  140.  
  141. /** 
  142. * @param $list 
  143. * @return array 
  144. */ 
  145. private function map_data_to_list( $list ) { 
  146.  
  147. // start with empty list map 
  148. $list_map = array(); 
  149.  
  150. // loop through other list fields 
  151. foreach( $list->merge_vars as $field ) { 
  152.  
  153. // skip EMAIL field 
  154. if( $field->tag === 'EMAIL' ) { 
  155. continue; 
  156.  
  157. // check if field is required 
  158. if( $field->req ) { 
  159. if( ! isset( $this->form_data[ $field->tag ] ) || '' === $this->form_data[ $field->tag ] ) { 
  160. $this->error_code = 'required_field_missing'; 
  161. return false; 
  162.  
  163. // if field is not set, continue. 
  164. if( ! isset( $this->form_data[ $field->tag ] ) ) { 
  165. continue; 
  166.  
  167. // grab field value from data 
  168. $field_value = $this->form_data[ $field->tag ]; 
  169.  
  170. // format field value according to its type 
  171. $field_value = $this->format_field_value( $field_value, $field->field_type ); 
  172.  
  173. // add field value to map 
  174. $list_map[ $field->tag ] = $field_value; 
  175.  
  176. // loop through list groupings if GROUPINGS data was sent 
  177. if( isset( $this->form_data['GROUPINGS'] ) && is_array( $this->form_data['GROUPINGS'] ) && ! empty( $list->interest_groupings ) ) { 
  178.  
  179. $list_map['GROUPINGS'] = array(); 
  180.  
  181. foreach( $list->interest_groupings as $grouping ) { 
  182.  
  183. // check if data for this group was sent 
  184. if( isset( $this->form_data['GROUPINGS'][$grouping->id] ) ) { 
  185. $group_data = $this->form_data['GROUPINGS'][$grouping->id]; 
  186. } elseif( isset( $this->form_data['GROUPINGS'][$grouping->name] ) ) { 
  187. $group_data = $this->form_data['GROUPINGS'][$grouping->name]; 
  188. } else { 
  189. // no data for this grouping was sent, just continue. 
  190. continue; 
  191.  
  192. // format new grouping 
  193. $grouping = array( 
  194. 'id' => $grouping->id,  
  195. 'groups' => $group_data,  
  196. ); 
  197.  
  198. // make sure groups is an array 
  199. if( ! is_array( $grouping['groups'] ) ) { 
  200. $grouping['groups'] = sanitize_text_field( $grouping['groups'] ); 
  201. $grouping['groups'] = explode( ', ', $grouping['groups'] ); 
  202.  
  203. $list_map['GROUPINGS'][] = $grouping; 
  204.  
  205. // unset GROUPINGS if no grouping data was found for this list 
  206. if( 0 === count( $list_map['GROUPINGS'] ) ) { 
  207. unset( $list_map['GROUPINGS'] ); 
  208.  
  209. // add to total map 
  210. return $list_map; 
  211.  
  212. /** 
  213. * Format field value according to its type 
  214. * @param $field_type 
  215. * @param $field_value 
  216. * @return array|string 
  217. */ 
  218. protected function format_field_value( $field_value, $field_type ) { 
  219.  
  220. $field_type = strtolower( $field_type ); 
  221.  
  222. switch( $field_type ) { 
  223.  
  224. // birthday fields need to be MM/DD for the MailChimp API 
  225. case 'birthday': 
  226. $field_value = (string) date( 'm/d', strtotime( $field_value ) ); 
  227. break; 
  228.  
  229. case 'address': 
  230.  
  231. // auto-format if addr1 is not set 
  232. if( ! isset( $field_value['addr1'] ) ) { 
  233.  
  234. // addr1, addr2, city, state, zip, country 
  235. $address_pieces = explode( ', ', $field_value ); 
  236.  
  237. // try to fill it.... this is a long shot 
  238. $field_value = array( 
  239. 'addr1' => $address_pieces[0],  
  240. 'city' => ( isset( $address_pieces[1] ) ) ? $address_pieces[1] : '',  
  241. 'state' => ( isset( $address_pieces[2] ) ) ? $address_pieces[2] : '',  
  242. 'zip' => ( isset( $address_pieces[3] ) ) ? $address_pieces[3] : '' 
  243. ); 
  244.  
  245.  
  246. break; 
  247.  
  248. /** 
  249. * @filter `mc4wp_format_field_value` 
  250. * @param mixed $field_value 
  251. * @param string $field_type 
  252. * @expects mixed 
  253. * Format a field value according to its MailChimp field type 
  254. */ 
  255. $field_value = apply_filters( 'mc4wp_format_field_value', $field_value, $field_type ); 
  256.  
  257. return $field_value; 
  258.  
  259. /** 
  260. * @param $var 
  261. * @return bool 
  262. */ 
  263. protected function is_internal_var( $var ) { 
  264.  
  265. if( $var[0] === '_' ) { 
  266. return true; 
  267.  
  268. // Ignore those fields, we don't need them 
  269. $ignored_vars = array( 'CPTCH_NUMBER', 'CNTCTFRM_CONTACT_ACTION', 'CPTCH_RESULT', 'CPTCH_TIME' ); 
  270. if( in_array( $var, $ignored_vars ) ) { 
  271. return true; 
  272.  
  273. return false; 
  274.