display-conditions
5 years ago
front
5 years ago
helpers
5 years ago
metas
5 years ago
palettes
5 years ago
provider
5 years ago
providers
5 years ago
templates
5 years ago
update
5 years ago
class-hustle-admin-page-abstract.php
5 years ago
class-hustle-condition-factory.php
6 years ago
class-hustle-dashboard-admin.php
5 years ago
class-hustle-data.php
5 years ago
class-hustle-db.php
6 years ago
class-hustle-module-admin.php
5 years ago
class-hustle-module-collection.php
5 years ago
class-hustle-module-decorator.php
5 years ago
class-hustle-module-page-abstract.php
5 years ago
class-hustle-notifications.php
5 years ago
class-hustle-settings-admin.php
5 years ago
class-hustle-upsell-page.php
5 years ago
class-hustle-wp-dashboard-page.php
5 years ago
hustle-collection.php
6 years ago
hustle-deletion.php
5 years ago
hustle-embedded-admin.php
6 years ago
hustle-entries-admin.php
5 years ago
hustle-entry-model.php
5 years ago
hustle-general-data-protection.php
6 years ago
hustle-init.php
5 years ago
hustle-mail.php
5 years ago
hustle-meta.php
5 years ago
hustle-migration.php
5 years ago
hustle-model.php
5 years ago
hustle-module-model.php
5 years ago
hustle-module-widget-legacy.php
5 years ago
hustle-module-widget.php
5 years ago
hustle-modules-common-admin-ajax.php
5 years ago
hustle-popup-admin.php
6 years ago
hustle-providers-admin.php
5 years ago
hustle-providers.php
6 years ago
hustle-settings-admin-ajax.php
5 years ago
hustle-settings-page.php
5 years ago
hustle-slidein-admin.php
6 years ago
hustle-sshare-admin.php
5 years ago
hustle-sshare-model.php
5 years ago
hustle-tracking-model.php
5 years ago
opt-in-geo.php
5 years ago
opt-in-utils.php
5 years ago
opt-in-wpmudev-api.php
6 years ago
hustle-mail.php
238 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for sending emails. |
| 4 | * |
| 5 | * @since 3.0.5 |
| 6 | */ |
| 7 | class Hustle_Mail { |
| 8 | |
| 9 | /** |
| 10 | * Email recipient |
| 11 | * The email address that will receive the mail |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $recipient = ''; |
| 16 | |
| 17 | /** |
| 18 | * Email message |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $message = ''; |
| 23 | |
| 24 | /** |
| 25 | * Email subject |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $subject = ''; |
| 30 | |
| 31 | /** |
| 32 | * Email 'from' address |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $sender_email = ''; |
| 37 | |
| 38 | /** |
| 39 | * Email 'from' name |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $sender_name = ''; |
| 44 | |
| 45 | /** |
| 46 | * Email headers |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | protected $headers = array(); |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Main constructor |
| 55 | * |
| 56 | * @since 3.0.5 |
| 57 | * @param string $recipient The email recipient. |
| 58 | * @param string $message The email message. |
| 59 | * @param string $subject The email subject. |
| 60 | */ |
| 61 | public function __construct( $recipient = '', $message = '', $subject = '' ) { |
| 62 | if ( ! empty( $recipient ) && filter_var( $recipient, FILTER_VALIDATE_EMAIL ) ) { |
| 63 | $this->recipient = $recipient; |
| 64 | } |
| 65 | if ( ! empty( $message ) ) { |
| 66 | $this->message = $message; |
| 67 | } |
| 68 | if ( ! empty( $subject ) ) { |
| 69 | $this->subject = $subject; |
| 70 | } |
| 71 | |
| 72 | $general_settings = Hustle_Settings_Admin::get_general_settings(); |
| 73 | $this->sender_email = $general_settings['sender_email_address']; |
| 74 | $this->sender_name = $general_settings['sender_email_name']; |
| 75 | $this->set_headers(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set recipient |
| 80 | * |
| 81 | * @since 3.0.5 |
| 82 | * @param string $recipient The email recipient. |
| 83 | */ |
| 84 | public function set_recipient( $recipient ) { |
| 85 | if ( filter_var( $recipient, FILTER_VALIDATE_EMAIL ) ) { |
| 86 | $this->recipient = $recipient; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set message |
| 92 | * |
| 93 | * @since 3.0.5 |
| 94 | * @param string $message The email message. |
| 95 | */ |
| 96 | public function set_message( $message ) { |
| 97 | $this->message = $message; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set headers. |
| 102 | * |
| 103 | * @since 3.0.5 |
| 104 | * @param array $headers The email headers. |
| 105 | */ |
| 106 | public function set_headers( $headers = array() ) { |
| 107 | if ( ! empty( $headers ) ) { |
| 108 | $this->headers = $headers; |
| 109 | |
| 110 | } else { |
| 111 | |
| 112 | $this->headers = array( |
| 113 | 'From: ' . $this->sender_name . ' <' . $this->sender_email . '>', |
| 114 | ); |
| 115 | $this->headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Set sender details. |
| 121 | * |
| 122 | * @since 3.0.5 |
| 123 | * @param array $sender_details - the sender details ( 'email' => 'email', 'name' => 'name' ). |
| 124 | */ |
| 125 | public function set_sender( $sender_details = array() ) { |
| 126 | if ( ! empty( $sender_details ) ) { |
| 127 | $this->sender_email = $sender_details['email']; |
| 128 | $this->sender_name = $sender_details['name']; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Clean mail variables |
| 134 | * |
| 135 | * @since 3.0.5 |
| 136 | */ |
| 137 | private function clean() { |
| 138 | $subject = stripslashes( $this->subject ); |
| 139 | $subject = wp_strip_all_tags( $subject ); |
| 140 | $this->subject = $subject; |
| 141 | |
| 142 | $message = stripslashes( $this->message ); |
| 143 | $message = wpautop( $message ); |
| 144 | $message = make_clickable( $message ); |
| 145 | $this->message = $message; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Send mail |
| 150 | * |
| 151 | * @since 3.0.5 |
| 152 | * @return bool |
| 153 | */ |
| 154 | public function send() { |
| 155 | $sent = false; |
| 156 | if ( ! empty( $this->recipient ) && ! empty( $this->subject ) && ! empty( $this->message ) ) { |
| 157 | $this->clean(); |
| 158 | $sent = wp_mail( $this->recipient, $this->subject, $this->message, $this->headers ); |
| 159 | } |
| 160 | return $sent; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Send the actual email. |
| 165 | * |
| 166 | * @since 3.0.5 |
| 167 | * @return bool |
| 168 | */ |
| 169 | public function process_mail() { |
| 170 | return $this->send(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Does the process to submit the unsubscription email. |
| 175 | * |
| 176 | * @since 3.0.5 |
| 177 | * @param string $email Email to be unsubscribed. |
| 178 | * @param array $modules_id IDs of the modules to which it will be unsubscribed. |
| 179 | * @param string $referer URL referer. |
| 180 | * @return boolean |
| 181 | */ |
| 182 | public static function handle_unsubscription_user_email( $email, $modules_id, $referer ) { |
| 183 | if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 184 | Opt_In_Utils::maybe_log( __METHOD__, 'The provided email address is not valid.' ); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | if ( ! filter_var( $referer, FILTER_VALIDATE_URL ) ) { |
| 189 | Opt_In_Utils::maybe_log( __METHOD__, 'The provided referer is not valid.' ); |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | $module = new Hustle_Module_Model(); |
| 194 | $nonce = $module->create_unsubscribe_nonce( $email, $modules_id ); |
| 195 | if ( ! $nonce ) { |
| 196 | Opt_In_Utils::maybe_log( __METHOD__, 'There was an error getting the nonce.' ); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | $parsed_url = wp_parse_url( $referer, PHP_URL_QUERY ); |
| 201 | $concatenate = empty( $parsed_url ) ? '?' : '&'; |
| 202 | $email = apply_filters( 'hustle_unsubscribe_email_recipient', $email, $modules_id, $referer ); |
| 203 | $unsubscribe_url = apply_filters( |
| 204 | 'hustle_unsubscribe_email_url', |
| 205 | $referer . $concatenate . 'token=' . $nonce . '&email=' . rawurlencode( $email ), |
| 206 | $email, |
| 207 | $modules_id, |
| 208 | $referer |
| 209 | ); |
| 210 | |
| 211 | $email_settings = Hustle_Settings_Admin::get_unsubscribe_email_settings(); |
| 212 | $message = str_replace( '{hustle_unsubscribe_link}', $unsubscribe_url, $email_settings['email_body'] ); |
| 213 | $message = apply_filters( 'hustle_unsubscribe_email_message', $message, $unsubscribe_url, $email, $modules_id, $referer ); |
| 214 | |
| 215 | $email_handler = new self( $email, $message, $email_settings['email_subject'] ); |
| 216 | $sent = $email_handler->process_mail(); |
| 217 | |
| 218 | return $sent; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Static function to send an email. |
| 223 | * |
| 224 | * @since 3.0.5 |
| 225 | * @param string $to Email recipient. |
| 226 | * @param string $subject Email subject. |
| 227 | * @param string $body Email body. |
| 228 | * @return bool |
| 229 | */ |
| 230 | public static function send_email( $to, $subject, $body ) { |
| 231 | $email_handler = new self( $to, $body, $subject ); |
| 232 | $sent = $email_handler->process_mail(); |
| 233 | |
| 234 | return $sent; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 |