Passed
Pull Request — dev/2.5.0 (#244)
by Sudar
21:29 queued 18:00
created

LogListTable::column_star()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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

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

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