| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use WP_Defender\Controller\Audit_Logging; |
| 6 |
use WP_Defender\Model\Audit_Log; |
| 7 |
|
| 8 |
class Audit_Report extends \WP_Defender\Model\Notification { |
| 9 |
protected $table = 'wd_audit_report'; |
| 10 |
|
| 11 |
public function before_load() { |
| 12 |
$default = array( |
| 13 |
'title' => __( 'Audit Logging - Reporting', 'wpdef' ), |
| 14 |
'slug' => 'audit-report', |
| 15 |
'status' => self::STATUS_DISABLED, |
| 16 |
'description' => __( 'Schedule Defender to automatically email you a summary of all your website events.', 'wpdef' ), |
| 17 |
// @since 3.0.0 Fix 'Guest'-line. |
| 18 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 19 |
'out_house_recipients' => array(), |
| 20 |
'type' => 'report', |
| 21 |
'frequency' => 'weekly', |
| 22 |
'day' => 'sunday', |
| 23 |
'time' => '4:00', |
| 24 |
'day_n' => '1', |
| 25 |
'dry_run' => false, |
| 26 |
'configs' => array(), |
| 27 |
); |
| 28 |
$this->import( $default ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @throws \Exception |
| 33 |
*/ |
| 34 |
public function send() { |
| 35 |
$date_to = new \DateTime( 'now', wp_timezone() ); |
| 36 |
switch ( $this->frequency ) { |
| 37 |
case 'daily': |
| 38 |
$date_from = new \DateTime( '-24 hours', wp_timezone() ); |
| 39 |
break; |
| 40 |
case 'weekly': |
| 41 |
$date_from = new \DateTime( '-7 days', wp_timezone() ); |
| 42 |
break; |
| 43 |
case 'monthly': |
| 44 |
default: |
| 45 |
$date_from = new \DateTime( '-30 days', wp_timezone() ); |
| 46 |
break; |
| 47 |
} |
| 48 |
$data = Audit_Log::query( $date_from->getTimestamp(), $date_to->getTimestamp(), [], '', '', false ); |
| 49 |
|
| 50 |
$list = []; |
| 51 |
// Collect data. |
| 52 |
foreach ( $data as $item ) { |
| 53 |
if ( ! isset( $list[ $item->event_type ][ $item->action_type ] ) ) { |
| 54 |
$list[ $item->event_type ][ $item->action_type ] = 0; |
| 55 |
} |
| 56 |
$list[ $item->event_type ][ $item->action_type ] ++; |
| 57 |
} |
| 58 |
// Send data. |
| 59 |
foreach ( $this->in_house_recipients as $recipient ) { |
| 60 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
$this->send_to_user( $recipient['name'], $recipient['email'], $data, $list ); |
| 64 |
} |
| 65 |
foreach ( $this->out_house_recipients as $recipient ) { |
| 66 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
$this->send_to_user( $recipient['name'], $recipient['email'], $data, $list ); |
| 70 |
} |
| 71 |
// Last sent should be the previous timestamp. |
| 72 |
$this->last_sent = $this->est_timestamp; |
| 73 |
$this->est_timestamp = $this->get_next_run()->getTimestamp(); |
| 74 |
$this->save(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param string $name |
| 79 |
* @param string $email |
| 80 |
* @param array $data |
| 81 |
* @param array $list |
| 82 |
* |
| 83 |
* @throws \DI\DependencyException |
| 84 |
* @throws \DI\NotFoundException |
| 85 |
*/ |
| 86 |
private function send_to_user( $name, $email, $data, $list ) { |
| 87 |
$site_url = network_site_url(); |
| 88 |
$subject = sprintf( __( "Here's what's been happening at %s", 'wpdef' ), $site_url ); |
| 89 |
$audit_logging = wd_di()->get( Audit_Logging::class ); |
| 90 |
if ( count( $data ) ) { |
| 91 |
$logs_url = network_admin_url( 'admin.php?page=wdf-logging&view=logs' ); |
| 92 |
// Need for activated Mask Login feature. |
| 93 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 94 |
$message = $audit_logging->render_partial( 'email/audit-report-table', [ |
| 95 |
'logs_url' => $logs_url, |
| 96 |
'name' => $name, |
| 97 |
'list' => $list, |
| 98 |
'site_url' => $site_url |
| 99 |
], false ); |
| 100 |
} else { |
| 101 |
$message = $audit_logging->render_partial( 'email/audit-report-no-events', [ |
| 102 |
'name' => $name, |
| 103 |
'site_url' => $site_url |
| 104 |
], false ); |
| 105 |
} |
| 106 |
// Main email template. |
| 107 |
$content = $audit_logging->render_partial( |
| 108 |
'email/index', |
| 109 |
array( |
| 110 |
'title' => __( 'Audit Logging', 'wpdef' ), |
| 111 |
'content_body' => $message, |
| 112 |
), |
| 113 |
false |
| 114 |
); |
| 115 |
|
| 116 |
$headers = defender_noreply_html_header( |
| 117 |
defender_noreply_email( 'wd_audit_noreply_email' ) |
| 118 |
); |
| 119 |
|
| 120 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 121 |
if ( $ret ) { |
| 122 |
$this->save_log( $email ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Define labels for settings key. |
| 128 |
* |
| 129 |
* @param string|null $key |
| 130 |
* |
| 131 |
* @return string|array|null |
| 132 |
*/ |
| 133 |
public function labels( $key = null ) { |
| 134 |
$labels = array( |
| 135 |
'report' => __( 'Audit Logging - Reporting', 'wpdef' ), |
| 136 |
'subscribers' => __( 'Recipients', 'wpdef' ), |
| 137 |
'day' => __( 'Day of', 'wpdef' ), |
| 138 |
'day_n' => __( 'Day of', 'wpdef' ), |
| 139 |
'time' => __( 'Time of day', 'wpdef' ), |
| 140 |
'frequency' => __( 'Frequency', 'wpdef' ), |
| 141 |
); |
| 142 |
|
| 143 |
if ( ! is_null( $key ) ) { |
| 144 |
return $labels[ $key ] ?? null; |
| 145 |
} |
| 146 |
|
| 147 |
return $labels; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Additional converting rules. |
| 152 |
* |
| 153 |
* @param array $configs |
| 154 |
* |
| 155 |
* @return array |
| 156 |
* @since 3.1.0 |
| 157 |
*/ |
| 158 |
public function type_casting( $configs ) { |
| 159 |
return $configs; |
| 160 |
} |
| 161 |
} |
| 162 |
|