v0.2.0
This commit is contained in:
commit
8a85a4d9af
1 changed files with 3258 additions and 0 deletions
385
includes/class-robotstxt-telemetry-logs-table.php
Normal file
385
includes/class-robotstxt-telemetry-logs-table.php
Normal file
|
|
@ -0,0 +1,385 @@
|
|||
<?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 int $per_page = 20;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
array(
|
||||
'singular' => 'telemetry_log',
|
||||
'plural' => 'telemetry_logs',
|
||||
'ajax' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get columns.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_columns(): array {
|
||||
return array(
|
||||
'created_at' => __( 'Date', 'robotstxt-telemetry' ),
|
||||
'method' => __( 'Method', 'robotstxt-telemetry' ),
|
||||
'host' => __( 'Host', 'robotstxt-telemetry' ),
|
||||
'path' => __( 'Path', 'robotstxt-telemetry' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare items for display.
|
||||
*/
|
||||
public function prepare_items(): void {
|
||||
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 %i
|
||||
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',
|
||||
$table_name,
|
||||
$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 %i
|
||||
WHERE ( %s = \'\' OR method = %s )
|
||||
AND ( %s = \'\' OR host = %s )
|
||||
AND ( %s = \'\' OR created_at >= %s )
|
||||
AND ( %s = \'\' OR created_at <= %s )',
|
||||
$table_name,
|
||||
$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 $item Column data.
|
||||
* @param string $column_name Column name.
|
||||
* @return string
|
||||
*/
|
||||
public function column_default( $item, $column_name ): string {
|
||||
switch ( $column_name ) {
|
||||
case 'created_at':
|
||||
return esc_html( $item[ $column_name ] ?? '' );
|
||||
case 'method':
|
||||
case 'host':
|
||||
case 'path':
|
||||
return esc_html( $item[ $column_name ] ?? '' );
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add row actions.
|
||||
*
|
||||
* @param array $item Item data.
|
||||
* @return string
|
||||
*/
|
||||
public function column_created_at( $item ): string {
|
||||
$log_id = (int) $item['id'];
|
||||
|
||||
$view_url = add_query_arg(
|
||||
array(
|
||||
'page' => 'robotstxt-telemetry-logs',
|
||||
'action' => 'view',
|
||||
'log' => $log_id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
);
|
||||
|
||||
$delete_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'page' => 'robotstxt-telemetry-logs',
|
||||
'action' => 'delete',
|
||||
'log' => $log_id,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
),
|
||||
'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>',
|
||||
);
|
||||
|
||||
return esc_html( $item['created_at'] ) . $this->row_actions( $actions );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the URL column without query parameters.
|
||||
*
|
||||
* @param array $item Item data.
|
||||
* @return string
|
||||
*/
|
||||
public function column_url( $item ): string {
|
||||
if ( empty( $item['url'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$parsed_url = wp_parse_url( $item['url'] );
|
||||
if ( empty( $parsed_url ) || empty( $parsed_url['host'] ) ) {
|
||||
return esc_html( $item['url'] );
|
||||
}
|
||||
|
||||
$scheme = $parsed_url['scheme'] ?? 'https';
|
||||
$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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Format parameters column output.
|
||||
*
|
||||
* @param array $item Item data.
|
||||
* @return string
|
||||
*/
|
||||
private function format_params_column( array $item ): string {
|
||||
$params = array();
|
||||
|
||||
if ( ! empty( $item['url'] ) ) {
|
||||
$parsed_url = wp_parse_url( $item['url'] );
|
||||
if ( ! empty( $parsed_url['query'] ) ) {
|
||||
$fallback_query = wp_parse_args( $parsed_url['query'] );
|
||||
$params = array_merge( $params, $fallback_query );
|
||||
}
|
||||
}
|
||||
|
||||
$query_params = $this->decode_json_params( $item['query_json'] ?? '' );
|
||||
if ( ! empty( $query_params ) ) {
|
||||
$params = array_merge( $params, $query_params );
|
||||
}
|
||||
|
||||
$body_params = $this->decode_json_params( $item['body_json'] ?? '' );
|
||||
if ( ! empty( $body_params ) ) {
|
||||
$params = array_merge( $params, $body_params );
|
||||
}
|
||||
|
||||
if ( empty( $body_params ) && ! empty( $item['raw_body'] ) ) {
|
||||
$fallback_body = wp_parse_args( $item['raw_body'] );
|
||||
if ( ! empty( $fallback_body ) ) {
|
||||
$params = array_merge( $params, $fallback_body );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $params ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$lines = array();
|
||||
foreach ( $params as $key => $value ) {
|
||||
if ( is_array( $value ) ) {
|
||||
$value = wp_json_encode( $value );
|
||||
}
|
||||
|
||||
$lines[] = sprintf( '%s=%s', $key, $value );
|
||||
}
|
||||
|
||||
return '<pre style="white-space: pre-wrap; margin: 0;">' . esc_html( implode( "\n", $lines ) ) . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode JSON parameters into arrays.
|
||||
*
|
||||
* @param string $value JSON string.
|
||||
* @return array
|
||||
*/
|
||||
private function decode_json_params( string $value ): array {
|
||||
if ( empty( $value ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$decoded = json_decode( $value, true );
|
||||
if ( is_array( $decoded ) ) {
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
if ( is_string( $decoded ) ) {
|
||||
$parsed = wp_parse_args( $decoded );
|
||||
if ( ! empty( $parsed ) ) {
|
||||
return $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
$parsed = wp_parse_args( $value );
|
||||
if ( ! empty( $parsed ) ) {
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extra controls for filtering.
|
||||
*
|
||||
* @param string $which Top or bottom.
|
||||
*/
|
||||
public function extra_tablenav( $which ): void {
|
||||
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( string $key ): string {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filters.
|
||||
if ( ! isset( $_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( ?string $value ): ?string {
|
||||
if ( empty( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$date = DateTime::createFromFormat( 'Y-m-d', $value );
|
||||
if ( false === $date ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $date->format( 'Y-m-d' );
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue