PluginProbe
Email Log / 1.5
Email Log v1.5
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, at email-log.php

353 lines 13.0 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
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 */
48 /* Copyright 2009 Sudar Muthu (email : sudar@sudarmuthu.com)
49
50 This program is free software; you can redistribute it and/or modify
51 it under the terms of the GNU General Public License, version 2, as
52 published by the Free Software Foundation.
53
54 This program is distributed in the hope that it will be useful,
55 but WITHOUT ANY WARRANTY; without even the implied warranty of
56 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57 GNU General Public License for more details.
58
59 You should have received a copy of the GNU General Public License
60 along with this program; if not, write to the Free Software
61 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
62 */
63
64 /**
65 * The main Plugin class
66 */
67 class EmailLog {
68
69 private $admin_page;
70 private $admin_screen;
71
72 const FILTER_NAME = 'wp_mail_log';
73 const PAGE_SLUG = 'email-log';
74 const DELETE_LOG_NONCE_FIELD = 'sm-delete-email-log-nonce';
75 const DELETE_LOG_ACTION = 'sm-delete-email-log';
76
77 const TABLE_NAME = 'email_log'; /* Database table name */
78 const DB_OPTION_NAME = 'email-log-db'; /* Database option name */
79 const DB_VERSION = '0.1'; /* Database version */
80
81 /**
82 * Initalize the plugin by registering the hooks
83 */
84 function __construct() {
85
86 global $wpdb;
87
88 // Load localization domain
89 $this->translations = dirname(plugin_basename(__FILE__)) . '/languages/' ;
90 load_plugin_textdomain( 'email-log', false, $this->translations);
91
92 // Register hooks
93 add_action( 'admin_menu', array(&$this, 'register_settings_page') );
94
95 // Register Filter
96 add_filter('wp_mail', array(&$this, 'log_email'));
97 add_filter('set-screen-option', array(&$this, 'save_screen_options'), 10, 3);
98 add_filter( 'plugin_row_meta', array( &$this, 'add_plugin_links' ), 10, 2 );
99
100 $plugin = plugin_basename(__FILE__);
101 add_filter("plugin_action_links_$plugin", array(&$this, 'add_action_links'));
102
103 $this->table_name = $wpdb->prefix . self::TABLE_NAME;
104 }
105
106 /**
107 * Adds additional links in the Plugin listing. Based on http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/
108 */
109 function add_plugin_links($links, $file) {
110 $plugin = plugin_basename(__FILE__);
111
112 if ($file == $plugin) // only for this plugin
113 return array_merge( $links,
114 array( '<a href="http://sudarmuthu.com/out/buy-email-log-forward-email-addon" target="_blank">' . __('Buy Addons', 'email-log') . '</a>' )
115 );
116 return $links;
117 }
118
119 /**
120 * Register the settings page
121 */
122 function register_settings_page() {
123 //Save the handle to your admin page - you'll need it to create a WP_Screen object
124 $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') );
125
126 add_action("load-{$this->admin_page}",array(&$this,'create_settings_panel'));
127 }
128
129 /**
130 * Display email logs
131 */
132 function display_logs() {
133
134 $this->logs_table->prepare_items( $this->get_per_page() );
135 ?>
136 <div class="wrap">
137 <?php screen_icon(); ?>
138 <h2><?php _e('Email Logs', 'email-log');?></h2>
139 <?php
140 if ( $this->logs_deleted != '' ) {
141 $logs_deleted = intval( $this->logs_deleted );
142
143 if ( $logs_deleted > 0 ) {
144 echo "<div class = 'updated'><p>" . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . "</p></div>";
145 } else {
146 echo "<div class = 'updated'><p>" . __( 'There was some problem in deleting the email logs' , 'email-log') . "</p></div>";
147 }
148 unset($this->logs_deleted);
149 }
150 ?>
151 <form id="email-logs-search" method="get">
152 <input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" >
153 <?php
154 $this->logs_table->search_box( __('Search Logs', 'email-log'), 'search_id' );
155 ?>
156 </form>
157
158 <form id="email-logs-filter" method="get">
159 <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
160 <?php
161 wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD );
162 $this->logs_table->display();
163 ?>
164 </form>
165 </div>
166 <?php
167 // Display credits in Footer
168 add_action( 'in_admin_footer', array(&$this, 'add_footer_links'));
169 }
170
171 /**
172 * Add settings Panel
173 */
174 function create_settings_panel() {
175
176 /**
177 * Create the WP_Screen object against your admin page handle
178 * This ensures we're working with the right admin page
179 */
180 $this->admin_screen = WP_Screen::get($this->admin_page);
181
182 /**
183 * Content specified inline
184 */
185 $this->admin_screen->add_help_tab(
186 array(
187 'title' => __('About Plugin', 'email-log'),
188 'id' => 'about_tab',
189 'content' => '<p>' . __('Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log') . '</p>',
190 'callback' => false
191 )
192 );
193
194 // Add help sidebar
195 $this->admin_screen->set_help_sidebar(
196 '<p><strong>' . __('More information', 'email-log') . '</strong></p>' .
197 '<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __('Plugin Homepage/support', 'email-log') . '</a></p>' .
198 '<p><a href = "http://sudarmuthu.com/blog">' . __("Plugin author's blog", 'email-log') . '</a></p>' .
199 '<p><a href = "http://sudarmuthu.com/wordpress/">' . __("Other Plugin's by Author", 'email-log') . '</a></p>'
200 );
201
202 // Add screen options
203 $this->admin_screen->add_option(
204 'per_page',
205 array(
206 'label' => __('Entries per page', 'email-log'),
207 'default' => 20,
208 'option' => 'per_page'
209 )
210 );
211
212 if(!class_exists('WP_List_Table')){
213 require_once( ABSPATH . WPINC . '/class-wp-list-table.php' );
214 }
215
216 if (!class_exists( 'Email_Log_List_Table' ) ) {
217 require_once dirname( __FILE__ ) . '/include/class-email-log-list-table.php';
218 }
219
220 //Prepare Table of elements
221 $this->logs_table = new Email_Log_List_Table();
222 }
223
224 /**
225 * Save Screen option
226 */
227 function save_screen_options($status, $option, $value) {
228 if ( 'per_page' == $option ) return $value;
229 }
230
231 /**
232 * Get the per page option
233 */
234 private function get_per_page() {
235 $screen = get_current_screen();
236 $option = $screen->get_option('per_page', 'option');
237
238 $per_page = get_user_meta(get_current_user_id(), $option, TRUE);
239
240 if ( empty ( $per_page) || $per_page < 1 ) {
241 $per_page = $screen->get_option( 'per_page', 'default' );
242 }
243
244 return $per_page;
245 }
246
247 /**
248 * hook to add action links
249 *
250 * @param <type> $links
251 * @return <type>
252 */
253 function add_action_links( $links ) {
254 // Add a link to this plugin's settings page
255 $settings_link = '<a href="tools.php?page=email-log">' . __("Log", 'email-log') . '</a>';
256 array_unshift( $links, $settings_link );
257 return $links;
258 }
259
260 /**
261 * Adds Footer links. Based on http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
262 */
263 function add_footer_links() {
264 $plugin_data = get_plugin_data( __FILE__ );
265 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']);
266 }
267
268 /**
269 * Log all email to database
270 *
271 * @global object $wpdb
272 * @param array $mail_info Information about email
273 * @return array Information about email
274 */
275 function log_email($mail_info) {
276
277 global $wpdb;
278
279 $attachment_present = (count ($mail_info['attachments']) > 0) ? "true" : "false";
280
281 // Log into the database
282 $wpdb->insert($this->table_name, array(
283 'to_email' => is_array($mail_info['to']) ? $mail_info['to'][0] : $mail_info['to'],
284 'subject' => $mail_info['subject'],
285 'message' => $mail_info['message'],
286 'headers' => is_array($mail_info['headers']) ? implode("\r\n", $mail_info['headers']) : $mail_info['headers'],
287 'attachments' => $attachment_present,
288 'sent_date' => current_time('mysql')
289 ));
290
291 // return filtered array
292 return apply_filters(self::FILTER_NAME, $mail_info);
293 }
294
295 /**
296 * Check whether a key is present. If present returns the value, else returns the default value
297 *
298 * @param <array> $array - Array whose key has to be checked
299 * @param <string> $key - key that has to be checked
300 * @param <string> $default - the default value that has to be used, if the key is not found (optional)
301 *
302 * @return <mixed> If present returns the value, else returns the default value
303 * @author Sudar
304 */
305 private function array_get($array, $key, $default = NULL) {
306 return isset($array[$key]) ? $array[$key] : $default;
307 }
308 }
309
310 /**
311 * Helper class to create and maintain tables
312 */
313 class EmailLogInit {
314
315 /**
316 * Create database table when the Plugin is installed for the first time
317 *
318 * @global object $wpdb
319 * @global string $smel_table_name Table Name
320 */
321 function on_activate() {
322
323 global $wpdb;
324 $table_name = $wpdb->prefix . EmailLog::TABLE_NAME;
325
326 if($wpdb->get_var("show tables like '{$table_name}'") != $table_name) {
327
328 $sql = "CREATE TABLE " . $table_name . " (
329 id mediumint(9) NOT NULL AUTO_INCREMENT,
330 to_email VARCHAR(100) NOT NULL,
331 subject VARCHAR(250) NOT NULL,
332 message TEXT NOT NULL,
333 headers TEXT NOT NULL,
334 attachments TEXT NOT NULL,
335 sent_date timestamp NOT NULL,
336 PRIMARY KEY (id)
337 );";
338
339 require_once( ABSPATH . 'wp-admin/upgrade.php' );
340 dbDelta($sql);
341
342 add_option(EmailLog::DB_OPTION_NAME, EmailLog::DB_VERSION);
343 }
344 }
345 }
346
347 // When the Plugin installed
348 register_activation_hook(__FILE__, array('EmailLogInit', 'on_activate'));
349
350 // Start this plugin once all other plugins are fully loaded
351 add_action( 'init', 'EmailLog' ); function EmailLog() { global $EmailLog; $EmailLog = new EmailLog(); }
352 ?>
353