$event, 'milestone' => $hit, 'days' => $days ); /** * A countdown milestone was reached. Extensions can hook in here, e.g. to send chat alerts. */ do_action( 'sunsetly_milestone_reached', $event, $hit, $days, $report ); if ( Sunsetly_Settings::get( 'email_milestones' ) ) { $subject = 0 === $days /* translators: 1: event title, 2: site name. */ ? sprintf( __( '[Today] %1$s — %2$s', 'sunsetly' ), $event['title'], self::site_name() ) /* translators: 1: number of days left, 2: event title, 3: site name. */ : sprintf( _n( '[%1$d day left] %2$s — %3$s', '[%1$d days left] %2$s — %3$s', $days, 'sunsetly' ), $days, $event['title'], self::site_name() ); $intro = sprintf( /* translators: 1: title, 2: date, 3: days */ __( 'Advance notice: "%1$s" happens on %2$s (%3$s). Plan the upgrade now so it is a calm, scheduled job instead of an emergency.', 'sunsetly' ), $event['title'], $event['date'], /* translators: %d: number of days. */ 0 === $days ? __( 'today', 'sunsetly' ) : sprintf( _n( 'in %d day', 'in %d days', $days, 'sunsetly' ), $days ) ); $related = array_values( array_filter( $report['findings'], function ( $f ) { return 'lifecycle' === $f['category'] || 'readiness' === $f['category']; } ) ); self::send( $subject, $intro, $related, $report, 'milestone' ); } } update_option( self::SENT_MILESTONES, $sent, false ); return $fired; } /** * Weekly / monthly digest. * * @param array $report Report. */ public static function maybe_digest( $report ) { $freq = Sunsetly_Settings::get( 'digest' ); if ( 'never' === $freq ) { return; } $interval = 'monthly' === $freq ? 30 * DAY_IN_SECONDS : WEEK_IN_SECONDS; $last = (int) get_option( self::LAST_DIGEST, 0 ); if ( 0 === $last ) { // Don't send a digest on the very first scan; start the clock instead. update_option( self::LAST_DIGEST, time(), false ); return; } if ( ( time() - $last ) < $interval - HOUR_IN_SECONDS ) { return; } update_option( self::LAST_DIGEST, time(), false ); /* translators: 1: grade, 2: score, 3: site */ $subject = sprintf( __( 'Site health %1$s (%2$d/100) — %3$s maintenance digest', 'sunsetly' ), $report['grade'], $report['score'], self::site_name() ); $top = array_slice( array_values( array_filter( $report['findings'], function ( $f ) { return 'info' !== $f['severity']; } ) ), 0, 10 ); self::send( $subject, __( 'Here is your scheduled Sunsetly summary.', 'sunsetly' ), $top, $report, 'digest' ); } /** * Send an HTML email. * * @param string $subject Subject. * @param string $intro Intro. * @param array $findings Findings. * @param array $report Report. * @param string $context Context. * @return bool */ public static function send( $subject, $intro, $findings, $report, $context ) { $recipients = Sunsetly_Settings::recipients(); $html = self::email_html( $intro, $findings, $report ); $headers = array( 'Content-Type: text/html; charset=UTF-8' ); $mail = apply_filters( 'sunsetly_email', array( 'to' => $recipients, 'subject' => $subject, 'html' => $html, 'headers' => $headers, ), $context, $findings, $report ); if ( empty( $mail['to'] ) ) { return false; } return wp_mail( $mail['to'], $mail['subject'], $mail['html'], $mail['headers'] ); } /** * Email body. * * @param string $intro Intro. * @param array $findings Findings. * @param array $report Report. * @return string */ public static function email_html( $intro, $findings, $report ) { $brand = apply_filters( 'sunsetly_branding', array( 'name' => 'Sunsetly', 'color' => '#2747d9', 'logo' => '', 'footer' => __( 'Sent by Sunsetly — vulnerability & PHP lifecycle early warning for WordPress.', 'sunsetly' ), ) ); $colors = array( 'critical' => '#b42318', 'high' => '#d9480f', 'medium' => '#b7791f', 'low' => '#2b6cb0', 'info' => '#4a5568', ); ob_start(); ?>