*/ public static function collect_all( int $recent_count = Module_Registry::DEFAULT_RECENT_EVENTS_COUNT ): array { $modules = Module_Registry::get_sorted_modules(); $data = array(); foreach ( $modules as $key => $module ) { $module_data = array( 'enabled' => Module_Registry::is_module_enabled( $key ), 'events' => array(), 'stats' => array(), ); if ( $module_data['enabled'] ) { if ( ! empty( $module['data_callback'] ) && is_callable( $module['data_callback'] ) ) { $module_data['events'] = call_user_func( $module['data_callback'], $recent_count ); } if ( ! empty( $module['stats_callback'] ) && is_callable( $module['stats_callback'] ) ) { $module_data['stats'] = call_user_func( $module['stats_callback'] ); } } $data[ $key ] = $module_data; } return $data; } /** * Get recent error log entries. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_error_log_events( int $count = 3 ): array { $errors = Logs_List::get_error_items( false, $count ); if ( empty( $errors ) ) { return array(); } $events = array(); foreach ( $errors as $entry ) { $message = $entry['message'] ?? ''; $severity = $entry['severity'] ?? 'error'; $events[] = array( 'time' => self::format_time_ago( $entry['timestamp'] ?? '' ), 'message' => \wp_trim_words( $message, 12, '...' ), 'type' => self::classify_error_level( $message ), ); } return $events; } /** * Get recent fatal error events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_fatals_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\WP_Fatals_Entity' ) || ! WP_Fatals_Entity::is_installed() ) { return array(); } $results = WP_Fatals_Entity::load_array( '%d = %d', array( 1, 1 ), null, ' ORDER BY datetime DESC LIMIT ' . absint( $count ) ); $events = array(); foreach ( (array) $results as $row ) { $events[] = array( 'time' => self::format_time_ago( $row['datetime'] ?? '' ), 'message' => \wp_trim_words( $row['message'] ?? $row['error_message'] ?? '', 10, '...' ), 'type' => 'fatal', ); } return $events; } /** * Get cron jobs statistics. * * @return array */ public static function get_crons_stats(): array { if ( ! class_exists( '\ADVAN\Helpers\Crons_Helper' ) ) { return array(); } $events = Crons_Helper::get_events(); $total = count( $events ); $filtered = \ADVAN\Lists\Crons_List::get_filtered_events( $events ); return array( 'total' => $total, 'core' => count( $filtered['core'] ?? array() ), 'custom' => count( $filtered['custom'] ?? array() ), 'noaction' => count( $filtered['noaction'] ?? array() ), ); } /** * Get transients statistics. * * @return array */ public static function get_transients_stats(): array { if ( ! class_exists( '\ADVAN\Helpers\Transients_Helper' ) ) { return array(); } $all = Transients_Helper::get_transient_items( array( 'all' => true ) ); if ( empty( $all ) ) { return array( 'total' => 0, 'core' => 0, 'custom' => 0, 'expired' => 0, 'with_expiration' => 0, 'persistent' => 0, ); } $filtered = \ADVAN\Lists\Transients_List::get_filtered_transients( $all ); return array( 'total' => count( $all ), 'core' => count( $filtered['core'] ?? array() ), 'custom' => count( $all ) - count( $filtered['core'] ?? array() ), 'expired' => count( $filtered['expired'] ?? array() ), 'with_expiration' => count( $filtered['with_expiration'] ?? array() ), 'persistent' => count( $filtered['persistent'] ?? array() ), ); } /** * Get recent HTTP request events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_requests_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\Requests_Log_Entity' ) || ! Requests_Log_Entity::is_installed() ) { return array(); } // The dashboard does not display request bodies, responses or stack traces. $connection = Requests_Log_Entity::get_connection(); $table = Requests_Log_Entity::get_table_name( $connection ); $results = Requests_Log_Entity::get_results( $connection->prepare( "SELECT date_added, url FROM {$table} ORDER BY id DESC LIMIT %d", absint( $count ) ), $connection ); $events = array(); foreach ( (array) $results as $row ) { $events[] = array( 'time' => self::format_time_ago( $row['date_added'] ?? '' ), 'message' => \wp_trim_words( $row['url'] ?? '', 8, '...' ), 'type' => ( ! empty( $row['response_code'] ) && $row['response_code'] >= 400 ) ? 'error' : 'info', ); } return $events; } /** * Get HTTP request statistics. * * @return array */ public static function get_requests_stats(): array { if ( ! class_exists( '\ADVAN\Entities\Requests_Log_Entity' ) || ! Requests_Log_Entity::is_installed() ) { return array(); } $connection = Requests_Log_Entity::get_connection(); $table = Requests_Log_Entity::get_table_name( $connection ); $rows = Requests_Log_Entity::get_results( $connection->prepare( "SELECT COALESCE(SUM(direction = %s), 0) AS internal, COALESCE(SUM(direction = %s), 0) AS external FROM {$table} WHERE direction IN (%s, %s)", 'internal', 'external', 'internal', 'external' ), $connection ); return array( 'internal' => (int) ( $rows[0]['internal'] ?? 0 ), 'external' => (int) ( $rows[0]['external'] ?? 0 ), ); } /** * Get recent mail events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_mail_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\WP_Mail_Entity' ) || ! WP_Mail_Entity::is_installed() ) { return array(); } $results = WP_Mail_Entity::load_array( '%d = %d', array( 1, 1 ), null, ' ORDER BY id DESC LIMIT ' . absint( $count ) ); $events = array(); foreach ( (array) $results as $row ) { $status = 'info'; if ( ! empty( $row['error'] ) ) { $status = 'error'; } elseif ( ! empty( $row['status'] ) && 'failed' === $row['status'] ) { $status = 'error'; } $events[] = array( 'time' => self::format_time_ago( $row['time'] ?? '' ), 'message' => wp_trim_words( $row['subject'] ?? $row['to'] ?? '', 8, '...' ), 'type' => $status, ); } return $events; } /** * Get recent security events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_security_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\Security_Events_Entity' ) || ! Security_Events_Entity::is_installed() ) { return array(); } $results = Security_Events_Entity::load_array( '%d = %d', array( 1, 1 ), null, ' ORDER BY id DESC LIMIT ' . \absint( $count ) ); $events = array(); foreach ( (array) $results as $row ) { $severity = ! empty( $row['severity'] ) ? strtolower( $row['severity'] ) : 'info'; $event_type = $row['event_type'] ?? ''; $description = $row['description'] ?? ''; $display_text = $event_type; if ( ! empty( $description ) && $description !== $event_type ) { $display_text = $event_type . ' — ' . \wp_trim_words( $description, 6, '...' ); } $events[] = array( 'time' => self::format_time_ago( $row['created_at'] ?? '' ), 'message' => $display_text, 'type' => in_array( $severity, array( 'critical', 'high' ), true ) ? 'error' : ( 'medium' === $severity ? 'warning' : 'info' ), ); } return $events; } /** * Get recent hooks capture events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_login_audit_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\Login_Audit_Entity' ) || ! Login_Audit_Entity::is_installed() ) { return array(); } $results = Login_Audit_Entity::load_array( '%d = %d', array( 1, 1 ), null, ' ORDER BY id DESC LIMIT ' . \absint( $count ) ); $labels = Login_Audit_Entity::get_event_type_labels(); $events = array(); foreach ( (array) $results as $row ) { $severity = ! empty( $row['severity'] ) ? strtolower( $row['severity'] ) : 'info'; $event_type = $row['event_type'] ?? ''; $label = $labels[ $event_type ] ?? $event_type; $username = $row['username'] ?? ''; $display = $label; if ( ! empty( $username ) ) { $display .= ' — ' . $username; } $events[] = array( 'time' => self::format_time_ago( $row['created_at'] ?? '' ), 'message' => $display, 'type' => in_array( $severity, array( 'critical', 'high' ), true ) ? 'error' : ( 'medium' === $severity ? 'warning' : 'info' ), ); } return $events; } /** * Get recent hooks capture events. * * @param int $count Number of entries to retrieve. * * @return array */ public static function get_hooks_events( int $count = 3 ): array { if ( ! class_exists( '\ADVAN\Entities\Hooks_Capture_Entity' ) || ! Hooks_Capture_Entity::is_installed() ) { return array(); } $results = Hooks_Capture_Entity::load_array( '%d = %d', array( 1, 1 ), null, ' ORDER BY id DESC LIMIT ' . \absint( $count ) ); $events = array(); foreach ( (array) $results as $row ) { $events[] = array( 'time' => self::format_time_ago( $row['date_added'] ?? '' ), 'message' => \wp_trim_words( $row['hook_name'] ?? $row['tag'] ?? '', 8, '...' ), 'type' => 'info', ); } return $events; } /** * Get code snippets statistics. * * @return array */ public static function get_snippets_stats(): array { if ( ! class_exists( '\ADVAN\Entities\Snippet_Entity' ) || ! Snippet_Entity::is_installed() ) { return array(); } $total = Snippet_Entity::count(); $enabled = Snippet_Entity::count( 'status = %d', array( Snippet_Entity::STATUS_ENABLED ) ); $disabled = Snippet_Entity::count( 'status = %d', array( Snippet_Entity::STATUS_DISABLED ) ); return array( 'total' => $total, 'enabled' => $enabled, 'disabled' => $disabled, ); } /** * Get database tables statistics. * * @return array */ public static function get_tables_stats(): array { $tables = Common_Table::get_all_tables_status(); if ( empty( $tables ) ) { return array( 'total' => 0, 'total_size' => '0 MB' ); } $total_size = 0; foreach ( $tables as $table ) { $total_size += ( (int) ( $table['Data_length'] ?? 0 ) + (int) ( $table['Index_length'] ?? 0 ) ); } return array( 'total' => count( $tables ), 'total_size' => \size_format( $total_size ), ); } /** * Get performance statistics. * * @return array */ public static function get_performance_stats(): array { $stats = array( 'php_version' => PHP_VERSION, 'wp_version' => \get_bloginfo( 'version' ), 'memory_limit' => ini_get( 'memory_limit' ), 'max_exec_time' => ini_get( 'max_execution_time' ) . 's', ); return $stats; } /** * Get traffic statistics for today. * * @return array */ public static function get_traffic_stats(): array { if ( ! class_exists( '\ADVAN\Entities\Traffic_Site_Stats_Entity' ) || ! Traffic_Site_Stats_Entity::is_installed() ) { return array(); } $today = \current_time( 'Y-m-d' ); $totals = Traffic_Site_Stats_Entity::get_totals( $today, $today ); $realtime = 0; if ( class_exists( '\ADVAN\Controllers\Traffic_Aggregator' ) ) { $realtime = Traffic_Aggregator::get_realtime_count(); } return array( 'online' => $realtime, 'visitors' => (int) ( $totals['visitors'] ?? 0 ), 'pageviews' => (int) ( $totals['pageviews'] ?? 0 ), ); } /** * Get REST API endpoint statistics. * * @return array */ public static function get_rest_api_stats(): array { if ( ! class_exists( '\ADVAN\Helpers\Rest_Api_Helper' ) ) { return array(); } $endpoints = Rest_Api_Helper::get_all_endpoints(); $rules = Rest_Api_Helper::get_endpoint_rules(); $total = count( $endpoints ); $disabled = 0; $obfuscated = 0; foreach ( $endpoints as $ep ) { $route = $ep['route'] ?? ''; if ( ! empty( $rules[ $route ]['disabled'] ) ) { ++$disabled; } if ( ! empty( $rules[ $route ]['obfuscate'] ) ) { ++$obfuscated; } } return array( 'total' => $total, 'enabled' => $total - $disabled, 'disabled' => $disabled, 'obfuscated' => $obfuscated, ); } /** * Get security summary statistics. * * @return array */ public static function get_security_stats(): array { if ( ! class_exists( '\ADVAN\Entities\Security_Events_Entity' ) || ! Security_Events_Entity::is_installed() ) { return array(); } $total = Security_Events_Entity::count(); $week_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-7 days' ) ); $critical = Security_Events_Entity::count( "severity = %s AND created_at >= %s", array( 'critical', $week_ago ) ); return array( 'total_events' => $total, 'critical_7_days' => $critical, ); } /** * Get server info statistics. * * @return array */ public static function get_server_info_stats(): array { $stats = array(); if ( Settings::get_option( 'advana_server_info_cpu_enable' ) ) { $stats['cpu'] = array( 'enabled' => true, 'label' => 'CPU', 'value' => System_Analytics::get_cpu_usage(), ); } if ( Settings::get_option( 'advana_server_info_mem_enable' ) ) { $stats['memory'] = array( 'enabled' => true, 'label' => 'Memory', 'value' => System_Analytics::get_memory_usage(), ); } if ( Settings::get_option( 'advana_server_info_hdd_enable' ) ) { $stats['disk'] = array( 'enabled' => true, 'label' => 'Disk', 'value' => System_Analytics::get_disk_usage(), ); } return $stats; } /** * Format a timestamp or datetime string as "X ago". * * @param int|float|string $time Unix timestamp (int/float) or datetime string. * * @return string */ private static function format_time_ago( $time ): string { if ( empty( $time ) ) { return ''; } if ( is_numeric( $time ) ) { $timestamp = (int) $time; } else { $timestamp = strtotime( $time ); } if ( ! $timestamp || $timestamp <= 0 ) { return ''; } $diff = time() - $timestamp; if ( $diff < 0 ) { return __( 'Just now', '0-day-analytics' ); } /* translators: %s: Human-readable time interval, e.g. "5 minutes" */ return sprintf( __( '%s ago', '0-day-analytics' ), WP_Helper::interval( $diff ) ); } /** * Classify error level from a log message. * * @param string $message The error message. * * @return string */ private static function classify_error_level( string $message ): string { $message_lower = strtolower( $message ); if ( str_contains( $message_lower, 'fatal' ) || str_contains( $message_lower, 'critical' ) ) { return 'fatal'; } if ( str_contains( $message_lower, 'warning' ) ) { return 'warning'; } if ( str_contains( $message_lower, 'deprecated' ) || str_contains( $message_lower, 'notice' ) ) { return 'notice'; } return 'error'; } } }