robotstxt-telemetry/includes/class-robotstxt-telemetry-logs-table.php
2026-08-20 14:31:43 +00:00

300 lines
8.7 KiB
PHP

<?php
/**
* Logs list table.
*
* @package RobotstxtTelemetry
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
if ( ! class_exists( 'Robotstxt_Telemetry_Logs_Table' ) ) {
/**
* Class Robotstxt_Telemetry_Logs_Table
*/
class Robotstxt_Telemetry_Logs_Table extends WP_List_Table {
/**
* Number of items per page.
*
* @var int
*/
private $per_page = 20;
/**
* Constructor.
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'telemetry_log',
'plural' => 'telemetry_logs',
'ajax' => false,
)
);
}
/**
* Get columns.
*
* @return array<string,string>
*/
public function get_columns() {
return array(
'created_at' => __( 'Date', 'robotstxt-telemetry' ),
'method' => __( 'Method', 'robotstxt-telemetry' ),
'host' => __( 'Host', 'robotstxt-telemetry' ),
'path' => __( 'Path', 'robotstxt-telemetry' ),
);
}
/**
* Prepare items for display.
*
* @return void
*/
public function prepare_items() {
global $wpdb;
$table_name = Robotstxt_Telemetry_DB::get_table_name();
$paged = max( 1, $this->get_pagenum() );
$offset = ( $paged - 1 ) * $this->per_page;
$columns = $this->get_columns();
$this->_column_headers = array( $columns, array(), array() );
$method = strtoupper( $this->get_filter_value( 'method' ) );
$host = $this->get_filter_value( 'host' );
$date_from = $this->sanitize_date_filter( $this->get_filter_value( 'date_from' ) );
$date_from_value = ! empty( $date_from ) ? $date_from . ' 00:00:00' : '';
$date_to = $this->sanitize_date_filter( $this->get_filter_value( 'date_to' ) );
$date_to_value = ! empty( $date_to ) ? $date_to . ' 23:59:59' : '';
$this->items = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; list output is never cached.
$wpdb->prepare(
"SELECT * FROM {$table_name} WHERE ( %s = '' OR method = %s ) AND ( %s = '' OR host = %s ) AND ( %s = '' OR created_at >= %s ) AND ( %s = '' OR created_at <= %s ) ORDER BY created_at DESC LIMIT %d OFFSET %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
$method,
$method,
$host,
$host,
$date_from_value,
$date_from_value,
$date_to_value,
$date_to_value,
$this->per_page,
$offset
),
ARRAY_A
);
$total = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; pagination count is never cached.
$wpdb->prepare(
"SELECT COUNT(*) FROM {$table_name} WHERE ( %s = '' OR method = %s ) AND ( %s = '' OR host = %s ) AND ( %s = '' OR created_at >= %s ) AND ( %s = '' OR created_at <= %s )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
$method,
$method,
$host,
$host,
$date_from_value,
$date_from_value,
$date_to_value,
$date_to_value
)
);
$this->set_pagination_args(
array(
'total_items' => $total,
'per_page' => $this->per_page,
)
);
}
/**
* Render default column output.
*
* @param array<string,mixed> $item Column data.
* @param string $column_name Column name.
* @return string
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'created_at':
case 'method':
case 'host':
case 'path':
$value = isset( $item[ $column_name ] ) && is_string( $item[ $column_name ] ) ? $item[ $column_name ] : '';
return esc_html( $value );
default:
return '';
}
}
/**
* Add row actions.
*
* @param array<string,mixed> $item Item data.
* @return string
*/
public function column_created_at( $item ) {
$log_id = isset( $item['id'] ) && is_numeric( $item['id'] ) ? (int) $item['id'] : 0;
$view_url = add_query_arg(
array(
'page' => 'robotstxt-telemetry-logs',
'action' => 'view',
'log' => $log_id,
),
Robotstxt_Telemetry_Network::base_url()
);
$delete_url = wp_nonce_url(
add_query_arg(
array(
'page' => 'robotstxt-telemetry-logs',
'action' => 'delete',
'log' => $log_id,
),
Robotstxt_Telemetry_Network::base_url()
),
'robotstxt_telemetry_delete_log_' . $log_id
);
$actions = array(
'view' => '<a href="' . esc_url( $view_url ) . '">' . esc_html__( 'View', 'robotstxt-telemetry' ) . '</a>',
'delete' => '<a href="' . esc_url( $delete_url ) . '">' . esc_html__( 'Delete', 'robotstxt-telemetry' ) . '</a>',
);
$created_at = isset( $item['created_at'] ) && is_string( $item['created_at'] ) ? $item['created_at'] : '';
return esc_html( $created_at ) . $this->row_actions( $actions );
}
/**
* Render the URL column without query parameters.
*
* @param array<string,mixed> $item Item data.
* @return string
*/
public function column_url( $item ) {
$url = isset( $item['url'] ) && is_string( $item['url'] ) ? $item['url'] : '';
if ( '' === $url ) {
return '';
}
$parsed_url = wp_parse_url( $url );
if ( ! is_array( $parsed_url ) || empty( $parsed_url['host'] ) ) {
return esc_html( $url );
}
$scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] : 'https';
$path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
$port = isset( $parsed_url['port'] ) ? ':' . (int) $parsed_url['port'] : '';
$base = sprintf( '%s://%s%s%s', $scheme, $parsed_url['host'], $port, $path );
return esc_html( $base );
}
/**
* Extra controls for filtering.
*
* @param string $which Top or bottom.
* @return void
*/
public function extra_tablenav( $which ) {
if ( 'top' !== $which ) {
return;
}
$method = strtoupper( $this->get_filter_value( 'method' ) );
$host = $this->get_filter_value( 'host' );
$date_from = $this->get_filter_value( 'date_from' );
$date_to = $this->get_filter_value( 'date_to' );
echo '<div class="alignleft actions">';
echo '<label class="screen-reader-text" for="robotstxt-telemetry-method-filter">' . esc_html__( 'Filter by method', 'robotstxt-telemetry' ) . '</label>';
echo '<select name="method" id="robotstxt-telemetry-method-filter">';
echo '<option value="">' . esc_html__( 'All methods', 'robotstxt-telemetry' ) . '</option>';
$methods = array( 'GET', 'POST', 'HEAD', 'PUT', 'PATCH', 'DELETE' );
foreach ( $methods as $option ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $option ),
selected( strtoupper( (string) $method ), $option, false ),
esc_html( $option )
);
}
echo '</select>';
echo '<label class="screen-reader-text" for="robotstxt-telemetry-host-filter">' . esc_html__( 'Filter by host', 'robotstxt-telemetry' ) . '</label>';
printf(
'<input type="text" name="host" id="robotstxt-telemetry-host-filter" value="%s" placeholder="%s" />',
esc_attr( (string) $host ),
esc_attr__( 'Host', 'robotstxt-telemetry' )
);
echo '<label class="screen-reader-text" for="robotstxt-telemetry-date-from">' . esc_html__( 'From date', 'robotstxt-telemetry' ) . '</label>';
printf(
'<input type="date" name="date_from" id="robotstxt-telemetry-date-from" value="%s" />',
esc_attr( (string) $date_from )
);
echo '<label class="screen-reader-text" for="robotstxt-telemetry-date-to">' . esc_html__( 'To date', 'robotstxt-telemetry' ) . '</label>';
printf(
'<input type="date" name="date_to" id="robotstxt-telemetry-date-to" value="%s" />',
esc_attr( (string) $date_to )
);
submit_button( __( 'Filter', 'robotstxt-telemetry' ), '', 'filter_action', false );
echo '</div>';
}
/**
* Get a sanitized filter value from the request.
*
* Values are only used to build read-only list filters.
*
* @param string $key Filter key.
* @return string
*/
private function get_filter_value( $key ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filters.
if ( ! isset( $_GET[ $key ] ) || ! is_string( $_GET[ $key ] ) ) {
return '';
}
return sanitize_text_field( wp_unslash( $_GET[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
/**
* Sanitize date inputs.
*
* @param string|null $value Date string.
* @return string|null
*/
private function sanitize_date_filter( $value ) {
if ( empty( $value ) ) {
return null;
}
$date = DateTime::createFromFormat( 'Y-m-d', $value );
if ( false === $date ) {
return null;
}
return $date->format( 'Y-m-d' );
}
}
}