* or write to the Free Software Foundation, Inc., 51 Franklin St, * Fifth Floor, Boston, MA 02110-1301 USA **/ // no direct access defined( 'ABSPATH' ) or die( 'Restricted Access' ); defined( 'DS' ) or define('DS', DIRECTORY_SEPARATOR); if( ! is_admin() ){ return; } class chimpexpress { public $_settings; private $_errors = array(); private $_notices = array(); static $instance = false; private $_optionsName = 'chimpexpress'; private $_optionsGroup = 'chimpexpress-options'; private $_url = "https://us1.api.mailchimp.com/1.3/"; private $_listener_query_var = 'chimpexpressListener'; private $_timeout = 30; private $MCAPI = false; // public $_api = false; function __construct() { require_once( WP_PLUGIN_DIR . DS . 'chimpexpress' . DS . 'class-MCAPI.php' ); if( ! $this->MCAPI ){ $this->MCAPI = new chimpexpressMCAPI; if( ! isset( $_SESSION['MCping'] ) || ! $_SESSION['MCping'] ){ $ping = $this->MCAPI->ping(); $_SESSION['MCping'] = $ping; if($ping){ $MCname = $this->MCAPI->getAccountDetails(); $_SESSION['MCusername'] = $MCname['username']; } } } $this->_getSettings(); // Get the datacenter from the API key $datacenter = substr( strrchr($this->_settings['apikey'], '-'), 1 ); if ( empty( $datacenter ) ) { $datacenter = "us1"; } // Put the datacenter and version into the url $this->_url = "https://{$datacenter}.api.mailchimp.com/{$this->_settings['version']}/"; // include cache library require_once( WP_PLUGIN_DIR . DS . 'chimpexpress' . DS . 'class-JG_Cache.php' ); // include WP filesystem if ( ! function_exists( 'WP_Filesystem' ) ){ require( ABSPATH .DS. 'wp-admin' .DS. 'includes' .DS. 'file.php' ); WP_Filesystem(); } global $wp_filesystem; /** * Add filters and actions */ add_filter( 'init', array( $this, 'chimpexpressLoadLanguage') ); add_action( 'admin_init', array($this, 'registerOptions') ); add_action( 'admin_menu', array($this, 'adminMenu') ); add_action( 'admin_menu', array($this, 'chimpexpress_add_box') ); add_action( 'template_redirect', array( $this, 'listener' )); // add_filter( 'query_vars', array( $this, 'addMailChimpListenerVar' )); register_activation_hook( __FILE__, array( $this, 'activatePlugin' ) ); add_filter( 'pre_update_option_' . $this->_optionsName, array( $this, 'optionUpdate' ), null, 2 ); // add_action( 'admin_notices', array($this->MCAPI, 'showMessages') ); // compose ajax callbacks add_action('wp_ajax_compose_clear_cache', array($this,'compose_clear_cache_callback')); add_action('wp_ajax_compose_gotoStep', array($this,'compose_gotoStep_callback')); add_action('wp_ajax_compose_removeDraft', array($this,'compose_removeDraft_callback')); add_action('wp_ajax_sanitize', array($this,'sanitize_callback')); // import ajax callbacks add_action('wp_ajax_import', array($this,'import_callback')); // archive ajax callbacks add_action('wp_ajax_archive_deleteLP', array($this,'archive_deleteLP_callback')); // archive post box callback add_action('wp_ajax_load_campaigns', array($this,'load_campaigns_callback')); add_action('wp_ajax_ftp_find_root', array($this,'ftp_find_root')); add_action('wp_ajax_ftp_test', array($this,'ftp_test_callback')); } public function optionUpdate($newvalue, $oldvalue) { if ( !empty( $_POST['get-apikey'] ) ) { unset( $_POST['get-apikey'] ); // If the user set their username at the same time as they requested an API key or changes the username if ( empty($this->_settings['username']) || $oldvalue['username'] != $newvalue['username'] ) { $this->_settings['username'] = $newvalue['username']; $this->_updateSettings(); } // If the user set their password at the same time as they requested an API key or changed the password if ( empty($this->_settings['password']) || $oldvalue['password'] != $newvalue['password'] ) { $this->_settings['password'] = $newvalue['password']; $this->_updateSettings(); } $this->_getSettings(); // Get API keys, if one doesn't exist, the login will create one $keys = $this->MCAPI->apikeys(); // var_dump($keys);die; // Set the API key if ( is_array($keys) && !empty( $keys ) && !is_wp_error($keys) ) { $newvalue['apikey'] = $keys[0]['apikey']; $this->MCAPI->_addNotice( __('API Key saved', 'chimpexpress').": {$newvalue['apikey']}"); } } elseif ( !empty( $_POST['expire-apikey'] ) ) { unset( $_POST['expire-apikey'] ); // If the user set their username at the same time as they requested to expire the API key if ( empty($this->_settings['username']) ) { $this->_settings['username'] = $newvalue['username']; } // If the user set their password at the same time as they requested to expire the API key if ( empty($this->_settings['password']) ) { $this->_settings['password'] = $newvalue['password']; } // Get API keys, if one doesn't exist, the login will create one $expired = $this->MCAPI->apikeyExpire( $this->_settings['username'], $this->_settings['password'] ); // Empty the API key and add a notice if ( empty($expired['error']) ) { $newvalue['apikey'] = ''; $this->MCAPI->_addNotice( __('API Key expired', 'chimpexpress').": {$oldvalue['apikey']}"); } } /* elseif ( !empty( $_POST['regenerate-security-key']) ) { unset( $_POST['expire-apikey'] ); $newvalue['listener_security_key'] = $this->_generateSecurityKey(); $this->MCAPI->_addNotice("New Security Key: {$newvalue['listener_security_key']}"); } */ // clear error messages $this->MCAPI->_emptyErrors(); $this->MCAPI->_emptyNotices(); // clear cache if present $cacheDir = 'wp-content' .DS. 'plugins' .DS. 'chimpexpress' .DS. 'cache'; if( is_dir( ABSPATH . $cacheDir ) ){ $cache = new chimpexpressJG_Cache( $cacheDir ); $templates = $cache->get('templates'); if ( $templates ){ $this->compose_clear_cache_callback(); } } return $newvalue; } public function activatePlugin() { $this->_updateSettings(); } public function getSetting( $settingName, $default = false ) { if ( empty( $this->_settings ) ) { $this->_getSettings(); } if ( isset( $this->_settings[$settingName] ) ) { return $this->_settings[$settingName]; } else { return $default; } } public function _getSettings() { if (empty($this->_settings)) { $this->_settings = get_option( $this->_optionsName ); } if ( !is_array( $this->_settings ) ) { $this->_settings = array(); } $defaults = array( 'username' => '', 'password' => '', 'apikey' => '', 'CEaccess' => 'manage_options', 'debugging' => 'off', 'debugging_email' => '', 'listener_security_key' => $this->_generateSecurityKey(), 'version' => '1.3', 'GAprofile' => '', 'ftpHost' => '', 'ftpUser' => '', 'ftpPasswd' => '', 'ftpPath' => '' ); $this->_settings = wp_parse_args($this->_settings, $defaults); } private function _generateSecurityKey() { return sha1(time()); } private function _updateSettings() { update_option( $this->_optionsName, $this->_settings ); } public function registerOptions() { register_setting( $this->_optionsGroup, $this->_optionsName ); } public function adminMenu() { $pages = array(); $pages[] = add_menu_page( __('Dashboard', 'chimpexpress'), 'ChimpExpress', $this->_settings['CEaccess'], 'ChimpExpressDashboard', array($this, 'main'), plugins_url( 'images' . DS . 'logo_16.png', __FILE__ ) ); $pages[] = add_submenu_page( 'ChimpExpressDashboard', __('Import', 'chimpexpress'), __('Import', 'chimpexpress'), $this->_settings['CEaccess'], 'ChimpExpressImport', array($this, 'import'), '' ); $pages[] = add_submenu_page( 'ChimpExpressDashboard', __('Compose', 'chimpexpress'), __('Compose', 'chimpexpress'), $this->_settings['CEaccess'], 'ChimpExpressCompose', array($this, 'compose'), '' ); $pages[] = add_submenu_page( 'ChimpExpressDashboard', __('Landing Page Archive', 'chimpexpress'), __('Landing Pages', 'chimpexpress'), $this->_settings['CEaccess'], 'ChimpExpressArchive', array($this, 'archive'), '' ); // invisible menus $pages[] = add_submenu_page( 'ChimpExpressArchive', __('Edit Landing Page', 'chimpexpress'), __('Edit Landing Page', 'chimpexpress'), $this->_settings['CEaccess'], 'ChimpExpressEditLandingPage', array($this, 'editLP'), '' ); $pages[] = add_options_page( __('Settings', 'chimpexpress'), 'ChimpExpress', 'manage_options', 'ChimpExpressConfig', array($this, 'options') ); // enqueue css and js files foreach( $pages as $page ){ add_action('admin_print_styles-' . $page, array($this, 'chimpexpressAddAdminHead') ); } } function chimpexpressAddAdminHead() { // add css files wp_enqueue_style( 'chimpexpress', plugins_url( 'css' . DS . 'chimpexpress.css', __FILE__ ) ); wp_enqueue_style( 'colorbox', plugins_url( 'css' . DS . 'colorbox.css', __FILE__ ) ); // add js files wp_enqueue_script( 'chimpexpress', plugins_url( 'js' . DS . 'jquery.colorbox-min.js', __FILE__ ) ); } function compose_clear_cache_callback() { global $wp_filesystem; if( $wp_filesystem->method == 'direct' ){ $wp_filesystem->delete( ABSPATH . 'wp-content' .DS. 'plugins' .DS. 'chimpexpress' .DS. 'cache', true); } else { $ftpstream = @ftp_connect( $this->_settings['ftpHost'] ); $login = @ftp_login($ftpstream, $this->_settings['ftpUser'], $this->_settings['ftpPasswd']); @ftp_chdir($ftpstream, $this->_settings['ftpPath'] ); $dir = 'wp-content' .DS. 'plugins' .DS. 'chimpexpress' .DS. 'cache'; $this->ftp_delAll($ftpstream, $dir); @ftp_close($ftpstream); } return; } function compose_gotoStep_callback() { include( WP_PLUGIN_DIR . DS . 'chimpexpress' . DS . 'compose.php' ); exit; } function compose_removeDraft_callback() { $cid = $_POST['cid']; $this->MCAPI->campaignDelete($cid); if ( is_dir( WP_PLUGIN_DIR . DS . 'chimpexpress' .DS. 'tmp' ) ){ global $wp_filesystem; if( $wp_filesystem->method == 'direct' ){ $wp_filesystem->delete( WP_PLUGIN_DIR . DS . 'chimpexpress' .DS. 'tmp', true); } else { $ftpstream = @ftp_connect( $this->_settings['ftpHost'] ); $login = @ftp_login($ftpstream, $this->_settings['ftpUser'], $this->_settings['ftpPasswd']); @ftp_chdir($ftpstream, $this->_settings['ftpPath'] ); $this->ftp_delAll($ftpstream, 'wp-content' .DS. 'plugins' .DS. 'chimpexpress' .DS. 'tmp' ); @ftp_close($ftpstream); } } exit; } function sanitize_callback() { $string = explode('*|@|*', $_POST['string'] ); foreach( $string as $s ){ if( sanitize_title( $s ) == '' ){ echo ''; exit; } } echo 1; exit; } function import_callback() { global $wpdb, $current_user; $type = $_POST['type']; $cid = $_POST['cid']; $subject = html_entity_decode( $_POST['subject'] ); $fileName = html_entity_decode( $_POST['fileName'] ); // get next post/page id $table_status = $wpdb->get_results( $wpdb->prepare("SHOW TABLE STATUS LIKE '$wpdb->posts'") ); $next_increment = $table_status[0]->Auto_increment; if($type=='post'){ // create permalink $guid = get_option('home') . '/?p=' . $next_increment; // var_dump($campaignContent['html']);die; if( $_POST['datatype'] == 'html' ){ // get campaign contents $campaignContent = $this->MCAPI->campaignContent( $cid, false ); // process html contents $html = $campaignContent['html']; preg_match('##is', $html, $styles); $html = preg_replace( '/]*?>/is', '', $html ); $html = preg_replace( '!
(.*)!is', '', $html ); $html = preg_replace( '!]*>!is', '', $html); $html = preg_replace( '!!is', '', $html); $html = str_replace( array('',''), '', $html ); $style = ''; for($i=0;$i'.__('Choose a campaign to copy content into your post', 'chimpexpress').':
'; echo '
';
echo ''.__('more', 'chimpexpress').'';
echo '