MS_Addon_Mailchimp

Add-On controller for: MailChimp.

Defined (1)

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

/app/addon/mailchimp/class-ms-addon-mailchimp.php  
  1. class MS_Addon_Mailchimp extends MS_Addon { 
  2.  
  3. /** 
  4. * The Add-on ID 
  5. * @since 1.0.0 
  6. */ 
  7. const ID = 'mailchimp'; 
  8.  
  9. /** 
  10. * Mailchimp API object 
  11. * @var M2_Mailchimp 
  12. */ 
  13. static protected $mailchimp_api = null; 
  14.  
  15. /** 
  16. * Checks if the current Add-on is enabled 
  17. * @since 1.0.0 
  18. * @return bool 
  19. */ 
  20. static public function is_active() { 
  21. return MS_Model_Addon::is_enabled( self::ID ); 
  22.  
  23. /** 
  24. * Returns the Add-on ID (self::ID). 
  25. * @since 1.0.1.0 
  26. * @return string 
  27. */ 
  28. public function get_id() { 
  29. return self::ID; 
  30.  
  31. /** 
  32. * Initializes the Add-on. Always executed. 
  33. * @since 1.0.0 
  34. */ 
  35. public function init() { 
  36. if ( self::is_active() ) { 
  37. $this->add_filter( 
  38. 'ms_controller_settings_get_tabs',  
  39. 'settings_tabs',  
  40. 10, 2 
  41. ); 
  42.  
  43. $this->add_action( 
  44. 'ms_controller_settings_enqueue_scripts_' . self::ID,  
  45. 'enqueue_scripts' 
  46. ); 
  47.  
  48. $this->add_filter( 
  49. 'ms_view_settings_edit_render_callback',  
  50. 'manage_render_callback',  
  51. 10, 3 
  52. ); 
  53.  
  54. // Watch for REGISTER event: Subscribe user to list. 
  55. $this->add_action( 
  56. 'ms_model_event_'. MS_Model_Event::TYPE_MS_REGISTERED,  
  57. 'subscribe_registered',  
  58. 10, 2 
  59. ); 
  60.  
  61. // Watch for SIGN UP event: Subscribe user to list. 
  62. $this->add_action( 
  63. 'ms_model_event_'. MS_Model_Event::TYPE_MS_SIGNED_UP,  
  64. 'subscribe_members',  
  65. 10, 2 
  66. ); 
  67.  
  68. // Watch for DEACTIVATE event: Subscribe user to list. 
  69. $this->add_action( 
  70. 'ms_model_event_'. MS_Model_Event::TYPE_MS_DEACTIVATED,  
  71. 'subscribe_deactivated',  
  72. 10, 2 
  73. ); 
  74.  
  75. /** 
  76. * Registers the Add-On 
  77. * @since 1.0.0 
  78. * @param array $list The Add-Ons list. 
  79. * @return array The updated Add-Ons list. 
  80. */ 
  81. public function register( $list ) { 
  82. $list[ self::ID ] = (object) array( 
  83. 'name' => __( 'MailChimp Integration', 'membership2' ),  
  84. 'description' => __( 'Enable MailChimp integration.', 'membership2' ),  
  85. 'icon' => 'dashicons dashicons-email',  
  86. ); 
  87.  
  88. return $list; 
  89.  
  90. /** 
  91. * A new user registered (not a Member yet). 
  92. * @since 1.0.0 
  93. * @param mixed $event 
  94. * @param mixed $member 
  95. */ 
  96. public function subscribe_registered( $event, $member ) { 
  97. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_registered' ) ) { 
  98. if ( ! self::is_user_subscribed( $member->email, $list_id ) ) { 
  99. self::subscribe_user( $member, $list_id ); 
  100.  
  101. /** 
  102. * A user subscribed to a membership. 
  103. * @since 1.0.0 
  104. * @param mixed $event 
  105. * @param mixed $member 
  106. */ 
  107. public function subscribe_members( $event, $subscription ) { 
  108. $member = $subscription->get_member(); 
  109.  
  110. /** Verify if is subscribed to registered mail list and remove it. */ 
  111. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_registered' ) ) { 
  112. if ( self::is_user_subscribed( $member->email, $list_id ) ) { 
  113. self::unsubscribe_user( $member->email, $list_id ); 
  114.  
  115. /** Verify if is subscribed to deactivated mail list and remove it. */ 
  116. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_deactivated' ) ) { 
  117. if ( self::is_user_subscribed( $member->email, $list_id ) ) { 
  118. self::unsubscribe_user( $member->email, $list_id ); 
  119.  
  120. /** Subscribe to members mail list. */ 
  121. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_members' ) ) { 
  122. if ( ! self::is_user_subscribed( $member->email, $list_id ) ) { 
  123. self::subscribe_user( $member, $list_id ); 
  124.  
  125. /** 
  126. * A membership was deactivated (e.g. expired or manually cancelled) 
  127. * @since 1.0.0 
  128. * @param mixed $event 
  129. * @param mixed $member 
  130. */ 
  131. public function subscribe_deactivated( $event, $subscription ) { 
  132. $member = $subscription->get_member(); 
  133.  
  134. // Verify if is subscribed to registered mail list and remove it. 
  135. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_registered' ) ) { 
  136. if ( self::is_user_subscribed( $member->email, $list_id ) ) { 
  137. self::unsubscribe_user( $member->email, $list_id ); 
  138.  
  139. // Verify if is subscribed to members mail list and remove it. 
  140. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_members' ) ) { 
  141. if ( self::is_user_subscribed( $member->email, $list_id ) ) { 
  142. self::unsubscribe_user( $member->email, $list_id ); 
  143.  
  144. // Subscribe to deactiveted members mail list. 
  145. if ( $list_id = self::$settings->get_custom_setting( 'mailchimp', 'mail_list_deactivated' ) ) { 
  146. if ( ! self::is_user_subscribed( $member->email, $list_id ) ) { 
  147. self::subscribe_user( $member, $list_id ); 
  148.  
  149. /** 
  150. * Add mailchimp settings tab in settings page. 
  151. * @since 1.0.0 
  152. * @filter ms_controller_membership_get_tabs 
  153. * @param array $tabs The current tabs. 
  154. * @param int $membership_id The membership id to edit 
  155. * @return array The filtered tabs. 
  156. */ 
  157. public function settings_tabs( $tabs ) { 
  158. $tabs[ self::ID ] = array( 
  159. 'title' => __( 'MailChimp', 'membership2' ),  
  160. 'url' => MS_Controller_Plugin::get_admin_url( 
  161. 'settings',  
  162. array( 'tab' => self::ID ) 
  163. ),  
  164. ); 
  165.  
  166. return $tabs; 
  167.  
  168. /** 
  169. * Enqueue admin scripts in the settings screen. 
  170. * @since 1.0.0 
  171. */ 
  172. public function enqueue_scripts() { 
  173. $data = array( 
  174. 'ms_init' => array( 'view_settings_mailchimp' ),  
  175. ); 
  176.  
  177. lib3()->ui->data( 'ms_data', $data ); 
  178. wp_enqueue_script( 'ms-admin' ); 
  179.  
  180. /** 
  181. * Add mailchimp views callback. 
  182. * @since 1.0.0 
  183. * @filter ms_view_settings_edit_render_callback 
  184. * @param array $callback The current function callback. 
  185. * @param string $tab The current membership rule tab. 
  186. * @param array $data The data shared to the view. 
  187. * @return array The filtered callback. 
  188. */ 
  189. public function manage_render_callback( $callback, $tab, $data ) { 
  190. if ( self::ID == $tab ) { 
  191. $view = MS_Factory::load( 'MS_Addon_Mailchimp_View' ); 
  192. $view->data = $data; 
  193. $callback = array( $view, 'render_tab' ); 
  194.  
  195. return $callback; 
  196.  
  197. /** 
  198. * Get mailchimp api lib status. 
  199. * @since 1.0.0 
  200. * @return boolean true on successfully loaded api, false otherwise. 
  201. */ 
  202. public static function get_api_status() { 
  203. $status = false; 
  204.  
  205. try { 
  206. self::load_mailchimp_api(); 
  207. $status = true; 
  208. catch( Exception $e ) { 
  209. MS_Helper_Debug::log( $e ); 
  210.  
  211. return $status; 
  212.  
  213. /** 
  214. * Load the Mailchimp API 
  215. * @since 1.0.0 
  216. * @return M2_Mailchimp Object 
  217. */ 
  218. public static function load_mailchimp_api() { 
  219. if ( empty( self::$mailchimp_api ) ) { 
  220. $options = apply_filters( 
  221. 'ms_addon_mailchimp_load_mailchimp_api_options',  
  222. array( 
  223. 'timeout' => false,  
  224. 'ssl_verifypeer' => false,  
  225. 'ssl_verifyhost' => false,  
  226. 'ssl_cainfo' => false,  
  227. 'debug' => false,  
  228. ); 
  229.  
  230. if ( ! class_exists( 'Mailchimp' ) ) { 
  231. require_once MS_Plugin::instance()->dir . '/lib/mailchimp-api/Mailchimp.php'; 
  232.  
  233. $api = new M2_Mailchimp( 
  234. self::$settings->get_custom_setting( 'mailchimp', 'api_key' ),  
  235. $options 
  236. ); 
  237.  
  238. // Pinging the server 
  239. $ping = $api->helper->ping(); 
  240.  
  241. if ( is_wp_error( $ping ) ) { 
  242. throw new Exception( $ping ); 
  243.  
  244. self::$mailchimp_api = $api; 
  245.  
  246. return self::$mailchimp_api; 
  247.  
  248. /** 
  249. * Get the lists of a Mailchimp account. 
  250. * @return Array Lists info 
  251. */ 
  252. public static function get_mail_lists() { 
  253. static $Mail_lists = null; 
  254.  
  255. if ( null === $Mail_lists ) { 
  256. $Mail_lists = array( 0 => __( 'none', 'membership2' ) ); 
  257. if ( self::get_api_status() ) { 
  258. $page = 0; 
  259. $items_per_page = 25; 
  260. $iterations = 0; 
  261.  
  262. do { 
  263. $lists = self::$mailchimp_api->lists->getList( 
  264. array(),  
  265. $page,  
  266. $items_per_page 
  267. ); 
  268.  
  269. $page += 1; 
  270. $iterations += 1; 
  271.  
  272. if ( is_wp_error( $lists ) ) { 
  273. $has_more = false; 
  274. MS_Helper_Debug::log( $lists ); 
  275. } else { 
  276. $has_more = count( $lists['data'] ) >= $items_per_page; 
  277. foreach ( $lists['data'] as $list ) { 
  278. $Mail_lists[ $list['id'] ] = $list['name']; 
  279.  
  280. // Force to exit the loop after max. 100 API calls (2500 lists). 
  281. if ( $iterations > 100 ) { 
  282. $has_more = false; 
  283. } while ( $has_more ); 
  284.  
  285. return $Mail_lists; 
  286.  
  287. /** 
  288. * Check if a user is subscribed in the list 
  289. * @param string $user_email 
  290. * @param string $list_id 
  291. * @return bool True if the user is subscribed already to the list 
  292. */ 
  293. public static function is_user_subscribed( $user_email, $list_id ) { 
  294. $subscribed = false; 
  295.  
  296. if ( is_email( $user_email ) && self::get_api_status() ) { 
  297. $emails = array( 
  298. array( 'email' => $user_email ),  
  299. ); 
  300.  
  301. $results = self::$mailchimp_api->lists->memberInfo( $list_id, $emails ); 
  302.  
  303. if ( is_wp_error( $results ) ) { 
  304. MS_Helper_Debug::log( $results ); 
  305. } elseif ( ! empty( $results['success_count'] ) 
  306. && ! empty( $results['data'][0]['status'] ) 
  307. && 'subscribed' == $results['data'][0]['status'] 
  308. ) { 
  309. $subscribed = true; 
  310.  
  311. return $subscribed; 
  312.  
  313. /** 
  314. * Subscribe a user to a Mailchimp list 
  315. * @since 1.0.0 
  316. * @param MS_Model_Member $member 
  317. * @param int $list_id 
  318. */ 
  319. public static function subscribe_user( $member, $list_id ) { 
  320. if ( is_email( $member->email ) && self::get_api_status() ) { 
  321. $auto_opt_in = self::$settings->get_custom_setting( 
  322. 'mailchimp',  
  323. 'auto_opt_in' 
  324. ); 
  325. $auto_opt_in = lib3()->is_true( $auto_opt_in ); 
  326.  
  327. $update = apply_filters( 
  328. 'ms_addon_mailchimp_subscribe_user_update',  
  329. true,  
  330. $member,  
  331. $list_id 
  332. ); 
  333.  
  334. $merge_vars = array(); 
  335. $merge_vars['FNAME'] = $member->first_name; 
  336. $merge_vars['LNAME'] = $member->last_name; 
  337.  
  338. if ( $auto_opt_in ) { 
  339. $merge_vars['optin_ip'] = $_SERVER['REMOTE_ADDR']; 
  340. $merge_vars['optin_time'] = MS_Helper_Period::current_time(); 
  341.  
  342. if ( empty( $merge_vars['FNAME'] ) ) { 
  343. unset( $merge_vars['FNAME'] ); 
  344. if ( empty( $merge_vars['LNAME'] ) ) { 
  345. unset( $merge_vars['LNAME'] ); 
  346.  
  347. $merge_vars = apply_filters( 
  348. 'ms_addon_mailchimp_subscribe_user_merge_vars',  
  349. $merge_vars,  
  350. $member,  
  351. $list_id 
  352. ); 
  353.  
  354. $email_field = array( 'email' => $member->email ); 
  355.  
  356. $res = self::$mailchimp_api->lists->subscribe( 
  357. $list_id,  
  358. $email_field,  
  359. $merge_vars,  
  360. 'html',  
  361. ( ! $auto_opt_in ),  
  362. $update 
  363. ); 
  364.  
  365. /** 
  366. * Update a user data in a list 
  367. * @since 1.0.0 
  368. * @param string $user_email 
  369. * @param string $list_id 
  370. * @param array $merge_vars { 
  371. * $FNAME => First name 
  372. * $LNAME => Last Name 
  373. * } 
  374. */ 
  375. public static function update_user( $user_email, $list_id, $merge_vars ) { 
  376. if ( self::get_api_status() ) { 
  377. $merge_vars['update_existing'] = true; 
  378.  
  379. return self::$mailchimp_api->lists->updateMember( 
  380. $list_id,  
  381. array( 'email' => $user_email ),  
  382. $merge_vars 
  383. ); 
  384.  
  385. /** 
  386. * Unsubscribe a user from a list 
  387. * @param string $user_email 
  388. * @param string $list_id 
  389. * @param bool $delete True if the user is gonna be deleted from the list (not only unsubscribed) 
  390. */ 
  391. public static function unsubscribe_user( $user_email, $list_id, $delete = false ) { 
  392. if ( self::get_api_status() ) { 
  393. return self::$mailchimp_api->lists->unsubscribe( 
  394. $list_id,  
  395. array( 'email' => $user_email ),  
  396. $delete 
  397. );