| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Handles the scheduling and sending of audit logging reports. | |
| 3 | + * Audit Report notification model. | |
| 4 | 4 | * |
| 5 | 5 | * @package WP_Defender\Model\Notification |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -6,19 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Model\Notification; |
| 9 | 9 | |
| 10 | -use DateTime; | |
| 11 | -use Exception; | |
| 12 | -use WP_Defender\Component\Mail; | |
| 13 | -use WP_Defender\Model\Audit_Log; | |
| 14 | -use WP_Defender\Component\Notification; | |
| 15 | -use WP_Defender\Controller\Audit_Logging; | |
| 16 | - | |
| 17 | 10 | /** |
| 18 | - * Model for audit logging settings. | |
| 11 | + * Audit Report notification model. | |
| 19 | 12 | */ |
| 20 | -class Audit_Report extends \WP_Defender\Model\Notification { | |
| 13 | +class Audit_Report extends Free_Report { | |
| 21 | 14 | |
| 22 | 15 | /** |
| 23 | 16 | * Table name. |
| 24 | 17 | * |
| @@ -24,8 +17,9 @@ | ||
| 24 | 17 | * |
| 25 | 18 | * @var string |
| 26 | 19 | */ |
| 27 | 20 | protected $table = 'wd_audit_report'; |
| 21 | + | |
| 28 | 22 | /** |
| 29 | 23 | * Slug identifier for the audit report. |
| 30 | 24 | * |
| 31 | 25 | * @var string |
| @@ -32,162 +26,30 @@ | ||
| 32 | 26 | */ |
| 33 | 27 | public const SLUG = 'audit-report'; |
| 34 | 28 | |
| 35 | 29 | /** |
| 36 | - * Constructor method. | |
| 37 | - * Sets default values for the class. | |
| 30 | + * Sets default values. | |
| 38 | 31 | */ |
| 39 | 32 | protected function before_load(): void { |
| 40 | - $default = array( | |
| 41 | - 'title' => esc_html__( 'Audit Logging - Reporting', 'defender-security' ), | |
| 42 | - 'slug' => self::SLUG, | |
| 43 | - 'status' => self::STATUS_DISABLED, | |
| 44 | - 'description' => esc_html__( | |
| 45 | - 'Schedule Defender to automatically email you a summary of all your website events.', | |
| 46 | - 'defender-security' | |
| 47 | - ), | |
| 48 | - // @since 3.0.0 Fix 'Guest'-line. | |
| 49 | - 'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), | |
| 50 | - 'out_house_recipients' => array(), | |
| 51 | - 'type' => 'report', | |
| 52 | - 'frequency' => 'weekly', | |
| 53 | - 'day' => 'sunday', | |
| 54 | - 'time' => '4:00', | |
| 55 | - 'day_n' => 1, | |
| 56 | - 'dry_run' => false, | |
| 57 | - 'configs' => array(), | |
| 58 | - ); | |
| 59 | - $this->import( $default ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * Sends the email. | |
| 64 | - * Retrieves data based on logic. | |
| 65 | - * | |
| 66 | - * @return void | |
| 67 | - * @throws Exception When an error occurs. | |
| 68 | - */ | |
| 69 | - public function send() { | |
| 70 | - $date_to = new DateTime( 'now', wp_timezone() ); | |
| 71 | - switch ( $this->frequency ) { | |
| 72 | - case 'daily': | |
| 73 | - $date_from = new DateTime( '-24 hours', wp_timezone() ); | |
| 74 | - break; | |
| 75 | - case 'weekly': | |
| 76 | - $date_from = new DateTime( '-7 days', wp_timezone() ); | |
| 77 | - break; | |
| 78 | - case 'monthly': | |
| 79 | - default: | |
| 80 | - $date_from = new DateTime( '-30 days', wp_timezone() ); | |
| 81 | - break; | |
| 82 | - } | |
| 83 | - $data = Audit_Log::query( $date_from->getTimestamp(), $date_to->getTimestamp(), array(), '', '', false ); | |
| 84 | - | |
| 85 | - $collection = array(); | |
| 86 | - // Collect data. | |
| 87 | - foreach ( $data as $item ) { | |
| 88 | - if ( ! isset( $collection[ $item->event_type ][ $item->action_type ] ) ) { | |
| 89 | - $collection[ $item->event_type ][ $item->action_type ] = 0; | |
| 90 | - } | |
| 91 | - ++$collection[ $item->event_type ][ $item->action_type ]; | |
| 92 | - } | |
| 93 | - $service = wd_di()->get( Notification::class ); | |
| 94 | - // Send data. | |
| 95 | - foreach ( $this->in_house_recipients as $recipient ) { | |
| 96 | - if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { | |
| 97 | - continue; | |
| 98 | - } | |
| 99 | - $this->send_to_user( $recipient['name'], $recipient['email'], $data, $collection, $service ); | |
| 100 | - } | |
| 101 | - foreach ( $this->out_house_recipients as $recipient ) { | |
| 102 | - if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { | |
| 103 | - continue; | |
| 104 | - } | |
| 105 | - $this->send_to_user( $recipient['name'], $recipient['email'], $data, $collection, $service ); | |
| 106 | - } | |
| 107 | - // Last sent should be the previous timestamp. | |
| 108 | - $this->last_sent = $this->est_timestamp; | |
| 109 | - $this->est_timestamp = $this->get_next_run()->getTimestamp(); | |
| 110 | - $this->save(); | |
| 111 | - } | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * Constructs and sends the email to the specified recipient. | |
| 115 | - * | |
| 116 | - * @param string $name The recipient's name. | |
| 117 | - * @param string $email The recipient's email address. | |
| 118 | - * @param array $data The audit log data. | |
| 119 | - * @param array $collection The summarized list of events. | |
| 120 | - * @param object $service The notification service object. | |
| 121 | - */ | |
| 122 | - private function send_to_user( $name, $email, $data, $collection, $service ) { | |
| 123 | - $site_url = network_site_url(); | |
| 124 | - // Use smart quotes to display the email subject in HTML correctly and save escaping. | |
| 125 | - $subject = sprintf( | |
| 126 | - /* translators: %s: Site URL. */ | |
| 127 | - esc_html__( 'Here`s what`s been happening at %s', 'defender-security' ), | |
| 128 | - $site_url | |
| 129 | - ); | |
| 130 | - $audit_logging = wd_di()->get( Audit_Logging::class ); | |
| 131 | - $mail_object = wd_di()->get( Mail::class ); | |
| 132 | - $plugin_label = $mail_object->get_sender_name( self::SLUG ); | |
| 133 | - if ( count( $data ) > 0 ) { | |
| 134 | - $logs_url = network_admin_url( 'admin.php?page=wdf-logging&view=logs' ); | |
| 135 | - // Need for activated Mask Login feature. | |
| 136 | - $logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); | |
| 137 | - $header = sprintf( | |
| 138 | - /* translators: 1. Plugin label. 2. Site URL. */ | |
| 139 | - esc_html__( 'Audit Update From %1$s! %2$s', 'defender-security' ), | |
| 140 | - $plugin_label, | |
| 141 | - $site_url | |
| 142 | - ); | |
| 143 | - $message = $audit_logging->render_partial( | |
| 144 | - 'email/audit-report-table', | |
| 33 | + $this->import( | |
| 34 | + array_merge( | |
| 35 | + $this->base_defaults(), | |
| 145 | 36 | array( |
| 146 | - 'logs_url' => $logs_url, | |
| 147 | - 'name' => $name, | |
| 148 | - 'collection' => $collection, | |
| 149 | - 'header' => $header, | |
| 150 | - ), | |
| 151 | - false | |
| 152 | - ); | |
| 153 | - } else { | |
| 154 | - $message = $audit_logging->render_partial( | |
| 155 | - 'email/audit-report-no-events', | |
| 156 | - array( | |
| 157 | - 'name' => $name, | |
| 158 | - 'site_url' => $site_url, | |
| 159 | - ), | |
| 160 | - false | |
| 161 | - ); | |
| 162 | - } | |
| 163 | - $unsubscribe_link = $service->create_unsubscribe_url( $this->slug, $email ); | |
| 164 | - // Main email template. | |
| 165 | - $content = $audit_logging->render_partial( | |
| 166 | - 'email/index', | |
| 167 | - array( | |
| 168 | - 'title' => esc_html__( 'Audit Logging', 'defender-security' ), | |
| 169 | - 'content_body' => $message, | |
| 170 | - 'unsubscribe_link' => $unsubscribe_link, | |
| 171 | - ), | |
| 172 | - false | |
| 37 | + 'slug' => self::SLUG, | |
| 38 | + 'title' => esc_html__( 'Audit Logging - Reporting', 'defender-security' ), | |
| 39 | + 'description' => esc_html__( | |
| 40 | + 'Schedule Defender to automatically email you a summary of all your website events.', | |
| 41 | + 'defender-security' | |
| 42 | + ), | |
| 43 | + ) | |
| 44 | + ) | |
| 173 | 45 | ); |
| 174 | - | |
| 175 | - $headers = $mail_object->get_headers( | |
| 176 | - defender_noreply_email( 'wd_audit_noreply_email' ), | |
| 177 | - self::SLUG | |
| 178 | - ); | |
| 179 | - | |
| 180 | - $ret = wp_mail( $email, $subject, $content, $headers ); | |
| 181 | - if ( $ret ) { | |
| 182 | - $this->save_log( $email ); | |
| 183 | - } | |
| 184 | 46 | } |
| 185 | 47 | |
| 186 | 48 | /** |
| 187 | 49 | * Define labels. |
| 188 | 50 | * |
| 189 | - * @return array The array of labels. | |
| 51 | + * @return array | |
| 190 | 52 | */ |
| 191 | 53 | public function labels(): array { |
| 192 | 54 | return array( |
| 193 | 55 | 'report' => esc_html__( 'Audit Logging - Reporting', 'defender-security' ), |
| @@ -198,16 +60,5 @@ | ||
| 198 | 60 | 'frequency' => esc_html__( 'Frequency', 'defender-security' ), |
| 199 | 61 | ); |
| 200 | 62 | } |
| 201 | 63 | |
| 202 | - /** | |
| 203 | - * Additional converting rules. | |
| 204 | - * | |
| 205 | - * @param array $configs The configuration data. | |
| 206 | - * | |
| 207 | - * @return array The type-casted configuration data. | |
| 208 | - * @since 3.1.0 | |
| 209 | - */ | |
| 210 | - public function type_casting( $configs ): array { | |
| 211 | - return is_array( $configs ) ? $configs : array(); | |
| 212 | - } | |
| 213 | 64 | } |