Completed
Pull Request — master (#236)
by Sudar
14:28 queued 07:07
created

LogListTable::column_to()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 3
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
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
	 * 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
	 * @param mixed                              $args
29
	 */
30 1
	public function __construct( $page, $args = array() ) {
31 1
		$this->page = $page;
32
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 1
			'screen'   => $this->page->get_screen(),
38 1
		) );
39
40 1
		parent::__construct( $args );
41 1
	}
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
	 * @see WP_List_Table::single_row_columns()
67
	 *
68
	 * @uses \EmailLog\Util\get_column_label()
69
	 *
70
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
71 1
	 */
72
	public function get_columns() {
73 1
		$columns = array(
74 1
			'cb' => '<input type="checkbox" />', // Render a checkbox instead of heading.
75 1
		);
76 1
77 1
		foreach ( array( 'sent_date', 'result', 'to_email', 'subject' ) as $column ) {
78
			$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

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

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