PluginProbe
Email Log / 1.5.2
Email Log v1.5.2
2.63 2.4.8 2.4.9 2.6 2.61 2.62 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.9 0.9.1 0.9.2 1.1 1.5 1.5.1 1.5.2 1.5.3 1.5.4 All 61 releases
email-log / email-log.php

email-log.php in Email Log 1.5.2, at email-log.php

366 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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. Compatible with WPMU too.
6 Donate Link: http://sudarmuthu.com/if-you-wanna-thank-me
7 Author: Sudar
8 Version: 1.5.2
9 Author URI: http://sudarmuthu.com/
10 Text Domain: email-log
11 Domain Path: languages/
12
13 === RELEASE NOTES ===
14 2009-10-08 - v0.1 - Initial Release
15 2009-10-15 - v0.2 - Added compatability for MySQL 4
16 2009-10-19 - v0.3 - Added compatability for MySQL 4 (Thanks Frank)
17 2010-01-02 - v0.4 - Added german translation (Thanks Frank)
18 2012-01-01 - v0.5 - Fixed a deprecation notice
19 2012-04-29 - v0.6 - (Dev time: 2 hours)
20 - Added option to delete individual email logs
21 - Moved pages per screen option to Screen options panel
22 - Added information to the screen help tab
23 - Added Lithuanian translations
24 2012-06-23 - v0.7 - (Dev time: 1 hour)
25 - Changed Timestamp(n) MySQL datatype to Timestamp (now compatible with MySQL 5.5+)
26 - Added the ability to bulk delete checkboxes
27 2012-07-12 - v0.8 - (Dev time: 1 hour)
28 - Fixed undefined notices - http://wordpress.org/support/topic/plugin-email-log-notices-undefined-indices
29 - Added Dutch translations
30 2012-07-23 - v0.8.1 - (Dev time: 0.5 hour)
31 - Reworded most error messages and fixed lot of typos
32 2013-01-08 - v0.9 - (Dev time: 1 hour)
33 - Use blog date/time for send date instead of server time
34 - Handle cases where the headers send is an array
35 2013-01-08 - v0.9.1 - (Dev time: 0.5 hour)
36 - Moved the menu under tools (Thanks samuelaguilera)
37 2013-03-14 - v0.9.2 - (Dev time: 0.5 hour)
38 - Added support for filters which can be used while logging emails
39 2013-04-01 - v0.9.3 - (Dev time: 0.5 hour)
40 - Moved table name into a separate constants file
41 2013-04-17 - v1.0 - (Dev time: 0.5 hour)
42 - Added support for buying pro addons
43 2013-04-27 - v1.1 - (Dev time: 0.5 hour)
44 - Added more documentation
45 2013-09-09 - v1.5 - (Dev time: 10 hours)
46 - Rewrote Admin interface using native tables
47 2013-09-09 - v1.5.1 - (Dev time: 0.5 hours)
48 - Correct the upgrade file include path. Issue #7
49 - Fix undfined notice error. Issue #8
50 - Update screenshots. Issue #6
51 2013-09-13 - v1.5.2 - (Dev time: 0.5 hours)
52 - Add the ability to override the fields displayed in the log page
53 - Add support for "More Fields" addon
54 */
55 /* Copyright 2009 Sudar Muthu (email : sudar@sudarmuthu.com)
56
57 This program is free software; you can redistribute it and/or modify
58 it under the terms of the GNU General Public License, version 2, as
59 published by the Free Software Foundation.
60
61 This program is distributed in the hope that it will be useful,
62 but WITHOUT ANY WARRANTY; without even the implied warranty of
63 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 GNU General Public License for more details.
65
66 You should have received a copy of the GNU General Public License
67 along with this program; if not, write to the Free Software
68 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
69 */
70
71 /**
72 * The main Plugin class
73 */
74 class EmailLog {
75
76 private $admin_screen;
77
78 const FILTER_NAME = 'wp_mail_log';
79 const PAGE_SLUG = 'email-log';
80 const DELETE_LOG_NONCE_FIELD = 'sm-delete-email-log-nonce';
81 const DELETE_LOG_ACTION = 'sm-delete-email-log';
82
83 // DB stuff
84 const TABLE_NAME = 'email_log'; /* Database table name */
85 const DB_OPTION_NAME = 'email-log-db'; /* Database option name */
86 const DB_VERSION = '0.1'; /* Database version */
87
88 //hooks
89 const HOOK_LOG_COLUMNS = 'email_log_manage_log_columns';
90 const HOOK_LOG_DISPLAY_COLUMNS = 'email_log_display_log_columns';
91
92 /**
93 * Initalize the plugin by registering the hooks
94 */
95 function __construct() {
96
97 global $wpdb;
98
99 // Load localization domain
100 $this->translations = dirname(plugin_basename(__FILE__)) . '/languages/' ;
101 load_plugin_textdomain( 'email-log', false, $this->translations);
102
103 // Register hooks
104 add_action( 'admin_menu', array(&$this, 'register_settings_page') );
105
106 // Register Filter
107 add_filter('wp_mail', array(&$this, 'log_email'));
108 add_filter('set-screen-option', array(&$this, 'save_screen_options'), 10, 3);
109 add_filter( 'plugin_row_meta', array( &$this, 'add_plugin_links' ), 10, 2 );
110
111 $plugin = plugin_basename(__FILE__);
112 add_filter("plugin_action_links_$plugin", array(&$this, 'add_action_links'));
113
114 $this->table_name = $wpdb->prefix . self::TABLE_NAME;
115 }
116
117 /**
118 * Adds additional links in the Plugin listing. Based on http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/
119 */
120 function add_plugin_links($links, $file) {
121 $plugin = plugin_basename(__FILE__);
122
123 if ($file == $plugin) // only for this plugin
124 return array_merge( $links,
125 array( '<a href="http://sudarmuthu.com/wordpress/email-log/pro-addons" target="_blank">' . __('Buy Addons', 'email-log') . '</a>' )
126 );
127 return $links;
128 }
129
130 /**
131 * Register the settings page
132 */
133 function register_settings_page() {
134 //Save the handle to your admin page - you'll need it to create a WP_Screen object
135 $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') );
136
137 add_action("load-{$this->admin_page}",array(&$this,'create_settings_panel'));
138 }
139
140 /**
141 * Display email logs
142 */
143 function display_logs() {
144
145 $this->logs_table->prepare_items( $this->get_per_page() );
146 ?>
147 <div class="wrap">
148 <?php screen_icon(); ?>
149 <h2><?php _e('Email Logs', 'email-log');?></h2>
150 <?php
151 if ( isset( $this->logs_deleted ) && $this->logs_deleted != '' ) {
152 $logs_deleted = intval( $this->logs_deleted );
153
154 if ( $logs_deleted > 0 ) {
155 echo "<div class = 'updated'><p>" . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . "</p></div>";
156 } else {
157 echo "<div class = 'updated'><p>" . __( 'There was some problem in deleting the email logs' , 'email-log') . "</p></div>";
158 }
159 unset($this->logs_deleted);
160 }
161 ?>
162 <form id="email-logs-search" method="get">
163 <input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" >
164 <?php
165 $this->logs_table->search_box( __('Search Logs', 'email-log'), 'search_id' );
166 ?>
167 </form>
168
169 <form id="email-logs-filter" method="get">
170 <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
171 <?php
172 wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD );
173 $this->logs_table->display();
174 ?>
175 </form>
176 </div>
177 <?php
178 // Display credits in Footer
179 add_action( 'in_admin_footer', array(&$this, 'add_footer_links'));
180 }
181
182 /**
183 * Add settings Panel
184 */
185 function create_settings_panel() {
186
187 /**
188 * Create the WP_Screen object against your admin page handle
189 * This ensures we're working with the right admin page
190 */
191 $this->admin_screen = WP_Screen::get($this->admin_page);
192
193 /**
194 * Content specified inline
195 */
196 $this->admin_screen->add_help_tab(
197 array(
198 'title' => __('About Plugin', 'email-log'),
199 'id' => 'about_tab',
200 'content' => '<p>' . __('Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log') . '</p>',
201 'callback' => false
202 )
203 );
204
205 // Add help sidebar
206 $this->admin_screen->set_help_sidebar(
207 '<p><strong>' . __('More information', 'email-log') . '</strong></p>' .
208 '<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __('Plugin Homepage/support', 'email-log') . '</a></p>' .
209 '<p><a href = "http://sudarmuthu.com/blog">' . __("Plugin author's blog", 'email-log') . '</a></p>' .
210 '<p><a href = "http://sudarmuthu.com/wordpress/">' . __("Other Plugin's by Author", 'email-log') . '</a></p>'
211 );
212
213 // Add screen options
214 $this->admin_screen->add_option(
215 'per_page',
216 array(
217 'label' => __('Entries per page', 'email-log'),
218 'default' => 20,
219 'option' => 'per_page'
220 )
221 );
222
223 if(!class_exists('WP_List_Table')){
224 require_once( ABSPATH . WPINC . '/class-wp-list-table.php' );
225 }
226
227 if (!class_exists( 'Email_Log_List_Table' ) ) {
228 require_once dirname( __FILE__ ) . '/include/class-email-log-list-table.php';
229 }
230
231 //Prepare Table of elements
232 $this->logs_table = new Email_Log_List_Table();
233 }
234
235 /**
236 * Save Screen option
237 */
238 function save_screen_options($status, $option, $value) {
239 if ( 'per_page' == $option ) return $value;
240 }
241
242 /**
243 * Get the per page option
244 */
245 private function get_per_page() {
246 $screen = get_current_screen();
247 $option = $screen->get_option('per_page', 'option');
248
249 $per_page = get_user_meta(get_current_user_id(), $option, TRUE);
250
251 if ( empty ( $per_page) || $per_page < 1 ) {
252 $per_page = $screen->get_option( 'per_page', 'default' );
253 }
254
255 return $per_page;
256 }
257
258 /**
259 * hook to add action links
260 *
261 * @param <type> $links
262 * @return <type>
263 */
264 function add_action_links( $links ) {
265 // Add a link to this plugin's settings page
266 $settings_link = '<a href="tools.php?page=email-log">' . __("Log", 'email-log') . '</a>';
267 array_unshift( $links, $settings_link );
268 return $links;
269 }
270
271 /**
272 * Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
273 */
274 function add_footer_links() {
275 $plugin_data = get_plugin_data( __FILE__ );
276 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']);
277 }
278
279 /**
280 * Log all email to database
281 *
282 * @global object $wpdb
283 * @param array $mail_info Information about email
284 * @return array Information about email
285 */
286 function log_email($mail_info) {
287
288 global $wpdb;
289
290 $attachment_present = (count ($mail_info['attachments']) > 0) ? "true" : "false";
291
292 // return filtered array
293 $mail_info = apply_filters(self::FILTER_NAME, $mail_info);
294
295 // Log into the database
296 $wpdb->insert($this->table_name, array(
297 'to_email' => is_array($mail_info['to']) ? $mail_info['to'][0] : $mail_info['to'],
298 'subject' => $mail_info['subject'],
299 'message' => $mail_info['message'],
300 'headers' => is_array($mail_info['headers']) ? implode("\n", $mail_info['headers']) : $mail_info['headers'],
301 'attachments' => $attachment_present,
302 'sent_date' => current_time('mysql')
303 ));
304
305 return $mail_info;
306 }
307
308 /**
309 * Check whether a key is present. If present returns the value, else returns the default value
310 *
311 * @param <array> $array - Array whose key has to be checked
312 * @param <string> $key - key that has to be checked
313 * @param <string> $default - the default value that has to be used, if the key is not found (optional)
314 *
315 * @return <mixed> If present returns the value, else returns the default value
316 * @author Sudar
317 */
318 private function array_get($array, $key, $default = NULL) {
319 return isset($array[$key]) ? $array[$key] : $default;
320 }
321 }
322
323 /**
324 * Helper class to create and maintain tables
325 */
326 class EmailLogInit {
327
328 /**
329 * Create database table when the Plugin is installed for the first time
330 *
331 * @global object $wpdb
332 * @global string $smel_table_name Table Name
333 */
334 function on_activate() {
335
336 global $wpdb;
337 $table_name = $wpdb->prefix . EmailLog::TABLE_NAME;
338
339 if($wpdb->get_var("show tables like '{$table_name}'") != $table_name) {
340
341 $sql = "CREATE TABLE " . $table_name . " (
342 id mediumint(9) NOT NULL AUTO_INCREMENT,
343 to_email VARCHAR(100) NOT NULL,
344 subject VARCHAR(250) NOT NULL,
345 message TEXT NOT NULL,
346 headers TEXT NOT NULL,
347 attachments TEXT NOT NULL,
348 sent_date timestamp NOT NULL,
349 PRIMARY KEY (id)
350 );";
351
352 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
353 dbDelta($sql);
354
355 add_option(EmailLog::DB_OPTION_NAME, EmailLog::DB_VERSION);
356 }
357 }
358 }
359
360 // When the Plugin installed
361 register_activation_hook(__FILE__, array('EmailLogInit', 'on_activate'));
362
363 // Start this plugin once all other plugins are fully loaded
364 add_action( 'init', 'EmailLog' ); function EmailLog() { global $EmailLog; $EmailLog = new EmailLog(); }
365 ?>
366