Advertisement
Guest User

MailChimp Integration & Ultimate Facebook Compatability Hack

a guest
Feb 18th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.74 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: MailChimp Sync
  4. Plugin URI: http://premium.wpmudev.org/project/mailchimp-newsletter-integration
  5. Description: Simply integrate MailChimp with your Multisite (or regular old single user WP) site - automatically add new users to your email lists and import all your existing users
  6. Author: WPMU DEV
  7. Version: 1.4
  8. Author URI: http://premium.wpmudev.org
  9. Network: true
  10. WDP ID: 73
  11. */
  12.  
  13. /*
  14. Copyright 2007-2013 Incsub (http://incsub.com)
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
  18. the Free Software Foundation.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; if not, write to the Free Software
  27. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  28. */
  29.  
  30. //------------------------------------------------------------------------//
  31. //---Hook-----------------------------------------------------------------//
  32. //------------------------------------------------------------------------//
  33.  
  34. class WPMUDEV_MailChimp_Sync {
  35.     public function __construct() {
  36.         $this->set_globals();
  37.         $this->includes();
  38.  
  39.         add_action( 'plugins_loaded', array( $this, 'mailchimp_localization' ) );
  40.  
  41.         add_action( 'init', array( $this, 'init' ), 1 );
  42.  
  43.         add_action( 'wpmu_new_user', array( $this, 'mailchimp_add_user' ) );
  44.         add_action( 'user_register', array( $this, 'mailchimp_add_user' ) );
  45.         add_action( 'make_ham_user', array( $this, 'mailchimp_add_user' ) );
  46.         add_action( 'profile_update', array( $this, 'mailchimp_edit_user' ) );
  47.         add_action( 'xprofile_updated_profile', array( $this, 'mailchimp_edit_user' ) ); //for buddypress
  48.  
  49.         add_action( 'make_spam_blog', array( $this, 'mailchimp_blog_users_remove' ) );
  50.         add_action( 'make_spam_user', array( $this, 'mailchimp_user_remove' ) );
  51.         add_action( 'delete_user', array( $this, 'mailchimp_user_remove' ) );
  52.         add_action( 'bp_core_action_set_spammer_status', array( $this, 'mailchimp_bp_spamming' ) , 10, 2); //for buddypress
  53.  
  54.         add_action( 'widgets_init', array( $this, 'mailchimp_widget_init' )  );
  55.     }
  56.  
  57.     private function set_globals() {
  58.         define( 'MAILCHIMP_MAX_LOG_LINES', 100 );
  59.         define( 'MAILCHIMP_LANG_DOMAIN', 'mailchimp' );
  60.         define( 'MAILCHIMP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
  61.         define( 'MAILCHIMP_ASSETS_URL', MAILCHIMP_PLUGIN_URL . 'assets/' );
  62.     }
  63.  
  64.     private function includes() {
  65.         require_once( 'helpers.php' );
  66.         require_once( 'integration.php' );
  67.  
  68.         // WPMUDEV Dashboard class
  69.         if ( is_admin() ) {
  70.             global $wpmudev_notices;
  71.             $wpmudev_notices[] = array(
  72.                 'id'=> 73,
  73.                 'name'=> 'MailChimp Sync',
  74.                 'screens' => array(
  75.                     'settings_page_mailchimp-network'
  76.                 )
  77.             );
  78.             include_once( 'externals/wpmudev-dash-notification.php' );
  79.         }
  80.  
  81.  
  82.     }
  83.  
  84.     public function init() {
  85.         if ( is_admin() ) {
  86.             require_once( 'admin_page.php' );
  87.             new WPMUDEV_MailChimp_Admin();
  88.         }
  89.  
  90.         if ( ! is_multisite() || ( is_multisite() && get_site_option( 'mailchimp_allow_shortcode', false ) ) ) {
  91.             require_once( 'shortcode.php' );
  92.             new WPMUDEV_MailChimp_Shortcode();
  93.         }
  94.     }
  95.  
  96.     function mailchimp_localization() {
  97.       // Load up the localization file if we're using WordPress in a different language
  98.         // Place it in this plugin's "languages" folder and name it "mailchimp-[value in wp-config].mo"
  99.       load_plugin_textdomain( 'mailchimp', false, '/mailchimp-sync/languages/' );
  100.     }
  101.  
  102.  
  103.  
  104.     function mailchimp_widget_init() {
  105.  
  106.         if ( ! is_multisite() || ( is_multisite() && get_site_option( 'mailchimp_allow_widget', false ) ) ) {
  107.             require_once( 'widget.php' );
  108.             register_widget( 'Incsub_Mailchimp_Widget' );
  109.         }
  110.     }
  111.  
  112.  
  113.     function mailchimp_add_user($uid) {
  114.  
  115.         if ( is_integer( $uid ) ) {
  116.             $user = get_userdata( $uid );
  117.         }
  118.         elseif ( is_array( $uid ) ) {
  119.             $user = new stdClass;
  120.             $user->spam = false;
  121.             $user->deleted = false;
  122.             $user->wdfb = (object) $uid['wdfb'];
  123.             $user->user_email = $uid['email'];
  124.             $user->user_firstname = $uid['first_name'];
  125.             $user->user_lastname = $uid['last_name'];
  126.         }
  127.         else {
  128.             return false;
  129.         }
  130.  
  131.         //check for spam
  132.         if ( $user->spam || $user->deleted )
  133.             return false;
  134.  
  135.         //remove + sign emails
  136.         if ( get_site_option('mailchimp_ignore_plus') == 'yes' && strstr($user->user_email, '+') ) {
  137.             return false;
  138.         }
  139.  
  140.         $mailchimp_mailing_list = get_site_option('mailchimp_mailing_list');
  141.         $mailchimp_auto_opt_in = get_site_option('mailchimp_auto_opt_in');
  142.         $api = mailchimp_load_API();
  143.  
  144.         $unsubscribed_list = $this->mailchimp_get_unsubscribed_users( $api, $mailchimp_mailing_list );
  145.  
  146.         if ( in_array( $user->user_email, $unsubscribed_list ) )
  147.             return false;
  148.  
  149.         if ( $mailchimp_auto_opt_in == 'yes' ) {
  150.             $merge_vars = array( 'OPTINIP' => $_SERVER['REMOTE_ADDR'], 'FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname );
  151.             $double_optin = false;
  152.         } else {
  153.             $merge_vars = array( 'FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname );
  154.             $double_optin = true;
  155.         }
  156.         $merge_vars = apply_filters( 'mailchimp_merge_vars', $merge_vars, $user );
  157.  
  158.         $mailchimp_subscribe = $api->listSubscribe( $mailchimp_mailing_list, $user->user_email, $merge_vars, '', $double_optin );
  159.  
  160.         if ( ! $mailchimp_subscribe )
  161.             $this->mailchimp_log_errors( $this->mailchimp_extract_api_errors( $api, $user->user_email ) );
  162.  
  163.         if (($api->errorCode) && ($api->errorCode != 214)) {
  164.             $error = "MailChimp listSubscribe() Error: " . $api->errorCode . " - " . $api->errorMessage;
  165.             trigger_error($error, E_USER_WARNING);
  166.         }
  167.     }
  168.  
  169.  
  170.  
  171.     function mailchimp_edit_user($uid) {
  172.  
  173.         $user = get_userdata( $uid );
  174.  
  175.         //check for spam
  176.         if ( $user->spam || $user->deleted )
  177.             return false;
  178.  
  179.         $mailchimp_mailing_list = get_site_option('mailchimp_mailing_list');
  180.         $mailchimp_auto_opt_in = get_site_option('mailchimp_auto_opt_in');
  181.         $api = mailchimp_load_API();
  182.  
  183.         $unsubscribed_list = $this->mailchimp_get_unsubscribed_users( $api, $mailchimp_mailing_list );
  184.  
  185.         if ( in_array( $user->user_email, $unsubscribed_list ) )
  186.             return false;
  187.  
  188.         $merge_vars = array( 'FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname );
  189.  
  190.         $merge_vars = apply_filters('mailchimp_merge_vars', $merge_vars, $user);
  191.         $mailchimp_update = $api->listUpdateMember($mailchimp_mailing_list, $user->user_email, $merge_vars);
  192.  
  193.         if ( ! $mailchimp_update )
  194.             $this->mailchimp_log_errors( $this->mailchimp_extract_api_errors( $api, $user->user_email ) );
  195.  
  196.     }
  197.  
  198.     function mailchimp_user_remove($uid) {
  199.  
  200.         $user = get_userdata( $uid );
  201.  
  202.         $mailchimp_mailing_list = get_site_option('mailchimp_mailing_list');
  203.         $api = mailchimp_load_API();
  204.         $mailchimp_unsubscribe = $api->listUnsubscribe($mailchimp_mailing_list, $user->user_email, true, false);
  205.         if ( ! $mailchimp_unsubscribe )
  206.             $this->mailchimp_log_errors( $this->mailchimp_extract_api_errors( $api, $user->user_email ) );
  207.     }
  208.  
  209.     function mailchimp_blog_users_remove( $blog_id ) {
  210.       $mailchimp_mailing_list = get_site_option('mailchimp_mailing_list');
  211.       $api = mailchimp_load_API();
  212.  
  213.       $emails = array();
  214.       $blogusers = get_users_of_blog( $blog_id );
  215.       if ($blogusers) {
  216.         foreach ($blogusers as $bloguser) {
  217.           $emails[] = $bloguser->user_email;
  218.         }
  219.       }
  220.  
  221.         $mailchimp_unsubscribe = $api->listBatchUnsubscribe($mailchimp_mailing_list, $emails, true, false);
  222.         if ( $mailchimp_unsubscribe['error_count'] )
  223.             $this->mailchimp_log_errors( $mailchimp_unsubscribe['errors'] );
  224.     }
  225.  
  226.     function mailchimp_bp_spamming( $user_id, $is_spam ) {
  227.       if ($is_spam)
  228.         $this->mailchimp_user_remove( $user_id );
  229.       else
  230.         $this->mailchimp_add_user( $user_id );
  231.     }
  232.  
  233.     function mailchimp_get_unsubscribed_users( $api, $mailchimp_import_mailing_list ) {
  234.         $unsubscribed_list = array();
  235.         $tmp_unsubscribed_list = $api->listMembers( $mailchimp_import_mailing_list, 'unsubscribed' );
  236.         if ( $tmp_unsubscribed_list['total'] > 0 ) {
  237.             foreach ( $tmp_unsubscribed_list['data'] as $unsubscribed ) {
  238.                 $unsubscribed_list[] = $unsubscribed['email'];
  239.             }
  240.         }
  241.         return $unsubscribed_list;
  242.     }
  243.     //------------------------------------------------------------------------//
  244.     //---Page Output Functions------------------------------------------------//
  245.     //------------------------------------------------------------------------//
  246.  
  247.  
  248.  
  249.  
  250.     function mailchimp_extract_api_errors( $api, $email ) {
  251.         return array(
  252.             array(
  253.                 'code' => $api->errorCode,
  254.                 'message' => $api->errorMessage,
  255.                 'email' => $email
  256.             )
  257.         );
  258.     }
  259.  
  260.     /**
  261.      * Log MailChimp errors
  262.      * @param Array $errors
  263.      * @return type
  264.      */
  265.     function mailchimp_log_errors( $errors ) {
  266.  
  267.         $current_log = get_site_option( 'mailchimp_error_log' );
  268.         $new_log = array();
  269.  
  270.  
  271.         foreach ( $errors as $error ) {
  272.  
  273.             $code = isset( $error['code'] ) ? $error['code'] : 0;
  274.             $message = isset( $error['message'] ) ? $error['message'] : '';
  275.             $email = isset( $error['email'] ) ? $error['email'] : '';
  276.             $date = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), time() );
  277.  
  278.             $new_log[] = compact( 'code', 'message', 'email', 'date' );
  279.  
  280.         }
  281.  
  282.  
  283.         if ( $current_log ) {
  284.  
  285.             $new_log = array_merge( $current_log, $new_log );
  286.  
  287.             // We'll only saved the last X lines of the log
  288.             $count = count( $new_log );
  289.             if ( $count > MAILCHIMP_MAX_LOG_LINES ) {
  290.                 $new_log = array_slice( $new_log, $count - $offset - 1 );
  291.             }
  292.  
  293.         }
  294.  
  295.         update_site_option( 'mailchimp_error_log', $new_log );
  296.  
  297.     }
  298. }
  299.  
  300. global $mailchimp_sync;
  301. $mailchimp_sync = new WPMUDEV_MailChimp_Sync();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement