Completed
Pull Request — dev/2.5.0 (#244)
by Sudar
15:53 queued 09:51
created

LogListTable::column_star()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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

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

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

360
		$log_list_type = esc_attr( /** @scrutinizer ignore-call */ Util\el_array_get( $_REQUEST, 'el_log_list_type', '' ) );
Loading history...
361 1
		switch ( $log_list_type ) {
362 1
			case 'starred':
363
				$this->log_list_type = 'starred';
364
				break;
365
			default:
366 1
				$this->log_list_type = 'all';
367
		}
368 1
	}
369
370
	/**
371
	 * Prepare data for display.
372
	 */
373
	public function prepare_items() {
374
		$this->_column_headers = $this->get_column_info();
375
376
		// Get current page number.
377
		$current_page_no = $this->get_pagenum();
378
		$per_page        = $this->page->get_per_page();
379
380
		if ( 'all' === $this->log_list_type ) {
381
			list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no );
382
383
			$this->items = $items;
384
		} else {
385
			$log_ids = get_user_meta(
386
				get_current_user_id(),
387
				LogListPage::STARRED_LOGS_META_KEY,
388
				true
389
			);
390
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  = $this->page->get_table_manager()->fetch_log_items_by_id( $log_ids, $additional_args );
402
			$total_items  = count( $log_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
	 * Displays default message when no items are found.
415
	 */
416
	public function no_items() {
417
		_e( 'Your email log is empty', 'email-log' );
418
	}
419
420
	/**
421
	 * Displays the search box.
422
	 *
423
	 * @since 2.0
424
	 *
425
	 * @param string $text     The 'submit' button label.
426
	 * @param string $input_id ID attribute value for the search input field.
427
	 */
428
	public function search_box( $text, $input_id ) {
429
		$input_text_id  = $input_id . '-search-input';
430
		$input_date_id  = $input_id . '-search-date-input';
431
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
432
433
		if ( ! empty( $_REQUEST['orderby'] ) )
434
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
435
		if ( ! empty( $_REQUEST['order'] ) )
436
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
437
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
438
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
439
		if ( ! empty( $_REQUEST['detached'] ) )
440
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
441
		?>
442
		<p class="search-box">
443
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
444
			<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' ); ?>" />
445
			<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' ); ?>" />
446
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
447
		</p>
448
		<?php
449
	}
450
}
451