Completed
Push — master ( 8bffa3...d70731 )
by Sudar
06:01 queued 03:08
created

EmailLog::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 2
eloc 13
c 3
b 0
f 1
nc 2
nop 0
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 8.8571
1
<?php namespace EmailLog\Core;
2
3
use EmailLog\Core\DB\TableManager;
4
5
/**
6
 * The main plugin class.
7
 *
8
 * @since Genesis
9
 */
10
class EmailLog {
11
12
	/**
13
	 * Version number.
14
	 *
15
	 * @since Genesis
16
	 * @var const VERSION
17
	 */
18
	const VERSION = '1.9.1';
19
20
	/**
21
	 * Flag to track if the plugin is loaded.
22
	 *
23
	 * @since 2.0
24
	 * @var bool
25
	 */
26
	private $loaded;
27
28
	/**
29
	 * @var string Plugin file path.
30
	 *
31
	 * @since 2.0
32
	 */
33
	private $plugin_file;
34
35
	/**
36
	 * Filesystem directory path where translations are stored.
37
	 *
38
	 * @since 2.0
39
	 * @var string $translations_path
40
	 */
41
	public $translations_path;
42
43
	/**
44
	 * @var object Table Manager.
45
	 *
46
	 * @since 2.0
47
	 */
48
	public $table_manager;
49
50
	/**
51
	 * Admin screen object.
52
	 *
53
	 * @since Genesis
54
	 * @access private
55
	 * @var string $include_path
56
	 */
57
	private $admin_screen;
58
59
	/**
60
	 * Filter name.
61
	 *
62
	 * @since Genesis
63
	 * @var const FILTER_NAME
64
	 */
65
	const FILTER_NAME              = 'wp_mail_log';
66
67
	/**
68
	 * Page slug to be used in admin dashboard hyperlinks.
69
	 *
70
	 * @since Genesis
71
	 * @var const PAGE_SLUG
72
	 */
73
	const PAGE_SLUG                = 'email-log';
74
75
	/**
76
	 * String value to generate nonce.
77
	 *
78
	 * @since Genesis
79
	 * @var const DELETE_LOG_NONCE_FIELD
80
	 */
81
	const DELETE_LOG_NONCE_FIELD   = 'sm-delete-email-log-nonce';
82
83
	/**
84
	 * String value to generate nonce.
85
	 *
86
	 * @since Genesis
87
	 * @var const DELETE_LOG_ACTION
88
	 */
89
	const DELETE_LOG_ACTION        = 'sm-delete-email-log';
90
91
	// JS Stuff
92
	const JS_HANDLE                = 'email-log';
93
94
	//hooks
95
	const HOOK_LOG_COLUMNS         = 'email_log_manage_log_columns';
96
	const HOOK_LOG_DISPLAY_COLUMNS = 'email_log_display_log_columns';
97
98
	/**
99
	 * Initialize the plugin.
100
	 */
101 2
	public function __construct( $file ) {
102 2
		$this->plugin_file = $file;
103 2
		$this->translations_path = dirname( plugin_basename( $this->plugin_file ) ) . '/languages/' ;
104 2
	}
105
106
	/**
107
	 * Load the plugin.
108
	 */
109 2
	public function load() {
110 2
		if ( $this->loaded ) {
111
			return;
112
		}
113
114
		// Load localization domain.
115 2
		load_plugin_textdomain( 'email-log', false, $this->translations_path );
116
117
		// Register hooks.
118 2
		add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
119
120
		// Register Filter.
121 2
		add_filter( 'wp_mail', array( $this, 'log_email' ) );
122 2
		add_filter( 'set-screen-option', array( $this, 'save_screen_options' ), 10, 3 );
123 2
		add_filter( 'plugin_row_meta', array( $this, 'add_plugin_links' ), 10, 2 );
124
125 2
		$plugin = plugin_basename( $this->plugin_file );
126 2
		add_filter( "plugin_action_links_$plugin", array( $this, 'add_action_links' ) );
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $plugin instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
127
128
		// Add our ajax call.
129 2
		add_action( 'wp_ajax_display_content', array( $this, 'display_content_callback' ) );
130
131 2
		$this->table_manager->load();
132
133 2
		$this->loaded = true;
134 2
	}
135
136
	/**
137
	 * Adds additional links in the plugin listing page.
138
	 * TODO: Move to UI namespace
139
	 *
140
	 * @since Genesis
141
	 *
142
	 * @see Additional links in the Plugin listing is based on
143
	 * @link http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/
144
	 *
145
	 * @param array $links Array with default links to display in plugins page.
146
	 * @param string $file The name of the plugin file.
147
	 * @return array Array with links to display in plugins page.
148
	 */
149
	public function add_plugin_links( $links, $file ) {
150
		$plugin = plugin_basename( $this->plugin_file );
151
152
		if ( $file == $plugin ) {
153
			// only for this plugin
154
			return array_merge( $links,
155
				array( '<a href="http://sudarmuthu.com/wordpress/email-log/pro-addons" target="_blank">' . __( 'Buy Addons', 'email-log' ) . '</a>' )
156
			);
157
		}
158
		return $links;
159
	}
160
161
	/**
162
	 * Registers the settings page.
163
	 * TODO: Move to UI namespace
164
	 *
165
	 * @since Genesis
166
	 */
167
	public function register_settings_page() {
168
		// Save the handle to your admin page - you'll need it to create a WP_Screen object
169
		$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' ) );
0 ignored issues
show
Bug introduced by
The property admin_page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
170
171
		add_action( "load-{$this->admin_page}", array( $this, 'create_settings_panel' ) );
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
172
	}
173
174
	/**
175
	 * Displays the stored email log.
176
	 * TODO: Move to UI namespace
177
	 *
178
	 * @since Genesis
179
	 */
180
	public function display_logs() {
181
		add_thickbox();
182
183
		$this->logs_table->prepare_items( $this->get_per_page() );
0 ignored issues
show
Bug introduced by
The property logs_table does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
184
		?>
185
		<div class="wrap">
186
			<h2><?php _e( 'Email Logs', 'email-log' );?></h2>
187
			<?php
188
			if ( isset( $this->logs_deleted ) && $this->logs_deleted != '' ) {
189
				$logs_deleted = intval( $this->logs_deleted );
0 ignored issues
show
Bug introduced by
The property logs_deleted does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
190
191
				if ( $logs_deleted > 0 ) {
192
					echo '<div class="updated"><p>' . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . '</p></div>';
193
				} else {
194
					echo '<div class="updated"><p>' . __( 'There was some problem in deleting the email logs' , 'email-log' ) . '</p></div>';
195
				}
196
				unset( $this->logs_deleted );
197
			}
198
			?>
199
			<form id="email-logs-search" method="get">
200
				<input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" >
201
				<?php
202
				$this->logs_table->search_box( __( 'Search Logs', 'email-log' ), 'search_id' );
203
				?>
204
			</form>
205
206
			<form id="email-logs-filter" method="get">
207
				<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
208
				<?php
209
				wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD );
210
				$this->logs_table->display();
211
				?>
212
			</form>
213
		</div>
214
		<?php
215
		/**
216
		 * Action to add additional content to email log admin footer.
217
		 *
218
		 * @since 1.8
219
		 */
220
		do_action( 'el_admin_footer' );
221
222
		// Display credits in Footer
223
		add_action( 'in_admin_footer', array( $this, 'add_footer_links' ) );
224
	}
225
226
	/**
227
	 * Adds settings panel for the plugin.
228
	 * TODO: Move to UI namespace
229
	 *
230
	 * @since Genesis
231
	 */
232
	public function create_settings_panel() {
233
234
		/**
235
		 * Create the WP_Screen object against your admin page handle
236
		 * This ensures we're working with the right admin page
237
		 */
238
		$this->admin_screen = \WP_Screen::get( $this->admin_page );
239
240
		/**
241
		 * Content specified inline
242
		 */
243
		$this->admin_screen->add_help_tab(
244
			array(
245
				'title'    => __( 'About Plugin', 'email-log' ),
246
				'id'       => 'about_tab',
247
				'content'  => '<p>' . __( 'Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log' ) . '</p>',
248
				'callback' => false,
249
			)
250
		);
251
252
		// Add help sidebar
253
		$this->admin_screen->set_help_sidebar(
254
			'<p><strong>' . __( 'More information', 'email-log' ) . '</strong></p>' .
255
			'<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __( 'Plugin Homepage/support', 'email-log' ) . '</a></p>' .
256
			'<p><a href = "http://sudarmuthu.com/blog">' . __( "Plugin author's blog", 'email-log' ) . '</a></p>' .
257
			'<p><a href = "http://sudarmuthu.com/wordpress/">' . __( "Other Plugin's by Author", 'email-log' ) . '</a></p>'
258
		);
259
260
		// Add screen options
261
		$this->admin_screen->add_option(
262
			'per_page',
263
			array(
264
				'label' => __( 'Entries per page', 'email-log' ),
265
				'default' => 20,
266
				'option' => 'per_page',
267
			)
268
		);
269
270
		//Prepare Table of elements
271
		$this->logs_table = new UI\LogTable();
272
	}
273
274
	/**
275
	 * AJAX callback for displaying email content.
276
	 * TODO: Move to UI namespace
277
	 *
278
	 * @since 1.6
279
	 */
280
	public function display_content_callback() {
281
		global $wpdb;
282
283
		if ( current_user_can( 'manage_options' ) ) {
284
			$table_name = $wpdb->prefix . TableManager::TABLE_NAME;
285
			$email_id   = absint( $_GET['email_id'] );
286
287
			$query   = $wpdb->prepare( 'SELECT * FROM ' . $table_name . ' WHERE id = %d', $email_id );
288
			$content = $wpdb->get_results( $query );
289
290
			echo wpautop( $content[0]->message );
291
		}
292
293
		die(); // this is required to return a proper result
294
	}
295
296
	/**
297
	 * Saves Screen options.
298
	 * TODO: Move to UI namespace
299
	 *
300
	 * @since Genesis
301
	 *
302
	 * @param bool|int $status Screen option value. Default false to skip.
303
	 * @param string   $option The option name.
304
	 * @param int      $value  The number of rows to use.
305
	 * @return bool|int
306
	 */
307
	function save_screen_options( $status, $option, $value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
308
		if ( 'per_page' == $option ) {
309
			return $value;
310
		} else {
311
			return $status;
312
		}
313
	}
314
315
	/**
316
	 * Gets the per page option.
317
	 * TODO: Move to UI namespace
318
	 *
319
	 * @since Genesis
320
	 *
321
	 * @return int Number of logs a user wanted to be displayed in a page.
322
	 */
323
	public static function get_per_page() {
324
		$screen = get_current_screen();
325
		$option = $screen->get_option( 'per_page', 'option' );
326
327
		$per_page = get_user_meta( get_current_user_id(), $option, true );
328
329
		if ( empty( $per_page ) || $per_page < 1 ) {
330
			$per_page = $screen->get_option( 'per_page', 'default' );
331
		}
332
333
		return $per_page;
334
	}
335
336
	/**
337
	 * Adds additional links.
338
	 * TODO: Move to UI namespace
339
	 *
340
	 * @since Genesis
341
	 *
342
	 * @param array $links
343
	 * @return array
344
	 */
345
	public function add_action_links( $links ) {
346
		// Add a link to this plugin's settings page
347
		$settings_link = '<a href="tools.php?page=email-log">' . __( 'Log', 'email-log' ) . '</a>';
348
		array_unshift( $links, $settings_link );
349
		return $links;
350
	}
351
352
	/**
353
	 * Adds Footer links.
354
	 * TODO: Move to UI namespace
355
	 *
356
	 * @since Genesis
357
	 *
358
	 * @see Function relied on
359
	 * @link http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
360
	 */
361
	public function add_footer_links() {
362
		$plugin_data = get_plugin_data( $this->plugin_file );
363
		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'] );
364
	}
365
366
	/**
367
	 * Logs email to database.
368
	 *
369
	 * @since Genesis
370
	 *
371
	 * @global object $wpdb
372
	 *
373
	 * @param array $mail_info Information about email.
374
	 * @return array Information about email.
375
	 */
376
	public function log_email( $mail_info ) {
377
		global $wpdb;
378
379
		$attachment_present = ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false';
380
381
		// return filtered array
382
		$mail_info  = apply_filters( self::FILTER_NAME, $mail_info );
383
		$table_name = $wpdb->prefix . TableManager::TABLE_NAME;
384
385
		if ( isset( $mail_info['message'] ) ) {
386
			$message = $mail_info['message'];
387
		} else {
388
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
389
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
390
			if ( isset( $mail_info['html'] ) ) {
391
				$message = $mail_info['html'];
392
			} else {
393
				$message = '';
394
			}
395
		}
396
397
		// Log into the database
398
		$wpdb->insert( $table_name, array(
399
			'to_email'    => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'],
400
			'subject'     => $mail_info['subject'],
401
			'message'     => $message,
402
			'headers'     => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'],
403
			'attachments' => $attachment_present,
404
			'sent_date'   => current_time( 'mysql' ),
405
		) );
406
407
		return $mail_info;
408
	}
409
}
410