Completed
Push — dev/2.5.0 ( ec1976...9862d9 )
by Sudar
03:52
created

LogListTable::get_columns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 17
ccs 5
cts 5
cp 1
crap 2
rs 10
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
	 * Started log item ids.
46
	 *
47
	 * @since 2.5.0
48
	 *
49
	 * @var array
50
	 */
51
	protected $stared_log_item_ids = [];
52
53
	/**
54
	 * Set up a constructor that references the parent constructor.
55
	 *
56
	 * We use the parent reference to set some default configs.
57
	 *
58
	 * @param \EmailLog\Core\UI\Page\LogListPage $page Page in which this table is rendered.
59
	 * @param array                              $args Args.
60
	 */
61 1
	public function __construct( $page, $args = array() ) {
62 1
		$this->page = $page;
63
64 1
		$args = wp_parse_args(
65 1
			$args,
66
			[
67 1
				'singular' => 'email-log',     // singular name of the listed records.
68 1
				'plural'   => 'email-logs',    // plural name of the listed records.
69
				'ajax'     => false,           // does this table support ajax?
70 1
				'screen'   => $this->page->get_screen(),
71
			]
72
		);
73
74 1
		parent::__construct( $args );
75
76 1
		$this->set_log_list_type();
77 1
	}
78
79
	/**
80
	 * Adds extra markup in the toolbars before or after the list.
81
	 *
82
	 * @access protected
83
	 *
84
	 * @param string $which Add the markup after (bottom) or before (top) the list.
85
	 */
86
	protected function extra_tablenav( $which ) {
87
		if ( 'top' == $which ) {
88
			/**
89
			 * Triggered before the logs list table is displayed.
90
			 *
91
			 * @since 2.2.5
92
			 * @since 2.4.0 Added $total_logs parameter
93
			 *
94
			 * @param int $total_logs Total number of logs.
95
			 */
96
			do_action( 'el_before_logs_list_table', $this->get_pagination_arg( 'total_items' ) );
97
		}
98
	}
99
100
	/**
101
	 * Returns the list of column and title names.
102
	 *
103
	 * @since 2.3.0 Retrieve Column labels using Utility methods.
104
	 * @since 2.3.2 Added `result` column.
105
	 * @since 2.4.0 Added `sent_status` column.
106
	 * @see WP_List_Table::single_row_columns()
107
	 *
108
	 * @uses \EmailLog\Util\get_column_label()
109
	 *
110
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
111
	 */
112 1
	public function get_columns() {
113
		$columns = array(
114 1
			'cb' => '<input type="checkbox" />',
115
		);
116
117 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'star' ) as $column ) {
118 1
			$columns[ $column ] = get_column_label( $column );
119
		}
120
121
		/**
122
		 * Filter the email log list table columns.
123
		 *
124
		 * @since 2.0.0
125
		 *
126
		 * @param array $columns Columns of email log list table.
127
		 */
128 1
		return apply_filters( 'el_manage_log_columns', $columns );
129
	}
130
131
	/**
132
	 * Returns the list of columns.
133
	 *
134
	 * @access protected
135
	 *
136
	 * @return array<string,array<boolean|string>> An associative array containing all the columns
137
	 *                                             that should be sortable: 'slugs'=>array('data_values',bool).
138
	 */
139
	protected function get_sortable_columns() {
140
		$sortable_columns = array(
141
			'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
142
			'to_email'  => array( 'to_email', false ),
143
			'subject'   => array( 'subject', false ),
144
		);
145
146
		return $sortable_columns;
147
	}
148
149
	/**
150
	 * Returns value for default columns.
151
	 *
152
	 * @access protected
153
	 *
154
	 * @param object $item        Data object.
155
	 * @param string $column_name Column Name.
156
	 */
157
	protected function column_default( $item, $column_name ) {
158
		/**
159
		 * Display Email Log list table columns.
160
		 *
161
		 * @since 2.0
162
		 *
163
		 * @param string $column_name Column Name.
164
		 * @param object $item        Data object.
165
		 */
166
		do_action( 'el_display_log_columns', $column_name, $item );
167
	}
168
169
	/**
170
	 * Display sent date column.
171
	 *
172
	 * @access protected
173
	 *
174
	 * @param object $item Current item object.
175
	 *
176
	 * @return string Markup to be displayed for the column.
177
	 */
178
	protected function column_sent_date( $item ) {
179
		$email_date = mysql2date(
180
			sprintf(
181
				/* translators: 1 Date of the log, 2 Time of the log */
182
				__( '%1$s @ %2$s', 'email-log' ),
183
				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

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