Passed
Push — dev/2.5.0 ( 544bec...ca0923 )
by Sudar
02:59
created

LogListTable::prepare_items()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
nc 5
nop 0
dl 0
loc 44
ccs 0
cts 27
cp 0
crap 42
rs 8.8657
c 1
b 0
f 0
1
<?php namespace EmailLog\Core\UI\ListTable;
2
3
use function EmailLog\Util\el_array_get;
4
use function EmailLog\Util\get_column_label;
5
use function EmailLog\Util\get_display_format_for_log_time;
6
use function EmailLog\Util\get_failure_icon;
7
use function EmailLog\Util\get_success_icon;
8
9 1
if ( ! class_exists( 'WP_List_Table' ) ) {
10
	require_once ABSPATH . WPINC . '/class-wp-list-table.php';
11
}
12
13
/**
14
 * Table to display Email Logs.
15
 *
16
 * Based on Custom List Table Example by Matt Van Andel.
17
 */
18
class LogListTable extends \WP_List_Table {
19
	/**
20
	 * @var object The page where this table is rendered.
21
	 *
22
	 * @since 2.0
23
	 */
24
	protected $page;
25
26
	/**
27
	 * Log list type. Either 'All' or 'Starred'.
28
	 *
29
	 * @since 2.5.0
30
	 *
31
	 * @var string
32
	 */
33
	protected $log_list_type = 'all';
34
35
	/**
36
	 * Total number of log items.
37
	 *
38
	 * @since 2.5.0
39
	 *
40
	 * @var int
41
	 */
42
	protected $total_log_count = 0;
43
44
	/**
45
	 * Total number of sent log items.
46
	 *
47
	 * @since 2.5.0
48
	 *
49
	 * @var int
50
	 */
51
	protected $total_sent_log_count = 0;
52
53
	/**
54
	 * Total number of failed log items.
55
	 *
56
	 * @since 2.5.0
57
	 *
58
	 * @var int
59
	 */
60
	protected $total_failed_log_count = 0;
61
62
	/**
63
	 * Starred log item ids.
64
	 *
65
	 * @since 2.5.0
66
	 *
67
	 * @var array
68
	 */
69
	protected $stared_log_item_ids = [];
70
71
	/**
72
	 * Set up a constructor that references the parent constructor.
73
	 *
74
	 * We use the parent reference to set some default configs.
75
	 *
76
	 * @param \EmailLog\Core\UI\Page\LogListPage $page Page in which this table is rendered.
77
	 * @param array                              $args Args.
78
	 */
79 1
	public function __construct( $page, $args = array() ) {
80 1
		$this->page = $page;
81
82 1
		$args = wp_parse_args(
83 1
			$args,
84
			[
85 1
				'singular' => 'email-log',     // singular name of the listed records.
86 1
				'plural'   => 'email-logs',    // plural name of the listed records.
87
				'ajax'     => false,           // does this table support ajax?
88 1
				'screen'   => $this->page->get_screen(),
89
			]
90
		);
91
92 1
		parent::__construct( $args );
93
94 1
		$this->set_log_list_type();
95 1
	}
96
97
	/**
98
	 * Adds extra markup in the toolbars before or after the list.
99
	 *
100
	 * @access protected
101
	 *
102
	 * @param string $which Add the markup after (bottom) or before (top) the list.
103
	 */
104
	protected function extra_tablenav( $which ) {
105
		if ( 'top' == $which ) {
106
			/**
107
			 * Triggered before the logs list table is displayed.
108
			 *
109
			 * @since 2.2.5
110
			 * @since 2.4.0 Added $total_logs parameter
111
			 *
112
			 * @param int $total_logs Total number of logs.
113
			 */
114
			do_action( 'el_before_logs_list_table', $this->get_pagination_arg( 'total_items' ) );
115
		}
116
	}
117
118
	/**
119
	 * Returns the list of column and title names.
120
	 *
121
	 * @since 2.3.0 Retrieve Column labels using Utility methods.
122
	 * @since 2.3.2 Added `result` column.
123
	 * @since 2.4.0 Added `sent_status` column.
124
	 * @see WP_List_Table::single_row_columns()
125
	 *
126
	 * @uses \EmailLog\Util\get_column_label()
127
	 *
128
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
129
	 */
130 1
	public function get_columns() {
131
		$columns = array(
132 1
			'cb' => '<input type="checkbox" />',
133
		);
134
135 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'star' ) as $column ) {
136 1
			$columns[ $column ] = get_column_label( $column );
137
		}
138
139
		/**
140
		 * Filter the email log list table columns.
141
		 *
142
		 * @since 2.0.0
143
		 *
144
		 * @param array $columns Columns of email log list table.
145
		 */
146 1
		return apply_filters( 'el_manage_log_columns', $columns );
147
	}
148
149
	/**
150
	 * Returns the list of columns.
151
	 *
152
	 * @access protected
153
	 *
154
	 * @return array<string,array<boolean|string>> An associative array containing all the columns
155
	 *                                             that should be sortable: 'slugs'=>array('data_values',bool).
156
	 */
157
	protected function get_sortable_columns() {
158
		$sortable_columns = array(
159
			'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
160
			'to_email'  => array( 'to_email', false ),
161
			'subject'   => array( 'subject', false ),
162
		);
163
164
		return $sortable_columns;
165
	}
166
167
	/**
168
	 * Returns value for default columns.
169
	 *
170
	 * @access protected
171
	 *
172
	 * @param object $item        Data object.
173
	 * @param string $column_name Column Name.
174
	 */
175
	protected function column_default( $item, $column_name ) {
176
		/**
177
		 * Display Email Log list table columns.
178
		 *
179
		 * @since 2.0
180
		 *
181
		 * @param string $column_name Column Name.
182
		 * @param object $item        Data object.
183
		 */
184
		do_action( 'el_display_log_columns', $column_name, $item );
185
	}
186
187
	/**
188
	 * Display sent date column.
189
	 *
190
	 * @access protected
191
	 *
192
	 * @param object $item Current item object.
193
	 *
194
	 * @return string Markup to be displayed for the column.
195
	 */
196
	protected function column_sent_date( $item ) {
197
		$email_date = mysql2date(
198
			sprintf(
199
				/* translators: 1 Date of the log, 2 Time of the log */
200
				__( '%1$s @ %2$s', 'email-log' ),
201
				get_option( 'date_format', 'F j, Y' ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format', 'F j, Y') can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
				/** @scrutinizer ignore-type */ get_option( 'date_format', 'F j, Y' ),
Loading history...
202
				get_display_format_for_log_time()
203
			),
204
			$item->sent_date
205
		);
206
207
		$actions = array();
208
209
		$content_ajax_url = add_query_arg(
210
			array(
211
				'action' => 'el-log-list-view-message',
212
				'log_id' => $item->id,
213
				'width'  => '800',
214
				'height' => '550',
215
			),
216
			'admin-ajax.php'
217
		);
218
219
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
220
			esc_url( $content_ajax_url ),
221
			__( 'Email Content', 'email-log' ),
222
			__( 'View Content', 'email-log' )
223
		);
224
225
		$delete_url = add_query_arg(
226
			array(
227
				'page'                   => $_REQUEST['page'],
228
				'action'                 => 'el-log-list-delete',
229
				$this->_args['singular'] => $item->id,
230
			)
231
		);
232
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
233
234
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
235
			esc_url( $delete_url ),
236
			__( 'Delete', 'email-log' )
237
		);
238
239
		/**
240
		 * This filter can be used to modify the list of row actions that are displayed.
241
		 *
242
		 * @since 1.8
243
		 *
244
		 * @param array  $actions List of actions.
245
		 * @param object $item    The current log item.
246
		 */
247
		$actions = apply_filters( 'el_row_actions', $actions, $item );
248
249
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
250
			/*$1%s*/ $email_date,
251
			/*$2%s*/ $item->id,
252
			/*$3%s*/ $this->row_actions( $actions )
253
		);
254
	}
255
256
	/**
257
	 * To field.
258
	 *
259
	 * @access protected
260
	 *
261
	 * @param object $item
262
	 *
263
	 * @return string
264
	 */
265
	protected function column_to_email( $item ) {
266
		/**
267
		 * Filters the `To` field before outputting on the table.
268
		 *
269
		 * @since 2.3.0
270
		 *
271
		 * @param string $email `To` field
272
		 */
273
		$email = apply_filters( 'el_log_list_column_to_email', esc_html( $item->to_email ) );
274
275
		return $email;
276
	}
277
278
	/**
279
	 * Subject field.
280
	 *
281
	 * @access protected
282
	 *
283
	 * @param object $item
284
	 *
285
	 * @return string
286
	 */
287
	protected function column_subject( $item ) {
288
		return esc_html( $item->subject );
289
	}
290
291
	/**
292
	 * Markup for action column.
293
	 *
294
	 * @access protected
295
	 *
296
	 * @param object $item
297
	 *
298
	 * @return string
299
	 */
300
	protected function column_cb( $item ) {
301
		return sprintf(
302
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
303
			/*$1%s*/ $this->_args['singular'],
304
			/*$2%s*/ $item->id
305
		);
306
	}
307
308
	/**
309
	 * Markup for Status column.
310
	 *
311
	 * @since 2.3.2
312
	 * @since 2.4.0 Output the error message as tooltip.
313
	 *
314
	 * @param object $item Email Log item.
315
	 *
316
	 * @return string Column markup.
317
	 */
318
	protected function column_result( $item ) {
319
		// For older records that does not have value in the result column,
320
		// $item->result will be null.
321
		if ( is_null( $item->result ) ) {
322
			return '';
323
		}
324
325
		$icon = get_failure_icon();
326
		if ( $item->result ) {
327
			$icon = get_success_icon();
328
		}
329
330
		if ( ! isset( $item->error_message ) ) {
331
			return $icon;
332
		}
333
334
		return sprintf(
335
			'<span class="%3$s" title="%2$s">%1$s</span>',
336
			$icon,
337
			esc_attr( $item->error_message ),
338
			'el-help'
339
		);
340
	}
341
342
	/**
343
	 * Display column to star Email logs.
344
	 *
345
	 * @since 2.5.0
346
	 *
347
	 * @param object $item Email Log item.
348
	 *
349
	 * @return string
350
	 */
351
	protected function column_star( $item ) {
352
		$starred_ids = $this->stared_log_item_ids;
353
354
		$class = 'dashicons-star-empty';
355
		if ( ! empty( $starred_ids ) && in_array( $item->id, $starred_ids ) ) {
356
			$class = 'dashicons-star-filled';
357
		}
358
359
		return sprintf(
360
			'<a class="el-star-email" href="#" data-log-id="%2$s">%1$s</a> <img class="el-star-spinner" src="%3$s">',
361
			sprintf( '<span class="dashicons %s"></span>', $class ),
362
			$item->id,
363
			includes_url( 'images/spinner.gif' )
364
		);
365
	}
366
367
	/**
368
	 * Specify the list of bulk actions.
369
	 *
370
	 * @access protected
371
	 *
372
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
373
	 */
374
	protected function get_bulk_actions() {
375
		$actions = array(
376
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
377
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
378
		);
379
		$actions = apply_filters( 'el_bulk_actions', $actions );
380
381
		return $actions;
382
	}
383
384
	/**
385
	 * Sets the Log List type.
386
	 *
387
	 * Two types of views are available using the View Logs table - All & Starred.
388
	 *
389
	 * @since 2.5.0
390
	 *
391
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::__construct()
392
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::get_views()
393
	 */
394 1
	protected function set_log_list_type() {
395 1
		$this->log_list_type = sanitize_text_field( el_array_get( $_REQUEST, 'el_log_list_type', 'all' ) );
396 1
	}
397
398
	/**
399
	 * Prepare data for display.
400
	 */
401
	public function prepare_items() {
402
		$table_manager = $this->page->get_table_manager();
403
404
		$this->_column_headers = $this->get_column_info();
405
406
		// Get current page number.
407
		$current_page_no = $this->get_pagenum();
408
		$per_page        = $this->page->get_per_page();
409
410
		$this->total_log_count = $table_manager->get_logs_count();
411
412
		$this->stared_log_item_ids = $table_manager->get_starred_log_item_ids();
413
414
		$this->total_sent_log_count = $table_manager->get_result_logs_count( [ 'el_log_list_type' => 'sent' ] );
415
416
		$this->total_failed_log_count = $table_manager->get_result_logs_count( [ 'el_log_list_type' => 'failed' ] );
417
418
		$total_items = $table_manager->get_result_logs_count( $_GET );
419
420
		if ( 'all' === $this->log_list_type ) {
421
			$this->items = $table_manager->fetch_log_items( $_GET, $per_page, $current_page_no );
422
		} elseif ( 'starred' === $this->log_list_type ) {
423
			$log_ids = $this->stared_log_item_ids;
424
			if ( empty( $log_ids ) ) {
425
				$log_ids = array( 0 );
426
			}
427
428
			$additional_args = array(
429
				'output_type'     => OBJECT,
430
				'current_page_no' => $current_page_no,
431
				'per_page'        => $per_page,
432
			);
433
434
			$this->items = $table_manager->fetch_log_items_by_id( $log_ids, $additional_args );
435
			$total_items = count( $this->stared_log_item_ids );
436
		} elseif ( 'sent' === $this->log_list_type || 'failed' === $this->log_list_type ) {
437
			$this->items = $table_manager->fetch_log_items( $_GET, $per_page, $current_page_no );
438
		}
439
440
		// Register pagination options & calculations.
441
		$this->set_pagination_args( array(
442
			'total_items' => $total_items,
443
			'per_page'    => $per_page,
444
			'total_pages' => ceil( $total_items / $per_page ),
445
		) );
446
	}
447
448
	/**
449
	 * @inheritdoc
450
	 */
451
	protected function get_views() {
452
		return [
453
			'all_logs'     => sprintf(
454
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
455
				__( 'All', 'email-log' ),
456
				$this->total_log_count,
457
				'admin.php?page=email-log&el_log_list_type=all',
458
				'all' === $this->log_list_type ? ' class="current"' : ''
459
			),
460
			'starred_logs' => sprintf(
461
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
462
				__( 'Starred', 'email-log' ),
463
				count( $this->stared_log_item_ids ),
464
				'admin.php?page=email-log&el_log_list_type=starred',
465
				'starred' === $this->log_list_type ? ' class="current"' : ''
466
			),
467
			'sent_logs'    => sprintf(
468
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
469
				__( 'Sent', 'email-log' ),
470
				$this->total_sent_log_count,
471
				'admin.php?page=email-log&el_log_list_type=sent',
472
				'sent' === $this->log_list_type ? ' class="current"' : ''
473
			),
474
			'failed_logs'  => sprintf(
475
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
476
				__( 'Failed', 'email-log' ),
477
				$this->total_failed_log_count,
478
				'admin.php?page=email-log&el_log_list_type=failed',
479
				'failed' === $this->log_list_type ? ' class="current"' : ''
480
			),
481
		];
482
	}
483
484
	/**
485
	 * Displays default message when no items are found.
486
	 */
487
	public function no_items() {
488
		if ( 'starred' === $this->log_list_type ) {
489
			_e( 'Your have not starred any email logs yet.', 'email-log' );
490
		} else {
491
			_e( 'Your email log is empty.', 'email-log' );
492
		}
493
	}
494
495
	/**
496
	 * Displays the search box.
497
	 *
498
	 * @since 2.0
499
	 *
500
	 * @param string $text     The 'submit' button label.
501
	 * @param string $input_id ID attribute value for the search input field.
502
	 */
503
	public function search_box( $text, $input_id ) {
504
		$input_text_id  = $input_id . '-search-input';
505
		$input_date_id  = $input_id . '-search-date-input';
506
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
507
508
		if ( ! empty( $_REQUEST['orderby'] ) )
509
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
510
		if ( ! empty( $_REQUEST['order'] ) )
511
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
512
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
513
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
514
		if ( ! empty( $_REQUEST['detached'] ) )
515
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
516
		?>
517
		<p class="search-box">
518
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
519
			<input type="search" id="<?php echo esc_attr( $input_date_id ); ?>" name="d" value="<?php echo $input_date_val; ?>" placeholder="<?php _e( 'Search by date', 'email-log' ); ?>" />
520
			<input type="search" id="<?php echo esc_attr( $input_text_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php _e( 'Search by term', 'email-log' ); ?>" />
521
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
522
		</p>
523
		<?php
524
	}
525
}
526