Passed
Push — 215-feature/change-log-row-col... ( 168e2c...c72fd7 )
by Sudar
12:57
created

LogListTable::column_result()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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

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

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