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

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