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