current_time( 'mysql' ), 'plugin' => $plugin_name, 'action' => $action, 'user' => $user_display_name, 'old_version' => $old_version, 'new_version' => $new_version, ]; $buffer = get_transient( 'astr_email_buffer' ); if ( ! is_array( $buffer ) ) { $buffer = []; } $buffer[] = $new_event; $delay_opt = get_option( 'astr_email_delay', '5min' ); $delay_map = [ '10sec' => 10,//testing '5min' => 5 * MINUTE_IN_SECONDS, '1h' => HOUR_IN_SECONDS, '6h' => 6 * HOUR_IN_SECONDS, '1d' => DAY_IN_SECONDS, '1w' => WEEK_IN_SECONDS, ]; $delay = isset( $delay_map[ $delay_opt ] ) ? $delay_map[ $delay_opt ] : 5 * MINUTE_IN_SECONDS; set_transient( 'astr_email_buffer', $buffer, $delay * 2 ); $next = wp_next_scheduled( 'astr_send_notification_event' ); if ( ! $next ) { wp_schedule_single_event( time() + $delay, 'astr_send_notification_event' ); } elseif ( $next > time() + $delay ) { wp_unschedule_event( $next, 'astr_send_notification_event' ); wp_schedule_single_event( time() + $delay, 'astr_send_notification_event' ); } } function astr_send_buffered_email_notification() { $buffer = get_transient( 'astr_email_buffer' ); if ( empty( $buffer ) || ! is_array( $buffer ) ) { return; } usort( $buffer, function ( $a, $b ) { return strtotime( $b['time'] ) - strtotime( $a['time'] ); } ); $emails = array_filter( array_map( 'trim', (array) get_option( 'astr_notification_emails', [] ) ) ); if ( empty( $emails ) ) { return; } $rows = ''; foreach ( $buffer as $event ) { $action_label = ucfirst( $event['action'] ); if ( $event['action'] === 'updated' && ! empty( $event['old_version'] ) && ! empty( $event['new_version'] ) ) { $action_label = 'Updated (' . $event['old_version'] . ' → ' . $event['new_version'] . ')'; } if ( $event['action'] === 'rolled_back' && ! empty( $event['old_version'] ) && ! empty( $event['new_version'] ) ) { $action_label = 'Reverted (' . $event['old_version'] . ' → ' . $event['new_version'] . ')'; } $rows .= sprintf( ' %s %s %s %s ', esc_html( $action_label ), esc_html( $event['plugin'] ), esc_html( gmdate( 'H:i', strtotime( $event['time'] ) ) ), esc_html( $event['user'] ) ); } $message = '

Plugin Activity Report

The following plugin actions occurred:

' . $rows . '
Action Plugin Time User
'; $subject = 'Plugin activity report'; $headers = [ 'Content-Type: text/html; charset=UTF-8' ]; $any_sent = false; foreach ( $emails as $email ) { if ( is_email( $email ) ) { $sent = wp_mail( $email, $subject, $message, $headers ); if ( $sent ) { $any_sent = true; } } } $next = wp_next_scheduled( 'astr_send_notification_event' ); if ( $any_sent && ! $next ) { delete_transient( 'astr_email_buffer' ); } } add_action( 'astr_send_notification_event', 'astr_send_buffered_email_notification' ); function astr_send_test_email() { $emails = array_filter( array_map( 'trim', (array) get_option( 'astr_notification_emails', [] ) ) ); if ( empty( $emails ) ) { return false; } $subject = 'Test Email from Adwise SiteTrace'; $message = '

Test Email

This is a test email to verify that notifications are working correctly.

Time: ' . esc_html( current_time( 'mysql' ) ) . '

'; $headers = [ 'Content-Type: text/html; charset=UTF-8' ]; $sent_any = false; foreach ( $emails as $email ) { if ( is_email( $email ) ) { if ( wp_mail( $email, $subject, $message, $headers ) ) { $sent_any = true; } } } return $sent_any; } function astr_set_default_events() { if ( ! get_option( 'astr_notification_events' ) ) { update_option( 'astr_notification_events', [ 'installed', 'updated', 'activated', 'deactivated', 'removed' ] ); } }