| 1 |
<?php |
| 2 |
/** |
| 3 |
Plugin Name: Email Log |
| 4 |
Plugin URI: http://sudarmuthu.com/wordpress/email-log |
| 5 |
Description: Logs every email sent through WordPress |
| 6 |
Donate Link: http://sudarmuthu.com/if-you-wanna-thank-me |
| 7 |
Author: Sudar |
| 8 |
Version: 1.7.2 |
| 9 |
Author URI: http://sudarmuthu.com/ |
| 10 |
Text Domain: email-log |
| 11 |
Domain Path: languages/ |
| 12 |
|
| 13 |
=== RELEASE NOTES === |
| 14 |
Check readme file for full release notes |
| 15 |
*/ |
| 16 |
|
| 17 |
/* Copyright 2009 Sudar Muthu (email : sudar@sudarmuthu.com) |
| 18 |
|
| 19 |
This program is free software; you can redistribute it and/or modify |
| 20 |
it under the terms of the GNU General Public License, version 2, as |
| 21 |
published by the Free Software Foundation. |
| 22 |
|
| 23 |
This program is distributed in the hope that it will be useful, |
| 24 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
GNU General Public License for more details. |
| 27 |
|
| 28 |
You should have received a copy of the GNU General Public License |
| 29 |
along with this program; if not, write to the Free Software |
| 30 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 31 |
*/ |
| 32 |
|
| 33 |
/** |
| 34 |
* Plugin Root File |
| 35 |
* |
| 36 |
* @since 1.7.2 |
| 37 |
*/ |
| 38 |
if ( ! defined( 'EMAIL_LOG_PLUGIN_FILE' ) ) { |
| 39 |
define( 'EMAIL_LOG_PLUGIN_FILE', __FILE__ ); |
| 40 |
} |
| 41 |
|
| 42 |
// handle installation and table creation |
| 43 |
require_once dirname( __FILE__ ) . '/include/install.php'; |
| 44 |
|
| 45 |
/** |
| 46 |
* The main Plugin class |
| 47 |
*/ |
| 48 |
class EmailLog { |
| 49 |
|
| 50 |
private $admin_screen; |
| 51 |
|
| 52 |
const FILTER_NAME = 'wp_mail_log'; |
| 53 |
const PAGE_SLUG = 'email-log'; |
| 54 |
const DELETE_LOG_NONCE_FIELD = 'sm-delete-email-log-nonce'; |
| 55 |
const DELETE_LOG_ACTION = 'sm-delete-email-log'; |
| 56 |
|
| 57 |
// DB stuff |
| 58 |
const TABLE_NAME = 'email_log'; /* Database table name */ |
| 59 |
const DB_OPTION_NAME = 'email-log-db'; /* Database option name */ |
| 60 |
const DB_VERSION = '0.1'; /* Database version */ |
| 61 |
|
| 62 |
//hooks |
| 63 |
const HOOK_LOG_COLUMNS = 'email_log_manage_log_columns'; |
| 64 |
const HOOK_LOG_DISPLAY_COLUMNS = 'email_log_display_log_columns'; |
| 65 |
|
| 66 |
/** |
| 67 |
* Initalize the plugin by registering the hooks |
| 68 |
*/ |
| 69 |
function __construct() { |
| 70 |
// Load localization domain |
| 71 |
$this->translations = dirname(plugin_basename(__FILE__)) . '/languages/' ; |
| 72 |
load_plugin_textdomain( 'email-log', false, $this->translations); |
| 73 |
|
| 74 |
// Register hooks |
| 75 |
add_action( 'admin_menu', array(&$this, 'register_settings_page') ); |
| 76 |
|
| 77 |
// Register Filter |
| 78 |
add_filter('wp_mail', array(&$this, 'log_email')); |
| 79 |
add_filter('set-screen-option', array(&$this, 'save_screen_options'), 10, 3); |
| 80 |
add_filter( 'plugin_row_meta', array( &$this, 'add_plugin_links' ), 10, 2 ); |
| 81 |
|
| 82 |
$plugin = plugin_basename(__FILE__); |
| 83 |
add_filter("plugin_action_links_$plugin", array(&$this, 'add_action_links')); |
| 84 |
|
| 85 |
//Add our ajax call |
| 86 |
add_action( 'wp_ajax_display_content', array(&$this, 'display_content_callback')); |
| 87 |
|
| 88 |
// Add our javascript in the footer |
| 89 |
add_action( 'admin_footer', array(&$this, 'include_js') ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Adds additional links in the Plugin listing. Based on http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/ |
| 94 |
*/ |
| 95 |
function add_plugin_links($links, $file) { |
| 96 |
$plugin = plugin_basename(__FILE__); |
| 97 |
|
| 98 |
if ($file == $plugin) // only for this plugin |
| 99 |
return array_merge( $links, |
| 100 |
array( '<a href="http://sudarmuthu.com/wordpress/email-log/pro-addons" target="_blank">' . __('Buy Addons', 'email-log') . '</a>' ) |
| 101 |
); |
| 102 |
return $links; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Register the settings page |
| 107 |
*/ |
| 108 |
function register_settings_page() { |
| 109 |
//Save the handle to your admin page - you'll need it to create a WP_Screen object |
| 110 |
$this->admin_page = add_submenu_page( 'tools.php', __('Email Log', 'email-log'), __('Email Log', 'email-log'), 'manage_options', self::PAGE_SLUG , array( &$this, 'display_logs') ); |
| 111 |
|
| 112 |
add_action("load-{$this->admin_page}",array(&$this,'create_settings_panel')); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Display email logs |
| 117 |
*/ |
| 118 |
function display_logs() { |
| 119 |
|
| 120 |
$this->logs_table->prepare_items( $this->get_per_page() ); |
| 121 |
?> |
| 122 |
<div class="wrap"> |
| 123 |
<h2><?php _e('Email Logs', 'email-log');?></h2> |
| 124 |
<?php |
| 125 |
if ( isset( $this->logs_deleted ) && $this->logs_deleted != '' ) { |
| 126 |
$logs_deleted = intval( $this->logs_deleted ); |
| 127 |
|
| 128 |
if ( $logs_deleted > 0 ) { |
| 129 |
echo "<div class = 'updated'><p>" . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . "</p></div>"; |
| 130 |
} else { |
| 131 |
echo "<div class = 'updated'><p>" . __( 'There was some problem in deleting the email logs' , 'email-log') . "</p></div>"; |
| 132 |
} |
| 133 |
unset($this->logs_deleted); |
| 134 |
} |
| 135 |
?> |
| 136 |
<form id="email-logs-search" method="get"> |
| 137 |
<input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" > |
| 138 |
<?php |
| 139 |
$this->logs_table->search_box( __('Search Logs', 'email-log'), 'search_id' ); |
| 140 |
?> |
| 141 |
</form> |
| 142 |
|
| 143 |
<form id="email-logs-filter" method="get"> |
| 144 |
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" /> |
| 145 |
<?php |
| 146 |
wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD ); |
| 147 |
$this->logs_table->display(); |
| 148 |
?> |
| 149 |
</form> |
| 150 |
</div> |
| 151 |
<?php |
| 152 |
// Display credits in Footer |
| 153 |
add_action( 'in_admin_footer', array(&$this, 'add_footer_links')); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Add settings Panel |
| 158 |
*/ |
| 159 |
function create_settings_panel() { |
| 160 |
|
| 161 |
/** |
| 162 |
* Create the WP_Screen object against your admin page handle |
| 163 |
* This ensures we're working with the right admin page |
| 164 |
*/ |
| 165 |
$this->admin_screen = WP_Screen::get($this->admin_page); |
| 166 |
|
| 167 |
/** |
| 168 |
* Content specified inline |
| 169 |
*/ |
| 170 |
$this->admin_screen->add_help_tab( |
| 171 |
array( |
| 172 |
'title' => __('About Plugin', 'email-log'), |
| 173 |
'id' => 'about_tab', |
| 174 |
'content' => '<p>' . __('Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log') . '</p>', |
| 175 |
'callback' => false |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
// Add help sidebar |
| 180 |
$this->admin_screen->set_help_sidebar( |
| 181 |
'<p><strong>' . __('More information', 'email-log') . '</strong></p>' . |
| 182 |
'<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __('Plugin Homepage/support', 'email-log') . '</a></p>' . |
| 183 |
'<p><a href = "http://sudarmuthu.com/blog">' . __("Plugin author's blog", 'email-log') . '</a></p>' . |
| 184 |
'<p><a href = "http://sudarmuthu.com/wordpress/">' . __("Other Plugin's by Author", 'email-log') . '</a></p>' |
| 185 |
); |
| 186 |
|
| 187 |
// Add screen options |
| 188 |
$this->admin_screen->add_option( |
| 189 |
'per_page', |
| 190 |
array( |
| 191 |
'label' => __('Entries per page', 'email-log'), |
| 192 |
'default' => 20, |
| 193 |
'option' => 'per_page' |
| 194 |
) |
| 195 |
); |
| 196 |
|
| 197 |
if(!class_exists('WP_List_Table')){ |
| 198 |
require_once( ABSPATH . WPINC . '/class-wp-list-table.php' ); |
| 199 |
} |
| 200 |
|
| 201 |
if (!class_exists( 'Email_Log_List_Table' ) ) { |
| 202 |
require_once dirname( __FILE__ ) . '/include/class-email-log-list-table.php'; |
| 203 |
} |
| 204 |
|
| 205 |
//Prepare Table of elements |
| 206 |
$this->logs_table = new Email_Log_List_Table(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Include JavaScript displaying email content |
| 211 |
* |
| 212 |
* @since 1.6 |
| 213 |
*/ |
| 214 |
function include_js() { |
| 215 |
// TODO: Move this to a separate js file |
| 216 |
?> |
| 217 |
<script type="text/javascript"> |
| 218 |
jQuery(document).ready(function($) { |
| 219 |
|
| 220 |
$(".email_content").click(function() { |
| 221 |
|
| 222 |
var w = window.open('', 'newwin', 'width=650,height=500'); |
| 223 |
|
| 224 |
var email_id = $(this).attr('id').replace('email_content_',''); |
| 225 |
data = { |
| 226 |
action: 'display_content', |
| 227 |
email_id: email_id |
| 228 |
}; |
| 229 |
|
| 230 |
$.post(ajaxurl, data, function (response) { |
| 231 |
$(w.document.body).html(response); |
| 232 |
}); |
| 233 |
}); |
| 234 |
}); |
| 235 |
</script> |
| 236 |
<?php |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* AJAX callback for displaying email content |
| 241 |
* |
| 242 |
* @since 1.6 |
| 243 |
*/ |
| 244 |
function display_content_callback() { |
| 245 |
global $wpdb; |
| 246 |
|
| 247 |
$table_name = $wpdb->prefix . self::TABLE_NAME; |
| 248 |
$email_id = absint( $_POST['email_id'] ); |
| 249 |
|
| 250 |
// Select the matching item from the database |
| 251 |
$query = $wpdb->prepare( "SELECT * FROM " . $table_name . " WHERE id = %d", $email_id ); |
| 252 |
$content = $wpdb->get_results( $query ); |
| 253 |
|
| 254 |
// Write the message content to the screen |
| 255 |
echo $content[0]->message; |
| 256 |
|
| 257 |
die(); // this is required to return a proper result |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Save Screen option |
| 262 |
*/ |
| 263 |
function save_screen_options($status, $option, $value) { |
| 264 |
if ( 'per_page' == $option ) return $value; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get the per page option |
| 269 |
* |
| 270 |
* @static |
| 271 |
* @access public |
| 272 |
* @return int $per_page Number of logs a user wanted to be displayed in a page |
| 273 |
* |
| 274 |
*/ |
| 275 |
public static function get_per_page() { |
| 276 |
$screen = get_current_screen(); |
| 277 |
$option = $screen->get_option('per_page', 'option'); |
| 278 |
|
| 279 |
$per_page = get_user_meta(get_current_user_id(), $option, TRUE); |
| 280 |
|
| 281 |
if ( empty ( $per_page) || $per_page < 1 ) { |
| 282 |
$per_page = $screen->get_option( 'per_page', 'default' ); |
| 283 |
} |
| 284 |
|
| 285 |
return $per_page; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* hook to add action links |
| 290 |
* |
| 291 |
* @param <type> $links |
| 292 |
* @return <type> |
| 293 |
*/ |
| 294 |
function add_action_links( $links ) { |
| 295 |
// Add a link to this plugin's settings page |
| 296 |
$settings_link = '<a href="tools.php?page=email-log">' . __("Log", 'email-log') . '</a>'; |
| 297 |
array_unshift( $links, $settings_link ); |
| 298 |
return $links; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/ |
| 303 |
*/ |
| 304 |
function add_footer_links() { |
| 305 |
$plugin_data = get_plugin_data( __FILE__ ); |
| 306 |
printf('%1$s ' . __("plugin", 'email-log') .' | ' . __("Version", 'email-log') . ' %2$s | '. __('by', 'email-log') . ' %3$s<br />', $plugin_data['Title'], $plugin_data['Version'], $plugin_data['Author']); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Log all email to database |
| 311 |
* |
| 312 |
* @global object $wpdb |
| 313 |
* @param array $mail_info Information about email |
| 314 |
* @return array Information about email |
| 315 |
*/ |
| 316 |
function log_email($mail_info) { |
| 317 |
|
| 318 |
global $wpdb; |
| 319 |
|
| 320 |
$attachment_present = (count ($mail_info['attachments']) > 0) ? "true" : "false"; |
| 321 |
|
| 322 |
// return filtered array |
| 323 |
$mail_info = apply_filters(self::FILTER_NAME, $mail_info); |
| 324 |
$table_name = $wpdb->prefix . self::TABLE_NAME; |
| 325 |
|
| 326 |
// Log into the database |
| 327 |
$wpdb->insert( $table_name, array( |
| 328 |
'to_email' => is_array($mail_info['to']) ? $mail_info['to'][0] : $mail_info['to'], |
| 329 |
'subject' => $mail_info['subject'], |
| 330 |
'message' => $mail_info['message'], |
| 331 |
'headers' => is_array($mail_info['headers']) ? implode("\n", $mail_info['headers']) : $mail_info['headers'], |
| 332 |
'attachments' => $attachment_present, |
| 333 |
'sent_date' => current_time('mysql') |
| 334 |
)); |
| 335 |
|
| 336 |
return $mail_info; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Check whether a key is present. If present returns the value, else returns the default value |
| 341 |
* |
| 342 |
* @param <array> $array - Array whose key has to be checked |
| 343 |
* @param <string> $key - key that has to be checked |
| 344 |
* @param <string> $default - the default value that has to be used, if the key is not found (optional) |
| 345 |
* |
| 346 |
* @return <mixed> If present returns the value, else returns the default value |
| 347 |
* @author Sudar |
| 348 |
*/ |
| 349 |
private function array_get($array, $key, $default = NULL) { |
| 350 |
return isset($array[$key]) ? $array[$key] : $default; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// Start this plugin once all other plugins are fully loaded |
| 355 |
add_action( 'init', 'EmailLog' ); function EmailLog() { global $EmailLog; $EmailLog = new EmailLog(); } |
| 356 |
?> |
| 357 |
|