(int) $from, 'to' => (int) $to, 'label' => $label, ); } /** * Summarizes a date range for display. * * @since 0.1.0 * * @param array{from:int,to:int,label:string} $range Date range. * @return array */ public static function summary_for_period( $range ) { $since = Event_Log::format_utc( $range['from'] ); $until = Event_Log::format_utc( $range['to'] ); $counts = Event_Log::counts_by_event( $since, $until ); $metrics = array( // The aggregate legacy `auto_updates` event is intentionally excluded: // the same updates are already represented by the component events. 'updates' => self::sum( $counts, array( 'core_updated', 'plugin_updated', 'theme_updated' ) ), 'backups' => self::sum( $counts, array( 'backup_created' ) ), 'spam' => self::sum( $counts, array( 'spam_blocked' ) ), 'failed_logins' => self::sum( $counts, array( 'login_failed' ) ), 'support' => self::sum( $counts, array( 'support_request', 'support_sent' ) ), 'content' => self::sum( $counts, array( 'content_published' ) ), ); $metrics['security'] = $metrics['spam'] + $metrics['failed_logins']; /** * Add custom metrics (e.g. uptime from a monitoring tool). * * @since 0.1.0 * * @param array $metrics Metrics. * @param array $range Date range. * @param array $counts Raw event counters. */ $metrics = apply_filters( 'quietadmin_report_metrics', $metrics, $range, $counts ); $highlights = array(); if ( $metrics['updates'] > 0 ) { $highlights[] = sprintf( /* translators: %d: number of updates. */ _n( '%d security and feature update installed', '%d security and feature updates installed', $metrics['updates'], 'oms-quietadmin' ), $metrics['updates'] ); } if ( $metrics['spam'] > 0 ) { $highlights[] = sprintf( /* translators: %d: number of spam comments. */ _n( '%d spam message blocked', '%d spam messages blocked', $metrics['spam'], 'oms-quietadmin' ), $metrics['spam'] ); } if ( $metrics['backups'] > 0 && Options::get( 'report_include_backup' ) ) { $highlights[] = sprintf( /* translators: %d: number of backups. */ _n( '%d backup created', '%d backups created', $metrics['backups'], 'oms-quietadmin' ), $metrics['backups'] ); } if ( $metrics['failed_logins'] > 0 ) { $highlights[] = sprintf( /* translators: %d: number of recorded failed login attempts. */ _n( '%d failed login attempt recorded', '%d failed login attempts recorded', $metrics['failed_logins'], 'oms-quietadmin' ), $metrics['failed_logins'] ); } if ( empty( $highlights ) ) { $highlights[] = __( 'No special incidents were logged during the reporting period.', 'oms-quietadmin' ); } /** * Filters the report highlights. * * @since 0.1.0 * * @param string[] $highlights Sentences for the report. * @param array $metrics Metrics. * @param array $range Date range. */ $highlights = apply_filters( 'quietadmin_report_highlights', $highlights, $metrics, $range ); return array( 'range' => $range, 'metrics' => $metrics, 'highlights' => array_values( array_filter( (array) $highlights ) ), 'events' => Event_Log::get_events( $since, $until, 20 ), 'agency' => array( 'name' => (string) Options::get( 'agency_name' ), 'url' => (string) Options::get( 'agency_url' ), 'logo' => Options::agency_logo_url( 'medium' ), 'email' => (string) Options::get( 'support_email' ), ), ); } /** * Sums counters of several event keys. * * @since 0.1.0 * * @param array $counts Counters. * @param string[] $keys Keys. * @return int */ protected static function sum( $counts, $keys ) { $total = 0; foreach ( (array) $keys as $key ) { if ( isset( $counts[ $key ] ) ) { $total += (int) $counts[ $key ]; } } return $total; } /** * Cron handler: daily maintenance (real status check, clean up log). * * @since 0.1.0 * * @return void */ public static function cron_daily() { // Remove a leftover monthly report event while the premium code is not running. if ( ! defined( 'QUIETADMIN_PRO_VERSION' ) ) { wp_clear_scheduled_hook( 'quietadmin_monthly_report' ); } // Run a real check (SSL, PHP, updates, backups, error events) // instead of just setting a timestamp. StatusLights::run_checks(); /** * Retention period of the log in days. * * @since 0.1.0 * * @param int $days Days. */ $days = (int) apply_filters( 'quietadmin_log_retention_days', 180 ); Event_Log::prune( $days ); } }