P0 security: - PRV_Gateway_Client: remove response body from RuntimeException (HTTP status only) - PRV_Capture_Writer: allowlist-only write (prompt/response text, never headers/auth) - Mandatory capture order: can_afford → probe → persist_result → update_row_cost → write_meta → capture_io - capture_io last and wrapped in swallowing try/catch New tables (SCHEMA_VERSION 3): - prv_call_meta: cost/metadata per call, kept indefinitely - prv_call_io: rendered prompt + raw response, pruned on retention window (default 90d) New classes: - PRV_Call_Meta_Table, PRV_Call_Io_Table: table schema + dbDelta - PRV_Capture_Writer: metadata + I/O write; security allowlist enforced - PRV_Prune_Cron: daily prv_daily_prune cron deletes aged prv_call_io rows only - PRV_Cost_Rollup_Query: MTD summary, model breakdown, drill-down, projection - PRV_Call_Log_Query: paginated + filtered call-log query + per-call I/O fetch - PRV_Costs_Page + PRV_Costs_Renderer: Costs admin sub-page (cap progress, bento, model table, drill-down) - PRV_Call_Log_Page + PRV_Call_Log_Table_Renderer: Call Log admin sub-page (filter bar, paginated table, WCAG AA pagination) - PRV_Call_Drawer_Renderer: non-modal complementary drawer (5 states: Normal/Pruned/Legacy/Failed/Loading) - PRV_Call_Drawer_Script: inline JS for drawer (300-line split from renderer) - PRV_Call_Detail_Ajax: wp_ajax_prv_call_detail AJAX handler - PRV_Admin_Page_Css: CSS provider (300-line split from PRV_Admin_Page) - PRV_Probe_Run_Executor: inner probe loop (300-line split from PRV_Probe_Runner) Updated: - PRV_Probe_Result: tokens_in/tokens_out params + getters - Both providers: report tokens_in/tokens_out from usage.prompt_tokens/completion_tokens - PRV_Probe_Runner: slimmed to lock+UUID+delegation; injects PRV_Capture_Writer - PRV_Config: get_io_retention_days(); PRV_IO_RETENTION_DEFAULT_DAYS constant - PRV_Upgrader/Activator/Deactivator/Plugin: register new tables, cron, pages, AJAX - Settings: retention days field + save in controller - uninstall.php: DROP both new tables, clear prv_daily_prune cron - Subnav 4-tab strip (Dashboard | Costs | Call Log | Settings) on all pages - ARCHITECTURE.md, CHANGELOG.md, CONTEXT.md, CONVENTIONS.md updated Tests: - test-capture-writer.php: write_meta, write_io, P0 security (401 record has no key/Bearer/sk-or-) - test-cost-audit.php: MTD reconciliation, projection, prune, forced capture_io exception, table drops Agent-Session: peptiderepo-geomonitor-v0.3.0-cost-audit
212 lines
6.1 KiB
PHP
212 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* PR Vision top-level admin page.
|
|
*
|
|
* @package PrVision
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* PR Vision top-level admin page.
|
|
*
|
|
* Registers the "PR Vision" menu item (manage_options), enqueues Chart.js
|
|
* and the dashboard stylesheet, handles the "Run now" POST action (nonce +
|
|
* capability), and delegates rendering to the registered panels via
|
|
* PRV_Collector_Registry.
|
|
*
|
|
* Who triggers: PRV_Plugin::init() → is_admin() guard.
|
|
* Dependencies: PRV_Collector_Registry, PRV_Probe_Runner.
|
|
*
|
|
* @see class-prv-plugin.php — Calls register_hooks().
|
|
* @see class-prv-collector-registry.php — Provides panels.
|
|
* @see class-prv-probe-runner.php — Invoked by the Run now action.
|
|
* @see class-prv-ai-visibility-panel.php — Panel renderer, consumes CSS added here.
|
|
* @see ARCHITECTURE.md — §Admin dashboard.
|
|
* @package PrVision
|
|
*/
|
|
class PRV_Admin_Page {
|
|
|
|
/**
|
|
* Admin menu slug.
|
|
*/
|
|
const MENU_SLUG = 'pr-vision';
|
|
|
|
/**
|
|
* Nonce action for the Run now form.
|
|
*/
|
|
const NONCE_ACTION = 'prv_run_now';
|
|
|
|
/**
|
|
* Nonce field name.
|
|
*/
|
|
const NONCE_FIELD = 'prv_nonce';
|
|
|
|
/**
|
|
* Register WordPress admin hooks.
|
|
*
|
|
* Side effects: Adds admin_menu and admin_post actions.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_hooks(): void {
|
|
add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
|
|
add_action( 'admin_post_prv_run_now', array( $this, 'handle_run_now' ) );
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
|
|
}
|
|
|
|
/**
|
|
* Add the "PR Vision" top-level menu page.
|
|
*
|
|
* Side effects: Adds a WP admin menu entry.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add_menu_page(): void {
|
|
add_menu_page(
|
|
__( 'PR Vision', 'pr-vision' ),
|
|
__( 'PR Vision', 'pr-vision' ),
|
|
'manage_options',
|
|
self::MENU_SLUG,
|
|
array( $this, 'render_page' ),
|
|
'dashicons-chart-line',
|
|
58
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Enqueue Chart.js and dashboard styles on the PR Vision page only.
|
|
*
|
|
* Chart.js is loaded from the jsdelivr CDN. The dashboard panel degrades
|
|
* gracefully when the CDN is blocked (typeof guard + .prv-noscript fallback).
|
|
*
|
|
* @param string $hook Current admin page hook.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function enqueue_assets( string $hook ): void {
|
|
if ( false === strpos( $hook, self::MENU_SLUG ) ) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_script(
|
|
'prv-chartjs',
|
|
'https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js',
|
|
array(),
|
|
'4.4.3',
|
|
true
|
|
);
|
|
|
|
wp_add_inline_style( 'wp-admin', $this->get_dashboard_css() );
|
|
}
|
|
|
|
/**
|
|
* Handle the "Run now" POST action.
|
|
*
|
|
* Validates nonce and capability before dispatching to PRV_Probe_Runner.
|
|
* Redirects back to the admin page with a status query arg.
|
|
*
|
|
* Side effects: Triggers a full probe run; redirects.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle_run_now(): void {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'Insufficient permissions.', 'pr-vision' ), 403 );
|
|
}
|
|
|
|
$nonce = isset( $_POST[ self::NONCE_FIELD ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::NONCE_FIELD ] ) ) : '';
|
|
if ( ! wp_verify_nonce( $nonce, self::NONCE_ACTION ) ) {
|
|
wp_die( esc_html__( 'Security check failed.', 'pr-vision' ), 403 );
|
|
}
|
|
|
|
$runner = new PRV_Probe_Runner();
|
|
$counts = $runner->run();
|
|
|
|
$redirect = add_query_arg(
|
|
array(
|
|
'page' => self::MENU_SLUG,
|
|
'prv_run_done' => 1,
|
|
'prv_probed' => (int) $counts['probed'],
|
|
'prv_skipped' => (int) ( $counts['skipped_budget'] + $counts['skipped_error'] ),
|
|
),
|
|
admin_url( 'admin.php' )
|
|
);
|
|
|
|
wp_safe_redirect( $redirect );
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Render the full PR Vision admin page.
|
|
*
|
|
* Side effects: Outputs HTML directly.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function render_page(): void {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
wp_die( esc_html__( 'Insufficient permissions.', 'pr-vision' ) );
|
|
}
|
|
|
|
echo '<div class="wrap">';
|
|
echo '<h1>' . esc_html__( 'PR Vision — AI Visibility', 'pr-vision' ) . '</h1>';
|
|
|
|
$this->render_run_done_notice();
|
|
|
|
echo '<form method="post" action="' . esc_url( admin_url( 'admin-post.php' ) ) . '">';
|
|
echo '<input type="hidden" name="action" value="prv_run_now">';
|
|
wp_nonce_field( self::NONCE_ACTION, self::NONCE_FIELD );
|
|
submit_button( __( 'Run now', 'pr-vision' ), 'secondary', 'prv_run_now_btn', false );
|
|
echo '</form>';
|
|
|
|
$registry = PRV_Collector_Registry::instance();
|
|
|
|
foreach ( $registry->get_collectors() as $key => $collector ) {
|
|
$panel = $registry->get_panel( $key );
|
|
if ( null === $panel ) {
|
|
continue;
|
|
}
|
|
$data = $collector->collect();
|
|
$panel->render( $data );
|
|
}
|
|
|
|
echo '</div>';
|
|
}
|
|
|
|
/**
|
|
* Render a success or partial notice after a Run now redirect.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function render_run_done_notice(): void {
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
if ( empty( $_GET['prv_run_done'] ) ) {
|
|
return;
|
|
}
|
|
$probed = isset( $_GET['prv_probed'] ) ? absint( wp_unslash( $_GET['prv_probed'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, Generic.Formatting.MultipleStatementAlignment.NotSameWarning, Generic.Formatting.MultipleStatementAlignment.IncorrectWarning
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
|
|
$skipped = isset( $_GET['prv_skipped'] ) ? absint( wp_unslash( $_GET['prv_skipped'] ) ) : 0;
|
|
|
|
echo '<div class="notice notice-success is-dismissible"><p>';
|
|
echo esc_html(
|
|
sprintf(
|
|
/* translators: 1: probed count, 2: skipped count */
|
|
__( 'Run complete: %1$d probes collected, %2$d skipped (budget or error).', 'pr-vision' ),
|
|
$probed,
|
|
$skipped
|
|
)
|
|
);
|
|
echo '</p></div>';
|
|
} /**
|
|
* Return the combined CSS string for all PR Vision admin screens.
|
|
*
|
|
* Delegates to PRV_Admin_Page_Css to keep this class under 300 lines.
|
|
*
|
|
* @return string CSS string.
|
|
*/
|
|
private function get_dashboard_css(): string {
|
|
return PRV_Admin_Page_Css::get();
|
|
}
|
|
}
|