v0.6.1
This commit is contained in:
parent
8a85a4d9af
commit
e9782eaa82
1 changed files with 1711 additions and 440 deletions
|
|
@ -18,17 +18,26 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
/**
|
||||
* Register hooks.
|
||||
*/
|
||||
public function register(): void {
|
||||
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(): void {
|
||||
public function register_menu() {
|
||||
if ( Robotstxt_Telemetry_Network::is_global() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_menu_page(
|
||||
__( 'Telemetry', 'robotstxt-telemetry' ),
|
||||
__( 'Telemetry', 'robotstxt-telemetry' ),
|
||||
|
|
@ -58,19 +67,89 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Register the uninstall behavior setting.
|
||||
* 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_settings(): void {
|
||||
register_setting(
|
||||
'robotstxt_telemetry_settings',
|
||||
'robotstxt_telemetry_delete_on_uninstall',
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
'sanitize_callback' => array( $this, 'sanitize_checkbox' ),
|
||||
'show_in_rest' => false,
|
||||
)
|
||||
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';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -79,23 +158,55 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
* @param mixed $value Raw input value.
|
||||
* @return bool
|
||||
*/
|
||||
public function sanitize_checkbox( $value ): bool {
|
||||
public function sanitize_checkbox( $value ) {
|
||||
return (bool) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle admin actions.
|
||||
*/
|
||||
public function handle_actions(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
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;
|
||||
}
|
||||
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- State change is protected by check_admin_referer() below.
|
||||
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
|
||||
$action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : '';
|
||||
// In global mode the shared logs belong to the Network Admin only.
|
||||
$capability = Robotstxt_Telemetry_Network::is_global() ? 'manage_network_options' : 'manage_options';
|
||||
|
||||
if ( 'robotstxt-telemetry-logs' !== $page || 'delete' !== $action ) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -115,7 +226,71 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
'page' => 'robotstxt-telemetry-logs',
|
||||
'deleted' => 1,
|
||||
),
|
||||
admin_url( 'admin.php' )
|
||||
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;
|
||||
|
|
@ -124,28 +299,95 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
/**
|
||||
* Render the settings saved notice.
|
||||
*/
|
||||
public function render_saved_notice(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
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 ( 'true' !== $settings_updated || 'robotstxt-telemetry' !== $page ) {
|
||||
if ( 'robotstxt-telemetry' !== $page ) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'robotstxt-telemetry' ) . '</p></div>';
|
||||
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(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
public function render_settings_page() {
|
||||
if ( ! current_user_can( Robotstxt_Telemetry_Network::manage_capability() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -158,29 +400,115 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
echo '<form method="post" action="options.php">';
|
||||
settings_fields( 'robotstxt_telemetry_settings' );
|
||||
|
||||
echo '<table class="form-table" role="presentation">';
|
||||
echo '<tbody><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) get_option( '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>';
|
||||
$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(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
@ -199,12 +527,36 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
return;
|
||||
}
|
||||
|
||||
$deleted_notice = isset( $_GET['deleted'] ) ? absint( wp_unslash( $_GET['deleted'] ) ) : 0;
|
||||
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 ( $deleted_notice > 0 ) {
|
||||
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();
|
||||
|
||||
|
|
@ -215,12 +567,31 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
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( int $log_id ): void {
|
||||
private function render_single_log( $log_id ) {
|
||||
$log = Robotstxt_Telemetry_DB::get_log( $log_id );
|
||||
|
||||
if ( empty( $log ) ) {
|
||||
|
|
@ -228,7 +599,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
return;
|
||||
}
|
||||
|
||||
echo '<p><a href="' . esc_url( admin_url( 'admin.php?page=robotstxt-telemetry-logs' ) ) . '">' . esc_html__( 'Back to logs', 'robotstxt-telemetry' ) . '</a></p>';
|
||||
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>';
|
||||
|
||||
|
|
@ -254,7 +625,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
*
|
||||
* @param array $log Log data.
|
||||
*/
|
||||
private function render_telemetry_analysis( array $log ): void {
|
||||
private function render_telemetry_analysis( $log ) {
|
||||
$sections = Robotstxt_Telemetry_Analysis::build_sections( $log );
|
||||
|
||||
if ( empty( $sections ) ) {
|
||||
|
|
@ -279,7 +650,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
* @param string $label Label.
|
||||
* @param string $value Value.
|
||||
*/
|
||||
private function render_log_row( string $label, $value ): void {
|
||||
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>';
|
||||
|
|
@ -292,7 +663,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
* @param string|null $value JSON string.
|
||||
* @return string
|
||||
*/
|
||||
private function format_json_field( ?string $value ): string {
|
||||
private function format_json_field( $value ) {
|
||||
if ( empty( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
|
@ -312,7 +683,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_Admin' ) ) {
|
|||
* @param string $type Param source.
|
||||
* @return string
|
||||
*/
|
||||
private function format_params_field( array $log, string $type ): string {
|
||||
private function format_params_field( $log, $type ) {
|
||||
$params = array();
|
||||
|
||||
if ( 'body' === $type ) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue