MC4WP_Integration

Class MC4WP_Integration.

Defined (1)

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

/includes/integrations/class-integration.php  
  1. abstract class MC4WP_Integration { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $type = 'integration'; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. protected $checkbox_name = '_mc4wp_subscribe'; 
  12.  
  13. /** 
  14. * @var array 
  15. */ 
  16. protected $options; 
  17.  
  18. /** 
  19. * Constructor 
  20. */ 
  21. public function __construct() { 
  22. $this->checkbox_name = '_mc4wp_subscribe' . '_' . $this->type; 
  23.  
  24. /** 
  25. * Get the checkbox options 
  26. * @return array 
  27. */ 
  28. public function get_options() { 
  29.  
  30. if( $this->options === null ) { 
  31. $this->options = mc4wp_get_options( 'checkbox' ); 
  32.  
  33. return $this->options; 
  34.  
  35. /** 
  36. * Is this a spam request? 
  37. * @return bool 
  38. */ 
  39. protected function is_spam() { 
  40.  
  41. // check if honeypot was filled 
  42. if( $this->is_honeypot_filled() ) { 
  43. return true; 
  44.  
  45. // check user agent 
  46. if( ! isset( $_SERVER['HTTP_USER_AGENT'] ) || strlen( $_SERVER['HTTP_USER_AGENT'] ) < 2 ) { 
  47. return true; 
  48.  
  49. /** 
  50. * @filter `mc4wp_is_spam` 
  51. * @expects boolean True if this is a spam request 
  52. * @default false 
  53. */ 
  54. return apply_filters( 'mc4wp_is_spam', false ); 
  55.  
  56. /** 
  57. * Was the honeypot filled? 
  58. * @return bool 
  59. */ 
  60. protected function is_honeypot_filled() { 
  61.  
  62. // Check if honeypot was filled (by spam bots) 
  63. if( isset( $_POST['_mc4wp_required_but_not_really'] ) && ! empty( $_POST['_mc4wp_required_but_not_really'] ) ) { 
  64. return true; 
  65.  
  66. return false; 
  67.  
  68. /** 
  69. * Should the checkbox be pre-checked? 
  70. * @return bool 
  71. */ 
  72. public function is_prechecked() { 
  73. $opts = $this->get_options(); 
  74. return (bool) $opts['precheck']; 
  75.  
  76. /** 
  77. * Get the text for the label element 
  78. * @return string 
  79. */ 
  80. public function get_label_text() { 
  81.  
  82. $opts = $this->get_options(); 
  83.  
  84. // Get general label text 
  85. $label = $opts['label']; 
  86.  
  87. // Override label text if a specific text for this integration is set 
  88. if ( isset( $opts['text_' . $this->type . '_label'] ) && ! empty( $opts['text_' . $this->type . '_label'] ) ) { 
  89. // custom label text was set 
  90. $label = $opts['text_' . $this->type . '_label']; 
  91.  
  92. // replace label variables 
  93. $label = MC4WP_Tools::replace_variables( $label, array(), array_values( $opts['lists'] ) ); 
  94.  
  95. return $label; 
  96.  
  97. /** 
  98. * @return bool 
  99. */ 
  100. public function checkbox_was_checked() { 
  101. return ( isset( $_POST[ $this->checkbox_name ] ) && $_POST[ $this->checkbox_name ] == 1 ); 
  102.  
  103. /** 
  104. * Outputs a checkbox 
  105. */ 
  106. public function output_checkbox() { 
  107. echo $this->get_checkbox(); 
  108.  
  109. /** 
  110. * @param mixed $args Array or string 
  111. * @return string 
  112. */ 
  113. public function get_checkbox( $args = array() ) { 
  114.  
  115. $checked = ( $this->is_prechecked() ) ? 'checked ' : ''; 
  116.  
  117. // set label text 
  118. if ( isset( $args['labels'][0] ) ) { 
  119. // cf 7 shortcode 
  120. $label = $args['labels'][0]; 
  121. } else { 
  122. $label = $this->get_label_text(); 
  123.  
  124. // CF7 checkbox? 
  125. if( is_array( $args ) && isset( $args['options'] ) ) { 
  126.  
  127. // check for default:0 or default:1 to set the checked attribute 
  128. if( in_array( 'default:1', $args['options'] ) ) { 
  129. $checked = 'checked'; 
  130. } else if( in_array( 'default:0', $args['options'] ) ) { 
  131. $checked = ''; 
  132.  
  133.  
  134. // before checkbox HTML (comment, ...) 
  135. $before = '<!-- MailChimp for WordPress v'. MC4WP_LITE_VERSION .' - https://mc4wp.com/ -->'; 
  136. $before .= apply_filters( 'mc4wp_before_checkbox', '', $this->type ); 
  137.  
  138. // checkbox 
  139. $content = '<p id="mc4wp-checkbox" class="mc4wp-checkbox-' . $this->type .'">'; 
  140. $content .= '<label>'; 
  141. $content .= '<input type="checkbox" name="'. esc_attr( $this->checkbox_name ) .'" value="1" '. $checked . '/> '; 
  142. $content .= $label; 
  143. $content .= '</label>'; 
  144. $content .= '</p>'; 
  145.  
  146. // after checkbox HTML (..., honeypot, closing comment) 
  147. $after = apply_filters( 'mc4wp_after_checkbox', '', $this->type ); 
  148. $after .= '<div style="position: absolute; left:-5000px;"><input type="text" name="_mc4wp_required_but_not_really" value="" tabindex="-1" /></div>'; 
  149. $after .= '<!-- / MailChimp for WordPress -->'; 
  150.  
  151. return $before . $content . $after; 
  152.  
  153. /** 
  154. * @return array 
  155. */ 
  156. protected function get_lists() { 
  157.  
  158. // get checkbox lists options 
  159. $opts = $this->get_options(); 
  160. $lists = $opts['lists']; 
  161.  
  162. // get lists from form, if set. 
  163. if( isset( $_POST['_mc4wp_lists'] ) && ! empty( $_POST['_mc4wp_lists'] ) ) { 
  164.  
  165. $lists = $_POST['_mc4wp_lists']; 
  166.  
  167. // make sure lists is an array 
  168. if( ! is_array( $lists ) ) { 
  169.  
  170. // sanitize value 
  171. $lists = sanitize_text_field( $lists ); 
  172. $lists = array( $lists ); 
  173.  
  174.  
  175. // allow plugins to filter final 
  176. $lists = apply_filters( 'mc4wp_lists', $lists ); 
  177.  
  178. return $lists; 
  179.  
  180. /** 
  181. * Makes a subscription request 
  182. * @param string $email 
  183. * @param array $merge_vars 
  184. * @param int $related_object_ID 
  185. * @return string|boolean 
  186. */ 
  187. protected function subscribe( $email, array $merge_vars = array(), $type = '', $related_object_id = 0 ) { 
  188.  
  189. $type = ( '' !== $type ) ? $type : $this->type; 
  190.  
  191. $api = mc4wp_get_api(); 
  192. $opts = $this->get_options(); 
  193. $lists = $this->get_lists(); 
  194.  
  195. if( empty( $lists) ) { 
  196.  
  197. // show helpful error message to admins, but only if not using ajax 
  198. if( $this->show_error_messages() ) { 
  199. wp_die( 
  200. '<h3>' . __( 'MailChimp for WordPress - Error', 'mailchimp-for-wp' ) . '</h3>' . 
  201. '<p>' . sprintf( __( 'Please select a list to subscribe to in the <a href="%s">checkbox settings</a>.', 'mailchimp-for-wp' ), admin_url( 'admin.php?page=mailchimp-for-wp-checkbox-settings' ) ) . '</p>' . 
  202. '<p style="font-style:italic; font-size:12px;">' . __( 'This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp' ) . '</p>',  
  203. __( 'MailChimp for WordPress - Error', 'mailchimp-for-wp' ),  
  204. array( 'back_link' => true ) 
  205. ); 
  206.  
  207. return 'no_lists_selected'; 
  208.  
  209. $merge_vars = MC4WP_Tools::guess_merge_vars( $merge_vars ); 
  210.  
  211. // set ip address 
  212. if( ! isset( $merge_vars['OPTIN_IP'] ) ) { 
  213. $merge_vars['OPTIN_IP'] = MC4WP_tools::get_client_ip(); 
  214.  
  215. $result = false; 
  216.  
  217. /** 
  218. * @filter `mc4wp_merge_vars` 
  219. * @expects array 
  220. * @param array $merge_vars 
  221. * @param string $type 
  222. * Use this to filter the final merge vars before the request is sent to MailChimp 
  223. */ 
  224. $merge_vars = apply_filters( 'mc4wp_merge_vars', $merge_vars, $type ); 
  225.  
  226. /** 
  227. * @filter `mc4wp_merge_vars` 
  228. * @expects string 
  229. * @param string $email_type 
  230. * Use this to change the email type this users should receive 
  231. */ 
  232. $email_type = apply_filters( 'mc4wp_email_type', 'html' ); 
  233.  
  234. /** 
  235. * @action `mc4wp_before_subscribe` 
  236. * @param string $email 
  237. * @param array $merge_vars 
  238. * Runs before the request is sent to MailChimp 
  239. */ 
  240. do_action( 'mc4wp_before_subscribe', $email, $merge_vars ); 
  241.  
  242. foreach( $lists as $list_id ) { 
  243. $result = $api->subscribe( $list_id, $email, $merge_vars, $email_type, $opts['double_optin'], $opts['update_existing'], true, $opts['send_welcome'] ); 
  244. do_action( 'mc4wp_subscribe', $email, $list_id, $merge_vars, $result, 'checkbox', $type, $related_object_id ); 
  245.  
  246. /** 
  247. * @action `mc4wp_after_subscribe` 
  248. * @param string $email 
  249. * @param array $merge_vars 
  250. * @param boolean $result 
  251. * Runs after the request is sent to MailChimp 
  252. */ 
  253. do_action( 'mc4wp_after_subscribe', $email, $merge_vars, $result ); 
  254.  
  255. // if result failed, show error message (only to admins for non-AJAX) 
  256. if ( $result !== true && $api->has_error() && $this->show_error_messages() ) { 
  257. wp_die( '<h3>' . __( 'MailChimp for WordPress - Error', 'mailchimp-for-wp' ) . '</h3>' . 
  258. '<p>' . __( 'The MailChimp server returned the following error message as a response to our sign-up request:', 'mailchimp-for-wp' ) . '</p>' . 
  259. '<pre>' . $api->get_error_message() . '</pre>' . 
  260. '<p>' . __( 'This is the data that was sent to MailChimp:', 'mailchimp-for-wp' ) . '</p>' . 
  261. '<strong>' . __( 'Email address:', 'mailchimp-for-wp' ) . '</strong>' . 
  262. '<pre>' . esc_html( $email ) . '</pre>' . 
  263. '<strong>' . __( 'Merge variables:', 'mailchimp-for-wp' ) . '</strong>' . 
  264. '<pre>' . esc_html( print_r( $merge_vars, true ) ) . '</pre>' . 
  265. '<p style="font-style:italic; font-size:12px;">' . __( 'This message is only visible to administrators for debugging purposes.', 'mailchimp-for-wp' ) . '</p>',  
  266. __( 'MailChimp for WordPress - Error', 'mailchimp-for-wp' ), array( 'back_link' => true ) ); 
  267.  
  268. return $result; 
  269.  
  270. /** 
  271. * Should we show error messages? 
  272. * - Not for AJAX requests 
  273. * - Not for non-admins 
  274. * - Not for CF7 requests (which uses a different AJAX mechanism) 
  275. * @return bool 
  276. */ 
  277. protected function show_error_messages() { 
  278. return ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) 
  279. && ( ! isset( $_POST['_wpcf7_is_ajax_call'] ) || $_POST['_wpcf7_is_ajax_call'] != 1 ) 
  280. && current_user_can( 'manage_options' );