Passed
Pull Request — dev/2.5.0 (#244)
by
unknown
18:08 queued 15:14
created

LogListTable::extra_tablenav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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