Completed
Pull Request — dev/2.5.0 (#244)
by Sudar
14:29 queued 09:41
created

LogListTable::get_views()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 11
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 12
rs 9.9
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
	 * @var string Log list type. Either 'All' or 'Starred'.
25
	 */
26
	protected $log_list_type;
27
28
	/**
29
	 * Set up a constructor that references the parent constructor.
30
	 *
31
	 * We use the parent reference to set some default configs.
32
	 *
33
	 * @param \EmailLog\Core\UI\Page\LogListPage $page
34
	 * @param mixed                              $args
35
	 */
36 1
	public function __construct( $page, $args = array() ) {
37 1
		$this->page = $page;
38
39 1
		$args = wp_parse_args( $args, array(
40 1
			'singular' => 'email-log',     // singular name of the listed records
41 1
			'plural'   => 'email-logs',    // plural name of the listed records
42
			'ajax'     => false,           // does this table support ajax?
43 1
			'screen'   => $this->page->get_screen(),
44
		) );
45
46 1
		parent::__construct( $args );
47
48 1
		$this->set_log_list_type();
49 1
	}
50
51
	/**
52
	 * Adds extra markup in the toolbars before or after the list.
53
	 *
54
	 * @access protected
55
	 *
56
	 * @param string $which Add the markup after (bottom) or before (top) the list.
57
	 */
58
	protected function extra_tablenav( $which ) {
59
		if ( 'top' == $which ) {
60
			/**
61
			 * Triggered before the logs list table is displayed.
62
			 *
63
			 * @since 2.2.5
64
			 * @since 2.4.0 Added $total_logs parameter
65
			 *
66
			 * @param int $total_logs Total number of logs.
67
			 */
68
			do_action( 'el_before_logs_list_table', $this->get_pagination_arg( 'total_items' ) );
69
		}
70
	}
71
72
	/**
73
	 * Returns the list of column and title names.
74
	 *
75
	 * @since 2.3.0 Retrieve Column labels using Utility methods.
76
	 * @since 2.3.2 Added `result` column.
77
	 * @since 2.4.0 Added `sent_status` column.
78
	 * @see WP_List_Table::single_row_columns()
79
	 *
80
	 * @uses \EmailLog\Util\get_column_label()
81
	 *
82
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
83
	 */
84 1
	public function get_columns() {
85
		$columns = array(
86 1
			'cb' => '<input type="checkbox" />',
87
		);
88
89 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'star' ) as $column ) {
90 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

90
			$columns[ $column ] = /** @scrutinizer ignore-call */ Util\get_column_label( $column );
Loading history...
91
		}
92
93
		/**
94
		 * Filter the email log list table columns.
95
		 *
96
		 * @since 2.0.0
97
		 *
98
		 * @param array $columns Columns of email log list table.
99
		 */
100 1
		return apply_filters( 'el_manage_log_columns', $columns );
101
	}
102
103
	/**
104
	 * Returns the list of columns.
105
	 *
106
	 * @access protected
107
	 *
108
	 * @return array<string,array<boolean|string>> An associative array containing all the columns
109
	 *                                             that should be sortable: 'slugs'=>array('data_values',bool).
110
	 */
111
	protected function get_sortable_columns() {
112
		$sortable_columns = array(
113
			'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
114
			'to_email'  => array( 'to_email', false ),
115
			'subject'   => array( 'subject', false ),
116
		);
117
118
		return $sortable_columns;
119
	}
120
121
	/**
122
	 * Returns value for default columns.
123
	 *
124
	 * @access protected
125
	 *
126
	 * @param object $item        Data object.
127
	 * @param string $column_name Column Name.
128
	 */
129
	protected function column_default( $item, $column_name ) {
130
		/**
131
		 * Display Email Log list table columns.
132
		 *
133
		 * @since 2.0
134
		 *
135
		 * @param string $column_name Column Name.
136
		 * @param object $item        Data object.
137
		 */
138
		do_action( 'el_display_log_columns', $column_name, $item );
139
	}
140
141
	/**
142
	 * Display sent date column.
143
	 *
144
	 * @access protected
145
	 *
146
	 * @param object $item Current item object.
147
	 *
148
	 * @return string Markup to be displayed for the column.
149
	 */
150
	protected function column_sent_date( $item ) {
151
		$email_date = mysql2date(
152
			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

152
			sprintf( __( '%s @ %s', 'email-log' ), /** @scrutinizer ignore-type */ get_option( 'date_format', 'F j, Y' ), 'g:i:s a' ),
Loading history...
153
			$item->sent_date
154
		);
155
156
		$actions = array();
157
158
		$content_ajax_url = add_query_arg(
159
			array(
160
				'action' => 'el-log-list-view-message',
161
				'log_id' => $item->id,
162
				'width'  => '800',
163
				'height' => '550',
164
			),
165
			'admin-ajax.php'
166
		);
167
168
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
169
			esc_url( $content_ajax_url ),
170
			__( 'Email Content', 'email-log' ),
171
			__( 'View Content', 'email-log' )
172
		);
173
174
		$delete_url = add_query_arg(
175
			array(
176
				'page'                   => $_REQUEST['page'],
177
				'action'                 => 'el-log-list-delete',
178
				$this->_args['singular'] => $item->id,
179
			)
180
		);
181
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
182
183
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
184
			esc_url( $delete_url ),
185
			__( 'Delete', 'email-log' )
186
		);
187
188
		/**
189
		 * This filter can be used to modify the list of row actions that are displayed.
190
		 *
191
		 * @since 1.8
192
		 *
193
		 * @param array  $actions List of actions.
194
		 * @param object $item    The current log item.
195
		 */
196
		$actions = apply_filters( 'el_row_actions', $actions, $item );
197
198
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
199
			/*$1%s*/ $email_date,
200
			/*$2%s*/ $item->id,
201
			/*$3%s*/ $this->row_actions( $actions )
202
		);
203
	}
204
205
	/**
206
	 * To field.
207
	 *
208
	 * @access protected
209
	 *
210
	 * @param object $item
211
	 *
212
	 * @return string
213
	 */
214
	protected function column_to_email( $item ) {
215
		/**
216
		 * Filters the `To` field before outputting on the table.
217
		 *
218
		 * @since 2.3.0
219
		 *
220
		 * @param string $email `To` field
221
		 */
222
		$email = apply_filters( 'el_log_list_column_to_email', esc_html( $item->to_email ) );
223
224
		return $email;
225
	}
226
227
	/**
228
	 * Subject field.
229
	 *
230
	 * @access protected
231
	 *
232
	 * @param object $item
233
	 *
234
	 * @return string
235
	 */
236
	protected function column_subject( $item ) {
237
		return esc_html( $item->subject );
238
	}
239
240
	/**
241
	 * Markup for action column.
242
	 *
243
	 * @access protected
244
	 *
245
	 * @param object $item
246
	 *
247
	 * @return string
248
	 */
249
	protected function column_cb( $item ) {
250
		return sprintf(
251
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
252
			/*$1%s*/ $this->_args['singular'],
253
			/*$2%s*/ $item->id
254
		);
255
	}
256
257
	/**
258
	 * Markup for Status column.
259
	 *
260
	 * @since 2.3.2
261
	 * @since 2.4.0 Output the error message as tooltip.
262
	 *
263
	 * @param object $item Email Log item.
264
	 *
265
	 * @return string Column markup.
266
	 */
267
	protected function column_result( $item ) {
268
		// For older records that does not have value in the result column,
269
		// $item->result will be null.
270
		if ( is_null( $item->result ) ) {
271
			return '';
272
		}
273
274
		$icon = \EmailLog\Util\get_failure_icon();
275
		if ( $item->result ) {
276
			$icon = \EmailLog\Util\get_success_icon();
277
		}
278
279
		if ( ! isset( $item->error_message ) ) {
280
			return $icon;
281
		}
282
283
		return sprintf(
284
			'<span class="%3$s" title="%2$s">%1$s</span>',
285
			$icon,
286
			esc_attr( $item->error_message ),
287
			'el-help'
288
		);
289
	}
290
291
	/**
292
	 * Display column to star Email logs.
293
	 *
294
	 * @since 2.4.0
295
	 *
296
	 * @param mixed $item
297
	 *
298
	 * @return string
299
	 */
300
	protected function column_star( $item ) {
301
		$current_user_id = get_current_user_id();
302
		$class           = 'dashicons-star-empty';
303
304
		$starred_ids = get_user_meta( $current_user_id, LogListPage::STARRED_LOGS_META_KEY, true );
305
306
		if ( ! empty( $starred_ids ) && in_array( $item->id, $starred_ids ) ) {
0 ignored issues
show
Bug introduced by
It seems like $starred_ids can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, 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

306
		if ( ! empty( $starred_ids ) && in_array( $item->id, /** @scrutinizer ignore-type */ $starred_ids ) ) {
Loading history...
307
			$class = 'dashicons-star-filled';
308
		}
309
310
		return sprintf(
311
			'<a class="el-star-email" href="%2$s" data-log-id="%3$s">%1$s</a>',
312
			sprintf( '<span class="dashicons %s"></span>', $class ),
313
			'#',
314
			$item->id
315
		);
316
	}
317
318
	/**
319
	 * Specify the list of bulk actions.
320
	 *
321
	 * @access protected
322
	 *
323
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
324
	 */
325
	protected function get_bulk_actions() {
326
		$actions = array(
327
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
328
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
329
		);
330
		$actions = apply_filters( 'el_bulk_actions', $actions );
331
332
		return $actions;
333
	}
334
335
	/**
336
	 * @inheritdoc
337
	 */
338
	protected function get_views() {
339
		return array(
340
			'all_logs'     => sprintf(
341
				'<a href="%2$s"%3$s>%1$s</a>',
342
				__( 'All', 'email-log' ),
343
				'admin.php?page=email-log&el_log_list_type=all',
344
				'all' === $this->log_list_type ? ' class="current"' : ''
345
			),
346
			'starred_logs' => sprintf(
347
				'<a href="%2$s"%3$s>%1$s</a>',
348
				__( 'Starred', 'email-log' ),
349
				'admin.php?page=email-log&el_log_list_type=starred',
350
				'starred' === $this->log_list_type ? ' class="current"' : ''
351
			),
352
		);
353
	}
354
355
	/**
356
	 * Sets the Log List type.
357
	 *
358
	 * Two types of views are avialable using the View Logs table - All & Starred.
359
	 *
360
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::__construct()
361
	 * @used-by \EmailLog\Core\UI\ListTable\LogListTable::get_views()
362
	 */
363 1
	protected function set_log_list_type() {
364 1
		$log_list_type = 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

364
		$log_list_type = sanitize_text_field( /** @scrutinizer ignore-call */ Util\el_array_get( $_REQUEST, 'el_log_list_type', 'all' ) );
Loading history...
365
366 1
		if ( 'starred' === $log_list_type ) {
367
			$this->log_list_type = 'starred';
368
		} else {
369 1
			$this->log_list_type = 'all';
370
		}
371 1
	}
372
373
	/**
374
	 * Prepare data for display.
375
	 */
376
	public function prepare_items() {
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
		if ( 'all' === $this->log_list_type ) {
384
			list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no );
385
386
			$this->items = $items;
387
		} else {
388
			$log_ids = get_user_meta(
389
				get_current_user_id(),
390
				LogListPage::STARRED_LOGS_META_KEY,
391
				true
392
			);
393
394
			if ( empty( $log_ids ) ) {
395
				$log_ids = array( 0 );
396
			}
397
398
			$additional_args = array(
399
				'output_type'     => OBJECT,
400
				'current_page_no' => $current_page_no,
401
				'per_page'        => $per_page,
402
			);
403
404
			$this->items  = $this->page->get_table_manager()->fetch_log_items_by_id( $log_ids, $additional_args );
405
			$total_items  = count( $log_ids );
406
		}
407
408
		// Register pagination options & calculations.
409
		$this->set_pagination_args( array(
410
			'total_items' => $total_items,
411
			'per_page'    => $per_page,
412
			'total_pages' => ceil( $total_items / $per_page ),
413
		) );
414
	}
415
416
	/**
417
	 * Displays default message when no items are found.
418
	 */
419
	public function no_items() {
420
		_e( 'Your email log is empty', 'email-log' );
421
	}
422
423
	/**
424
	 * Displays the search box.
425
	 *
426
	 * @since 2.0
427
	 *
428
	 * @param string $text     The 'submit' button label.
429
	 * @param string $input_id ID attribute value for the search input field.
430
	 */
431
	public function search_box( $text, $input_id ) {
432
		$input_text_id  = $input_id . '-search-input';
433
		$input_date_id  = $input_id . '-search-date-input';
434
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
435
436
		if ( ! empty( $_REQUEST['orderby'] ) )
437
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
438
		if ( ! empty( $_REQUEST['order'] ) )
439
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
440
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
441
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
442
		if ( ! empty( $_REQUEST['detached'] ) )
443
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
444
		?>
445
		<p class="search-box">
446
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
447
			<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' ); ?>" />
448
			<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' ); ?>" />
449
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
450
		</p>
451
		<?php
452
	}
453
}
454