Passed
Pull Request — dev/2.4.0 (#244)
by Sudar
04:16
created

LogListTable::column_star()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 12
rs 9.8666
1
<?php namespace EmailLog\Core\UI\ListTable;
2
3
use EmailLog\Util;
4
use \EmailLog\Core\UI\Page\LogListPage;
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
			 */
62
			do_action( 'el_before_logs_list_table' );
63
		}
64
	}
65
66
	/**
67
	 * Returns the list of column and title names.
68
	 *
69
	 * @since 2.3.0 Retrieve Column labels using Utility methods.
70
	 * @since 2.3.2 Added `result` column.
71
	 * @since 2.4.0 Added `sent_status` column.
72
	 * @see WP_List_Table::single_row_columns()
73
	 *
74
	 * @uses \EmailLog\Util\get_column_label()
75
	 *
76
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
77
	 */
78 1
	public function get_columns() {
79
		$columns = array(
80 1
			'cb' => '<input type="checkbox" />',
81
		);
82
83 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'star' ) as $column ) {
84 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

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

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

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

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