1
|
|
|
<?php namespace EmailLog\Core; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The main plugin class. |
5
|
|
|
* |
6
|
|
|
* @since Genesis |
7
|
|
|
*/ |
8
|
|
|
class EmailLog { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Version number. |
12
|
|
|
* |
13
|
|
|
* @since Genesis |
14
|
|
|
* @var const VERSION |
15
|
|
|
*/ |
16
|
|
|
const VERSION = '1.9.1'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Flag to track if the plugin is loaded. |
20
|
|
|
* |
21
|
|
|
* @since 2.0 |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $loaded; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Filesystem directory path where translations are stored. |
28
|
|
|
* |
29
|
|
|
* @since 2.0 |
30
|
|
|
* @var string $translations_path |
31
|
|
|
*/ |
32
|
|
|
public $translations_path; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Admin screen object. |
36
|
|
|
* |
37
|
|
|
* @since Genesis |
38
|
|
|
* @access private |
39
|
|
|
* @var string $include_path |
40
|
|
|
*/ |
41
|
|
|
private $admin_screen; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Filter name. |
45
|
|
|
* |
46
|
|
|
* @since Genesis |
47
|
|
|
* @var const FILTER_NAME |
48
|
|
|
*/ |
49
|
|
|
const FILTER_NAME = 'wp_mail_log'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Page slug to be used in admin dashboard hyperlinks. |
53
|
|
|
* |
54
|
|
|
* @since Genesis |
55
|
|
|
* @var const PAGE_SLUG |
56
|
|
|
*/ |
57
|
|
|
const PAGE_SLUG = 'email-log'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* String value to generate nonce. |
61
|
|
|
* |
62
|
|
|
* @since Genesis |
63
|
|
|
* @var const DELETE_LOG_NONCE_FIELD |
64
|
|
|
*/ |
65
|
|
|
const DELETE_LOG_NONCE_FIELD = 'sm-delete-email-log-nonce'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* String value to generate nonce. |
69
|
|
|
* |
70
|
|
|
* @since Genesis |
71
|
|
|
* @var const DELETE_LOG_ACTION |
72
|
|
|
*/ |
73
|
|
|
const DELETE_LOG_ACTION = 'sm-delete-email-log'; |
74
|
|
|
|
75
|
|
|
// DB stuff |
76
|
|
|
const TABLE_NAME = 'email_log'; /* Database table name */ |
77
|
|
|
const DB_OPTION_NAME = 'email-log-db'; /* Database option name */ |
78
|
|
|
const DB_VERSION = '0.1'; /* Database version */ |
79
|
|
|
|
80
|
|
|
// JS Stuff |
81
|
|
|
const JS_HANDLE = 'email-log'; |
82
|
|
|
|
83
|
|
|
//hooks |
84
|
|
|
const HOOK_LOG_COLUMNS = 'email_log_manage_log_columns'; |
85
|
|
|
const HOOK_LOG_DISPLAY_COLUMNS = 'email_log_display_log_columns'; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Initialize the plugin. |
89
|
|
|
*/ |
90
|
|
|
public function __construct( $file ) { |
91
|
|
|
$this->plugin_file = $file; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Load the plugin. |
96
|
|
|
*/ |
97
|
|
|
public function load() { |
98
|
|
|
if ( $this->loaded ) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Load localization domain. |
103
|
|
|
$this->translations_path = dirname( plugin_basename( $this->plugin_file ) ) . '/languages/' ; |
104
|
|
|
load_plugin_textdomain( 'email-log', false, $this->translations_path ); |
105
|
|
|
|
106
|
|
|
// Register hooks. |
107
|
|
|
add_action( 'admin_menu', array( $this, 'register_settings_page' ) ); |
108
|
|
|
|
109
|
|
|
// Register Filter. |
110
|
|
|
add_filter( 'wp_mail', array( $this, 'log_email' ) ); |
111
|
|
|
add_filter( 'set-screen-option', array( $this, 'save_screen_options' ), 10, 3 ); |
112
|
|
|
add_filter( 'plugin_row_meta', array( $this, 'add_plugin_links' ), 10, 2 ); |
113
|
|
|
|
114
|
|
|
$plugin = plugin_basename( $this->plugin_file ); |
115
|
|
|
add_filter( "plugin_action_links_$plugin", array( $this, 'add_action_links' ) ); |
|
|
|
|
116
|
|
|
|
117
|
|
|
// Add our ajax call. |
118
|
|
|
add_action( 'wp_ajax_display_content', array( $this, 'display_content_callback' ) ); |
119
|
|
|
|
120
|
|
|
$this->loaded = true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Adds additional links in the plugin listing page. |
125
|
|
|
* TODO: Move to UI namespace |
126
|
|
|
* |
127
|
|
|
* @since Genesis |
128
|
|
|
* |
129
|
|
|
* @see Additional links in the Plugin listing is based on |
130
|
|
|
* @link http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/ |
131
|
|
|
* |
132
|
|
|
* @param array $links Array with default links to display in plugins page. |
133
|
|
|
* @param string $file The name of the plugin file. |
134
|
|
|
* @return array Array with links to display in plugins page. |
135
|
|
|
*/ |
136
|
|
|
public function add_plugin_links( $links, $file ) { |
137
|
|
|
$plugin = plugin_basename( $this->plugin_file ); |
138
|
|
|
|
139
|
|
|
if ( $file == $plugin ) { |
140
|
|
|
// only for this plugin |
141
|
|
|
return array_merge( $links, |
142
|
|
|
array( '<a href="http://sudarmuthu.com/wordpress/email-log/pro-addons" target="_blank">' . __( 'Buy Addons', 'email-log' ) . '</a>' ) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
return $links; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Registers the settings page. |
150
|
|
|
* TODO: Move to UI namespace |
151
|
|
|
* |
152
|
|
|
* @since Genesis |
153
|
|
|
*/ |
154
|
|
|
public function register_settings_page() { |
155
|
|
|
// Save the handle to your admin page - you'll need it to create a WP_Screen object |
156
|
|
|
$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' ) ); |
|
|
|
|
157
|
|
|
|
158
|
|
|
add_action( "load-{$this->admin_page}", array( $this, 'create_settings_panel' ) ); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Displays the stored email log. |
163
|
|
|
* TODO: Move to UI namespace |
164
|
|
|
* |
165
|
|
|
* @since Genesis |
166
|
|
|
*/ |
167
|
|
|
public function display_logs() { |
168
|
|
|
add_thickbox(); |
169
|
|
|
|
170
|
|
|
$this->logs_table->prepare_items( $this->get_per_page() ); |
|
|
|
|
171
|
|
|
?> |
172
|
|
|
<div class="wrap"> |
173
|
|
|
<h2><?php _e( 'Email Logs', 'email-log' );?></h2> |
174
|
|
|
<?php |
175
|
|
|
if ( isset( $this->logs_deleted ) && $this->logs_deleted != '' ) { |
176
|
|
|
$logs_deleted = intval( $this->logs_deleted ); |
|
|
|
|
177
|
|
|
|
178
|
|
|
if ( $logs_deleted > 0 ) { |
179
|
|
|
echo '<div class="updated"><p>' . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . '</p></div>'; |
180
|
|
|
} else { |
181
|
|
|
echo '<div class="updated"><p>' . __( 'There was some problem in deleting the email logs' , 'email-log' ) . '</p></div>'; |
182
|
|
|
} |
183
|
|
|
unset( $this->logs_deleted ); |
184
|
|
|
} |
185
|
|
|
?> |
186
|
|
|
<form id="email-logs-search" method="get"> |
187
|
|
|
<input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" > |
188
|
|
|
<?php |
189
|
|
|
$this->logs_table->search_box( __( 'Search Logs', 'email-log' ), 'search_id' ); |
190
|
|
|
?> |
191
|
|
|
</form> |
192
|
|
|
|
193
|
|
|
<form id="email-logs-filter" method="get"> |
194
|
|
|
<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" /> |
195
|
|
|
<?php |
196
|
|
|
wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD ); |
197
|
|
|
$this->logs_table->display(); |
198
|
|
|
?> |
199
|
|
|
</form> |
200
|
|
|
</div> |
201
|
|
|
<?php |
202
|
|
|
/** |
203
|
|
|
* Action to add additional content to email log admin footer. |
204
|
|
|
* |
205
|
|
|
* @since 1.8 |
206
|
|
|
*/ |
207
|
|
|
do_action( 'el_admin_footer' ); |
208
|
|
|
|
209
|
|
|
// Display credits in Footer |
210
|
|
|
add_action( 'in_admin_footer', array( $this, 'add_footer_links' ) ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Adds settings panel for the plugin. |
215
|
|
|
* TODO: Move to UI namespace |
216
|
|
|
* |
217
|
|
|
* @since Genesis |
218
|
|
|
*/ |
219
|
|
|
public function create_settings_panel() { |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Create the WP_Screen object against your admin page handle |
223
|
|
|
* This ensures we're working with the right admin page |
224
|
|
|
*/ |
225
|
|
|
$this->admin_screen = \WP_Screen::get( $this->admin_page ); |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Content specified inline |
229
|
|
|
*/ |
230
|
|
|
$this->admin_screen->add_help_tab( |
231
|
|
|
array( |
232
|
|
|
'title' => __( 'About Plugin', 'email-log' ), |
233
|
|
|
'id' => 'about_tab', |
234
|
|
|
'content' => '<p>' . __( 'Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log' ) . '</p>', |
235
|
|
|
'callback' => false, |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
// Add help sidebar |
240
|
|
|
$this->admin_screen->set_help_sidebar( |
241
|
|
|
'<p><strong>' . __( 'More information', 'email-log' ) . '</strong></p>' . |
242
|
|
|
'<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __( 'Plugin Homepage/support', 'email-log' ) . '</a></p>' . |
243
|
|
|
'<p><a href = "http://sudarmuthu.com/blog">' . __( "Plugin author's blog", 'email-log' ) . '</a></p>' . |
244
|
|
|
'<p><a href = "http://sudarmuthu.com/wordpress/">' . __( "Other Plugin's by Author", 'email-log' ) . '</a></p>' |
245
|
|
|
); |
246
|
|
|
|
247
|
|
|
// Add screen options |
248
|
|
|
$this->admin_screen->add_option( |
249
|
|
|
'per_page', |
250
|
|
|
array( |
251
|
|
|
'label' => __( 'Entries per page', 'email-log' ), |
252
|
|
|
'default' => 20, |
253
|
|
|
'option' => 'per_page', |
254
|
|
|
) |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
//Prepare Table of elements |
258
|
|
|
$this->logs_table = new UI\LogTable(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* AJAX callback for displaying email content. |
263
|
|
|
* TODO: Move to UI namespace |
264
|
|
|
* |
265
|
|
|
* @since 1.6 |
266
|
|
|
*/ |
267
|
|
|
public function display_content_callback() { |
268
|
|
|
global $wpdb; |
269
|
|
|
|
270
|
|
|
if ( current_user_can( 'manage_options' ) ) { |
271
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME; |
272
|
|
|
$email_id = absint( $_GET['email_id'] ); |
273
|
|
|
|
274
|
|
|
$query = $wpdb->prepare( 'SELECT * FROM ' . $table_name . ' WHERE id = %d', $email_id ); |
275
|
|
|
$content = $wpdb->get_results( $query ); |
276
|
|
|
|
277
|
|
|
echo wpautop( $content[0]->message ); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
die(); // this is required to return a proper result |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Saves Screen options. |
285
|
|
|
* TODO: Move to UI namespace |
286
|
|
|
* |
287
|
|
|
* @since Genesis |
288
|
|
|
* |
289
|
|
|
* @param bool|int $status Screen option value. Default false to skip. |
290
|
|
|
* @param string $option The option name. |
291
|
|
|
* @param int $value The number of rows to use. |
292
|
|
|
* @return bool|int |
293
|
|
|
*/ |
294
|
|
|
function save_screen_options( $status, $option, $value ) { |
|
|
|
|
295
|
|
|
if ( 'per_page' == $option ) { |
296
|
|
|
return $value; |
297
|
|
|
} else { |
298
|
|
|
return $status; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Gets the per page option. |
304
|
|
|
* TODO: Move to UI namespace |
305
|
|
|
* |
306
|
|
|
* @since Genesis |
307
|
|
|
* |
308
|
|
|
* @return int Number of logs a user wanted to be displayed in a page. |
309
|
|
|
*/ |
310
|
|
|
public static function get_per_page() { |
311
|
|
|
$screen = get_current_screen(); |
312
|
|
|
$option = $screen->get_option( 'per_page', 'option' ); |
313
|
|
|
|
314
|
|
|
$per_page = get_user_meta( get_current_user_id(), $option, true ); |
315
|
|
|
|
316
|
|
|
if ( empty( $per_page ) || $per_page < 1 ) { |
317
|
|
|
$per_page = $screen->get_option( 'per_page', 'default' ); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return $per_page; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Adds additional links. |
325
|
|
|
* TODO: Move to UI namespace |
326
|
|
|
* |
327
|
|
|
* @since Genesis |
328
|
|
|
* |
329
|
|
|
* @param array $links |
330
|
|
|
* @return array |
331
|
|
|
*/ |
332
|
|
|
public function add_action_links( $links ) { |
333
|
|
|
// Add a link to this plugin's settings page |
334
|
|
|
$settings_link = '<a href="tools.php?page=email-log">' . __( 'Log', 'email-log' ) . '</a>'; |
335
|
|
|
array_unshift( $links, $settings_link ); |
336
|
|
|
return $links; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Adds Footer links. |
341
|
|
|
* TODO: Move to UI namespace |
342
|
|
|
* |
343
|
|
|
* @since Genesis |
344
|
|
|
* |
345
|
|
|
* @see Function relied on |
346
|
|
|
* @link http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/ |
347
|
|
|
*/ |
348
|
|
|
public function add_footer_links() { |
349
|
|
|
$plugin_data = get_plugin_data( $this->plugin_file ); |
350
|
|
|
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'] ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Logs email to database. |
355
|
|
|
* |
356
|
|
|
* @since Genesis |
357
|
|
|
* |
358
|
|
|
* @global object $wpdb |
359
|
|
|
* |
360
|
|
|
* @param array $mail_info Information about email. |
361
|
|
|
* @return array Information about email. |
362
|
|
|
*/ |
363
|
|
|
public function log_email( $mail_info ) { |
364
|
|
|
global $wpdb; |
365
|
|
|
|
366
|
|
|
$attachment_present = ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false'; |
367
|
|
|
|
368
|
|
|
// return filtered array |
369
|
|
|
$mail_info = apply_filters( self::FILTER_NAME, $mail_info ); |
370
|
|
|
$table_name = $wpdb->prefix . self::TABLE_NAME; |
371
|
|
|
|
372
|
|
|
if ( isset( $mail_info['message'] ) ) { |
373
|
|
|
$message = $mail_info['message']; |
374
|
|
|
} else { |
375
|
|
|
// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20 |
376
|
|
|
// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them. |
377
|
|
|
if ( isset( $mail_info['html'] ) ) { |
378
|
|
|
$message = $mail_info['html']; |
379
|
|
|
} else { |
380
|
|
|
$message = ''; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
// Log into the database |
385
|
|
|
$wpdb->insert( $table_name, array( |
386
|
|
|
'to_email' => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'], |
387
|
|
|
'subject' => $mail_info['subject'], |
388
|
|
|
'message' => $message, |
389
|
|
|
'headers' => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'], |
390
|
|
|
'attachments' => $attachment_present, |
391
|
|
|
'sent_date' => current_time( 'mysql' ), |
392
|
|
|
) ); |
393
|
|
|
|
394
|
|
|
return $mail_info; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: