Completed
Push — master ( e85e3d...0703ae )
by Sudar
03:41
created

LogListTable::column_to()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php namespace EmailLog\Core\UI;
2
3
use EmailLog\Core\DB\TableManager;
4
use EmailLog\Core\EmailLog as EmailLog;
5
6 2
if ( ! class_exists( 'WP_List_Table' ) ) {
7
	require_once ABSPATH . WPINC . '/class-wp-list-table.php';
8
}
9
10
/**
11
 * Table to display Email Logs.
12
 *
13
 * Based on Custom List Table Example by Matt Van Andel.
14
 *
15
 * @author  Sudar
16
 * @package Email Log
17
 */
18
class LogListTable extends \WP_List_Table {
19
20
	/**
21
	 * Set up a constructor that references the parent constructor.
22
	 *
23
	 * We use the parent reference to set some default configs.
24
	 */
25 2
	public function __construct( $args = array()) {
26 2
		$args = wp_parse_args( $args, array(
27 2
			'singular' => 'email-log',     // singular name of the listed records
28 2
			'plural'   => 'email-logs',    // plural name of the listed records
29 2
			'ajax'     => false,           // does this table support ajax?
30 2
			'screen'   => null,
31 2
		) );
32
33 2
		parent::__construct( $args );
34 2
	}
35
36
	/**
37
	 * Adds extra markup in the toolbars before or after the list.
38
	 *
39
	 * @access protected
40
	 *
41
	 * @param string $which Add the markup after (bottom) or before (top) the list.
42
	 */
43
	protected function extra_tablenav( $which ) {
44
		if ( 'top' == $which ) {
45
			// The code that goes before the table is here.
46
			echo '<span id = "el-pro-msg">';
47
			_e( 'More fields are available in Pro addon. ', 'email-log' );
48
			echo '<a href = "http://sudarmuthu.com/out/buy-email-log-more-fields-addon" style = "color:red">';
49
			_e( 'Buy Now', 'email-log' );
50
			echo '</a>';
51
			echo '</span>';
52
		}
53
54
		if ( 'bottom' == $which ) {
55
			// The code that goes after the table is here.
56
			echo '<p>&nbsp;</p>';
57
			echo '<p>&nbsp;</p>';
58
59
			echo '<p>';
60
			_e( 'The following are the list of pro addons that are currently available for purchase.', 'email-log' );
61
			echo '</p>';
62
63
			echo '<ul style="list-style:disc; padding-left:35px">';
64
65
			echo '<li>';
66
			echo '<strong>', __( 'Email Log - Resend Email', 'email-log' ), '</strong>', ' - ';
67
			echo __( 'Adds the ability to resend email from logs.', 'email-log' );
68
			echo ' <a href = "http://sudarmuthu.com/wordpress/email-log/pro-addons#resend-email-addon">', __( 'More Info', 'email-log' ), '</a>.';
69
			echo ' <a href = "http://sudarmuthu.com/out/buy-email-log-resend-email-addon">', __( 'Buy now', 'email-log' ), '</a>';
70
			echo '</li>';
71
72
			echo '<li>';
73
			echo '<strong>', __( 'Email Log - More fields', 'email-log' ), '</strong>', ' - ';
74
			echo __( 'Adds more fields (From, CC, BCC, Reply To, Attachment) to the logs page.', 'email-log' );
75
			echo ' <a href = "http://sudarmuthu.com/wordpress/email-log/pro-addons#more-fields-addon">', __( 'More Info', 'email-log' ), '</a>.';
76
			echo ' <a href = "http://sudarmuthu.com/out/buy-email-log-more-fields-addon">', __( 'Buy now', 'email-log' ), '</a>';
77
			echo '</li>';
78
79
			echo '<li>';
80
			echo '<strong>', __( 'Email Log - Forward Email', 'email-log' ), '</strong>', ' - ';
81
			echo __( 'This addon allows you to send a copy of all emails send from WordPress to another email address', 'email-log' );
82
			echo ' <a href = "http://sudarmuthu.com/wordpress/email-log/pro-addons#forward-email-addon">', __( 'More Info', 'email-log' ), '</a>.';
83
			echo ' <a href = "http://sudarmuthu.com/out/buy-email-log-forward-email-addon">', __( 'Buy now', 'email-log' ), '</a>';
84
			echo '</li>';
85
86
			echo '</ul>';
87
		}
88
	}
89
90
	/**
91
	 * Returns the list of column and title names.
92
	 *
93
	 * @see WP_List_Table::::single_row_columns()
94
	 *
95
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
96
	 */
97 2
	public function get_columns() {
98
		$columns = array(
99 2
			'cb'        => '<input type="checkbox" />', //Render a checkbox instead of text
100 2
			'sent_date' => __( 'Sent at', 'email-log' ),
101 2
			'to'        => __( 'To', 'email-log' ),
102 2
			'subject'   => __( 'Subject', 'email-log' ),
103 2
		);
104
105 2
		return apply_filters( EmailLog::HOOK_LOG_COLUMNS, $columns );
106
	}
107
108
	/**
109
	 * Returns the list of columns.
110
	 *
111
	 * @access protected
112
	 *
113
	 * @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values',bool).
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string|boolean>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
114
	 */
115
	protected function get_sortable_columns() {
116
		$sortable_columns = array(
117
			'sent_date'   => array( 'sent_date', true ), //true means it's already sorted
118
			'to'          => array( 'to_email', false ),
119
			'subject'     => array( 'subject', false ),
120
		);
121
		return $sortable_columns;
122
	}
123
124
	/**
125
	 * Returns value for default columns.
126
	 *
127
	 * @access protected
128
	 *
129
	 * @param object $item
130
	 * @param string $column_name
131
	 */
132
	protected function column_default( $item, $column_name ) {
133
		do_action( EmailLog::HOOK_LOG_DISPLAY_COLUMNS, $column_name, $item );
134
	}
135
136
	/**
137
	 * Display sent date column.
138
	 *
139
	 * @access protected
140
	 *
141
	 * @param  object $item Current item object.
142
	 * @return string       Markup to be displayed for the column.
143
	 */
144
	protected function column_sent_date( $item ) {
145
		$email_date = mysql2date(
146
			sprintf( __( '%s @ %s', 'email-log' ), get_option( 'date_format', 'F j, Y' ), get_option( 'time_format', 'g:i A' ) ),
147
			$item->sent_date
148
		);
149
150
		$actions = array();
151
152
		$content_ajax_url = add_query_arg(
153
			array(
154
				'action'    => 'display_content',
155
				'email_id'  => $item->id,
156
				'TB_iframe' => 'true',
157
				'width'     => '600',
158
				'height'    => '550',
159
			),
160
			'admin-ajax.php'
161
		);
162
163
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
164
			esc_url( $content_ajax_url ),
165
			__( 'Email Content', 'email-log' ),
166
			__( 'View Content', 'email-log' )
167
		);
168
169
		$delete_url = add_query_arg(
170
			array(
171
				'page'                           => $_REQUEST['page'],
172
				'action'                         => 'delete',
173
				$this->_args['singular']         => $item->id,
174
				EmailLog::DELETE_LOG_NONCE_FIELD => wp_create_nonce( EmailLog::DELETE_LOG_ACTION ),
175
			)
176
		);
177
178
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
179
			esc_url( $delete_url ),
180
			__( 'Delete', 'email-log' )
181
		);
182
183
		/**
184
		 * This filter can be used to modify the list of row actions that are displayed.
185
		 *
186
		 * @since 1.8
187
		 *
188
		 * @param array $actions List of actions.
189
		 * @param object $item The current log item.
190
		 */
191
		$actions = apply_filters( 'el_row_actions', $actions, $item );
192
193
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
194
			/*$1%s*/ $email_date,
195
			/*$2%s*/ $item->id,
196
			/*$3%s*/ $this->row_actions( $actions )
197
		);
198
	}
199
200
	/**
201
	 * To field.
202
	 *
203
	 * @access protected
204
	 *
205
	 * @param object $item
206
	 * @return string
207
	 */
208
	protected function column_to( $item ) {
209
		return esc_html( $item->to_email );
210
	}
211
212
	/**
213
	 * Subject field.
214
	 *
215
	 * @access protected
216
	 *
217
	 * @param object $item
218
	 * @return string
219
	 */
220
	protected function column_subject( $item ) {
221
		return esc_html( $item->subject );
222
	}
223
224
	/**
225
	 * Markup for action column.
226
	 *
227
	 * @access protected
228
	 *
229
	 * @param object $item
230
	 * @return string
231
	 */
232
	protected function column_cb( $item ) {
233
		return sprintf(
234
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
235
			/*$1%s*/ $this->_args['singular'],
236
			/*$2%s*/ $item->id
237
		);
238
	}
239
240
	/**
241
	 * Specify the list of bulk actions.
242
	 *
243
	 * @access protected
244
	 *
245
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
246
	 */
247
	protected function get_bulk_actions() {
248
		$actions = array(
249
			'delete'     => __( 'Delete', 'email-log' ),
250
			'delete-all' => __( 'Delete All Logs', 'email-log' ),
251
		);
252
		return $actions;
253
	}
254
255
	/**
256
	 * Handles bulk actions.
257
	 *
258
	 * @see $this->prepare_items()
259
	 */
260
	public function process_bulk_action() {
261
		global $wpdb;
262
263
		$email_log = email_log();
264
265
		if ( 'delete' === $this->current_action() ) {
266
			// Delete a list of logs by id.
267
268
			$nonce = $_REQUEST[ EmailLog::DELETE_LOG_NONCE_FIELD ];
269
			if ( wp_verify_nonce( $nonce, EmailLog::DELETE_LOG_ACTION ) ) {
270
271
				$ids = $_GET[ $this->_args['singular'] ];
272
273
				if ( is_array( $ids ) ) {
274
					$selected_ids = implode( ',', $ids );
275
				} else {
276
					$selected_ids = $ids;
277
				}
278
279
				// Can't use wpdb->prepare for the below query. If used it results in this bug
280
				// https://github.com/sudar/email-log/issues/13
281
282
				$selected_ids = esc_sql( $selected_ids );
283
284
				$table_name = $wpdb->prefix . TableManager::LOG_TABLE_NAME;
285
				// TODO: move this logic away from Email Log class
286
				$email_log->logs_deleted = $wpdb->query( "DELETE FROM $table_name where id IN ( $selected_ids )" ); //@codingStandardsIgnoreLine
287
			} else {
288
				wp_die( 'Cheating, Huh? ' );
289
			}
290
		} elseif ( 'delete-all' === $this->current_action() ) {
291
			// Delete all logs.
292
			$nonce = $_REQUEST[ EmailLog::DELETE_LOG_NONCE_FIELD ];
293
			if ( wp_verify_nonce( $nonce, EmailLog::DELETE_LOG_ACTION ) ) {
294
				$table_name = $wpdb->prefix . TableManager::LOG_TABLE_NAME;
295
				$email_log->logs_deleted = $wpdb->query( "DELETE FROM $table_name" ); //@codingStandardsIgnoreLine
296
			} else {
297
				wp_die( 'Cheating, Huh? ' );
298
			}
299
		}
300
	}
301
302
	/**
303
	 * Prepare data for display.
304
	 */
305
	public function prepare_items() {
306
		global $wpdb;
307
308
		$table_name = $wpdb->prefix . TableManager::LOG_TABLE_NAME;
309
		$this->_column_headers = $this->get_column_info();
310
311
		// Handle bulk actions.
312
		$this->process_bulk_action();
313
314
		// Get current page number.
315
		$current_page = $this->get_pagenum();
316
317
		$query = 'SELECT * FROM ' . $table_name;
318
		$count_query = 'SELECT count(*) FROM ' . $table_name;
319
		$query_cond = '';
320
321
		if ( isset( $_GET['s'] ) ) {
322
			$search_term = trim( esc_sql( $_GET['s'] ) );
323
			$query_cond .= " WHERE to_email LIKE '%$search_term%' OR subject LIKE '%$search_term%' ";
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $search_term 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...
324
		}
325
326
		// Ordering parameters.
327
		$orderby = ! empty( $_GET['orderby'] ) ? esc_sql( $_GET['orderby'] ) : 'sent_date';
328
		$order   = ! empty( $_GET['order'] ) ? esc_sql( $_GET['order'] ) : 'DESC';
329
330
		if ( ! empty( $orderby ) & ! empty( $order ) ) {
331
			$query_cond .= ' ORDER BY ' . $orderby . ' ' . $order;
332
		}
333
334
		// Find total number of items.
335
		$count_query = $count_query . $query_cond;
336
		$total_items = $wpdb->get_var( $count_query );
337
338
		// Adjust the query to take pagination into account.
339
		$per_page = EmailLog::get_per_page();
340
		if ( ! empty( $current_page ) && ! empty( $per_page ) ) {
341
			$offset = ( $current_page - 1 ) * $per_page;
342
			$query_cond .= ' LIMIT ' . (int) $offset . ',' . (int) $per_page;
343
		}
344
345
		// Fetch the items.
346
		$query = $query . $query_cond;
347
		$this->items = $wpdb->get_results( $query );
348
349
		// Register pagination options & calculations.
350
		$this->set_pagination_args( array(
351
			'total_items' => $total_items,
352
			'per_page'    => $per_page,
353
			'total_pages' => ceil( $total_items / $per_page ),
354
		) );
355
	}
356
357
	/**
358
	 * Displays default message when no items are found.
359
	 */
360
	public function no_items() {
361
		_e( 'Your email log is empty', 'email-log' );
362
	}
363
}
364