LogListTable::column_cb()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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

148
				/** @scrutinizer ignore-type */ get_option( 'date_format', 'F j, Y' ),
Loading history...
149
				get_display_format_for_log_time()
150
			),
151
			$item->sent_date
152
		);
153
154
		$actions = array();
155
156
		$content_ajax_url = add_query_arg(
157
			array(
158
				'action' => 'el-log-list-view-message',
159
				'log_id' => $item->id,
160
				'width'  => '800',
161
				'height' => '550',
162
			),
163
			'admin-ajax.php'
164
		);
165
166
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
167
			esc_url( $content_ajax_url ),
168
			__( 'Email Content', 'email-log' ),
169
			__( 'View Content', 'email-log' )
170
		);
171
172
		$delete_url = add_query_arg(
173
			array(
174
				'page'                   => $_REQUEST['page'],
175
				'action'                 => 'el-log-list-delete',
176
				$this->_args['singular'] => $item->id,
177
			)
178
		);
179
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
180
181
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
182
			esc_url( $delete_url ),
183
			__( 'Delete', 'email-log' )
184
		);
185
186
		/**
187
		 * This filter can be used to modify the list of row actions that are displayed.
188
		 *
189
		 * @since 1.8
190
		 *
191
		 * @param array  $actions List of actions.
192
		 * @param object $item    The current log item.
193
		 */
194
		$actions = apply_filters( 'el_row_actions', $actions, $item );
195
196
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
197
			/*$1%s*/ $email_date,
198
			/*$2%s*/ $item->id,
199
			/*$3%s*/ $this->row_actions( $actions )
200
		);
201
	}
202
203
	/**
204
	 * To field.
205
	 *
206
	 * @access protected
207
	 *
208
	 * @param object $item
209
	 *
210
	 * @return string
211
	 */
212
	protected function column_to_email( $item ) {
213
		/**
214
		 * Filters the `To` field before outputting on the table.
215
		 *
216
		 * @since 2.3.0
217
		 *
218
		 * @param string $email `To` field
219
		 */
220
		$email = apply_filters( 'el_log_list_column_to_email', esc_html( $item->to_email ) );
221
222
		return $email;
223
	}
224
225
	/**
226
	 * Subject field.
227
	 *
228
	 * @access protected
229
	 *
230
	 * @param object $item
231
	 *
232
	 * @return string
233
	 */
234
	protected function column_subject( $item ) {
235
		return esc_html( $item->subject );
236
	}
237
238
	/**
239
	 * Markup for action column.
240
	 *
241
	 * @access protected
242
	 *
243
	 * @param object $item
244
	 *
245
	 * @return string
246
	 */
247
	protected function column_cb( $item ) {
248
		return sprintf(
249
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
250
			/*$1%s*/ $this->_args['singular'],
251
			/*$2%s*/ $item->id
252
		);
253
	}
254
255
	/**
256
	 * Markup for Status column.
257
	 *
258
	 * @since 2.3.2
259
	 * @since 2.4.0 Output the error message as tooltip.
260
	 *
261
	 * @param object $item Email Log item.
262
	 *
263
	 * @return string Column markup.
264
	 */
265
	protected function column_result( $item ) {
266
		// For older records that does not have value in the result column,
267
		// $item->result will be null.
268
		if ( is_null( $item->result ) ) {
269
			return '';
270
		}
271
272
		$icon = \EmailLog\Util\get_failure_icon();
273
		if ( $item->result ) {
274
			$icon = \EmailLog\Util\get_success_icon();
275
		}
276
277
		if ( ! isset( $item->error_message ) ) {
278
			return $icon;
279
		}
280
281
		return sprintf(
282
			'<span class="%3$s" title="%2$s">%1$s</span>',
283
			$icon,
284
			esc_attr( $item->error_message ),
285
			'el-help'
286
		);
287
	}
288
289
	/**
290
	 * Specify the list of bulk actions.
291
	 *
292
	 * @access protected
293
	 *
294
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
295
	 */
296
	protected function get_bulk_actions() {
297
		$actions = array(
298
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
299
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
300
		);
301
		$actions = apply_filters( 'el_bulk_actions', $actions );
302
303
		return $actions;
304
	}
305
306
	/**
307
	 * Prepare data for display.
308
	 */
309
	public function prepare_items() {
310
		$this->_column_headers = $this->get_column_info();
311
312
		// Get current page number.
313
		$current_page_no = $this->get_pagenum();
314
		$per_page        = $this->page->get_per_page();
315
316
		list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no );
317
318
		$this->items = $items;
319
320
		// Register pagination options & calculations.
321
		$this->set_pagination_args( array(
322
			'total_items' => $total_items,
323
			'per_page'    => $per_page,
324
			'total_pages' => ceil( $total_items / $per_page ),
325
		) );
326
	}
327
328
	/**
329
	 * Displays default message when no items are found.
330
	 */
331
	public function no_items() {
332
		_e( 'Your email log is empty', 'email-log' );
333
	}
334
335
	/**
336
	 * Displays the search box.
337
	 *
338
	 * @since 2.0
339
	 *
340
	 * @param string $text     The 'submit' button label.
341
	 * @param string $input_id ID attribute value for the search input field.
342
	 */
343
	public function search_box( $text, $input_id ) {
344
		$input_text_id  = $input_id . '-search-input';
345
		$input_date_id  = $input_id . '-search-date-input';
346
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
347
348
		if ( ! empty( $_REQUEST['orderby'] ) )
349
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
350
		if ( ! empty( $_REQUEST['order'] ) )
351
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
352
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
353
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
354
		if ( ! empty( $_REQUEST['detached'] ) )
355
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
356
		?>
357
		<p class="search-box">
358
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
359
			<input type="search" id="<?php echo esc_attr( $input_date_id ); ?>" name="d" value="<?php echo esc_attr( $input_date_val ); ?>" placeholder="<?php _e( 'Search by date', 'email-log' ); ?>" />
360
			<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' ); ?>" />
361
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
362
		</p>
363
		<?php
364
	}
365
}
366