← All changes
|
src/model/notification/class-malware-notification.php
+64
-25
5.3.1
→
6.3.0
View file →
| @@ -12,12 +12,10 @@ | ||
| 12 | 12 | use WP_Defender\Model\Scan; |
| 13 | 13 | use WP_Defender\Component\Mail; |
| 14 | 14 | use WP_Defender\Component\Notification; |
| 15 | 15 | use WP_Defender\Model\Scan_Item; |
| 16 | -use WP_Defender\Model\Notification\Malware_Report; | |
| 17 | 16 | use WP_Defender\Traits\Scan_Email_Template; |
| 18 | 17 | use WP_Defender\Traits\Scan_Upsell; |
| 19 | -use WP_Defender\Behavior\WPMUDEV; | |
| 20 | 18 | |
| 21 | 19 | /** |
| 22 | 20 | * Handles the scheduling and sending of malware-related notifications. |
| 23 | 21 | */ |
| @@ -65,16 +63,16 @@ | ||
| 65 | 63 | */ |
| 66 | 64 | protected function before_load(): void { |
| 67 | 65 | $default = array( |
| 68 | 66 | 'slug' => self::SLUG, |
| 69 | - 'title' => esc_html__( 'Malware Scanning - Notification', 'defender-security' ), | |
| 67 | + 'title' => esc_html__( 'Issue - Alert', 'defender-security' ), | |
| 70 | 68 | 'status' => self::STATUS_DISABLED, |
| 71 | 69 | 'description' => esc_html__( |
| 72 | - 'Get email notifications when Defender has finished manual malware scans.', | |
| 70 | + 'Get email alerts when Defender has finished manual scans.', | |
| 73 | 71 | 'defender-security' |
| 74 | 72 | ), |
| 75 | 73 | // @since 3.0.0 Fix 'Guest'-line. |
| 76 | - 'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), | |
| 74 | + 'in_house_recipients' => array(), | |
| 77 | 75 | 'out_house_recipients' => array(), |
| 78 | 76 | 'type' => 'notification', |
| 79 | 77 | 'dry_run' => false, |
| 80 | 78 | 'configs' => array( |
| @@ -111,8 +109,44 @@ | ||
| 111 | 109 | } |
| 112 | 110 | } |
| 113 | 111 | |
| 114 | 112 | /** |
| 113 | + * Sends a manual scan notification to the user who triggered the scan. | |
| 114 | + * | |
| 115 | + * @param Scan $scan The scan object. | |
| 116 | + * | |
| 117 | + * @return bool Whether the manual scan notification was handled. | |
| 118 | + */ | |
| 119 | + private function send_manual_scan( Scan $scan ): bool { | |
| 120 | + $user_id = (int) get_transient( 'defender_scan_triggered_by_' . $scan->id ); | |
| 121 | + delete_transient( 'defender_scan_triggered_by_' . $scan->id ); | |
| 122 | + if ( $user_id <= 0 ) { | |
| 123 | + return false; | |
| 124 | + } | |
| 125 | + | |
| 126 | + $user = get_userdata( $user_id ); | |
| 127 | + if ( ! $user ) { | |
| 128 | + return true; | |
| 129 | + } | |
| 130 | + | |
| 131 | + $issues = $scan->get_issues( null, Scan_Item::STATUS_ACTIVE ); | |
| 132 | + if ( in_array( $scan->status, array( Scan::STATUS_ERROR, Scan::STATUS_IDLE ), true ) ) { | |
| 133 | + $templates = $this->configs['template']['error']; | |
| 134 | + } elseif ( 0 === count( $issues ) ) { | |
| 135 | + return true; | |
| 136 | + } else { | |
| 137 | + $templates = $this->configs['template']['found']; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $subject = $templates['subject']; | |
| 141 | + $content = nl2br( $templates['body'] ); | |
| 142 | + $service = wd_di()->get( Notification::class ); | |
| 143 | + $this->send_to_user( $user->user_email, $user->display_name, $subject, $content, $issues, $service ); | |
| 144 | + | |
| 145 | + return true; | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 115 | 149 | * Sends the malware notification email. |
| 116 | 150 | * Constructs and sends the malware notification email to the recipients. |
| 117 | 151 | * |
| 118 | 152 | * @param Scan $scan The scan object. |
| @@ -118,9 +152,14 @@ | ||
| 118 | 152 | * @param Scan $scan The scan object. |
| 119 | 153 | * |
| 120 | 154 | * @return void |
| 121 | 155 | */ |
| 122 | - public function send( Scan $scan ) { | |
| 156 | + public function send( Scan $scan ): void { | |
| 157 | + // Manual scan: notify only the triggering user when issues are found or the scan fails. | |
| 158 | + if ( ! $scan->is_automation && $this->send_manual_scan( $scan ) ) { | |
| 159 | + return; | |
| 160 | + } | |
| 161 | + | |
| 123 | 162 | $object = $this; |
| 124 | 163 | if ( $scan->is_automation ) { |
| 125 | 164 | $object = wd_di()->get( Malware_Report::class ); |
| 126 | 165 | // If Malware Reporting is unchecked, then return. |
| @@ -130,28 +169,30 @@ | ||
| 130 | 169 | // @since 2.7.0 Remove 'dry_run'-condition. |
| 131 | 170 | } |
| 132 | 171 | $issues = $scan->get_issues( null, Scan_Item::STATUS_ACTIVE ); |
| 133 | 172 | /** |
| 134 | - * When the Defender is unable to scan files for some reason and the 'error_send' option is checked. | |
| 173 | + * When the Defender is unable to scan files for some reason. | |
| 174 | + * The 'error_send' option was removed in v6. Its stored value is preserved for backward | |
| 175 | + * compatibility (downgrade safety), but error notifications are now always sent regardless | |
| 176 | + * of what the legacy value is. | |
| 135 | 177 | * Possible reasons: |
| 136 | 178 | * - no connection to Scan API, |
| 137 | 179 | * - no checksums received from wp.org, |
| 138 | 180 | * - scan process stuck. |
| 139 | 181 | */ |
| 140 | - if ( | |
| 141 | - isset( $object->configs['error_send'] ) && ! empty( | |
| 142 | - $object->configs['error_send'] | |
| 143 | - && in_array( $scan->status, array( Scan::STATUS_ERROR, Scan::STATUS_IDLE ), true ) | |
| 144 | - ) | |
| 145 | - ) { | |
| 182 | + if ( in_array( $scan->status, array( Scan::STATUS_ERROR, Scan::STATUS_IDLE ), true ) ) { | |
| 146 | 183 | $subject = $object->configs['template']['error']['subject']; |
| 147 | 184 | $content = nl2br( $object->configs['template']['error']['body'] ); |
| 148 | 185 | |
| 149 | - return $this->email_engine( $scan, $object, $subject, $content, $issues ); | |
| 186 | + $this->email_engine( $scan, $object, $subject, $content, $issues ); | |
| 187 | + | |
| 188 | + return; | |
| 150 | 189 | } |
| 151 | 190 | |
| 152 | - // Sometimes value may be as empty string for disabled 'always_send'. | |
| 153 | - if ( empty( $object->configs['always_send'] ) && 0 === count( $issues ) ) { | |
| 191 | + // The 'always_send' option was removed in v6. Its stored value is preserved for backward | |
| 192 | + // compatibility (downgrade safety), but it must have no effect here — never send when | |
| 193 | + // there are no issues, regardless of what the legacy value is. | |
| 194 | + if ( 0 === count( $issues ) ) { | |
| 154 | 195 | return; |
| 155 | 196 | } |
| 156 | 197 | // For another case. |
| 157 | 198 | $templates = count( $issues ) ? $object->configs['template']['found'] : $object->configs['template']['not_found']; |
| @@ -157,9 +198,9 @@ | ||
| 157 | 198 | $templates = count( $issues ) ? $object->configs['template']['found'] : $object->configs['template']['not_found']; |
| 158 | 199 | $subject = $templates['subject']; |
| 159 | 200 | $content = nl2br( $templates['body'] ); |
| 160 | 201 | |
| 161 | - return $this->email_engine( $scan, $object, $subject, $content, $issues ); | |
| 202 | + $this->email_engine( $scan, $object, $subject, $content, $issues ); | |
| 162 | 203 | } |
| 163 | 204 | |
| 164 | 205 | /** |
| 165 | 206 | * Sends the malware notification email to a specific user. |
| @@ -174,12 +215,10 @@ | ||
| 174 | 215 | * |
| 175 | 216 | * @return void |
| 176 | 217 | */ |
| 177 | 218 | private function send_to_user( $email, $name, $subject, $content, $issues, $service ) { |
| 178 | - $wpmudev = new WPMUDEV(); | |
| 179 | - $maybe_show_upsell = ! $wpmudev->is_whitelabel_enabled() && $wpmudev->is_pro(); | |
| 180 | - $controller_scan = wd_di()->get( \WP_Defender\Controller\Scan::class ); | |
| 181 | - $replacers = array( | |
| 219 | + $controller_scan = wd_di()->get( \WP_Defender\Controller\Scan::class ); | |
| 220 | + $replacers = array( | |
| 182 | 221 | 'issues_count' => is_array( $issues ) || $issues instanceof \Countable ? (string) count( $issues ) : '0', |
| 183 | 222 | 'issues_list' => $controller_scan->render_partial( |
| 184 | 223 | 'email/scan-issue-list', |
| 185 | 224 | array( |
| @@ -240,9 +279,9 @@ | ||
| 240 | 279 | $unsubscribe_link = $service->create_unsubscribe_url( $this->slug, $email ); |
| 241 | 280 | $template = $controller_scan->render_partial( |
| 242 | 281 | 'email/index', |
| 243 | 282 | array( |
| 244 | - 'title' => esc_html__( 'Malware Scanning', 'defender-security' ), | |
| 283 | + 'title' => esc_html__( 'Issues report', 'defender-security' ), | |
| 245 | 284 | 'content_body' => sprintf( '<div style="font-size: 16px">%s</div>', $content ), |
| 246 | 285 | 'unsubscribe_link' => $unsubscribe_link, |
| 247 | 286 | ), |
| 248 | 287 | false |
| @@ -265,12 +304,12 @@ | ||
| 265 | 304 | * @return array The array of malware notification settings labels. |
| 266 | 305 | */ |
| 267 | 306 | public function labels(): array { |
| 268 | 307 | return array( |
| 269 | - 'notification' => esc_html__( 'Malware Scanning - Notification', 'defender-security' ), | |
| 270 | - 'always_send_notification' => esc_html__( 'Send notifications when no issues are detected', 'defender-security' ), | |
| 308 | + 'notification' => esc_html__( 'Issue - Alert', 'defender-security' ), | |
| 309 | + 'always_send_notification' => esc_html__( 'Send alerts when no issues are detected', 'defender-security' ), | |
| 271 | 310 | 'error_send' => esc_html__( |
| 272 | - "Send notifications when Defender couldn't scan the files", | |
| 311 | + "Send alerts when Defender couldn't scan the files", | |
| 273 | 312 | 'defender-security' |
| 274 | 313 | ), |
| 275 | 314 | 'email_subject_issue_found' => esc_html__( 'Email subject when an issue is found', 'defender-security' ), |
| 276 | 315 | 'email_subject_issue_not_found' => esc_html__( 'Email subject when no issues are found', 'defender-security' ), |