mailin
Last commit date
css
13 years ago
js
13 years ago
lang
13 years ago
api_form.php
13 years ago
compatibility.php
13 years ago
cron.php
13 years ago
listings.php
13 years ago
mailin.php
13 years ago
mailin_widget.php
13 years ago
mailinapi.class.php
13 years ago
readme.txt
13 years ago
mailin.php
441 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | Plugin Name: Mailin |
| 5 | Plugin URI: http://mailin.fr |
| 6 | Description: The Mailin plugin provides quick and easy way to synchronize subscribers from wordpress site to Mailin website account and vice versa. |
| 7 | Version: 1.0.0 |
| 8 | Author: Mailin.fr |
| 9 | Author URI: http://www.mailin.fr |
| 10 | License: GPLv2 or later |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License |
| 16 | as published by the Free Software Foundation; either version 2 |
| 17 | of the License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | */ |
| 28 | |
| 29 | //Mailin version |
| 30 | define('MAILIN_VER', '1.0.0'); |
| 31 | |
| 32 | //Initiallize mailin constants |
| 33 | Initiallize_mailin_constants(); |
| 34 | |
| 35 | |
| 36 | // Get Mailin API class in application domain |
| 37 | if (!class_exists('mailin_API')) |
| 38 | { |
| 39 | require_once('mailinapi.class.php'); |
| 40 | } |
| 41 | |
| 42 | // Include Compatibility functions |
| 43 | require_once('compatibility.php'); |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Initillize mailin plugin |
| 48 | * @return void |
| 49 | */ |
| 50 | function mailin_init() |
| 51 | { |
| 52 | |
| 53 | mailin_load_resources(); |
| 54 | |
| 55 | //Internationalize the plugin |
| 56 | $i18n_file_name = 'mailin_lang'; |
| 57 | $locale = apply_filters( 'plugin_locale', get_locale(), $i18n_file_name); |
| 58 | |
| 59 | $filename = MAILIN_LANG_DIR.$i18n_file_name.'-'.$locale.'.mo'; |
| 60 | load_textdomain('mailin_i18n', $filename); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | add_action( 'init', 'mailin_init'); |
| 65 | |
| 66 | |
| 67 | //Initilize Widget |
| 68 | include_once('mailin_widget.php'); |
| 69 | |
| 70 | /** |
| 71 | * Loads JS and CSS |
| 72 | * @return void |
| 73 | */ |
| 74 | function mailin_load_resources() |
| 75 | { |
| 76 | wp_enqueue_style('mailin_wp_css', MAILIN_URL.'css/mailin_plugin.css'); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Loads Mailin Admin page css |
| 83 | * @return void |
| 84 | */ |
| 85 | function mailin_load_resources_admin() |
| 86 | { |
| 87 | wp_enqueue_style('mailin_admin_css', MAILIN_URL.'css/admin.css'); |
| 88 | } |
| 89 | |
| 90 | add_action('load-settings_page_mailin_options', 'mailin_load_resources_admin'); |
| 91 | |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Update lists and campaigns on refreshing a page |
| 96 | * |
| 97 | */ |
| 98 | |
| 99 | $mObj = new mailin_API(); |
| 100 | $api_key = get_option('mailin_apikey'); |
| 101 | |
| 102 | |
| 103 | if(is_admin() && isset($_GET['page']) && $_GET['page'] == 'mailin_options' && $api_key != '' && $_SERVER['REQUEST_METHOD'] == 'GET'){ |
| 104 | |
| 105 | $mObj->updateUserLists($api_key); |
| 106 | $mObj->updateUserCampaigns($api_key); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Gets or sets error/success message |
| 112 | * @return string/bool depending on get/set |
| 113 | **/ |
| 114 | function mailin_messages($msg = null) |
| 115 | { |
| 116 | |
| 117 | global $mailin_msg; |
| 118 | |
| 119 | if (!is_array($mailin_msg)) |
| 120 | { |
| 121 | $mailin_msg = array(); |
| 122 | } |
| 123 | |
| 124 | if (is_null($msg)) |
| 125 | { |
| 126 | return implode('', $mailin_msg); |
| 127 | } |
| 128 | |
| 129 | $mailin_msg[] = $msg; |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Handles forms submit |
| 136 | * @return void , sets error/success message |
| 137 | **/ |
| 138 | function mailin_form_submit() |
| 139 | { |
| 140 | |
| 141 | $action = ''; |
| 142 | |
| 143 | if(isset($_POST['mailin_form_action'])) |
| 144 | { |
| 145 | $action = trim($_POST['mailin_form_action']); |
| 146 | if($action == '') |
| 147 | { |
| 148 | return; |
| 149 | |
| 150 | }elseif($action == 'sync_users') { |
| 151 | |
| 152 | $mObj = new mailin_API(); |
| 153 | $mObj->syncUsers(); |
| 154 | |
| 155 | $message = '<p class="mailin_success" >'.__('Users synchronized successfully', 'mailin_i18n').'</p>'; |
| 156 | mailin_messages($message); |
| 157 | |
| 158 | }elseif($action == 'list_details') { |
| 159 | |
| 160 | $mObj = new mailin_API(); |
| 161 | $listDetails = $mObj->getListDetails($_POST['list_id']); |
| 162 | |
| 163 | }elseif($action == 'apikey_update') { |
| 164 | |
| 165 | $mObj = new mailin_API(); |
| 166 | $mObj->handle_apikey_form_submit(strip_tags(stripslashes($_POST['mailin_apikey']))); |
| 167 | |
| 168 | if(empty($mObj->_mailin_error)) |
| 169 | { |
| 170 | |
| 171 | $message = implode('<br/>', $mObj->_mailin_success); |
| 172 | $message = '<p class="mailin_success" >'.$message.'</p>'; |
| 173 | mailin_messages($message); |
| 174 | |
| 175 | }else{ |
| 176 | |
| 177 | $message = implode('<br/>', $mObj->_mailin_error); |
| 178 | $message = '<p class="mailin_error" >'.$message.'</p>'; |
| 179 | mailin_messages($message); |
| 180 | |
| 181 | } |
| 182 | }elseif($action == 'logout') { |
| 183 | |
| 184 | $mObj = new mailin_API(); |
| 185 | $mObj->handle_logout_form_submit(); |
| 186 | |
| 187 | |
| 188 | }elseif($action == 'update_list'){ |
| 189 | |
| 190 | $mailin_list = isset($_POST['mailin_list']) ? $_POST['mailin_list'] : '' ; |
| 191 | |
| 192 | $mObj = new mailin_API(); |
| 193 | $mObj->handle_updatelist_form_submit($mailin_list); |
| 194 | |
| 195 | if(!empty($mObj->_mailin_error)) |
| 196 | { |
| 197 | $message = implode('<br/>', $mObj->_mailin_error); |
| 198 | $message = '<p class="mailin_error" >'.$message.'</p>'; |
| 199 | mailin_messages($message); |
| 200 | } |
| 201 | |
| 202 | if(!empty($mObj->_mailin_success)) |
| 203 | { |
| 204 | $message = implode('<br/>', $mObj->_mailin_success); |
| 205 | $message = '<p class="mailin_success" >'.$message.'</p>'; |
| 206 | mailin_messages($message); |
| 207 | } |
| 208 | |
| 209 | }elseif($action == 'update_campaigns') { |
| 210 | |
| 211 | $mailin_list = isset($_POST['mailin_list']) ? $_POST['mailin_list'] : '' ; |
| 212 | |
| 213 | $mObj = new mailin_API(); |
| 214 | $mObj->updateUserCampaigns(); |
| 215 | |
| 216 | $message = '<p class="mailin_success" >'.__('Campaigns updated successfully', 'mailin_i18n').'</p>'; |
| 217 | mailin_messages($message); |
| 218 | |
| 219 | |
| 220 | }elseif($action == 'subscribe_form_submit'){ |
| 221 | |
| 222 | $mObj = new mailin_API(); |
| 223 | $mObj->handle_newsletter_subscribe_submit(); |
| 224 | |
| 225 | if(!empty($mObj->_mailin_error)) |
| 226 | { |
| 227 | $message = implode('<br/>', $mObj->_mailin_error); |
| 228 | $message = '<p class="mailin_error" >'.$message.'</p>'; |
| 229 | mailin_messages($message); |
| 230 | } |
| 231 | |
| 232 | if(!empty($mObj->_mailin_success)) |
| 233 | { |
| 234 | $message = implode('<br/>', $mObj->_mailin_success); |
| 235 | $message = '<p class="mailin_success" >'.$message.'</p>'; |
| 236 | mailin_messages($message); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | add_action('init', 'mailin_form_submit'); |
| 244 | |
| 245 | /** |
| 246 | * Loads the view of Mailin admin page |
| 247 | * @return void |
| 248 | **/ |
| 249 | function mailin_settings_admin_page() |
| 250 | { |
| 251 | |
| 252 | // CHECK IF API KEY EXISTS |
| 253 | $api_key = get_option('mailin_apikey'); |
| 254 | |
| 255 | ?> |
| 256 | <div class="wrap"> |
| 257 | <div style="float:left;margin-bottom:10px;width:670px;"> |
| 258 | <div class="icon32" id="icon-options-general"><br></div> |
| 259 | <h2><?php esc_html_e('Mailin Setup', 'mailin_i18n');?> </h2> |
| 260 | </div> |
| 261 | |
| 262 | <?php |
| 263 | if (mailin_messages() != '') |
| 264 | { |
| 265 | ?> |
| 266 | <div id="mc_message" class="mailin_row"><?php echo mailin_messages(); ?></div> |
| 267 | <?php |
| 268 | } |
| 269 | |
| 270 | if($api_key != '') |
| 271 | { |
| 272 | require "listings.php"; |
| 273 | }else{ |
| 274 | require "api_form.php"; |
| 275 | } |
| 276 | ?> |
| 277 | |
| 278 | </div> |
| 279 | <?php |
| 280 | } |
| 281 | |
| 282 | add_action('admin_menu', 'admin_menu'); |
| 283 | |
| 284 | |
| 285 | /** |
| 286 | * Adds setup navigation link under settings |
| 287 | * @return void |
| 288 | **/ |
| 289 | function admin_menu() |
| 290 | { |
| 291 | add_options_page('Mailin Setup', 'Mailin setup', 'administrator', 'mailin_options', 'mailin_settings_admin_page'); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * Links Mailin setup page to URL |
| 298 | * @return setup page link |
| 299 | **/ |
| 300 | function mailin_action_links($links) |
| 301 | { |
| 302 | $settings_page = add_query_arg(array('page' => 'mailin_options'), admin_url('options-general.php')); |
| 303 | $settings_link = '<a href="'.esc_url($settings_page).'">'.__('Settings', 'Mailin setup' ).'</a>'; |
| 304 | array_unshift($links, $settings_link); |
| 305 | return $links; |
| 306 | } |
| 307 | |
| 308 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'mailin_action_links', 10, 1); |
| 309 | |
| 310 | |
| 311 | /** |
| 312 | * Sets up Plugin URL , Plugin directory , Plugin Language Directory |
| 313 | * SETUP subscriber table name |
| 314 | * @return setup page link |
| 315 | **/ |
| 316 | function Initiallize_mailin_constants() |
| 317 | { |
| 318 | |
| 319 | $locations = array( |
| 320 | 'plugins' => array( |
| 321 | 'dir' => WP_PLUGIN_DIR, |
| 322 | 'url' => plugins_url() |
| 323 | ), |
| 324 | 'template' => array( |
| 325 | 'dir' => trailingslashit(get_template_directory()).'plugins/', |
| 326 | 'url' => trailingslashit(get_template_directory_uri()).'plugins/', |
| 327 | ) |
| 328 | ); |
| 329 | |
| 330 | $mailin_dirbase = trailingslashit(basename(dirname(__FILE__))); |
| 331 | $mailin_dir = trailingslashit(WP_PLUGIN_DIR).$mailin_dirbase; |
| 332 | $mailin_url = trailingslashit(WP_PLUGIN_URL).$mailin_dirbase; |
| 333 | |
| 334 | foreach ($locations as $key => $loc) |
| 335 | { |
| 336 | $dir = trailingslashit($loc['dir']).$mailin_dirbase; |
| 337 | $url = trailingslashit($loc['url']).$mailin_dirbase; |
| 338 | if (is_file($dir.basename(__FILE__))) |
| 339 | { |
| 340 | $mailin_dir = $dir; |
| 341 | $mailin_url = $url; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | //MAILIN DIRECTORY PATH |
| 347 | define('MAILIN_DIR', $mailin_dir); |
| 348 | |
| 349 | //MAILIN LANGUAGE DIRECTORY PATH |
| 350 | define('MAILIN_LANG_DIR', trailingslashit(MAILIN_DIR).'lang/'); |
| 351 | |
| 352 | //MAILIN PLUGIN DIRECTORY PATH |
| 353 | define('MAILIN_URL', $mailin_url); |
| 354 | |
| 355 | global $wpdb; |
| 356 | |
| 357 | //Subscribers table name |
| 358 | define('MAILIN_SUBSCRIBERS', $wpdb->prefix.'mailin_subscribers'); |
| 359 | |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Create subscribers table upon installation |
| 364 | */ |
| 365 | function mailin_install() |
| 366 | { |
| 367 | |
| 368 | global $wpdb; |
| 369 | $sql = "CREATE TABLE ".MAILIN_SUBSCRIBERS." ( |
| 370 | id int(11) NOT NULL AUTO_INCREMENT, |
| 371 | email VARCHAR(255) DEFAULT '' NOT NULL, |
| 372 | fname VARCHAR(55) DEFAULT '' NOT NULL, |
| 373 | lname VARCHAR(55) DEFAULT '' NOT NULL, |
| 374 | list VARCHAR(255) DEFAULT '' NOT NULL, |
| 375 | subscribed TINYINT(1) DEFAULT '1' NOT NULL, |
| 376 | create_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, |
| 377 | UNIQUE KEY id (id) |
| 378 | );"; |
| 379 | |
| 380 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 381 | dbDelta($sql); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * Called plugin is removed |
| 387 | * We do not remove previous data |
| 388 | */ |
| 389 | function mailin_remove() |
| 390 | { |
| 391 | |
| 392 | |
| 393 | } |
| 394 | |
| 395 | /* Runs when plugin is activated */ |
| 396 | register_activation_hook(__FILE__,'mailin_install'); |
| 397 | |
| 398 | /* Runs on plugin deactivation*/ |
| 399 | register_deactivation_hook( __FILE__, 'mailin_remove' ); |
| 400 | |
| 401 | |
| 402 | class siteUsers |
| 403 | { |
| 404 | |
| 405 | static function init() |
| 406 | { |
| 407 | add_action( 'user_register', array( __CLASS__, 'register_newly_added_user' ) ); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * This function is called when a new user is created |
| 412 | * User is added in mailinlist |
| 413 | */ |
| 414 | static function register_newly_added_user( $user_id = null ) |
| 415 | { |
| 416 | |
| 417 | if($user_id != null) |
| 418 | { |
| 419 | $info = get_userdata( $user_id ); |
| 420 | |
| 421 | if(is_object($info)) |
| 422 | { |
| 423 | if(isset($info->data->user_email) && $info->data->user_email != '') |
| 424 | { |
| 425 | $user_nicename = isset($info->data->user_nicename) ? $info->data->user_nicename : ''; |
| 426 | $selected_list = get_option('mailin_list_selected'); |
| 427 | $mObj = new mailin_API(); |
| 428 | $mObj->updateSubscribers($info->data->user_email , $selected_list, $user_nicename , '' ); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | wp_update_user( $args ); |
| 433 | } |
| 434 | |
| 435 | } |
| 436 | |
| 437 | siteUsers::init(); |
| 438 | |
| 439 | |
| 440 | ?> |
| 441 |