namespace WPForms\Logger;
if ( ! defined( 'ABSPATH' ) ) {
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
class ListTable extends WP_List_Table {
* @param Repository $repository Repository.
public function __construct( $repository ) {
$this->repository = $repository;
'plural' => esc_html__( 'Logs', 'wpforms-lite' ),
'singular' => esc_html__( 'Log', 'wpforms-lite' ),
[ 'default' => $this->get_items_per_page( $this->get_per_page_option_name() ) ]
private function hooks() {
'set_screen_option_' . $this->get_per_page_option_name(),
[ $this, 'set_items_per_page_option' ],
* Handles setting the items_per_page option for this screen.
* @param mixed $status Default false (to skip saving the current option).
* @param string $option Screen option name.
* @param int $value Screen option value.
* @noinspection PhpUnusedParameterInspection
public function set_items_per_page_option( $status, $option, $value ) {
* Whether the table has items to display or not.
public function has_items() {
// We can't use the empty function because it doesn't work with the Countable object.
return (bool) count( $this->items );
* Prepares the list of items for displaying.
public function prepare_items() {
$offset = $this->get_items_offset();
$search = $this->get_request_search_query();
$types = $this->get_items_type();
$per_page = $this->get_items_per_page( $this->get_per_page_option_name() );
$this->items = $this->repository->records( $per_page, $offset, $search, $types );
$total_items = $this->get_total();
$this->set_pagination_args(
'total_items' => $total_items,
'total_pages' => (int) ceil( $total_items / $per_page ),
* Return the type of records.
private function get_items_type() {
return filter_input( INPUT_GET, 'log_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
* Return the number of items to offset/skip for this current view.
private function get_items_offset() {
return $this->get_items_per_page( $this->get_per_page_option_name() ) * ( $this->get_pagenum() - 1 );
* Return the search filter for this request, if any.
private function get_request_search_query() {
return filter_input( INPUT_GET, 's', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
* @param Record $item List table item.
* @noinspection PhpUnused
public function column_log_title( $item ) {
'<a href="#" class="js-single-log-target" data-log-id="%1$d"><strong>%2$s</strong></a>',
absint( $item->get_id() ),
esc_html( $item->get_title() )
* @param Record $item List table item.
* @noinspection PhpUnused
public function column_message( $item ) {
$message = $item->get_message();
if ( preg_match( '/\[body].+{"error":"(.+)"}/i', $message, $m ) ) {
if ( preg_match( '/\[error] => (.+)/i', $message, $m ) ) {
return esc_html( $this->crop_message( $message ) );
* @param Record $item List table item.
* @noinspection PhpUnused
public function column_form_id( $item ) {
return absint( $item->get_form_id() );
* @param Record $item List table item.
* @noinspection PhpUnused
public function column_types( $item ) {
return esc_html( implode( ', ', $item->get_types( 'label' ) ) );
* @param Record $item List table item.
* @noinspection PhpUnused
public function column_date( $item ) {
return esc_html( $item->get_date( 'sql-local' ) );
* Crop message for preview on list table.
* @param string $message Message.
private function crop_message( $message ) {
return wp_html_excerpt( $message, 97, '...' );
* Prepares the _column_headers property which is used by WP_Table_List at rendering.
* It merges the columns and the sortable columns.
private function prepare_column_headers() {
$this->_column_headers = [
get_hidden_columns( $this->screen ),
* Return the columns' names for rendering.
public function get_columns() {
'log_title' => __( 'Log Title', 'wpforms-lite' ),
'message' => __( 'Message', 'wpforms-lite' ),
'form_id' => __( 'Form ID', 'wpforms-lite' ),
'types' => __( 'Types', 'wpforms-lite' ),
'date' => __( 'Date', 'wpforms-lite' ),
* Header before log table.
private function header() {
<div class="wpforms-admin-content-header">
<h4 class="wp-heading-inline"><?php esc_html_e( 'View Logs', 'wpforms-lite' ); ?>
<?php if ( $this->get_request_search_query() ) { ?>
printf( /* translators: %s - search query. */
esc_html__( 'Search results for "%s"', 'wpforms-lite' ),
esc_html( $this->get_request_search_query() )
$this->search_box( esc_html__( 'Search Logs', 'wpforms-lite' ), 'plugin' );
* Generate the table navigation above or below the table.
* @param string $which Which position.
protected function display_tablenav( $which ) {
<div class="tablenav <?php echo esc_attr( $which ); ?>">
if ( $which === 'top' ) {
$this->extra_tablenav( $which );
$this->pagination( $which );
* @param string $which Position of navigation (top or bottom).
protected function extra_tablenav( $which ) {
if ( ! $this->get_total() ) {
$this->log_type_select();
private function clear_all() {
<button name="clear-all" type="submit" class="button" value="1"><?php esc_html_e( 'Delete All Logs', 'wpforms-lite' ); ?></button>
* Update URL when table showing.
* _wp_http_referer is used only on bulk actions, we remove it to keep the $_GET shorter.
public function process_admin_ui() {
$nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'wpforms-table-' . $this->_args['plural'] ) ) {
if ( empty( $_REQUEST['_wp_http_referer'] ) && empty( $_REQUEST['clear-all'] ) ) {
if ( ! empty( $_REQUEST['clear-all'] ) ) {
$this->repository->clear_all();
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
[ '_wp_http_referer', '_wpnonce', 'clear-all' ],
* Message to be displayed when there are no items.
public function no_items() {
esc_html_e( 'No logs found.', 'wpforms-lite' );
* Print all hidden fields.
private function hidden_fields() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
foreach ( $_GET as $key => $value ) {
if ( $key[0] === '_' || $key === 'paged' || $key === 'ID' ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
* Select for choose a log type.
private function log_type_select() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_type = ! empty( $_GET['log_type'] ) ? sanitize_text_field( wp_unslash( $_GET['log_type'] ) ) : '';
<option value=""><?php esc_html_e( 'All Logs', 'wpforms-lite' ); ?></option>
<?php foreach ( Log::get_log_types() as $type_slug => $type ) { ?>
value="<?php echo esc_attr( $type_slug ); ?>"
<?php selected( $type_slug, $current_type ); ?>>
<?php echo esc_html( $type ); ?>
<input type="submit" class="button" value="<?php esc_attr_e( 'Apply', 'wpforms-lite' ); ?>">
public function popup_template() {
<script type="text/html" id="tmpl-wpforms-log-record">
<div class="wpforms-log-popup">
<div class="wpforms-log-popup-block">
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Log Title', 'wpforms-lite' ); ?></div>
<div class="wpforms-log-popup-title">{{ data.title }}</div>
<div class="wpforms-log-popup-block">
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Message', 'wpforms-lite' ); ?></div>
<div class="wpforms-log-popup-message">{{{ data.message }}}</div>
<div class="wpforms-log-popup-flex wpforms-log-popup-flex-column-2">
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Date', 'wpforms-lite' ); ?></div>
<div class="wpforms-log-popup-create-at">{{ data.create_at }}</div>
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Types', 'wpforms-lite' ); ?></div>
<div class="wpforms-log-popup-types">{{ data.types }}</div>
<div class="wpforms-log-popup-flex wpforms-log-popup-flex-column-4">
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Log ID', 'wpforms-lite' ); ?></div>
<div class="wpforms-log-popup-id">{{ data.ID }}</div>
<div class="wpforms-log-popup-label"><?php esc_html_e( 'Form ID', 'wpforms-lite' ); ?></div>