| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Model\Notification; |
| 4 |
|
| 5 |
use Calotes\Helper\Array_Cache; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Tweak_Reminder. |
| 9 |
* |
| 10 |
* @package WP_Defender\Model\Notification |
| 11 |
*/ |
| 12 |
class Tweak_Reminder extends \WP_Defender\Model\Notification { |
| 13 |
/** |
| 14 |
* Option name. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $table = 'wd_security_tweaks_reminder'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Load the default first before actual value imported from db. |
| 22 |
* |
| 23 |
* @return string|void |
| 24 |
*/ |
| 25 |
protected function before_load() { |
| 26 |
$params = array( |
| 27 |
'slug' => 'tweak-reminder', |
| 28 |
'title' => __( 'Security Recommendations - Notification', 'wpdef' ), |
| 29 |
'status' => self::STATUS_DISABLED, |
| 30 |
'description' => __( 'Get email notifications if/when a security recommendation needs fixing.', 'wpdef' ), |
| 31 |
// @since 3.0.0 Fix 'Guest'-line. |
| 32 |
'in_house_recipients' => is_user_logged_in() ? array( $this->get_default_user() ) : array(), |
| 33 |
'out_house_recipients' => array(), |
| 34 |
'type' => 'notification', |
| 35 |
'dry_run' => false, |
| 36 |
'configs' => array( |
| 37 |
'reminder' => 'weekly', |
| 38 |
), |
| 39 |
); |
| 40 |
$this->import( $params ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* |
| 45 |
* @return bool |
| 46 |
* @throws \Exception |
| 47 |
*/ |
| 48 |
public function maybe_send() { |
| 49 |
if ( self::STATUS_ACTIVE !== $this->status ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$est = new \DateTime( 'now', wp_timezone() ); |
| 54 |
$est->setTimestamp( $this->last_sent ); |
| 55 |
$now = new \DateTime( 'now', wp_timezone() ); |
| 56 |
$interval = \DateInterval::createFromDateString( (string) $est->getOffset() . 'seconds' ); |
| 57 |
$now->add( $interval ); |
| 58 |
|
| 59 |
switch ( $this->configs['reminder'] ) { |
| 60 |
case 'daily': |
| 61 |
$est->add( new \DateInterval( 'P1D' ) ); |
| 62 |
break; |
| 63 |
case 'weekly': |
| 64 |
$est->add( new \DateInterval( 'P1W' ) ); |
| 65 |
break; |
| 66 |
case 'monthly': |
| 67 |
default: |
| 68 |
$est->add( new \DateInterval( 'P1M' ) ); |
| 69 |
break; |
| 70 |
} |
| 71 |
$est->add( $interval ); |
| 72 |
// Testing. |
| 73 |
if ( defined( 'WP_DEFENDER_TESTING' ) && true === constant( 'WP_DEFENDER_TESTING' ) ) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
if ( $est->getTimestamp() < $now->getTimestamp() ) { |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
public function send() { |
| 84 |
$tweaks = wd_di()->get( \WP_Defender\Model\Setting\Security_Tweaks::class ); |
| 85 |
if ( 0 === (is_array($tweaks->issues) || $tweaks->issues instanceof \Countable ? count( $tweaks->issues ) : 0) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
$arr = Array_Cache::get( 'tweaks', 'tweaks' ); |
| 89 |
$issues = ''; |
| 90 |
$template = wd_di()->get( \WP_Defender\Controller\Security_Tweaks::class ); |
| 91 |
$status_img = defender_asset_url( '/assets/email-assets/img/[email protected]' ); |
| 92 |
foreach ( $tweaks->issues as $slug ) { |
| 93 |
if ( isset( $arr[ $slug ] ) ) { |
| 94 |
$issue = $arr[ $slug ]; |
| 95 |
$data = $issue->to_array(); |
| 96 |
$issues .= $template->render_partial( |
| 97 |
'email/tweak-issue', |
| 98 |
array( |
| 99 |
'data' => $data, |
| 100 |
'status_img' => $status_img, |
| 101 |
), |
| 102 |
false |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
foreach ( $this->in_house_recipients as $recipient ) { |
| 108 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
$this->send_to_user( $recipient['email'], $recipient['name'], $issues ); |
| 112 |
} |
| 113 |
|
| 114 |
foreach ( $this->out_house_recipients as $recipient ) { |
| 115 |
if ( self::USER_SUBSCRIBED !== $recipient['status'] ) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
$this->send_to_user( $recipient['email'], $recipient['name'], $issues ); |
| 119 |
} |
| 120 |
$this->last_sent = time(); |
| 121 |
$this->save(); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param $email |
| 126 |
* @param $name |
| 127 |
* @param $issues |
| 128 |
* |
| 129 |
* @throws \DI\DependencyException |
| 130 |
* @throws \DI\NotFoundException |
| 131 |
*/ |
| 132 |
public function send_to_user( $email, $name, $issues ) { |
| 133 |
$tweaks = wd_di()->get( \WP_Defender\Model\Setting\Security_Tweaks::class ); |
| 134 |
$logs_url = network_admin_url( 'admin.php?page=wdf-hardener' ); |
| 135 |
$logs_url = apply_filters( 'report_email_logs_link', $logs_url, $email ); |
| 136 |
|
| 137 |
$security_tweak = wd_di()->get( \WP_Defender\Controller\Security_Tweaks::class ); |
| 138 |
$content_body = $security_tweak->render_partial( |
| 139 |
'email/tweaks-reminder', |
| 140 |
array( |
| 141 |
'count' => is_array($tweaks->issues) || $tweaks->issues instanceof \Countable ? count( $tweaks->issues ) : 0, |
| 142 |
'view_url' => $logs_url, |
| 143 |
'name' => $name, |
| 144 |
'issues' => $issues, |
| 145 |
'site_url' => network_site_url(), |
| 146 |
), |
| 147 |
false |
| 148 |
); |
| 149 |
$content = $security_tweak->render_partial( |
| 150 |
'email/index', |
| 151 |
array( |
| 152 |
'title' => __( 'Security Report', 'wpdef' ), |
| 153 |
'content_body' => $content_body, |
| 154 |
), |
| 155 |
false |
| 156 |
); |
| 157 |
|
| 158 |
/* translators: */ |
| 159 |
$subject = _n( |
| 160 |
'Security Recommendation Report for %1$s. %2$s recommendation to action.', |
| 161 |
'Security Recommendation Report for %1$s. %2$s recommendations to action.', |
| 162 |
is_array($tweaks->issues) || $tweaks->issues instanceof \Countable ? count( $tweaks->issues ) : 0, |
| 163 |
'wpdef' |
| 164 |
); |
| 165 |
$subject = sprintf( $subject, network_site_url(), is_array($tweaks->issues) || $tweaks->issues instanceof \Countable ? count( $tweaks->issues ) : 0 ); |
| 166 |
|
| 167 |
$headers = defender_noreply_html_header( |
| 168 |
defender_noreply_email( 'wd_recommendation_noreply_email' ) |
| 169 |
); |
| 170 |
|
| 171 |
$ret = wp_mail( $email, $subject, $content, $headers ); |
| 172 |
if ( $ret ) { |
| 173 |
$this->save_log( $email ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Define labels for settings key. |
| 179 |
* |
| 180 |
* @param string|null $key |
| 181 |
* |
| 182 |
* @return string|array|null |
| 183 |
*/ |
| 184 |
public function labels( $key = null ) { |
| 185 |
$labels = array( |
| 186 |
'notification' => __( 'Security Recommendations - Notification', 'wpdef' ), |
| 187 |
'notification_repeat' => __( 'Frequency', 'wpdef' ), |
| 188 |
'subscribers' => __( 'Recipients', 'wpdef' ), |
| 189 |
); |
| 190 |
|
| 191 |
if ( ! is_null( $key ) ) { |
| 192 |
return $labels[ $key ] ?? null; |
| 193 |
} |
| 194 |
|
| 195 |
return $labels; |
| 196 |
} |
| 197 |
} |
| 198 |
|