700 lines
26 KiB
PHP
700 lines
26 KiB
PHP
<?php
|
|
/**
|
|
* Admin UI handler.
|
|
*
|
|
* @package RobotstxtTelemetry
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|
/**
|
|
* Class Robotstxt_Telemetry_Admin
|
|
*/
|
|
class Robotstxt_Telemetry_Admin {
|
|
|
|
/**
|
|
* Register hooks.
|
|
*/
|
|
public function register() {
|
|
add_action( 'admin_menu', array( $this, 'register_menu' ) );
|
|
add_action( 'network_admin_menu', array( $this, 'register_network_menu' ) );
|
|
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
|
add_action( 'admin_init', array( $this, 'handle_actions' ) );
|
|
add_action( 'admin_notices', array( $this, 'render_saved_notice' ) );
|
|
add_action( 'network_admin_notices', array( $this, 'render_saved_notice' ) );
|
|
}
|
|
|
|
/**
|
|
* Register admin menus.
|
|
*
|
|
* In global mode the per-site screens are replaced by the
|
|
* Network Admin ones.
|
|
*/
|
|
public function register_menu() {
|
|
if ( Robotstxt_Telemetry_Network::is_global() ) {
|
|
return;
|
|
}
|
|
|
|
add_menu_page(
|
|
__( 'Telemetry', 'robotstxt-telemetry' ),
|
|
__( 'Telemetry', 'robotstxt-telemetry' ),
|
|
'manage_options',
|
|
'robotstxt-telemetry',
|
|
array( $this, 'render_settings_page' ),
|
|
'dashicons-admin-generic'
|
|
);
|
|
|
|
add_submenu_page(
|
|
'robotstxt-telemetry',
|
|
__( 'Settings', 'robotstxt-telemetry' ),
|
|
__( 'Settings', 'robotstxt-telemetry' ),
|
|
'manage_options',
|
|
'robotstxt-telemetry',
|
|
array( $this, 'render_settings_page' )
|
|
);
|
|
|
|
add_submenu_page(
|
|
'robotstxt-telemetry',
|
|
__( 'Logs', 'robotstxt-telemetry' ),
|
|
__( 'Logs', 'robotstxt-telemetry' ),
|
|
'manage_options',
|
|
'robotstxt-telemetry-logs',
|
|
array( $this, 'render_logs_page' )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Register the Network Admin menus (Multisite only).
|
|
*
|
|
* The Network Settings screen always exposes the configuration
|
|
* mode; when the network runs in global mode it also hosts the
|
|
* shared settings and the central Logs screen.
|
|
*/
|
|
public function register_network_menu() {
|
|
if ( ! is_multisite() ) {
|
|
return;
|
|
}
|
|
|
|
add_menu_page(
|
|
__( 'Telemetry', 'robotstxt-telemetry' ),
|
|
__( 'Telemetry', 'robotstxt-telemetry' ),
|
|
'manage_network_options',
|
|
'robotstxt-telemetry',
|
|
array( $this, 'render_network_settings_page' ),
|
|
'dashicons-admin-generic'
|
|
);
|
|
|
|
add_submenu_page(
|
|
'robotstxt-telemetry',
|
|
__( 'Settings', 'robotstxt-telemetry' ),
|
|
__( 'Settings', 'robotstxt-telemetry' ),
|
|
'manage_network_options',
|
|
'robotstxt-telemetry',
|
|
array( $this, 'render_network_settings_page' )
|
|
);
|
|
|
|
if ( Robotstxt_Telemetry_Network::is_global() ) {
|
|
add_submenu_page(
|
|
'robotstxt-telemetry',
|
|
__( 'Logs', 'robotstxt-telemetry' ),
|
|
__( 'Logs', 'robotstxt-telemetry' ),
|
|
'manage_network_options',
|
|
'robotstxt-telemetry-logs',
|
|
array( $this, 'render_logs_page' )
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register the plugin settings.
|
|
*/
|
|
public function register_settings() {
|
|
$settings = array(
|
|
'robotstxt_telemetry_hash_useragent_url' => array( 'boolean', true, 'sanitize_checkbox' ),
|
|
'robotstxt_telemetry_wp_core_check' => array( 'string', 'safe', 'sanitize_mode' ),
|
|
'robotstxt_telemetry_wp_themes_check' => array( 'string', 'safe', 'sanitize_mode' ),
|
|
'robotstxt_telemetry_wp_plugins_check' => array( 'string', 'safe', 'sanitize_mode' ),
|
|
'robotstxt_telemetry_retention_period' => array( 'string', '12hours', 'sanitize_retention' ),
|
|
'robotstxt_telemetry_delete_on_uninstall' => array( 'boolean', false, 'sanitize_checkbox' ),
|
|
);
|
|
|
|
foreach ( $settings as $name => $config ) {
|
|
register_setting(
|
|
'robotstxt_telemetry_settings',
|
|
$name,
|
|
array( $this, $config[2] )
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sanitize a mode setting.
|
|
*
|
|
* @param mixed $value Raw input value.
|
|
* @return string
|
|
*/
|
|
public function sanitize_mode( $value ) {
|
|
return 'original' === $value ? 'original' : 'safe';
|
|
}
|
|
|
|
/**
|
|
* Sanitize a retention period setting.
|
|
*
|
|
* @param mixed $value Raw input value.
|
|
* @return string
|
|
*/
|
|
public function sanitize_retention( $value ) {
|
|
$valid = array( '12hours', '1day', '3days' );
|
|
|
|
return in_array( $value, $valid, true ) ? $value : '12hours';
|
|
}
|
|
|
|
/**
|
|
* Sanitize a checkbox value.
|
|
*
|
|
* @param mixed $value Raw input value.
|
|
* @return bool
|
|
*/
|
|
public function sanitize_checkbox( $value ) {
|
|
return (bool) $value;
|
|
}
|
|
|
|
/**
|
|
* Handle admin actions.
|
|
*/
|
|
public function handle_actions() {
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- State changes are protected by check_admin_referer() below.
|
|
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
|
|
$action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : '';
|
|
|
|
// Network-level actions are always restricted to super administrators.
|
|
if ( 'save_network_mode' === $action || 'save_network_settings' === $action ) {
|
|
if ( 'robotstxt-telemetry' === $page && current_user_can( 'manage_network_options' ) && is_multisite() ) {
|
|
$this->handle_network_actions( $action );
|
|
}
|
|
return;
|
|
}
|
|
|
|
// In global mode the shared logs belong to the Network Admin only.
|
|
$capability = Robotstxt_Telemetry_Network::is_global() ? 'manage_network_options' : 'manage_options';
|
|
|
|
if ( ! current_user_can( $capability ) ) {
|
|
return;
|
|
}
|
|
|
|
if ( 'robotstxt-telemetry-logs' !== $page ) {
|
|
return;
|
|
}
|
|
|
|
if ( 'delete_all' === $action ) {
|
|
check_admin_referer( 'robotstxt_telemetry_delete_all_logs' );
|
|
|
|
Robotstxt_Telemetry_DB::delete_all_logs();
|
|
|
|
wp_safe_redirect(
|
|
add_query_arg(
|
|
array(
|
|
'page' => 'robotstxt-telemetry-logs',
|
|
'deleted' => 'all',
|
|
),
|
|
Robotstxt_Telemetry_Network::base_url()
|
|
)
|
|
);
|
|
exit;
|
|
}
|
|
|
|
if ( 'delete' !== $action ) {
|
|
return;
|
|
}
|
|
|
|
$log_id = isset( $_GET['log'] ) ? absint( wp_unslash( $_GET['log'] ) ) : 0;
|
|
// phpcs:enable
|
|
if ( 0 === $log_id ) {
|
|
return;
|
|
}
|
|
|
|
check_admin_referer( 'robotstxt_telemetry_delete_log_' . $log_id );
|
|
|
|
Robotstxt_Telemetry_DB::delete_log( $log_id );
|
|
|
|
wp_safe_redirect(
|
|
add_query_arg(
|
|
array(
|
|
'page' => 'robotstxt-telemetry-logs',
|
|
'deleted' => 1,
|
|
),
|
|
Robotstxt_Telemetry_Network::base_url()
|
|
)
|
|
);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Handle Network Admin settings actions.
|
|
*
|
|
* @param string $action Action name.
|
|
*/
|
|
private function handle_network_actions( $action ) {
|
|
if ( 'save_network_mode' === $action ) {
|
|
check_admin_referer( 'robotstxt_telemetry_network_mode' );
|
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified by check_admin_referer() above.
|
|
$new_mode = isset( $_POST['robotstxt_telemetry_config_mode'] ) ? sanitize_key( wp_unslash( $_POST['robotstxt_telemetry_config_mode'] ) ) : 'per-site';
|
|
$new_mode = Robotstxt_Telemetry_Network::sanitize_mode( $new_mode );
|
|
$old_mode = get_site_option( Robotstxt_Telemetry_Network::MODE_OPTION, 'per-site' );
|
|
|
|
if ( 'global' === $new_mode && 'global' !== $old_mode ) {
|
|
Robotstxt_Telemetry_Network::seed_network_settings();
|
|
}
|
|
|
|
update_site_option( Robotstxt_Telemetry_Network::MODE_OPTION, $new_mode );
|
|
|
|
$this->redirect_network_settings( 'mode-updated' );
|
|
}
|
|
|
|
if ( 'save_network_settings' === $action ) {
|
|
check_admin_referer( 'robotstxt_telemetry_network_settings' );
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified by check_admin_referer() above.
|
|
$hash = ! empty( $_POST['robotstxt_telemetry_hash_useragent_url'] );
|
|
$core = isset( $_POST['robotstxt_telemetry_wp_core_check'] ) ? sanitize_key( wp_unslash( $_POST['robotstxt_telemetry_wp_core_check'] ) ) : 'safe';
|
|
$themes = isset( $_POST['robotstxt_telemetry_wp_themes_check'] ) ? sanitize_key( wp_unslash( $_POST['robotstxt_telemetry_wp_themes_check'] ) ) : 'safe';
|
|
$plugins = isset( $_POST['robotstxt_telemetry_wp_plugins_check'] ) ? sanitize_key( wp_unslash( $_POST['robotstxt_telemetry_wp_plugins_check'] ) ) : 'safe';
|
|
$retention = isset( $_POST['robotstxt_telemetry_retention_period'] ) ? sanitize_key( wp_unslash( $_POST['robotstxt_telemetry_retention_period'] ) ) : '12hours';
|
|
$delete = ! empty( $_POST['robotstxt_telemetry_delete_on_uninstall'] );
|
|
// phpcs:enable
|
|
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_hash_useragent_url', $hash );
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_wp_core_check', $this->sanitize_mode( $core ) );
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_wp_themes_check', $this->sanitize_mode( $themes ) );
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_wp_plugins_check', $this->sanitize_mode( $plugins ) );
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_retention_period', $this->sanitize_retention( $retention ) );
|
|
Robotstxt_Telemetry_Network::update_setting( 'robotstxt_telemetry_delete_on_uninstall', $delete );
|
|
|
|
$this->redirect_network_settings( 'settings-updated' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Redirect back to the Network Admin settings screen.
|
|
*
|
|
* @param string $flag Query flag to append.
|
|
*/
|
|
private function redirect_network_settings( $flag ) {
|
|
wp_safe_redirect(
|
|
add_query_arg(
|
|
array(
|
|
'page' => 'robotstxt-telemetry',
|
|
$flag => 'true',
|
|
),
|
|
network_admin_url( 'admin.php' )
|
|
)
|
|
);
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Render the settings saved notice.
|
|
*/
|
|
public function render_saved_notice() {
|
|
if ( ! current_user_can( Robotstxt_Telemetry_Network::manage_capability() ) ) {
|
|
return;
|
|
}
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only rendering of an admin screen.
|
|
$settings_updated = isset( $_GET['settings-updated'] ) ? sanitize_key( wp_unslash( $_GET['settings-updated'] ) ) : '';
|
|
$mode_updated = isset( $_GET['mode-updated'] ) ? sanitize_key( wp_unslash( $_GET['mode-updated'] ) ) : '';
|
|
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
|
|
// phpcs:enable
|
|
|
|
if ( 'robotstxt-telemetry' !== $page ) {
|
|
return;
|
|
}
|
|
|
|
if ( 'true' === $settings_updated ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'robotstxt-telemetry' ) . '</p></div>';
|
|
}
|
|
|
|
if ( 'true' === $mode_updated ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Configuration mode saved.', 'robotstxt-telemetry' ) . '</p></div>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render the Network Admin settings page (Multisite only).
|
|
*/
|
|
public function render_network_settings_page() {
|
|
if ( ! current_user_can( 'manage_network_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
$mode = get_site_option( Robotstxt_Telemetry_Network::MODE_OPTION, 'per-site' );
|
|
$is_global = Robotstxt_Telemetry_Network::is_global();
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__( 'Telemetry Network Settings', 'robotstxt-telemetry' ) . '</h1>';
|
|
|
|
echo '<form method="post" action="' . esc_url( add_query_arg( 'page', 'robotstxt-telemetry', network_admin_url( 'admin.php' ) ) ) . '">';
|
|
wp_nonce_field( 'robotstxt_telemetry_network_mode' );
|
|
echo '<input type="hidden" name="action" value="save_network_mode" />';
|
|
echo '<input type="hidden" name="page" value="robotstxt-telemetry" />';
|
|
|
|
echo '<table class="form-table" role="presentation">';
|
|
echo '<tbody>';
|
|
echo '<tr>';
|
|
echo '<th scope="row">' . esc_html__( 'Configuration mode', 'robotstxt-telemetry' ) . '</th>';
|
|
echo '<td>';
|
|
echo '<fieldset>';
|
|
echo '<legend class="screen-reader-text">' . esc_html__( 'Configuration mode', 'robotstxt-telemetry' ) . '</legend>';
|
|
echo '<p style="margin: 0;">';
|
|
echo '<label><input type="radio" name="robotstxt_telemetry_config_mode" value="per-site" ' . checked( $mode, 'per-site', false ) . '/> ' . esc_html__( 'Per-site', 'robotstxt-telemetry' ) . '</label>';
|
|
echo ' ';
|
|
echo '<label><input type="radio" name="robotstxt_telemetry_config_mode" value="global" ' . checked( $mode, 'global', false ) . '/> ' . esc_html__( 'Global', 'robotstxt-telemetry' ) . '</label>';
|
|
echo '</p>';
|
|
echo '<p class="description">' . esc_html__( 'Per-site: every site keeps its own settings and its own log table, managed by each site administrator. Global: one shared configuration and one central log on the main site collect the outbound requests of the whole network; the Telemetry screens are then managed from here only.', 'robotstxt-telemetry' ) . '</p>';
|
|
echo '</fieldset>';
|
|
echo '</td>';
|
|
echo '</tr></tbody>';
|
|
echo '</table>';
|
|
|
|
submit_button( __( 'Save configuration mode', 'robotstxt-telemetry' ) );
|
|
echo '</form>';
|
|
|
|
if ( ! $is_global ) {
|
|
echo '<p>' . esc_html__( 'Switch to Global mode to manage the settings and the logs for the whole network from this screen.', 'robotstxt-telemetry' ) . '</p>';
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
echo '<h2>' . esc_html__( 'Network settings', 'robotstxt-telemetry' ) . '</h2>';
|
|
|
|
echo '<form method="post" action="' . esc_url( add_query_arg( 'page', 'robotstxt-telemetry', network_admin_url( 'admin.php' ) ) ) . '">';
|
|
wp_nonce_field( 'robotstxt_telemetry_network_settings' );
|
|
echo '<input type="hidden" name="action" value="save_network_settings" />';
|
|
echo '<input type="hidden" name="page" value="robotstxt-telemetry" />';
|
|
|
|
$this->render_settings_fields();
|
|
|
|
submit_button();
|
|
echo '</form>';
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Render settings page.
|
|
*/
|
|
public function render_settings_page() {
|
|
if ( ! current_user_can( Robotstxt_Telemetry_Network::manage_capability() ) ) {
|
|
return;
|
|
}
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__( 'Telemetry Settings', 'robotstxt-telemetry' ) . '</h1>';
|
|
|
|
$manager_notice = new Robotstxt_Telemetry_Manager_Notice();
|
|
$manager_notice->render_settings_notice();
|
|
|
|
echo '<form method="post" action="options.php">';
|
|
settings_fields( 'robotstxt_telemetry_settings' );
|
|
|
|
$this->render_settings_fields();
|
|
|
|
submit_button();
|
|
echo '</form>';
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Render the settings fields table.
|
|
*
|
|
* Values honor the configuration mode: per-site options or the
|
|
* shared network options in global mode.
|
|
*/
|
|
private function render_settings_fields() {
|
|
echo '<table class="form-table" role="presentation">';
|
|
echo '<tbody>';
|
|
|
|
echo '<tr>';
|
|
echo '<th scope="row">' . esc_html__( 'Outbound requests', 'robotstxt-telemetry' ) . '</th>';
|
|
echo '<td>';
|
|
echo '<label for="robotstxt-telemetry-hash-useragent">';
|
|
echo '<input type="checkbox" id="robotstxt-telemetry-hash-useragent" name="robotstxt_telemetry_hash_useragent_url" value="1" ' . checked( (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_hash_useragent_url', true ), true, false ) . '/>';
|
|
echo '<span>' . esc_html__( 'Hash the site URL in the User-Agent of outbound requests', 'robotstxt-telemetry' ) . '</span>';
|
|
echo '</label>';
|
|
echo '<p class="description">' . esc_html__( 'Outbound requests normally identify this site with its URL (for example "WordPress/6.9; https://example.com/"). When enabled, the URL is replaced with a fixed hash so the site is no longer identifiable.', 'robotstxt-telemetry' ) . '</p>';
|
|
echo '</td>';
|
|
echo '</tr>';
|
|
|
|
$wp_checks = array(
|
|
'robotstxt_telemetry_wp_core_check' => array(
|
|
'label' => __( 'WordPress Core version check', 'robotstxt-telemetry' ),
|
|
'desc' => __( 'Safe sends only the WordPress version, PHP version, locale, MySQL version, and update channel. Original also sends site counts, database history, PHP extensions, and platform details.', 'robotstxt-telemetry' ),
|
|
),
|
|
'robotstxt_telemetry_wp_themes_check' => array(
|
|
'label' => __( 'WordPress Themes version check', 'robotstxt-telemetry' ),
|
|
'desc' => __( 'Safe sends only the theme name, version, update URI, template, and stylesheet, plus translation dates and locale. Original also sends author details and other theme metadata.', 'robotstxt-telemetry' ),
|
|
),
|
|
'robotstxt_telemetry_wp_plugins_check' => array(
|
|
'label' => __( 'WordPress Plugins version check', 'robotstxt-telemetry' ),
|
|
'desc' => __( 'Safe sends only the plugin version, update URI, and requirements, plus translation dates, locale, and the "all" flag. Original also sends names, descriptions, authors, and other plugin metadata.', 'robotstxt-telemetry' ),
|
|
),
|
|
);
|
|
|
|
foreach ( $wp_checks as $option_name => $check ) {
|
|
$mode = Robotstxt_Telemetry_Network::get_setting( $option_name, 'safe' );
|
|
echo '<tr>';
|
|
echo '<th scope="row">' . esc_html( $check['label'] ) . '</th>';
|
|
echo '<td>';
|
|
echo '<fieldset>';
|
|
echo '<legend class="screen-reader-text">' . esc_html( $check['label'] ) . '</legend>';
|
|
echo '<p style="margin: 0;">';
|
|
echo '<label><input type="radio" name="' . esc_attr( $option_name ) . '" value="original" ' . checked( $mode, 'original', false ) . '/> ' . esc_html__( 'Original', 'robotstxt-telemetry' ) . '</label>';
|
|
echo ' ';
|
|
echo '<label><input type="radio" name="' . esc_attr( $option_name ) . '" value="safe" ' . checked( $mode, 'safe', false ) . '/> ' . esc_html__( 'Safe', 'robotstxt-telemetry' ) . '</label>';
|
|
echo '</p>';
|
|
echo '<p class="description">' . esc_html( $check['desc'] ) . '</p>';
|
|
echo '</fieldset>';
|
|
echo '</td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
$retention = Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_retention_period', '12hours' );
|
|
|
|
echo '<tr>';
|
|
echo '<th scope="row">' . esc_html__( 'Log retention', 'robotstxt-telemetry' ) . '</th>';
|
|
echo '<td>';
|
|
echo '<label class="screen-reader-text" for="robotstxt-telemetry-retention">' . esc_html__( 'Log retention', 'robotstxt-telemetry' ) . '</label>';
|
|
echo '<select id="robotstxt-telemetry-retention" name="robotstxt_telemetry_retention_period">';
|
|
|
|
$retention_options = array(
|
|
'12hours' => __( '12 hours', 'robotstxt-telemetry' ),
|
|
'1day' => __( '1 day', 'robotstxt-telemetry' ),
|
|
'3days' => __( '3 days', 'robotstxt-telemetry' ),
|
|
);
|
|
|
|
foreach ( $retention_options as $value => $label ) {
|
|
printf(
|
|
'<option value="%s" %s>%s</option>',
|
|
esc_attr( $value ),
|
|
selected( $retention, $value, false ),
|
|
esc_html( $label )
|
|
);
|
|
}
|
|
|
|
echo '</select>';
|
|
echo '<p class="description">' . esc_html__( 'Logs older than the selected period are deleted (12 hours by default). A maximum of 1000 entries is always kept; the cleanup runs when browsing the logs and twice a day automatically.', 'robotstxt-telemetry' ) . '</p>';
|
|
echo '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<th scope="row">' . esc_html__( 'Uninstall behavior', 'robotstxt-telemetry' ) . '</th>';
|
|
echo '<td>';
|
|
echo '<label for="robotstxt-telemetry-delete-on-uninstall">';
|
|
echo '<input type="checkbox" id="robotstxt-telemetry-delete-on-uninstall" name="robotstxt_telemetry_delete_on_uninstall" value="1" ' . checked( (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_delete_on_uninstall', false ), true, false ) . '/>';
|
|
echo '<span>' . esc_html__( 'Delete all telemetry logs and plugin options on uninstall', 'robotstxt-telemetry' ) . '</span>';
|
|
echo '</label>';
|
|
echo '<p class="description">' . esc_html__( 'By default, all telemetry logs are preserved when the plugin is uninstalled.', 'robotstxt-telemetry' ) . '</p>';
|
|
echo '</td>';
|
|
echo '</tr></tbody>';
|
|
echo '</table>';
|
|
}
|
|
|
|
/**
|
|
* Render logs page.
|
|
*/
|
|
public function render_logs_page() {
|
|
$capability = Robotstxt_Telemetry_Network::is_global() ? 'manage_network_options' : Robotstxt_Telemetry_Network::manage_capability();
|
|
|
|
if ( ! current_user_can( $capability ) ) {
|
|
return;
|
|
}
|
|
|
|
require_once ROBOTSTXT_TELEMETRY_PLUGIN_DIR . '/includes/class-robotstxt-telemetry-logs-table.php';
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only rendering of an admin screen.
|
|
$view_action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
|
|
$log_id = isset( $_GET['log'] ) ? absint( wp_unslash( $_GET['log'] ) ) : 0;
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__( 'Telemetry Logs', 'robotstxt-telemetry' ) . '</h1>';
|
|
|
|
if ( 'view' === $view_action && $log_id > 0 ) {
|
|
$this->render_single_log( $log_id );
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
if ( 'confirm_delete_all' === $view_action ) {
|
|
$this->render_delete_all_confirm();
|
|
echo '</div>';
|
|
return;
|
|
}
|
|
|
|
// Apply the retention policy before listing.
|
|
Robotstxt_Telemetry_DB::cleanup();
|
|
|
|
$deleted_notice = isset( $_GET['deleted'] ) ? sanitize_key( wp_unslash( $_GET['deleted'] ) ) : '';
|
|
// phpcs:enable
|
|
if ( 'all' === $deleted_notice ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'All log entries deleted.', 'robotstxt-telemetry' ) . '</p></div>';
|
|
} elseif ( ! empty( $deleted_notice ) ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Log entry deleted.', 'robotstxt-telemetry' ) . '</p></div>';
|
|
}
|
|
|
|
$delete_all_url = wp_nonce_url(
|
|
add_query_arg(
|
|
array(
|
|
'page' => 'robotstxt-telemetry-logs',
|
|
'action' => 'confirm_delete_all',
|
|
),
|
|
Robotstxt_Telemetry_Network::base_url()
|
|
),
|
|
'robotstxt_telemetry_delete_all_confirm'
|
|
);
|
|
|
|
echo '<p><a href="' . esc_url( $delete_all_url ) . '" class="button">' . esc_html__( 'Delete all logs', 'robotstxt-telemetry' ) . '</a></p>';
|
|
|
|
$table = new Robotstxt_Telemetry_Logs_Table();
|
|
$table->prepare_items();
|
|
|
|
echo '<form method="get">';
|
|
echo '<input type="hidden" name="page" value="robotstxt-telemetry-logs" />';
|
|
$table->display();
|
|
echo '</form>';
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Render the delete-all confirmation screen.
|
|
*/
|
|
private function render_delete_all_confirm() {
|
|
check_admin_referer( 'robotstxt_telemetry_delete_all_confirm' );
|
|
|
|
echo '<div class="notice notice-warning"><p>' . esc_html__( 'You are about to permanently delete all telemetry log entries. This action cannot be undone.', 'robotstxt-telemetry' ) . '</p></div>';
|
|
|
|
$logs_page_url = add_query_arg( 'page', 'robotstxt-telemetry-logs', Robotstxt_Telemetry_Network::base_url() );
|
|
|
|
echo '<form method="post" action="' . esc_url( $logs_page_url ) . '">';
|
|
wp_nonce_field( 'robotstxt_telemetry_delete_all_logs' );
|
|
echo '<input type="hidden" name="action" value="delete_all" />';
|
|
echo '<input type="hidden" name="page" value="robotstxt-telemetry-logs" />';
|
|
submit_button( __( 'Yes, delete all logs', 'robotstxt-telemetry' ), 'delete' );
|
|
echo ' <a class="button" href="' . esc_url( $logs_page_url ) . '">' . esc_html__( 'Cancel', 'robotstxt-telemetry' ) . '</a>';
|
|
echo '</form>';
|
|
}
|
|
|
|
/**
|
|
* Render a single log entry.
|
|
*
|
|
* @param int $log_id Log ID.
|
|
*/
|
|
private function render_single_log( $log_id ) {
|
|
$log = Robotstxt_Telemetry_DB::get_log( $log_id );
|
|
|
|
if ( empty( $log ) ) {
|
|
echo '<p>' . esc_html__( 'Log entry not found.', 'robotstxt-telemetry' ) . '</p>';
|
|
return;
|
|
}
|
|
|
|
echo '<p><a href="' . esc_url( add_query_arg( 'page', 'robotstxt-telemetry-logs', Robotstxt_Telemetry_Network::base_url() ) ) . '">' . esc_html__( 'Back to logs', 'robotstxt-telemetry' ) . '</a></p>';
|
|
echo '<table class="widefat striped">';
|
|
echo '<tbody>';
|
|
|
|
$this->render_log_row( __( 'Date', 'robotstxt-telemetry' ), $log['created_at'] );
|
|
$this->render_log_row( __( 'Method', 'robotstxt-telemetry' ), $log['method'] );
|
|
$this->render_log_row( __( 'URL', 'robotstxt-telemetry' ), $log['url'] );
|
|
$this->render_log_row( __( 'Host', 'robotstxt-telemetry' ), $log['host'] );
|
|
$this->render_log_row( __( 'Path', 'robotstxt-telemetry' ), $log['path'] );
|
|
$this->render_log_row( __( 'Body Params', 'robotstxt-telemetry' ), $this->format_params_field( $log, 'body' ) );
|
|
$this->render_log_row( __( 'Headers', 'robotstxt-telemetry' ), $this->format_json_field( $log['headers_json'] ) );
|
|
$this->render_log_row( __( 'User Agent', 'robotstxt-telemetry' ), $log['user_agent'] );
|
|
$this->render_log_row( __( 'Raw Body', 'robotstxt-telemetry' ), $log['raw_body'] );
|
|
$this->render_log_row( __( 'Caller', 'robotstxt-telemetry' ), $log['caller'] );
|
|
|
|
echo '</tbody>';
|
|
echo '</table>';
|
|
|
|
$this->render_telemetry_analysis( $log );
|
|
}
|
|
|
|
/**
|
|
* Render telemetry analysis section.
|
|
*
|
|
* @param array $log Log data.
|
|
*/
|
|
private function render_telemetry_analysis( $log ) {
|
|
$sections = Robotstxt_Telemetry_Analysis::build_sections( $log );
|
|
|
|
if ( empty( $sections ) ) {
|
|
return;
|
|
}
|
|
|
|
echo '<h2>' . esc_html__( 'Telemetry Analysis', 'robotstxt-telemetry' ) . '</h2>';
|
|
echo '<table class="widefat striped">';
|
|
echo '<tbody>';
|
|
|
|
foreach ( $sections as $label => $value ) {
|
|
$this->render_log_row( $label, $value );
|
|
}
|
|
|
|
echo '</tbody>';
|
|
echo '</table>';
|
|
}
|
|
|
|
/**
|
|
* Render a table row.
|
|
*
|
|
* @param string $label Label.
|
|
* @param string $value Value.
|
|
*/
|
|
private function render_log_row( $label, $value ) {
|
|
echo '<tr>';
|
|
echo '<th scope="row" style="width: 180px; vertical-align: top;">' . esc_html( $label ) . '</th>';
|
|
echo '<td><pre style="white-space: pre-wrap; margin: 0;">' . esc_html( (string) $value ) . '</pre></td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
/**
|
|
* Format JSON fields for display.
|
|
*
|
|
* @param string|null $value JSON string.
|
|
* @return string
|
|
*/
|
|
private function format_json_field( $value ) {
|
|
if ( empty( $value ) ) {
|
|
return '';
|
|
}
|
|
|
|
$decoded = json_decode( $value, true );
|
|
if ( null !== $decoded && JSON_ERROR_NONE === json_last_error() ) {
|
|
return (string) wp_json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Format query/body params as line-per-parameter output.
|
|
*
|
|
* @param array $log Log data.
|
|
* @param string $type Param source.
|
|
* @return string
|
|
*/
|
|
private function format_params_field( $log, $type ) {
|
|
$params = array();
|
|
|
|
if ( 'body' === $type ) {
|
|
$params = Robotstxt_Telemetry_Analysis::extract_body_params( $log );
|
|
}
|
|
|
|
if ( empty( $params ) ) {
|
|
return '';
|
|
}
|
|
|
|
return Robotstxt_Telemetry_Analysis::format_params_lines( $params );
|
|
}
|
|
}
|
|
}
|