MC4WP_General_Integration

The MailChimp for WordPress Lite MC4WP General Integration class.

Defined (1)

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

/includes/integrations/class-general.php  
  1. class MC4WP_General_Integration extends MC4WP_Integration { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $type = 'general'; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. protected $checkbox_name = 'mc4wp-subscribe'; 
  12.  
  13. /** 
  14. * Constructor 
  15. */ 
  16. public function __construct() { 
  17.  
  18. // run backwards compatibility routine 
  19. $this->upgrade(); 
  20.  
  21. // hook actions 
  22. add_action( 'init', array( $this, 'maybe_subscribe'), 90 ); 
  23.  
  24.  
  25. /** 
  26. * Upgrade routine 
  27. */ 
  28. protected function upgrade() { 
  29. // set new $_POST trigger value 
  30. if( isset( $_POST['mc4wp-try-subscribe'] ) ) { 
  31. $_POST[ $this->checkbox_name ] = 1; 
  32. unset( $_POST['mc4wp-try-subscribe'] ); 
  33.  
  34. if( isset( $_POST['mc4wp-do-subscribe'] ) ) { 
  35. $_POST[ $this->checkbox_name ] = 1; 
  36. unset( $_POST['mc4wp-do-subscribe'] ); 
  37.  
  38. if( isset( $_POST['_mc4wp_subscribe'] ) ) { 
  39. $_POST[ $this->checkbox_name ] = 1; 
  40. unset( $_POST['_mc4wp_subscribe'] ); 
  41.  
  42. /** 
  43. * Maybe fire a general subscription request 
  44. */ 
  45. public function maybe_subscribe() { 
  46.  
  47. if( $this->is_spam() ) { 
  48. return false; 
  49.  
  50. if ( ! $this->checkbox_was_checked() ) { 
  51. return false; 
  52.  
  53. // don't run if this is a CF7 request 
  54. if( isset( $_POST['_wpcf7'] ) ) { 
  55. return false; 
  56.  
  57. // don't run if this is an Events Manager request 
  58. if( isset( $_POST['action'] ) && $_POST['action'] === 'booking_add' && isset( $_POST['event_id'] ) ) { 
  59. return false; 
  60.  
  61. // run! 
  62. return $this->try_subscribe(); 
  63.  
  64. /** 
  65. * Tries to create a sign-up request from the current $_POST data 
  66. */ 
  67. public function try_subscribe() { 
  68.  
  69. // start running.. 
  70. $email = null; 
  71. $merge_vars = array( 
  72. 'GROUPINGS' => array() 
  73. ); 
  74.  
  75. foreach( $_POST as $key => $value ) { 
  76.  
  77. if( $key[0] === '_' || $key === $this->checkbox_name ) { 
  78. continue; 
  79. } elseif( strtolower( substr( $key, 0, 6 ) ) === 'mc4wp-' ) { 
  80. // find extra fields which should be sent to MailChimp 
  81. $key = strtoupper( substr( $key, 6 ) ); 
  82. $value = ( is_scalar( $value ) ) ? sanitize_text_field( $value ) : $value; 
  83.  
  84. switch( $key ) { 
  85. case 'EMAIL': 
  86. $email = $value; 
  87. break; 
  88.  
  89. case 'GROUPINGS': 
  90.  
  91. $groupings = (array) $value; 
  92.  
  93. foreach( $groupings as $grouping_id_or_name => $groups ) { 
  94.  
  95. $grouping = array(); 
  96.  
  97. // group ID or group name given? 
  98. if(is_numeric( $grouping_id_or_name ) ) { 
  99. $grouping['id'] = absint( $grouping_id_or_name ); 
  100. } else { 
  101. $grouping['name'] = sanitize_text_field( stripslashes( $grouping_id_or_name ) ); 
  102.  
  103. // comma separated list should become an array 
  104. if( ! is_array( $groups ) ) { 
  105. $groups = explode( ', ', sanitize_text_field( $groups ) ); 
  106.  
  107. $grouping['groups'] = array_map( 'stripslashes', $groups ); 
  108.  
  109. // add grouping to array 
  110. $merge_vars['GROUPINGS'][] = $grouping; 
  111.  
  112. } // end foreach $groupings 
  113. break; 
  114.  
  115. default: 
  116. if( is_array( $value ) ) { 
  117. $value = sanitize_text_field( implode( ', ', $value ) ); 
  118.  
  119. $merge_vars[$key] = $value; 
  120. break; 
  121.  
  122. } elseif( ! $email && is_string( $value ) && is_email( $value ) ) { 
  123. // if no email is found yet, check if current field value is an email 
  124. $email = $value; 
  125. } elseif( ! $email && is_array( $value ) && isset( $value[0] ) && is_string( $value[0] ) && is_email( $value[0] ) ) { 
  126. // if no email is found yet, check if current value is an array and if first array value is an email 
  127. $email = $value[0]; 
  128. } else { 
  129. $simple_key = str_replace( array( '-', '_' ), '', strtolower( $key ) ); 
  130.  
  131. if( ! $email && in_array( $simple_key, array( 'email', 'emailaddress', 'contactemail' ) ) ) { 
  132. $email = $value; 
  133. } elseif( ! isset( $merge_vars['NAME'] ) && in_array( $simple_key, array( 'name', 'yourname', 'username', 'fullname', 'contactname' ) ) ) { 
  134. // find name field 
  135. $merge_vars['NAME'] = $value; 
  136. } elseif( ! isset( $merge_vars['FNAME'] ) && in_array( $simple_key, array( 'firstname', 'fname', 'givenname', 'forename' ) ) ) { 
  137. // find first name field 
  138. $merge_vars['FNAME'] = $value; 
  139. } elseif( ! isset( $merge_vars['LNAME'] ) && in_array( $simple_key, array( 'lastname', 'lname', 'surname', 'familyname' ) ) ) { 
  140. // find last name field 
  141. $merge_vars['LNAME'] = $value; 
  142.  
  143. // unset groupings if not used 
  144. if( empty( $merge_vars['GROUPINGS'] ) ) { 
  145. unset( $merge_vars['GROUPINGS'] ); 
  146.  
  147. // if email has not been found by the smart field guessing, return false.. Sorry 
  148. if ( ! $email ) { 
  149. return false; 
  150.  
  151. return $this->subscribe( $email, $merge_vars, $this->type ); 
  152.  
  153.