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

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