'#2a78d6', 'failed' => '#eb6834', ); const SEVERITY_COLORS = array( 'critical' => '#9e1f1f', 'high' => '#e85d2a', 'medium' => '#e3a01c', 'low' => '#3b82d6', 'info' => '#a7aaad', ); private static $data = null; public static function load() { $screen = get_current_screen(); if ( ! $screen ) { return; } // The page lays its cards out itself; only the License / Pro box is a registered box. add_filter( 'screen_options_show_screen', '__return_false' ); wp_register_script( 'svabg-dashboard', false, array(), SOFTVOLT_AI_BODYGUARD_VERSION, true ); wp_enqueue_script( 'svabg-dashboard' ); wp_add_inline_script( 'svabg-dashboard', self::tooltip_js() ); if ( ! Softvolt_Ai_Bodyguard_Pro::is_active() ) { add_meta_box( 'svabg_box_pro', __( 'AI GuardWall Pro', 'ai-guardwall' ), array( __CLASS__, 'box_pro' ), $screen, 'column4' ); } } public static function wp_dashboard_setup() { if ( ! current_user_can( Softvolt_Ai_Bodyguard::cap() ) ) { return; } wp_add_dashboard_widget( 'svabg_wp_dashboard', __( 'AI GuardWall', 'ai-guardwall' ), array( __CLASS__, 'wp_widget' ) ); wp_add_inline_style( 'dashboard', '#svabg_wp_dashboard .svabg-status{display:flex;align-items:center;gap:7px;margin:0 0 10px;font-weight:600;color:#8a6d00}' . '#svabg_wp_dashboard .svabg-status.is-live{color:#007017}#svabg_wp_dashboard .svabg-status.is-danger{color:#b32d2e}' . '#svabg_wp_dashboard .svabg-status__dot{width:9px;height:9px;border-radius:50%;background:#dba617}' . '#svabg_wp_dashboard .is-live .svabg-status__dot{background:#00a32a;box-shadow:0 0 0 3px rgba(0,163,42,.18)}#svabg_wp_dashboard .is-danger .svabg-status__dot{background:#d63638}' . '#svabg_wp_dashboard .svabg-glance{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px 12px;margin:0 0 10px}' . '#svabg_wp_dashboard .svabg-glance li{display:flex;align-items:center;gap:6px;margin:0}#svabg_wp_dashboard .svabg-glance .dashicons{color:#8c8f94}' . '#svabg_wp_dashboard .svabg-glance a{text-decoration:none}#svabg_wp_dashboard .svabg-glance .is-hot a{color:#d63638;font-weight:600}' . '#svabg_wp_dashboard .svabg-wpw__links{margin:12px -12px -12px;padding:10px 12px;border-top:1px solid #f0f0f1;background:#f6f7f7}' ); } public static function wp_widget() { $d = self::data(); self::status_line( $d ); self::glance_list( $d ); echo ''; } /* ------------------------------ the page ------------------------------ */ /** * The dashboard cards, top to bottom: score and key numbers, activity and * findings charts, scan coverage, protection coverage and health, then * the latest findings, logins and the License / Pro box. * * @since 1.5.0 */ public static function render() { $d = self::data(); $range = isset( $_GET['range'] ) ? absint( $_GET['range'] ) : 14; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only picks how many days the chart shows. $range = in_array( $range, array( 7, 14, 30 ), true ) ? $range : 14; echo '
'; echo '
'; self::card_score( $d ); echo '
'; echo '
'; self::kpi_tiles( $d ); echo '
'; echo '
'; self::card_activity( $range ); echo '
'; echo '
'; self::card_severity( $d ); echo '
'; echo '
'; self::card_scan( $d ); echo '
'; echo '
'; self::card_coverage( $d ); echo '
'; echo '
'; self::card_head( __( 'Health check', 'ai-guardwall' ), __( 'Things only you can change', 'ai-guardwall' ) ); self::box_health(); echo '
'; echo '
'; self::card_head( __( 'Latest findings', 'ai-guardwall' ), __( 'Most severe first', 'ai-guardwall' ) ); self::box_findings(); echo '
'; echo '
'; echo '
'; self::card_head( __( 'Logins', 'ai-guardwall' ), __( 'Today', 'ai-guardwall' ) ); self::box_logins(); echo '
'; self::registered_boxes( 'column4' ); echo '
'; echo '
'; } private static function card_head( $title, $sub = '', $extra = '' ) { echo '

' . esc_html( $title ) . '

'; if ( '' !== $sub ) { echo '

' . esc_html( $sub ) . '

'; } echo '
'; if ( '' !== $extra ) { echo '
'; echo wp_kses_post( $extra ); echo '
'; } echo '
'; } /** The boxes add-ons and editions register for a context, each as a card. */ private static function registered_boxes( $context ) { global $wp_meta_boxes; $screen = get_current_screen(); $id = $screen ? $screen->id : ''; if ( '' === $id || empty( $wp_meta_boxes[ $id ][ $context ] ) ) { return; } foreach ( $wp_meta_boxes[ $id ][ $context ] as $boxes ) { foreach ( (array) $boxes as $box ) { if ( empty( $box['callback'] ) || ! is_callable( $box['callback'] ) ) { continue; } echo '
'; self::card_head( wp_strip_all_tags( $box['title'] ) ); call_user_func( $box['callback'], null, $box ); echo '
'; } } } /* ------------------------------ score ------------------------------ */ private static function card_score( $d ) { $s = $d['score']; $tone = in_array( $s['grade'], array( 'A', 'B' ), true ) ? 'good' : ( 'C' === $s['grade'] ? 'warn' : 'bad' ); $r = 70; $c = 2 * M_PI * $r; $len = max( 0.01, min( 1, $s['score'] / 100 ) ) * $c; $done = count( wp_list_filter( $d['steps'], array( 'done' => true ) ) ); $total = count( $d['steps'] ); self::card_head( __( 'Security score', 'ai-guardwall' ), __( 'Hardening, health checks and findings together', 'ai-guardwall' ) ); echo '
'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo '
' . esc_html( $s['score'] ) . '/ 100
'; echo '
'; echo '
'; echo '' . esc_html( sprintf( /* translators: %s: grade letter */ __( 'Grade %s', 'ai-guardwall' ), $s['grade'] ) ) . ''; self::status_line( $d ); echo '

' . esc_html( sprintf( /* translators: 1: points on, 2: points */ __( '%1$d of %2$d protection points on', 'ai-guardwall' ), $s['rules_on'], $s['rules'] ) ) . '

'; echo '
'; echo '
'; echo '
'; echo '

' . esc_html__( 'Setup', 'ai-guardwall' ) . ' ' . esc_html( sprintf( /* translators: 1: steps done, 2: steps */ __( '%1$d of %2$d done', 'ai-guardwall' ), $done, $total ) ) . '

'; self::progress( $total ? $done / $total : 0, 'good' ); echo ''; echo '
'; echo '

'; echo ' ' . esc_html__( 'Scan now', 'ai-guardwall' ) . ''; echo '' . esc_html__( 'Hardening wizard', 'ai-guardwall' ) . ''; echo '

'; } /* ------------------------------ key numbers ------------------------------ */ private static function kpi_tiles( $d ) { $fw = Softvolt_Ai_Bodyguard_Firewall::stats( 14 ); $blocked = array(); foreach ( $fw as $day => $row ) { $blocked[ $day ] = (int) $row['blocked']; } $failed = Softvolt_Ai_Bodyguard_Logger::daily( 'login_failed', 14 ); self::kpi( 'dashicons-shield-alt', __( 'Attacks blocked', 'ai-guardwall' ), __( 'last 7 days', 'ai-guardwall' ), $blocked, self::SERIES['blocked'], Softvolt_Ai_Bodyguard_Admin::tab_url( 'traffic' ) ); self::kpi( 'dashicons-lock', __( 'Failed logins', 'ai-guardwall' ), __( 'last 7 days', 'ai-guardwall' ), $failed, self::SERIES['failed'], admin_url( 'admin.php?page=' . Softvolt_Ai_Bodyguard_Admin::ACTIVITY . '&event=login_failed' ) ); // Open findings, split by severity. $c = $d['counts']; echo ''; echo '' . esc_html__( 'Open findings', 'ai-guardwall' ) . ''; echo '' . esc_html( number_format_i18n( $c['total'] ) ) . ''; echo ''; foreach ( self::SEVERITY_COLORS as $level => $color ) { if ( ! empty( $c[ $level ] ) ) { echo ''; } } if ( ! $c['total'] ) { echo ''; } echo ''; echo '' . esc_html( $c['critical'] ? sprintf( /* translators: %s: number */ _n( '%s critical', '%s critical', $c['critical'], 'ai-guardwall' ), number_format_i18n( $c['critical'] ) ) : sprintf( /* translators: %s: number */ __( '%s high · none critical', 'ai-guardwall' ), number_format_i18n( $c['high'] ) ) ) . ''; echo ''; // The last scan. $last = $d['last']; echo ''; echo '' . esc_html__( 'Last scan', 'ai-guardwall' ) . ''; if ( $last ) { $s = $last['stats_arr']; echo '' . esc_html( sprintf( /* translators: %s: time span */ __( '%s ago', 'ai-guardwall' ), human_time_diff( strtotime( $last['finished_at'] . ' UTC' ) ) ) ) . ''; echo '' . esc_html( sprintf( /* translators: 1: files, 2: duration */ __( '%1$s files in %2$s', 'ai-guardwall' ), number_format_i18n( isset( $s['files_scanned'] ) ? $s['files_scanned'] : 0 ), self::duration( isset( $s['duration'] ) ? $s['duration'] : 0 ) ) ) . ''; echo '' . esc_html( sprintf( /* translators: %s: number */ _n( '%s file changed since the scan before', '%s files changed since the scan before', isset( $s['changes'] ) ? (int) $s['changes'] : 0, 'ai-guardwall' ), number_format_i18n( isset( $s['changes'] ) ? $s['changes'] : 0 ) ) ) . ''; } else { echo '' . esc_html__( 'Never', 'ai-guardwall' ) . ''; echo '' . esc_html__( 'Run the first scan', 'ai-guardwall' ) . ''; } echo ''; } /** * A key number for the last 7 days of a 14-day series, the change against * the 7 days before and a sparkline of all 14 days. */ private static function kpi( $icon, $label, $period, array $series, $color, $url ) { $values = array_values( $series ); $now = array_sum( array_slice( $values, -7 ) ); $before = array_sum( array_slice( $values, 0, 7 ) ); if ( $before > 0 ) { $pct = (int) round( 100 * ( $now - $before ) / $before ); $delta = 0 === $pct ? __( 'same as the week before', 'ai-guardwall' ) : sprintf( /* translators: %s: percent change, with sign */ __( '%s vs the week before', 'ai-guardwall' ), ( $pct > 0 ? '▲ ' : '▼ ' ) . abs( $pct ) . '%' ); } else { $delta = $now ? __( 'none the week before', 'ai-guardwall' ) : __( 'quiet for two weeks', 'ai-guardwall' ); } echo ''; echo '' . esc_html( $label ) . ' ' . esc_html( $period ) . ''; echo '' . esc_html( number_format_i18n( $now ) ) . ''; self::sparkline( $series, $color, $label ); echo '' . esc_html( $delta ) . ''; echo ''; } private static function sparkline( array $series, $color, $label ) { $w = 240; $h = 44; $values = array_values( $series ); $days = array_keys( $series ); $n = count( $values ); $max = max( 1, max( $values ) ); $pts = array(); foreach ( $values as $i => $v ) { $pts[] = array( round( 4 + ( $w - 8 ) * ( $n > 1 ? $i / ( $n - 1 ) : 0 ), 2 ), round( $h - 5 - ( $h - 12 ) * $v / $max, 2 ) ); } $line = ''; foreach ( $pts as $i => $p ) { $line .= ( $i ? ' L' : 'M' ) . $p[0] . ' ' . $p[1]; } $area = $line . ' L' . $pts[ $n - 1 ][0] . ' ' . ( $h - 1 ) . ' L' . $pts[0][0] . ' ' . ( $h - 1 ) . ' Z'; $end = $pts[ $n - 1 ]; echo ''; echo ''; echo ''; echo ''; foreach ( $pts as $i => $p ) { $slot = ( $w - 8 ) / max( 1, $n - 1 ); echo ''; } echo ''; } /* ------------------------------ activity chart ------------------------------ */ private static function card_activity( $range ) { $fw = Softvolt_Ai_Bodyguard_Firewall::stats( $range ); $failed = Softvolt_Ai_Bodyguard_Logger::daily( 'login_failed', $range ); $days = array_keys( $fw ); $series = array( 'blocked' => array( 'label' => __( 'Requests blocked', 'ai-guardwall' ), 'values' => array_map( function ( $row ) { return (int) $row['blocked']; }, array_values( $fw ) ), ), 'failed' => array( 'label' => __( 'Failed logins', 'ai-guardwall' ), 'values' => array_values( $failed ), ), ); $tabs = ''; /* translators: %d: days */ self::card_head( __( 'Threat activity', 'ai-guardwall' ), sprintf( _n( 'Blocked requests and failed logins, last %d day', 'Blocked requests and failed logins, last %d days', $range, 'ai-guardwall' ), $range ), $tabs ); echo ''; $w = 760; $h = 240; $left = 40; $right = 8; $top = 12; $base = $h - 28; $max = max( 1, max( array_merge( $series['blocked']['values'], $series['failed']['values'] ) ) ); $step = self::nice_step( $max / 4 ); $ymax = $step * 4; $n = count( $days ); $slot = ( $w - $left - $right ) / $n; $bar = min( 24, max( 3, ( $slot - 6 ) / 2 - 1 ) ); $empty = 0 === array_sum( $series['blocked']['values'] ) + array_sum( $series['failed']['values'] ); echo '
'; echo ''; for ( $i = 0; $i <= 4; $i++ ) { $y = round( $base - ( $base - $top ) * $i / 4, 2 ); echo ''; echo '' . esc_html( self::compact( $step * $i ) ) . ''; } $every = $n > 14 ? 5 : ( $n > 7 ? 2 : 1 ); foreach ( $days as $i => $day ) { $x0 = $left + $slot * $i; $cx = $x0 + $slot / 2; foreach ( array( 'blocked', 'failed' ) as $k => $key ) { $v = (int) $series[ $key ]['values'][ $i ]; if ( $v <= 0 ) { continue; } $bh = max( 2, ( $base - $top ) * $v / $ymax ); $x = 0 === $k ? $cx - $bar - 1 : $cx + 1; echo ''; } if ( 0 === ( $n - 1 - $i ) % $every ) { echo '' . esc_html( self::day_label( $day, $n > 7 ) ) . ''; } $tip = self::day_label( $day ) . ' · ' . sprintf( /* translators: %s: number */ __( '%s blocked', 'ai-guardwall' ), number_format_i18n( $series['blocked']['values'][ $i ] ) ) . ' · ' . sprintf( /* translators: %s: number */ __( '%s failed logins', 'ai-guardwall' ), number_format_i18n( $series['failed']['values'][ $i ] ) ); echo ''; } echo ''; echo ''; if ( $empty ) { echo '

' . esc_html__( 'All quiet: nothing blocked and no failed logins in this period.', 'ai-guardwall' ) . '

'; } echo '
'; // The same numbers as a table, for screen readers. echo ''; foreach ( $days as $i => $day ) { echo ''; } echo '
' . esc_html__( 'Requests blocked and failed logins per day', 'ai-guardwall' ) . '
' . esc_html__( 'Day', 'ai-guardwall' ) . '' . esc_html( $series['blocked']['label'] ) . '' . esc_html( $series['failed']['label'] ) . '
' . esc_html( self::day_label( $day ) ) . '' . esc_html( number_format_i18n( $series['blocked']['values'][ $i ] ) ) . '' . esc_html( number_format_i18n( $series['failed']['values'][ $i ] ) ) . '
'; $mode = Softvolt_Ai_Bodyguard_Settings::get( 'firewall_mode' ); $locked = Softvolt_Ai_Bodyguard_Lockouts::count_active(); echo '

' . esc_html( 'block' === $mode ? __( 'Firewall: blocking', 'ai-guardwall' ) : ( 'log' === $mode ? __( 'Firewall: log only', 'ai-guardwall' ) : __( 'Firewall: off', 'ai-guardwall' ) ) ) . ' ' . esc_html( sprintf( /* translators: %d: count active */ _n( '%d IP locked out right now.', '%d IPs locked out right now.', $locked, 'ai-guardwall' ), $locked ) ) . ' ' . esc_html__( 'Live traffic', 'ai-guardwall' ) . '

'; } /* ------------------------------ severity donut ------------------------------ */ private static function card_severity( $d ) { $c = $d['counts']; self::card_head( __( 'Open findings', 'ai-guardwall' ), __( 'By severity', 'ai-guardwall' ) ); $r = 62; $circ = 2 * M_PI * $r; $gap = $c['total'] > 1 ? 2.5 : 0; $start = 0; echo '
'; echo '
'; echo ''; foreach ( self::SEVERITY_COLORS as $level => $color ) { $v = isset( $c[ $level ] ) ? (int) $c[ $level ] : 0; if ( ! $v ) { continue; } $len = $circ * $v / $c['total']; $vis = max( 0.5, $len - $gap ); echo ''; $start += $len; } echo ''; echo '
' . esc_html( number_format_i18n( $c['total'] ) ) . '' . esc_html( _n( 'open', 'open', $c['total'], 'ai-guardwall' ) ) . '
'; echo '
'; echo ''; echo '
'; if ( ! $c['total'] ) { echo '

' . esc_html__( 'Nothing open. Run a scan now and then to keep it that way.', 'ai-guardwall' ) . '

'; } } /* ------------------------------ scan coverage ------------------------------ */ private static function card_scan( $d ) { $last = $d['last']; $s = $last ? $last['stats_arr'] : array(); $sub = $last ? sprintf( /* translators: 1: date, 2: files, 3: duration */ __( 'Finished %1$s · %2$s files · %3$s', 'ai-guardwall' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $last['finished_at'] . ' UTC' ) ), number_format_i18n( isset( $s['files_scanned'] ) ? $s['files_scanned'] : 0 ), self::duration( isset( $s['duration'] ) ? $s['duration'] : 0 ) ) : __( 'No scan yet', 'ai-guardwall' ); $tools = '' . esc_html__( 'Start new scan', 'ai-guardwall' ) . ' ' . esc_html__( 'Findings', 'ai-guardwall' ) . ''; self::card_head( __( 'Scan coverage', 'ai-guardwall' ), $sub, $tools ); self::stage_tracker( $last ); self::scan_trend(); } /** The areas a scan covers, grouped from the finding categories of the last scan. */ private static function stages() { return array( array( 'dashicons-wordpress', __( 'Core integrity', 'ai-guardwall' ), array( 'core' ) ), array( 'dashicons-admin-plugins', __( 'Plugins & themes', 'ai-guardwall' ), array( 'plugin', 'theme', 'mu', 'dropin' ) ), array( 'dashicons-upload', __( 'Uploads', 'ai-guardwall' ), array( 'upload' ) ), array( 'dashicons-media-code', __( 'Other files', 'ai-guardwall' ), array( 'root', 'other' ) ), array( 'dashicons-database', __( 'Database', 'ai-guardwall' ), array( 'db' ) ), array( 'dashicons-admin-generic', __( 'Configuration', 'ai-guardwall' ), array( 'config' ) ), array( 'dashicons-admin-users', __( 'Users & tasks', 'ai-guardwall' ), array( 'user', 'cron' ) ), ); } /** * One step per scanned area, joined by a line: clean, findings, or not scanned yet. * * @since 1.5.0 * @param array|null $last The last finished scan (Scanner::last_scan()). */ public static function stage_tracker( $last ) { $by_cat = $last && isset( $last['stats_arr']['by_category'] ) ? (array) $last['stats_arr']['by_category'] : array(); echo '
    '; foreach ( self::stages() as $stage ) { $n = 0; foreach ( $stage[2] as $cat ) { $n += isset( $by_cat[ $cat ] ) ? (int) $by_cat[ $cat ] : 0; } $state = ! $last ? 'wait' : ( $n ? 'warn' : 'ok' ); $text = 'wait' === $state ? __( 'Not scanned yet', 'ai-guardwall' ) : ( $n ? sprintf( /* translators: %s: number */ _n( '%s finding', '%s findings', $n, 'ai-guardwall' ), number_format_i18n( $n ) ) : __( 'Clean', 'ai-guardwall' ) ); echo '
  1. '; self::stage_icon( $state ); echo '' . esc_html( $stage[1] ) . '' . esc_html( $text ) . '
  2. '; } echo '
'; } /** * Three rings for the last scan: files checked, areas found clean and * open findings the AI analyst has reviewed. * * @since 1.5.0 * @param array $last The last finished scan (Scanner::last_scan()). */ public static function scan_rings( $last ) { $s = $last['stats_arr']; $files = isset( $s['files_scanned'] ) ? (int) $s['files_scanned'] : 0; $total = max( $files, isset( $s['total_files'] ) ? (int) $s['total_files'] : 0 ); $by_cat = isset( $s['by_category'] ) ? (array) $s['by_category'] : array(); $clean = 0; foreach ( self::stages() as $stage ) { $n = 0; foreach ( $stage[2] as $cat ) { $n += isset( $by_cat[ $cat ] ) ? (int) $by_cat[ $cat ] : 0; } $clean += $n ? 0 : 1; } $areas = count( self::stages() ); $open = 0; $reviewed = 0; foreach ( Softvolt_Ai_Bodyguard_Findings::count_by( 'ai_verdict' ) as $verdict => $n ) { $open += (int) $n; $reviewed += '' === (string) $verdict ? 0 : (int) $n; } echo '
'; self::ring( $total ? $files / $total : 1, __( 'Files checked', 'ai-guardwall' ), sprintf( /* translators: 1: files checked, 2: files */ __( '%1$s of %2$s files', 'ai-guardwall' ), number_format_i18n( $files ), number_format_i18n( $total ) ) ); self::ring( $clean / $areas, __( 'Areas clean', 'ai-guardwall' ), sprintf( /* translators: 1: clean areas, 2: areas */ __( '%1$d of %2$d areas without findings', 'ai-guardwall' ), $clean, $areas ) ); self::ring( $open ? $reviewed / $open : 1, __( 'Reviewed by the AI', 'ai-guardwall' ), $open ? sprintf( /* translators: 1: reviewed, 2: open findings */ __( '%1$s of %2$s open findings', 'ai-guardwall' ), number_format_i18n( $reviewed ), number_format_i18n( $open ) ) : __( 'Nothing open', 'ai-guardwall' ), $open && ! $reviewed ? Softvolt_Ai_Bodyguard_Admin::url( 'cleanup', 'findings' ) : '' ); echo '
'; } private static function ring( $fraction, $label, $sub, $url = '' ) { $fraction = max( 0, min( 1, $fraction ) ); $pct = (int) floor( 100 * $fraction ); $tone = $fraction >= 0.75 ? 'good' : ( $fraction >= 0.4 ? 'warn' : 'bad' ); $r = 30; $c = 2 * M_PI * $r; echo '
' . esc_html( $pct . '%' ) . ''; echo '' . esc_html( $label ) . '' . esc_html( $sub ) . ''; if ( '' !== $url ) { echo '' . esc_html__( 'Analyse with AI', 'ai-guardwall' ) . ''; } echo '
'; } /** A check, an exclamation mark or a clock, drawn crisp at any size. */ private static function stage_icon( $state ) { if ( 'ok' === $state ) { echo ''; } elseif ( 'warn' === $state ) { echo ''; } else { echo ''; } } /** Findings per finished scan, oldest first, as a small line chart. */ private static function scan_trend() { $scans = array_reverse( array_values( array_filter( Softvolt_Ai_Bodyguard_Scanner::scans( 12 ), function ( $scan ) { return 'finished' === $scan['status'] && isset( $scan['stats_arr']['findings'] ); } ) ) ); $scans = array_slice( $scans, -10 ); if ( count( $scans ) < 2 ) { return; } // The card spans the whole width, so the drawing is wide too and its text stays at 11-12px. $w = 1200; $h = 150; $left = 44; $top = 18; $base = $h - 24; $vals = array(); foreach ( $scans as $scan ) { $vals[] = (int) $scan['stats_arr']['findings']; } $step = self::nice_step( max( 1, max( $vals ) ) / 2 ); $ymax = $step * 2; $n = count( $vals ); $pts = array(); foreach ( $vals as $i => $v ) { $pts[] = array( round( $left + 12 + ( $w - $left - 24 ) * $i / ( $n - 1 ), 2 ), round( $base - ( $base - $top ) * $v / $ymax, 2 ) ); } $line = ''; foreach ( $pts as $i => $p ) { $line .= ( $i ? ' L' : 'M' ) . $p[0] . ' ' . $p[1]; } echo '

' . esc_html__( 'Findings per scan', 'ai-guardwall' ) . '

'; echo ''; for ( $i = 0; $i <= 2; $i++ ) { $y = round( $base - ( $base - $top ) * $i / 2, 2 ); echo ''; echo '' . esc_html( self::compact( $step * $i ) ) . ''; } echo ''; echo ''; foreach ( $scans as $i => $scan ) { $p = $pts[ $i ]; $when = wp_date( get_option( 'date_format' ), strtotime( $scan['finished_at'] . ' UTC' ) ); echo ''; echo '#' . esc_html( (int) $scan['id'] ) . ''; echo ''; } $last = $pts[ $n - 1 ]; echo '' . esc_html( number_format_i18n( $vals[ $n - 1 ] ) ) . ''; echo '
'; } /* ------------------------------ protection coverage ------------------------------ */ private static function card_coverage( $d ) { $per = array(); foreach ( Softvolt_Ai_Bodyguard_Hardening::rules() as $id => $rule ) { if ( ! empty( $rule['pro'] ) ) { continue; } $cat = $rule['category']; if ( ! isset( $per[ $cat ] ) ) { $per[ $cat ] = array( 0, 0 ); } ++$per[ $cat ][1]; if ( Softvolt_Ai_Bodyguard_Hardening::is_on( $id ) ) { ++$per[ $cat ][0]; } } $tools = '' . esc_html__( 'Hardening wizard', 'ai-guardwall' ) . ''; self::card_head( __( 'Protection coverage', 'ai-guardwall' ), __( 'Hardening rules switched on, by area', 'ai-guardwall' ), $tools ); echo ''; } private static function progress( $fraction, $tone, $label = '' ) { $pct = (int) round( 100 * max( 0, min( 1, $fraction ) ) ); echo ''; } /* ------------------------------ boxes ------------------------------ */ public static function box_findings() { $d = self::data(); $rows = Softvolt_Ai_Bodyguard_Findings::query( array( 'status' => 'open', 'per_page' => 6 ) ); if ( ! $rows['rows'] ) { echo '

' . esc_html__( 'No open findings. Run a scan to check files, the database and the configuration.', 'ai-guardwall' ) . '

'; return; } echo ''; echo '

' . esc_html__( 'View all findings', 'ai-guardwall' ) . '

'; } public static function box_health() { $checks = Softvolt_Ai_Bodyguard_Hardening::checks(); $warn = count( wp_list_filter( $checks, array( 'state' => 'warn' ) ) ); echo '

' . esc_html( $warn ? sprintf( /* translators: %s: warn */ _n( '%s needs attention', '%s need attention', $warn, 'ai-guardwall' ), number_format_i18n( $warn ) ) : __( 'All good', 'ai-guardwall' ) ) . '

'; echo ''; } public static function box_logins() { $failed = Softvolt_Ai_Bodyguard_Login_Security::failed_since( DAY_IN_SECONDS ); $locked = Softvolt_Ai_Bodyguard_Lockouts::count_since( DAY_IN_SECONDS ); $success = Softvolt_Ai_Bodyguard_Logger::count_since( 'login_success', DAY_IN_SECONDS ); echo ''; $recent = Softvolt_Ai_Bodyguard_Logger::query( array( 'event' => 'login_success', 'per_page' => 4 ) ); if ( $recent['rows'] ) { echo ''; } echo '

' . esc_html__( 'Activity log', 'ai-guardwall' ) . '

'; } public static function box_pro() { ?>

0 ) { $class = 'is-danger'; $text = sprintf( /* translators: %d: critical */ _n( '%d critical finding', '%d critical findings', $d['critical'], 'ai-guardwall' ), $d['critical'] ); } elseif ( $d['protected'] ) { $class = 'is-live'; $text = __( 'Protected', 'ai-guardwall' ); } echo '

' . esc_html( $text ) . '

'; } private static function glance_list( $d ) { $c = $d['counts']; $items = array( array( 'dashicons-warning', $d['findings_url'] . '&severity=critical', sprintf( /* translators: %s: critical */ __( '%s critical', 'ai-guardwall' ), number_format_i18n( $c['critical'] ) ), $c['critical'] > 0 ), array( 'dashicons-flag', $d['findings_url'], sprintf( /* translators: %s: total */ __( '%s open findings', 'ai-guardwall' ), number_format_i18n( $c['total'] ) ), false ), array( 'dashicons-shield-alt', '', sprintf( /* translators: %s: blocked */ __( '%s attacks blocked (7 days)', 'ai-guardwall' ), number_format_i18n( $d['blocked'] ) ), false ), array( 'dashicons-chart-bar', $d['dashboard_url'], sprintf( /* translators: 1: score, 2: grade */ __( 'Security score %1$d (%2$s)', 'ai-guardwall' ), $d['score']['score'], $d['score']['grade'] ), false ), ); echo ''; } private static function severity_label( $level ) { $labels = array( 'critical' => __( 'Critical', 'ai-guardwall' ), 'high' => __( 'High', 'ai-guardwall' ), 'medium' => __( 'Medium', 'ai-guardwall' ), 'low' => __( 'Low', 'ai-guardwall' ), 'info' => __( 'Info', 'ai-guardwall' ), ); return isset( $labels[ $level ] ) ? $labels[ $level ] : $level; } private static function severity_sentence( array $c ) { $parts = array(); foreach ( array_keys( self::SEVERITY_COLORS ) as $level ) { $parts[] = number_format_i18n( isset( $c[ $level ] ) ? $c[ $level ] : 0 ) . ' ' . self::severity_label( $level ); } /* translators: %s: counts per severity */ return sprintf( __( 'Open findings by severity: %s', 'ai-guardwall' ), implode( ', ', $parts ) ); } /** A clean axis step (1, 2 or 5 times a power of ten) at least $raw. */ private static function nice_step( $raw ) { if ( $raw <= 1 ) { return 1; } $p = pow( 10, floor( log10( $raw ) ) ); foreach ( array( 1, 2, 5, 10 ) as $m ) { if ( $m * $p >= $raw ) { return (int) ( $m * $p ); } } return (int) ( 10 * $p ); } private static function compact( $n ) { if ( $n >= 1000000 ) { return number_format_i18n( $n / 1000000, $n % 1000000 ? 1 : 0 ) . 'M'; } if ( $n >= 1000 ) { return number_format_i18n( $n / 1000, $n % 1000 ? 1 : 0 ) . 'K'; } return number_format_i18n( $n ); } /** A column with a 4px rounded top and a square foot on the baseline. */ private static function column_path( $x, $y, $w, $h ) { $r = min( 4, $w / 2, $h ); return sprintf( 'M%1$s %2$s V%3$s Q%1$s %4$s %5$s %4$s H%6$s Q%7$s %4$s %7$s %3$s V%2$s Z', round( $x, 2 ), round( $y + $h, 2 ), round( $y + $r, 2 ), round( $y, 2 ), round( $x + $r, 2 ), round( $x + $w - $r, 2 ), round( $x + $w, 2 ) ); } private static function day_label( $day, $short = false ) { return wp_date( $short ? 'j M' : get_option( 'date_format' ), strtotime( $day . ' 12:00:00 UTC' ) ); } private static function duration( $seconds ) { $seconds = (int) $seconds; if ( $seconds < 60 ) { /* translators: %d: seconds */ return sprintf( __( '%d s', 'ai-guardwall' ), $seconds ); } /* translators: 1: minutes, 2: seconds */ return sprintf( __( '%1$d min %2$d s', 'ai-guardwall' ), floor( $seconds / 60 ), $seconds % 60 ); } /** Hover and keyboard-focus tooltips for every [data-svabg-tip] mark. */ private static function tooltip_js() { return "( function () {\n" . "\tvar tip;\n" . "\tfunction show( el, x, y ) {\n" . "\t\tif ( ! tip ) {\n" . "\t\t\ttip = document.createElement( 'div' );\n" . "\t\t\ttip.className = 'svabg-tip';\n" . "\t\t\ttip.setAttribute( 'role', 'tooltip' );\n" . "\t\t\tdocument.body.appendChild( tip );\n" . "\t\t}\n" . "\t\ttip.textContent = el.getAttribute( 'data-svabg-tip' );\n" . "\t\ttip.style.display = 'block';\n" . "\t\tvar w = tip.offsetWidth, h = tip.offsetHeight;\n" . "\t\ttip.style.left = Math.max( 8, Math.min( window.innerWidth - w - 8, x - w / 2 ) ) + 'px';\n" . "\t\ttip.style.top = Math.max( 8, y - h - 12 ) + 'px';\n" . "\t}\n" . "\tfunction hide() {\n" . "\t\tif ( tip ) {\n" . "\t\t\ttip.style.display = 'none';\n" . "\t\t}\n" . "\t}\n" . "\tdocument.addEventListener( 'mousemove', function ( e ) {\n" . "\t\tvar el = e.target.closest && e.target.closest( '[data-svabg-tip]' );\n" . "\t\treturn el ? show( el, e.clientX, e.clientY ) : hide();\n" . "\t} );\n" . "\tdocument.addEventListener( 'focusin', function ( e ) {\n" . "\t\tvar el = e.target.closest && e.target.closest( '[data-svabg-tip]' );\n" . "\t\tif ( el ) {\n" . "\t\t\tvar r = el.getBoundingClientRect();\n" . "\t\t\tshow( el, r.left + r.width / 2, r.top );\n" . "\t\t}\n" . "\t} );\n" . "\tdocument.addEventListener( 'focusout', hide );\n" . "\twindow.addEventListener( 'scroll', hide, { passive: true } );\n" . "} )();\n"; } private static function data() { if ( null !== self::$data ) { return self::$data; } $counts = Softvolt_Ai_Bodyguard_Findings::counts( 0 ); $last = Softvolt_Ai_Bodyguard_Scanner::last_scan(); $score = Softvolt_Ai_Bodyguard_Hardening::score(); $blocked = 0; foreach ( Softvolt_Ai_Bodyguard_Firewall::stats( 7 ) as $day ) { $blocked += (int) $day['blocked']; } $settings_url = Softvolt_Ai_Bodyguard_Admin::url( 'settings' ); $scan_url = Softvolt_Ai_Bodyguard_Admin::url( 'cleanup', 'scan' ); $steps = array( 'model' => array( 'done' => Softvolt_Ai_Bodyguard_Settings::is_configured(), 'label' => __( 'Connect an AI model for the analyst', 'ai-guardwall' ), 'url' => Softvolt_Ai_Bodyguard_Admin::tab_url( 'model' ) ), 'scan' => array( 'done' => (bool) $last, 'label' => __( 'Run the first malware scan', 'ai-guardwall' ), 'url' => $scan_url . '&autostart=' . wp_create_nonce( 'svabg_autostart' ) ), 'hardening' => array( 'done' => $score['rules_on'] >= max( 1, (int) ( $score['rules'] * 0.5 ) ), 'label' => __( 'Walk through the hardening wizard', 'ai-guardwall' ), 'url' => Softvolt_Ai_Bodyguard_Admin::tab_url( 'hardening' ) ), 'twofa' => array( 'done' => Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'two_factor' ), 'label' => __( 'Switch on two-factor authentication', 'ai-guardwall' ), 'url' => Softvolt_Ai_Bodyguard_Admin::tab_url( 'usersec' ) ), ); $recent = $last && strtotime( $last['finished_at'] . ' UTC' ) > time() - 8 * DAY_IN_SECONDS; $protected = $recent && 'block' === Softvolt_Ai_Bodyguard_Settings::get( 'firewall_mode' ) && 0 === (int) $counts['critical']; self::$data = array( 'counts' => $counts, 'critical' => (int) $counts['critical'], 'last' => $last, 'score' => $score, 'blocked' => $blocked, 'protected' => $protected, 'steps' => $steps, 'settings_url' => $settings_url, 'scan_url' => $scan_url, 'findings_url' => Softvolt_Ai_Bodyguard_Admin::url( 'cleanup', 'findings' ), 'dashboard_url' => Softvolt_Ai_Bodyguard_Admin::url( 'dashboard' ), ); return self::$data; } }