PluginProbe
Email Log / 2.63
Email Log v2.63
2.63 2.4.8 2.4.9 2.6 2.61 2.62 trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.8.1 0.9 0.9.1 0.9.2 1.1 1.5 1.5.1 1.5.2 1.5.3 1.5.4 All 61 releases
email-log / include / Core / UI / ListTable / LogListTable.php

LogListTable.php in Email Log 2.63, at include/Core/UI/ListTable/LogListTable.php

487 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace EmailLog\Core\UI\ListTable;
2
3 use EmailLog\Util;
4 use EmailLog\Util\EmailHeaderParser;
5 use function EmailLog\Util\get_display_format_for_log_time;
6
7 if ( ! class_exists( 'WP_List_Table' ) ) {
8 require_once ABSPATH . WPINC . '/class-wp-list-table.php';
9 }
10
11 /**
12 * Table to display Email Logs.
13 *
14 * Based on Custom List Table Example by Matt Van Andel.
15 */
16 class LogListTable extends \WP_List_Table {
17 /**
18 * @var object The page where this table is rendered.
19 *
20 * @since 2.0
21 */
22 protected $page;
23 protected $parsed_headers = [];
24 /**
25 * Set up a constructor that references the parent constructor.
26 *
27 * We use the parent reference to set some default configs.
28 *
29 * @param \EmailLog\Core\UI\Page\LogListPage $page
30 * @param mixed $args
31 */
32 public function __construct( $page, $args = array() ) {
33 $this->page = $page;
34
35 $args = wp_parse_args( $args, array(
36 'singular' => 'email-log', // singular name of the listed records
37 'plural' => 'email-logs', // plural name of the listed records
38 'ajax' => false, // does this table support ajax?
39 'screen' => $this->page->get_screen(),
40 ) );
41
42 parent::__construct( $args );
43
44 add_action( 'admin_body_class', array( $this, 'add_body_class' ) );
45
46 add_action( 'el_display_log_columns', array( $this, 'display_column_data_in_log_list_table' ), 10, 2 );
47 add_action( 'el_view_log_after_headers', array( $this, 'add_headers_in_view_log_modal' ) );
48
49 add_filter( 'el_export_column_list', array( $this, 'add_columns_to_export_list' ) );
50 add_filter( 'el_export_raw_log', [ $this, 'add_columns_to_exported_raw_log' ] );
51 }
52
53 /**
54 * Adds extra markup in the toolbars before or after the list.
55 *
56 * @access protected
57 *
58 * @param string $which Add the markup after (bottom) or before (top) the list.
59 */
60 protected function extra_tablenav( $which ) {
61 if ( 'top' == $which ) {
62 /**
63 * Triggered before the logs list table is displayed.
64 *
65 * @since 2.2.5
66 * @since 2.4.0 Added $total_logs parameter
67 *
68 * @param int $total_logs Total number of logs.
69 */
70 do_action( 'el_before_logs_list_table', $this->get_pagination_arg( 'total_items' ) );
71 }
72 }
73
74 /**
75 * Returns the list of column and title names.
76 *
77 * @since 2.3.0 Retrieve Column labels using Utility methods.
78 * @since 2.3.2 Added `result` column.
79 * @since 2.4.0 Added `sent_status` column.
80 * @see WP_List_Table::single_row_columns()
81 *
82 * @uses \EmailLog\Util\get_column_label()
83 *
84 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
85 */
86 public function get_columns() {
87 $columns = array(
88 'cb' => '<input type="checkbox" />',
89 );
90
91 foreach ( array( 'sent_date', 'result', 'to_email', 'subject', 'cc', 'bcc', 'reply_to', 'attachments', 'ip_address' ) as $column ) {
92 $columns[ $column ] = Util\get_column_label( $column );
93 }
94
95 /**
96 * Filter the email log list table columns.
97 *
98 * @since 2.0.0
99 *
100 * @param array $columns Columns of email log list table.
101 */
102 return apply_filters( 'el_manage_log_columns', $columns );
103 }
104
105 /**
106 * Returns the list of columns.
107 *
108 * @access protected
109 *
110 * @return array<string,array<boolean|string>> An associative array containing all the columns
111 * that should be sortable: 'slugs'=>array('data_values',bool).
112 */
113 protected function get_sortable_columns() {
114 $sortable_columns = array(
115 'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
116 'to_email' => array( 'to_email', false ),
117 'subject' => array( 'subject', false ),
118 );
119
120 return $sortable_columns;
121 }
122
123 /**
124 * Returns value for default columns.
125 *
126 * @access protected
127 *
128 * @param object $item Data object.
129 * @param string $column_name Column Name.
130 */
131 protected function column_default( $item, $column_name ) {
132 /**
133 * Display Email Log list table columns.
134 *
135 * @since 2.0
136 *
137 * @param string $column_name Column Name.
138 * @param object $item Data object.
139 */
140 do_action( 'el_display_log_columns', $column_name, $item );
141 }
142
143 /**
144 * Display sent date column.
145 *
146 * @access protected
147 *
148 * @param object $item Current item object.
149 *
150 * @return string Markup to be displayed for the column.
151 */
152 protected function column_sent_date( $item ) {
153 $email_date = mysql2date(
154 sprintf(
155 /* translators: 1 Date of the log, 2 Time of the log */
156 __( '%1$s @ %2$s', 'email-log' ),
157 get_option( 'date_format', 'F j, Y' ),
158 get_display_format_for_log_time()
159 ),
160 $item->sent_date
161 );
162
163 $actions = array();
164
165 $content_ajax_url = add_query_arg(
166 array(
167 'action' => 'el-log-list-view-message',
168 'log_id' => $item->id,
169 'width' => '800',
170 'height' => '550',
171 ),
172 'admin-ajax.php'
173 );
174
175 $actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
176 esc_url( $content_ajax_url ),
177 __( 'Email Content', 'email-log' ),
178 __( 'View Content', 'email-log' )
179 );
180
181 $delete_url = add_query_arg(
182 array(
183 'page' => sanitize_text_field( wp_unslash( $_REQUEST['page'] ?? '')), //phpcs:ignore
184 'action' => 'el-log-list-delete',
185 $this->_args['singular'] => $item->id,
186 )
187 );
188 $delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
189
190 $actions['delete'] = sprintf( '<a href="%s">%s</a>',
191 esc_url( $delete_url ),
192 __( 'Delete', 'email-log' )
193 );
194
195 /**
196 * This filter can be used to modify the list of row actions that are displayed.
197 *
198 * @since 1.8
199 *
200 * @param array $actions List of actions.
201 * @param object $item The current log item.
202 */
203 $actions = apply_filters( 'el_row_actions', $actions, $item );
204
205 return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
206 /*$1%s*/ $email_date,
207 /*$2%s*/ $item->id,
208 /*$3%s*/ $this->row_actions( $actions )
209 );
210 }
211
212 /**
213 * To field.
214 *
215 * @access protected
216 *
217 * @param object $item
218 *
219 * @return string
220 */
221 protected function column_to_email( $item ) {
222 /**
223 * Filters the `To` field before outputting on the table.
224 *
225 * @since 2.3.0
226 *
227 * @param string $email `To` field
228 */
229 $email = apply_filters( 'el_log_list_column_to_email', esc_html( $item->to_email ) );
230
231 return $email;
232 }
233
234 /**
235 * Subject field.
236 *
237 * @access protected
238 *
239 * @param object $item
240 *
241 * @return string
242 */
243 protected function column_subject( $item ) {
244 return esc_html( $item->subject );
245 }
246
247 /**
248 * Markup for action column.
249 *
250 * @access protected
251 *
252 * @param object $item
253 *
254 * @return string
255 */
256 protected function column_cb( $item ) {
257 return sprintf(
258 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
259 /*$1%s*/ $this->_args['singular'],
260 /*$2%s*/ $item->id
261 );
262 }
263
264 /**
265 * Markup for Status column.
266 *
267 * @since 2.3.2
268 * @since 2.4.0 Output the error message as tooltip.
269 *
270 * @param object $item Email Log item.
271 *
272 * @return string Column markup.
273 */
274 protected function column_result( $item ) {
275 // For older records that does not have value in the result column,
276 // $item->result will be null.
277 if ( is_null( $item->result ) ) {
278 return '';
279 }
280
281 $icon = \EmailLog\Util\get_failure_icon();
282 if ( $item->result ) {
283 $icon = \EmailLog\Util\get_success_icon();
284 }
285
286 if ( ! isset( $item->error_message ) ) {
287 return $icon;
288 }
289
290 return sprintf(
291 '<span class="%3$s" title="%2$s">%1$s</span>',
292 $icon,
293 esc_attr( $item->error_message ),
294 'el-help'
295 );
296 }
297
298 /**
299 * Specify the list of bulk actions.
300 *
301 * @access protected
302 *
303 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
304 */
305 protected function get_bulk_actions() {
306 $actions = array(
307 'el-log-list-delete' => __( 'Delete', 'email-log' ),
308 'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
309 'el-log-export' => __( 'Export - PRO', 'email-log' ),
310 'el-log-export-all' => __( 'Export All Logs - PRO', 'email-log' ),
311 'el-log-resend' => __( 'Resend - PRO', 'email-log' ),
312 'el-log-resend-all' => __( 'Resend All Logs - PRO', 'email-log' ),
313 );
314 $actions = apply_filters( 'el_bulk_actions', $actions );
315
316 return $actions;
317 }
318
319 /**
320 * Prepare data for display.
321 */
322 public function prepare_items() {
323 $this->_column_headers = $this->get_column_info();
324
325 // Get current page number.
326 $current_page_no = $this->get_pagenum();
327 $per_page = $this->page->get_per_page();
328
329 //phpcs:ignore as $_GET is processed in fetch_log_items
330 list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no ); //phpcs:ignore
331
332 $this->items = $items;
333
334 // Register pagination options & calculations.
335 $this->set_pagination_args( array(
336 'total_items' => $total_items,
337 'per_page' => $per_page,
338 'total_pages' => ceil( $total_items / $per_page ),
339 ) );
340 }
341
342 /**
343 * Displays default message when no items are found.
344 */
345 public function no_items() {
346 esc_html_e( 'Your email log is empty', 'email-log' );
347 }
348
349 /**
350 * Displays the search box.
351 *
352 * @since 2.0
353 *
354 * @param string $text The 'submit' button label.
355 * @param string $input_id ID attribute value for the search input field.
356 */
357 public function search_box( $text, $input_id ) {
358 //phpcs:ignore nonce not needed as can be linked to directly
359
360 $input_text_id = $input_id . '-search-input';
361 $input_date_id = $input_id . '-search-date-input';
362 $input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( wp_unslash($_REQUEST['d']) ) : ''; //phpcs:ignore
363
364 if ( ! empty( $_REQUEST['orderby'] ) ) //phpcs:ignore
365 echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; //phpcs:ignore
366 if ( ! empty( $_REQUEST['order'] ) ) //phpcs:ignore
367 echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; //phpcs:ignore
368 if ( ! empty( $_REQUEST['post_mime_type'] ) ) //phpcs:ignore
369 echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />'; //phpcs:ignore
370 if ( ! empty( $_REQUEST['detached'] ) ) //phpcs:ignore
371 echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />'; //phpcs:ignore
372 ?>
373 <p class="search-box">
374 <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_html($text); ?>:</label>
375 <input type="search" id="<?php echo esc_attr( $input_date_id ); ?>" name="d" value="<?php echo esc_attr( $input_date_val ); ?>" placeholder="<?php esc_html_e( 'Filter by date', 'email-log' ); ?>" />
376 <input type="search" id="<?php echo esc_attr( $input_text_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php esc_html_e( 'Search by term', 'email-log' ); ?>" />
377 <?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
378 </p>
379 <?php
380 }
381
382 /**
383 * Adds additional Column headers to in the exported CSV.
384 *
385 * @param array $columns List of Columns.
386 *
387 * @return array List of all Columns.
388 */
389 public function add_columns_to_export_list( $columns ) {
390 return array_merge( $columns, array_keys( $this->get_columns() ) );
391 }
392
393 /**
394 * Display content for additional columns
395 *
396 * @param string $column_name Column Name.
397 * @param object $item Data object.
398 */
399 public function display_column_data_in_log_list_table( $column_name, $item ) {
400 if ( ! isset( $this->parsed_headers[ $item->id ] ) ) {
401 $parser = new EmailHeaderParser();
402 $this->parsed_headers[ $item->id ] = $parser->parse_headers( $item->headers );
403 }
404 $header = $this->parsed_headers[ $item->id ];
405
406 switch ( $column_name ) {
407 case 'cc':
408 echo '<a href="#" class="open-pro-dialog pro-feature" data-pro-feature="email-log-cc">PRO</a>';
409 break;
410 case 'bcc':
411 echo '<a href="#" class="open-pro-dialog pro-feature" data-pro-feature="email-log-bcc">PRO</a>';
412 break;
413 case 'reply_to':
414 echo '<a href="#" class="open-pro-dialog pro-feature" data-pro-feature="email-log-reply-to">PRO</a>';
415 break;
416 case 'attachments':
417 echo '<a href="#" class="open-pro-dialog pro-feature" data-pro-feature="email-log-attachment">PRO</a>';
418 break;
419 case 'ip_address':
420 echo '<a href="#" class="open-pro-dialog pro-feature" data-pro-feature="email-log-ip-address">PRO</a>';
421 break;
422 }
423 }
424
425 /**
426 * Adds additional column values to the log item that is getting exported to CSV.
427 *
428 * @param array $raw_log The raw log item.
429 *
430 * @return array The raw log with additional columns.
431 */
432 public function add_columns_to_exported_raw_log( $raw_log ) {
433 $raw_headers = '';
434 if ( isset( $raw_log[ 'headers' ] ) ) {
435 $raw_headers = $raw_log['headers'];
436 }
437
438 $parser = new EmailHeaderParser();
439 $header = $parser->parse_headers( $raw_headers );
440
441 $raw_log['from'] = ( isset( $header['from'] ) ? esc_attr( $header['from'] ) : '' );
442 $raw_log['cc'] = ( isset( $header['cc'] ) ? esc_attr( $header['cc'] ) : '' );
443 $raw_log['bcc'] = ( isset( $header['bcc'] ) ? esc_attr( $header['bcc'] ) : '' );
444 $raw_log['reply-to'] = ( isset( $header['reply-to'] ) ? esc_attr( $header['reply-to'] ) : '' );
445 $raw_log['attachments'] = 'true' === $raw_log['attachments'] ? 'Yes' : 'No';
446
447 return $raw_log;
448 }
449
450 /**
451 * Add additional headers to view message in thickbox.
452 *
453 * @since 2.0.0
454 *
455 * @param array $log_item Log item that is getting displayed.
456 */
457 public function add_headers_in_view_log_modal( $log_item ) {
458 $parser = new EmailHeaderParser();
459 $headers = $parser->parse_headers( $log_item['headers'] );
460 $columns = $this->get_columns();
461
462 foreach ( $columns as $key => $column ) {
463 if ( ! empty( $headers[ $key ] ) ) {
464 ?>
465 <tr style="background: #eee;">
466 <td style="padding: 5px;"><?php echo esc_html( $column ) . ':'; ?></td>
467 <td style="padding: 5px;"><?php echo esc_html( stripslashes( $headers[ $key ] ) ); ?></td>
468 </tr>
469 <?php
470 }
471 }
472 }
473
474 /**
475 * Adds the Class to the <body> tag in the Admin end.
476 *
477 * @since 2.1.0
478 *
479 * @param string $classes Body classes.
480 *
481 * @return string Modified body classes.
482 */
483 public function add_body_class( $classes ) {
484 return "$classes more-fields-addon";
485 }
486 }
487