* reason => count). No addresses, pages or form contents are involved. * * @package AccessNow\SpamEater */ namespace AccessNow\SpamEater; defined( 'ABSPATH' ) || exit; /** * Dashboard widget and weekly email. */ final class Dashboard { /** WP-Cron hook for the weekly email. */ const CRON_HOOK = 'accessnow_spam_eater_weekly_summary'; /** * The plugin. * * @var Plugin */ private $plugin; /** * Hook up the widget and the weekly email. * * @param Plugin $plugin The plugin. */ public function __construct( Plugin $plugin ) { $this->plugin = $plugin; add_action( 'wp_dashboard_setup', array( $this, 'add_widget' ) ); add_action( self::CRON_HOOK, array( $this, 'send_weekly_email' ) ); add_action( 'admin_init', array( $this, 'check_schedule' ) ); } /** * Schedule or unschedule the weekly email. * * @param bool $on Whether the weekly email is on. */ public static function schedule_weekly_email( $on ) { $next = wp_next_scheduled( self::CRON_HOOK ); if ( $on && ! $next ) { wp_schedule_event( time() + WEEK_IN_SECONDS, 'weekly', self::CRON_HOOK ); } elseif ( ! $on && $next ) { wp_clear_scheduled_hook( self::CRON_HOOK ); } } /** * Action: admin_init. Put the schedule right after the plugin was deactivated and * activated again (deactivating removes it). */ public function check_schedule() { $settings = get_option( 'accessnow_spam_eater_settings', array() ); self::schedule_weekly_email( is_array( $settings ) && ! empty( $settings['weekly_email'] ) ); } /** * Action: wp_dashboard_setup. Administrators only. */ public function add_widget() { if ( ! current_user_can( 'manage_options' ) ) { return; } wp_add_dashboard_widget( 'accessnow_spam_eater_summary', __( 'Spam Eater: last 30 days', 'accessnow-spam-eater' ), array( $this, 'render_widget' ) ); } /** * The widget's content. */ public function render_widget() { self::render_summary( $this->plugin, 30 ); echo '
' . esc_html__( 'Spam Eater settings', 'accessnow-spam-eater' ) . ' · ' . esc_html__( 'About this summary', 'accessnow-spam-eater' ) . '
'; } /** * Totals by reason and by day for a period. * * @param Plugin $plugin The plugin. * @param int $days Days. * @return array{days:array'; printf( /* translators: 1: number of submissions, 2: number of days. */ esc_html( _n( '%1$s spam submission refused in the last %2$s days.', '%1$s spam submissions refused in the last %2$s days.', $totals['total'], 'accessnow-spam-eater' ) ), esc_html( number_format_i18n( $totals['total'] ) ), esc_html( number_format_i18n( $days ) ) ); echo '
'; // The chart. Every figure in it is also in the tables below. // Fixed height, stretched across the width: the bars widen with the screen, and the // date labels are text outside the SVG so they never scale with it. echo ''; echo ''; if ( ! empty( $totals['reasons'] ) ) { echo '| ' . esc_html__( 'Reason', 'accessnow-spam-eater' ) . ' | ' . esc_html__( 'Refused', 'accessnow-spam-eater' ) . ' |
|---|---|
| ' . esc_html( $plugin->reason_label( $reason ) ) . ' | ' . esc_html( number_format_i18n( $value ) ) . ' |
'; printf( /* translators: %s: number of times. */ esc_html( _n( 'Repeated failed logins were made to wait %s time.', 'Repeated failed logins were made to wait %s times.', $totals['throttled'], 'accessnow-spam-eater' ) ), esc_html( number_format_i18n( $totals['throttled'] ) ) ); echo '
'; } echo '| ' . esc_html__( 'Day', 'accessnow-spam-eater' ) . ' | ' . esc_html__( 'Refused', 'accessnow-spam-eater' ) . ' |
|---|---|
| ' . esc_html( wp_date( $date, strtotime( $day . ' 12:00:00' ) ) ) . ' | ' . esc_html( number_format_i18n( $value ) ) . ' |
' . esc_html__( 'Submissions from addresses that are already blocked are turned away without being counted.', 'accessnow-spam-eater' ) . '
'; echo '