class-mail.php
158 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 | |
| 4 | /** |
| 5 | * Class to send an e-mail |
| 6 | */ |
| 7 | |
| 8 | if ( !class_exists('cmplz_mailer') ) { |
| 9 | class cmplz_mailer { |
| 10 | |
| 11 | public $to; |
| 12 | public $title; |
| 13 | public $headers; |
| 14 | public $message; |
| 15 | public $subject; |
| 16 | public $change_text; |
| 17 | public $sent_to_text; |
| 18 | public $what_now_text; |
| 19 | public $sent_by_text; |
| 20 | public $warning_blocks; |
| 21 | public $error = ''; |
| 22 | public $body; |
| 23 | |
| 24 | public function __construct() { |
| 25 | $this->sent_by_text = __("This email is part of the Complianz Notification System", "complianz-gdpr"); |
| 26 | $this->subject = __("Notification by Complianz", "complianz-gdpr"); |
| 27 | $this->title = __("Learn more about our features!", "complianz-gdpr"); |
| 28 | $this->sent_to_text = __("This email was sent to", "complianz-gdpr"); |
| 29 | $this->what_now_text = __( "What now?", "complianz-gdpr"); |
| 30 | $this->change_text = __("I didn't change any settings in the plugin.", "complianz-gdpr"); |
| 31 | $domain = '<a href="'.site_url().'">'.site_url().'</a>'; |
| 32 | $this->message = sprintf(__("You have enabled a feature on %s. We think it's important to let you know a little bit more about this feature so you can use it without worries.","complianz-gdpr"), $domain); |
| 33 | |
| 34 | add_action('wp_mail_failed', array($this, 'log_mailer_errors'), 10, 1); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Send a test email |
| 39 | * @return array |
| 40 | */ |
| 41 | public function send_test_mail(){ |
| 42 | if ( !cmplz_user_can_manage() ) { |
| 43 | return ['success' => false, 'message' => 'Not allowed']; |
| 44 | } |
| 45 | $this->to = cmplz_get_option('notifications_email_address' ); |
| 46 | if ( !is_email($this->to) ) { |
| 47 | $this->to = get_bloginfo('admin_email'); |
| 48 | } |
| 49 | |
| 50 | if ( !is_email($this->to) ){ |
| 51 | return ['success' => false, 'message' => __('Email address not valid',"complianz-gdpr")]; |
| 52 | } |
| 53 | $this->title = __("Complianz - Notification Test", "complianz-gdpr"); |
| 54 | $this->message = __("This email is confirmation that any email notices are likely to reach your inbox.", "complianz-gdpr"); |
| 55 | |
| 56 | $this->warning_blocks = [ |
| 57 | [ |
| 58 | 'title' => __("About notifications","complianz-gdpr"), |
| 59 | 'message' => __("Email notifications are only sent for important updates, changes or when certain features are enabled.","complianz-gdpr"), |
| 60 | 'url' => 'https://complianz.io/instructions/about-email-notifications/', |
| 61 | ] |
| 62 | ]; |
| 63 | $success = $this->send_mail(true); |
| 64 | if ($success) { |
| 65 | return ['success' => true, 'message' => __('Email sent! Please check your mail', "complianz-gdpr")]; |
| 66 | } |
| 67 | |
| 68 | if (empty($this->error)) { |
| 69 | $this->error = __('Email could not be sent.', "complianz-gdpr"); |
| 70 | } else { |
| 71 | $this->error = __('An error occurred:', "complianz-gdpr").'<br>'.$this->error; |
| 72 | } |
| 73 | return ['success' => false, 'message' => $this->error]; |
| 74 | } |
| 75 | |
| 76 | public function log_mailer_errors( $wp_error ){ |
| 77 | if (is_wp_error($wp_error)) { |
| 78 | $this->error = $wp_error->get_error_message(); |
| 79 | } |
| 80 | } |
| 81 | /** |
| 82 | * Send an e-mail with the correct login URL |
| 83 | * |
| 84 | * @param bool $override_rate_limit |
| 85 | * |
| 86 | * @return bool |
| 87 | */ |
| 88 | public function send_mail($override_rate_limit = false) { |
| 89 | if ( empty($this->message) || empty($this->subject) ) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | $this->to = cmplz_get_option('notifications_email_address' ); |
| 94 | if ( !is_email($this->to) ) { |
| 95 | $this->to = get_bloginfo('admin_email'); |
| 96 | } |
| 97 | |
| 98 | $template = file_get_contents(__DIR__.'/templates/email.html'); |
| 99 | $block_html = ''; |
| 100 | if (is_array($this->warning_blocks) && count($this->warning_blocks)>0) { |
| 101 | $block_template = file_get_contents(__DIR__.'/templates/block.html'); |
| 102 | foreach ($this->warning_blocks as $warning_block){ |
| 103 | $block_html .= str_replace( |
| 104 | ['{title}','{message}','{url}'], |
| 105 | [ sanitize_text_field($warning_block['title']), wp_kses_post($warning_block['message']), esc_url_raw($warning_block['url']) ], |
| 106 | $block_template); |
| 107 | } |
| 108 | } |
| 109 | $body = str_replace( |
| 110 | [ |
| 111 | '{title}', |
| 112 | '{message}', |
| 113 | '{warnings}', |
| 114 | '{email-address}', |
| 115 | '{learn-more}', |
| 116 | '{site_url}', |
| 117 | '{change_text}', |
| 118 | '{what_now}', |
| 119 | '{sent_to_text}', |
| 120 | '{sent_by_text}' |
| 121 | ], |
| 122 | [ |
| 123 | sanitize_text_field( $this->title ), |
| 124 | wp_kses_post( $this->message ), |
| 125 | $block_html, |
| 126 | $this->to, |
| 127 | __( "Learn more", "complianz-gdpr" ), |
| 128 | site_url(), |
| 129 | $this->change_text, |
| 130 | $this->what_now_text, |
| 131 | $this->sent_to_text, |
| 132 | $this->sent_by_text |
| 133 | ], $template ); |
| 134 | $success = wp_mail( $this->to, sanitize_text_field($this->subject), $body, array('Content-Type: text/html; charset=UTF-8') ); |
| 135 | set_transient('cmplz_email_recently_sent', true, 5 * MINUTE_IN_SECONDS ); |
| 136 | return $success; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sends a basic email. |
| 141 | * |
| 142 | * This method checks if the necessary properties (`to`, `message`, and `subject`) are set before sending the email. |
| 143 | * If any of these properties are empty, the method returns false. |
| 144 | * Otherwise, it sends the email using the `wp_mail` function. |
| 145 | * |
| 146 | * @return bool True if the email was sent successfully, false otherwise. |
| 147 | */ |
| 148 | public function send_basic_mail() {// |
| 149 | // check the properties before to send |
| 150 | if ( empty($this->to) || empty($this->message) || empty($this->subject) ) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | return wp_mail( $this->to, sanitize_text_field($this->subject), wp_kses_post($this->body), $this->headers); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 |