/app/addon/mailchimp/class-ms-addon-mailchimp.php

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