gmdate( 'Y-m-d', strtotime( '-6 days', strtotime( $today ) ) ), 'end' => $today, 'label' => __( 'Last 7 days', 'agency-maintenance-activity-reports' ), ); case 'last_30_days': return array( 'start' => gmdate( 'Y-m-d', strtotime( '-29 days', strtotime( $today ) ) ), 'end' => $today, 'label' => __( 'Last 30 days', 'agency-maintenance-activity-reports' ), ); case 'last_month': return array( 'start' => gmdate( 'Y-m-01', strtotime( 'first day of last month', strtotime( $today ) ) ), 'end' => gmdate( 'Y-m-t', strtotime( 'last day of last month', strtotime( $today ) ) ), 'label' => __( 'Last calendar month', 'agency-maintenance-activity-reports' ), ); case 'custom': $start = self::sanitize_date( $start ); $end = self::sanitize_date( $end ); if ( empty( $start ) || empty( $end ) ) { return self::resolve_date_range( 'last_30_days' ); } if ( strtotime( $start ) > strtotime( $end ) ) { $swap = $start; $start = $end; $end = $swap; } return array( 'start' => $start, 'end' => $end, 'label' => sprintf( /* translators: 1: start date, 2: end date */ __( 'Custom: %1$s to %2$s', 'agency-maintenance-activity-reports' ), $start, $end ), ); default: return array( 'start' => gmdate( 'Y-m-d', strtotime( '-29 days', strtotime( $today ) ) ), 'end' => $today, 'label' => __( 'Last 30 days', 'agency-maintenance-activity-reports' ), ); } } /** * Sanitize a Y-m-d date string. * * @param string $date Raw date. * @return string */ public static function sanitize_date( $date ) { $date = sanitize_text_field( $date ); $dt = DateTime::createFromFormat( 'Y-m-d', $date ); if ( ! $dt || $dt->format( 'Y-m-d' ) !== $date ) { return ''; } return $date; } /** * Build report payload. * * @param string $preset Date preset. * @param string $start Custom start. * @param string $end Custom end. * @return array */ public static function build( $preset = 'last_30_days', $start = '', $end = '' ) { global $wp_version; $range = self::resolve_date_range( $preset, $start, $end ); $events = AMAR_Tracker::get_events( $range['start'], $range['end'] ); $settings = AMAR_Settings::get_all(); $theme = wp_get_theme(); $plugins = get_option( 'active_plugins', array() ); $grouped = array( 'core_update' => array(), 'plugin_update' => array(), 'theme_update' => array(), 'plugin_activated' => array(), 'plugin_deactivated' => array(), 'theme_switched' => array(), 'manual_note' => array(), ); foreach ( $events as $event ) { $type = isset( $event->event_type ) ? $event->event_type : 'manual_note'; if ( ! isset( $grouped[ $type ] ) ) { $grouped[ $type ] = array(); } $grouped[ $type ][] = $event; } return array( 'range' => $range, 'events' => $events, 'grouped' => $grouped, 'event_count' => count( $events ), 'settings' => $settings, 'site' => array( 'name' => get_bloginfo( 'name' ), 'url' => home_url( '/' ), 'wp_version' => $wp_version, 'php_version' => PHP_VERSION, 'theme_name' => $theme->get( 'Name' ), 'theme_version' => $theme->get( 'Version' ), 'plugin_count' => is_array( $plugins ) ? count( $plugins ) : 0, 'report_date' => current_time( 'F j, Y' ), ), ); } /** * Render report HTML. * * @param array $report Report payload. * @param bool $for_email Whether output is for email. * @return string */ public static function render_html( $report, $for_email = false ) { $brand_color = ! empty( $report['settings']['brand_color'] ) ? $report['settings']['brand_color'] : '#2271b1'; $amar_style_attrs = self::build_email_style_attributes( $brand_color, $for_email ); if ( ! $for_email ) { self::enqueue_report_styles( $brand_color ); } ob_start(); include AMAR_PLUGIN_DIR . 'templates/report-html.php'; return (string) ob_get_clean(); } /** * Build pre-escaped style attributes for HTML email output. * * @param string $brand_color Hex brand color. * @param bool $for_email Whether output is for email. * @return array */ public static function build_email_style_attributes( $brand_color, $for_email ) { if ( ! $for_email ) { return array(); } $attrs = array(); $styles = self::get_email_inline_styles( $brand_color ); foreach ( $styles as $key => $css ) { $attrs[ $key ] = ' style="' . esc_attr( $css ) . '"'; } return $attrs; } /** * Print a pre-escaped inline style attribute for email HTML. * * @param array $attrs Pre-escaped attribute map. * @param string $key Attribute key. */ public static function print_style_attr( $attrs, $key ) { if ( empty( $attrs[ $key ] ) ) { return; } echo $attrs[ $key ]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in build_email_style_attributes(). } /** * Register and enqueue report styles for browser preview. * * @param string $brand_color Hex brand color. */ public static function enqueue_report_styles( $brand_color ) { wp_register_style( 'amar-report', AMAR_PLUGIN_URL . 'assets/css/report.css', array(), AMAR_VERSION ); wp_add_inline_style( 'amar-report', self::get_brand_style_rules( $brand_color ) ); wp_enqueue_style( 'amar-report' ); } /** * Build brand-specific CSS rules for report headers and accents. * * @param string $brand_color Hex brand color. * @return string */ public static function get_brand_style_rules( $brand_color ) { $brand_color = sanitize_hex_color( $brand_color ); if ( empty( $brand_color ) ) { $brand_color = '#2271b1'; } return sprintf( '.amar-report-header { background: %1$s; } .amar-section h2 { border-bottom-color: %1$s; } .amar-summary-item span { color: %1$s; }', $brand_color ); } /** * Build inline styles for HTML email clients. * * Email clients do not load external stylesheets, so styles are applied directly * to elements instead of using a document-level style block. * * @param string $brand_color Hex brand color. * @return array */ public static function get_email_inline_styles( $brand_color ) { $brand_color = sanitize_hex_color( $brand_color ); if ( empty( $brand_color ) ) { $brand_color = '#2271b1'; } $font_stack = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif'; return array( 'body' => 'margin:0;padding:24px;background:#f6f7f7;color:#1d2327;font-family:' . $font_stack . ';line-height:1.6;', 'report' => 'max-width:820px;margin:0 auto;background:#fff;border:1px solid #dcdcde;border-radius:8px;overflow:hidden;', 'header' => 'padding:28px 32px;background:' . $brand_color . ';color:#fff;', 'header_logo' => 'max-height:56px;margin-bottom:12px;display:block;', 'header_title' => 'margin:0 0 8px;font-size:28px;line-height:1.2;color:#fff;', 'header_text' => 'margin:0;opacity:0.95;color:#fff;', 'body_wrap' => 'padding:32px;', 'summary' => 'margin-bottom:24px;', 'summary_item' => 'background:#f6f7f7;border-radius:6px;padding:16px;text-align:center;margin-bottom:16px;', 'summary_value' => 'display:block;font-size:28px;font-weight:700;color:' . $brand_color . ';', 'meta_box' => 'background:#f6f7f7;border-radius:6px;padding:16px;margin-bottom:16px;', 'meta_label' => 'display:block;margin-bottom:4px;', 'section' => 'margin-bottom:28px;', 'section_title' => 'margin:0 0 12px;padding-bottom:8px;border-bottom:2px solid ' . $brand_color . ';font-size:20px;', 'empty' => 'color:#646970;font-style:italic;', 'table' => 'width:100%;border-collapse:collapse;', 'table_cell' => 'padding:10px 12px;border-bottom:1px solid #dcdcde;text-align:left;vertical-align:top;', 'table_head' => 'padding:10px 12px;border-bottom:1px solid #dcdcde;text-align:left;vertical-align:top;background:#f6f7f7;', 'footer' => 'padding:20px 32px 28px;border-top:1px solid #dcdcde;color:#646970;font-size:14px;', 'powered' => 'margin-top:12px;font-size:12px;', ); } /** * Replace tokens in a subject line. * * @param string $subject Subject template. * @param array $report Report payload. * @return string */ public static function parse_subject( $subject, $report ) { $replacements = array( '{site_name}' => $report['site']['name'], '{site_url}' => $report['site']['url'], '{agency_name}' => $report['settings']['agency_name'], '{date}' => $report['site']['report_date'], ); return strtr( $subject, $replacements ); } }