Completed
Pull Request — dev/2.5.0 (#244)
by Sudar
21:46 queued 12:27
created

LogListTable::no_items()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php namespace EmailLog\Core\UI\ListTable;
2
3
use \EmailLog\Core\UI\Page\LogListPage;
4
use EmailLog\Util;
5
6 1
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
class LogListTable extends \WP_List_Table {
16
	/**
17
	 * @var object The page where this table is rendered.
18
	 *
19
	 * @since 2.0
20
	 */
21
	protected $page;
22
23
	/**
24
	 * Log list type. Either 'All' or 'Starred'.
25
	 *
26
	 * @since 2.5.0
27
	 *
28
	 * @var string
29
	 */
30
	protected $log_list_type = 'all';
31
32
	/**
33
	 * Total number of log items.
34
	 *
35
	 * @since 2.5.0
36
	 *
37
	 * @var int
38
	 */
39
	protected $total_log_count = 0;
40
41
	/**
42
	 * Started log item ids.
43
	 *
44
	 * @since 2.5.0
45
	 *
46
	 * @var array
47
	 */
48
	protected $stared_log_item_ids = [];
49
50
	/**
51
	 * Set up a constructor that references the parent constructor.
52
	 *
53
	 * We use the parent reference to set some default configs.
54
	 *
55
	 * @param \EmailLog\Core\UI\Page\LogListPage $page
56
	 * @param mixed                              $args
57
	 */
58 1
	public function __construct( $page, $args = array() ) {
59 1
		$this->page = $page;
60
61 1
		$args = wp_parse_args( $args, array(
62 1
			'singular' => 'email-log',     // singular name of the listed records
63 1
			'plural'   => 'email-logs',    // plural name of the listed records
64
			'ajax'     => false,           // does this table support ajax?
65 1
			'screen'   => $this->page->get_screen(),
66
		) );
67
68 1
		parent::__construct( $args );
69
70 1
		$this->set_log_list_type();
71 1
	}
72
73
	/**
74
	 * Adds extra markup in the toolbars before or after the list.
75
	 *
76
	 * @access protected
77
	 *
78
	 * @param string $which Add the markup after (bottom) or before (top) the list.
79
	 */
80
	protected function extra_tablenav( $which ) {
81
		if ( 'top' == $which ) {
82
			/**
83
			 * Triggered before the logs list table is displayed.
84
			 *
85
			 * @since 2.2.5
86
			 * @since 2.4.0 Added $total_logs parameter
87
			 *
88
			 * @param int $total_logs Total number of logs.
89
			 */
90
			do_action( 'el_before_logs_list_table', $this->get_pagination_arg( 'total_items' ) );
91
		}
92
	}
93
94
	/**
95
	 * Returns the list of column and title names.
96
	 *
97
	 * @since 2.3.0 Retrieve Column labels using Utility methods.
98
	 * @since 2.3.2 Added `result` column.
99
	 * @since 2.4.0 Added `sent_status` column.
100
	 * @see WP_List_Table::single_row_columns()
101
	 *
102
	 * @uses \EmailLog\Util\get_column_label()
103
	 *
104
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
105
	 */
106 1
	public function get_columns() {
107
		$columns = array(
108 1
			'cb' => '<input type="checkbox" />',
109
		);
110
111 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'star' ) as $column ) {
112 1
			$columns[ $column ] = Util\get_column_label( $column );
0 ignored issues
show
Bug introduced by
The function get_column_label was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

112
			$columns[ $column ] = /** @scrutinizer ignore-call */ Util\get_column_label( $column );
Loading history...
113
		}
114
115
		/**
116
		 * Filter the email log list table columns.
117
		 *
118
		 * @since 2.0.0
119
		 *
120
		 * @param array $columns Columns of email log list table.
121
		 */
122 1
		return apply_filters( 'el_manage_log_columns', $columns );
123
	}
124
125
	/**
126
	 * Returns the list of columns.
127
	 *
128
	 * @access protected
129
	 *
130
	 * @return array<string,array<boolean|string>> An associative array containing all the columns
131
	 *                                             that should be sortable: 'slugs'=>array('data_values',bool).
132
	 */
133
	protected function get_sortable_columns() {
134
		$sortable_columns = array(
135
			'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
136
			'to_email'  => array( 'to_email', false ),
137
			'subject'   => array( 'subject', false ),
138
		);
139
140
		return $sortable_columns;
141
	}
142
143
	/**
144
	 * Returns value for default columns.
145
	 *
146
	 * @access protected
147
	 *
148
	 * @param object $item        Data object.
149
	 * @param string $column_name Column Name.
150
	 */
151
	protected function column_default( $item, $column_name ) {
152
		/**
153
		 * Display Email Log list table columns.
154
		 *
155
		 * @since 2.0
156
		 *
157
		 * @param string $column_name Column Name.
158
		 * @param object $item        Data object.
159
		 */
160
		do_action( 'el_display_log_columns', $column_name, $item );
161
	}
162
163
	/**
164
	 * Display sent date column.
165
	 *
166
	 * @access protected
167
	 *
168
	 * @param object $item Current item object.
169
	 *
170
	 * @return string Markup to be displayed for the column.
171
	 */
172
	protected function column_sent_date( $item ) {
173
		$email_date = mysql2date(
174
			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

174
			sprintf( __( '%s @ %s', 'email-log' ), /** @scrutinizer ignore-type */ get_option( 'date_format', 'F j, Y' ), 'g:i:s a' ),
Loading history...
175
			$item->sent_date
176
		);
177
178
		$actions = array();
179
180
		$content_ajax_url = add_query_arg(
181
			array(
182
				'action' => 'el-log-list-view-message',
183
				'log_id' => $item->id,
184
				'width'  => '800',
185
				'height' => '550',
186
			),
187
			'admin-ajax.php'
188
		);
189
190
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
191
			esc_url( $content_ajax_url ),
192
			__( 'Email Content', 'email-log' ),
193
			__( 'View Content', 'email-log' )
194
		);
195
196
		$delete_url = add_query_arg(
197
			array(
198
				'page'                   => $_REQUEST['page'],
199
				'action'                 => 'el-log-list-delete',
200
				$this->_args['singular'] => $item->id,
201
			)
202
		);
203
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
204
205
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
206
			esc_url( $delete_url ),
207
			__( 'Delete', 'email-log' )
208
		);
209
210
		/**
211
		 * This filter can be used to modify the list of row actions that are displayed.
212
		 *
213
		 * @since 1.8
214
		 *
215
		 * @param array  $actions List of actions.
216
		 * @param object $item    The current log item.
217
		 */
218
		$actions = apply_filters( 'el_row_actions', $actions, $item );
219
220
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
221
			/*$1%s*/ $email_date,
222
			/*$2%s*/ $item->id,
223
			/*$3%s*/ $this->row_actions( $actions )
224
		);
225
	}
226
227
	/**
228
	 * To field.
229
	 *
230
	 * @access protected
231
	 *
232
	 * @param object $item
233
	 *
234
	 * @return string
235
	 */
236
	protected function column_to_email( $item ) {
237
		/**
238
		 * Filters the `To` field before outputting on the table.
239
		 *
240
		 * @since 2.3.0
241
		 *
242
		 * @param string $email `To` field
243
		 */
244
		$email = apply_filters( 'el_log_list_column_to_email', esc_html( $item->to_email ) );
245
246
		return $email;
247
	}
248
249
	/**
250
	 * Subject field.
251
	 *
252
	 * @access protected
253
	 *
254
	 * @param object $item
255
	 *
256
	 * @return string
257
	 */
258
	protected function column_subject( $item ) {
259
		return esc_html( $item->subject );
260
	}
261
262
	/**
263
	 * Markup for action column.
264
	 *
265
	 * @access protected
266
	 *
267
	 * @param object $item
268
	 *
269
	 * @return string
270
	 */
271
	protected function column_cb( $item ) {
272
		return sprintf(
273
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
274
			/*$1%s*/ $this->_args['singular'],
275
			/*$2%s*/ $item->id
276
		);
277
	}
278
279
	/**
280
	 * Markup for Status column.
281
	 *
282
	 * @since 2.3.2
283
	 * @since 2.4.0 Output the error message as tooltip.
284
	 *
285
	 * @param object $item Email Log item.
286
	 *
287
	 * @return string Column markup.
288
	 */
289
	protected function column_result( $item ) {
290
		// For older records that does not have value in the result column,
291
		// $item->result will be null.
292
		if ( is_null( $item->result ) ) {
293
			return '';
294
		}
295
296
		$icon = \EmailLog\Util\get_failure_icon();
297
		if ( $item->result ) {
298
			$icon = \EmailLog\Util\get_success_icon();
299
		}
300
301
		if ( ! isset( $item->error_message ) ) {
302
			return $icon;
303
		}
304
305
		return sprintf(
306
			'<span class="%3$s" title="%2$s">%1$s</span>',
307
			$icon,
308
			esc_attr( $item->error_message ),
309
			'el-help'
310
		);
311
	}
312
313
	/**
314
	 * Display column to star Email logs.
315
	 *
316
	 * @since 2.5.0
317
	 *
318
	 * @param object $item Email Log item.
319
	 *
320
	 * @return string
321
	 */
322
	protected function column_star( $item ) {
323
		$starred_ids = $this->stared_log_item_ids;
324
325
		$class = 'dashicons-star-empty';
326
		if ( ! empty( $starred_ids ) && in_array( $item->id, $starred_ids ) ) {
327
			$class = 'dashicons-star-filled';
328
		}
329
330
		return sprintf(
331
			'<a class="el-star-email" href="%2$s" data-log-id="%3$s">%1$s</a>',
332
			sprintf( '<span class="dashicons %s"></span>', $class ),
333
			'#',
334
			$item->id
335
		);
336
	}
337
338
	/**
339
	 * Specify the list of bulk actions.
340
	 *
341
	 * @access protected
342
	 *
343
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
344
	 */
345
	protected function get_bulk_actions() {
346
		$actions = array(
347
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
348
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
349
		);
350
		$actions = apply_filters( 'el_bulk_actions', $actions );
351
352
		return $actions;
353
	}
354
355
	/**
356
	 * Sets the Log List type.
357
	 *
358
	 * Two types of views are available using the View Logs table - All & Starred.
359
	 *
360
	 * @since 2.5.0
361
	 *
362
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::__construct()
363
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::get_views()
364
	 */
365 1
	protected function set_log_list_type() {
366 1
		if ( 'starred' === sanitize_text_field( Util\el_array_get( $_REQUEST, 'el_log_list_type', 'all' ) ) ) {
0 ignored issues
show
Bug introduced by
The function el_array_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

366
		if ( 'starred' === sanitize_text_field( /** @scrutinizer ignore-call */ Util\el_array_get( $_REQUEST, 'el_log_list_type', 'all' ) ) ) {
Loading history...
367
			$this->log_list_type = 'starred';
368
		}
369 1
	}
370
371
	/**
372
	 * Prepare data for display.
373
	 */
374
	public function prepare_items() {
375
		$table_manager = $this->page->get_table_manager();
376
377
		$this->_column_headers = $this->get_column_info();
378
379
		// Get current page number.
380
		$current_page_no = $this->get_pagenum();
381
		$per_page        = $this->page->get_per_page();
382
383
		$this->total_log_count = $table_manager->get_logs_count();
384
385
		$this->stared_log_item_ids = $table_manager->get_starred_log_item_ids();
386
387
		if ( 'all' === $this->log_list_type ) {
388
			$this->items = $table_manager->fetch_log_items( $_GET, $per_page, $current_page_no );
389
			$total_items = $this->total_log_count;
390
		} else {
391
			$log_ids = $this->stared_log_item_ids;
392
			if ( empty( $log_ids ) ) {
393
				$log_ids = array( 0 );
394
			}
395
396
			$additional_args = array(
397
				'output_type'     => OBJECT,
398
				'current_page_no' => $current_page_no,
399
				'per_page'        => $per_page,
400
			);
401
402
			$this->items = $table_manager->fetch_log_items_by_id( $log_ids, $additional_args );
403
			$total_items = count( $this->stared_log_item_ids );
404
		}
405
406
		// Register pagination options & calculations.
407
		$this->set_pagination_args( array(
408
			'total_items' => $total_items,
409
			'per_page'    => $per_page,
410
			'total_pages' => ceil( $total_items / $per_page ),
411
		) );
412
	}
413
414
	/**
415
	 * @inheritdoc
416
	 */
417
	protected function get_views() {
418
		return [
419
			'all_logs'     => sprintf(
420
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
421
				__( 'All', 'email-log' ),
422
				$this->total_log_count,
423
				'admin.php?page=email-log&el_log_list_type=all',
424
				'all' === $this->log_list_type ? ' class="current"' : ''
425
			),
426
			'starred_logs' => sprintf(
427
				'<a href="%3$s"%4$s>%1$s (%2$d)</a>',
428
				__( 'Starred', 'email-log' ),
429
				count( $this->stared_log_item_ids ),
430
				'admin.php?page=email-log&el_log_list_type=starred',
431
				'starred' === $this->log_list_type ? ' class="current"' : ''
432
			),
433
		];
434
	}
435
436
	/**
437
	 * Displays default message when no items are found.
438
	 */
439
	public function no_items() {
440
		if ( 'starred' === $this->log_list_type ) {
441
			_e( 'Your have not starred any email logs yet.', 'email-log' );
442
		} else {
443
			_e( 'Your email log is empty.', 'email-log' );
444
		}
445
	}
446
447
	/**
448
	 * Displays the search box.
449
	 *
450
	 * @since 2.0
451
	 *
452
	 * @param string $text     The 'submit' button label.
453
	 * @param string $input_id ID attribute value for the search input field.
454
	 */
455
	public function search_box( $text, $input_id ) {
456
		$input_text_id  = $input_id . '-search-input';
457
		$input_date_id  = $input_id . '-search-date-input';
458
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
459
460
		if ( ! empty( $_REQUEST['orderby'] ) )
461
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
462
		if ( ! empty( $_REQUEST['order'] ) )
463
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
464
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
465
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
466
		if ( ! empty( $_REQUEST['detached'] ) )
467
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
468
		?>
469
		<p class="search-box">
470
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
471
			<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' ); ?>" />
472
			<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' ); ?>" />
473
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
474
		</p>
475
		<?php
476
	}
477
}
478