actions.php
6 years ago
class-give-email-tags.php
6 years ago
class-give-emails.php
6 years ago
functions.php
6 years ago
template.php
6 years ago
class-give-emails.php
432 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Emails |
| 4 | * |
| 5 | * This class handles all emails sent through Give |
| 6 | * |
| 7 | * @package Give |
| 8 | * @subpackage Classes/Emails |
| 9 | * @copyright Copyright (c) 2016, GiveWP |
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | * @since 1.0 |
| 12 | */ |
| 13 | |
| 14 | // Exit if accessed directly. |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Give_Emails Class. |
| 21 | * |
| 22 | * @property $from_address |
| 23 | * @property $from_name |
| 24 | * @property $content_type |
| 25 | * @property $headers |
| 26 | * @property $html |
| 27 | * @property $template |
| 28 | * @property $heading |
| 29 | * |
| 30 | * @since 1.0 |
| 31 | */ |
| 32 | class Give_Emails { |
| 33 | |
| 34 | /** |
| 35 | * Holds the from address. |
| 36 | * |
| 37 | * @since 1.0 |
| 38 | */ |
| 39 | private $from_address; |
| 40 | |
| 41 | /** |
| 42 | * Holds the from name. |
| 43 | * |
| 44 | * @since 1.0 |
| 45 | */ |
| 46 | private $from_name; |
| 47 | |
| 48 | /** |
| 49 | * Holds the email content type. |
| 50 | * |
| 51 | * @since 1.0 |
| 52 | */ |
| 53 | private $content_type; |
| 54 | |
| 55 | /** |
| 56 | * Holds the email headers. |
| 57 | * |
| 58 | * @since 1.0 |
| 59 | */ |
| 60 | private $headers; |
| 61 | |
| 62 | /** |
| 63 | * Whether to send email in HTML. |
| 64 | * |
| 65 | * @since 1.0 |
| 66 | */ |
| 67 | private $html = true; |
| 68 | |
| 69 | /** |
| 70 | * The email template to use. |
| 71 | * |
| 72 | * @since 1.0 |
| 73 | */ |
| 74 | private $template; |
| 75 | |
| 76 | /** |
| 77 | * The header text for the email. |
| 78 | * |
| 79 | * @since 1.0 |
| 80 | */ |
| 81 | private $heading = ''; |
| 82 | |
| 83 | /** |
| 84 | * Email template tags argument. |
| 85 | * This helps to decode email template tags, |
| 86 | * |
| 87 | * @since 1.0 |
| 88 | */ |
| 89 | public $tag_args = array(); |
| 90 | |
| 91 | /** |
| 92 | * Form ID |
| 93 | * |
| 94 | * @since 1.0 |
| 95 | */ |
| 96 | public $form_id = 0; |
| 97 | |
| 98 | /** |
| 99 | * Get things going. |
| 100 | * |
| 101 | * @since 1.0 |
| 102 | */ |
| 103 | public function __construct() { |
| 104 | |
| 105 | if ( 'none' === $this->get_template() ) { |
| 106 | $this->html = false; |
| 107 | } |
| 108 | |
| 109 | add_action( 'give_email_send_before', array( $this, 'send_before' ) ); |
| 110 | add_action( 'give_email_send_after', array( $this, 'send_after' ) ); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set a property. |
| 116 | * |
| 117 | * @since 1.0 |
| 118 | * |
| 119 | * @param $key |
| 120 | * @param $value |
| 121 | */ |
| 122 | public function __set( $key, $value ) { |
| 123 | $this->$key = $value; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get the email from name. |
| 128 | * |
| 129 | * @since 1.0 |
| 130 | */ |
| 131 | public function get_from_name() { |
| 132 | if ( ! $this->from_name ) { |
| 133 | $this->from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); |
| 134 | } |
| 135 | |
| 136 | return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the email from address. |
| 141 | * |
| 142 | * @since 1.0 |
| 143 | */ |
| 144 | public function get_from_address() { |
| 145 | if ( ! $this->from_address ) { |
| 146 | $this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) ); |
| 147 | } |
| 148 | |
| 149 | return apply_filters( 'give_email_from_address', $this->from_address, $this ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the email content type. |
| 154 | * |
| 155 | * @since 1.0 |
| 156 | */ |
| 157 | public function get_content_type() { |
| 158 | if ( ! $this->content_type ) { |
| 159 | $this->content_type = $this->html |
| 160 | ? apply_filters( 'give_email_default_content_type', 'text/html', $this ) |
| 161 | : 'text/plain'; |
| 162 | } |
| 163 | |
| 164 | return apply_filters( 'give_email_content_type', $this->content_type, $this ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the email headers. |
| 169 | * |
| 170 | * @since 1.0 |
| 171 | */ |
| 172 | public function get_headers() { |
| 173 | if ( ! $this->headers ) { |
| 174 | $this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n"; |
| 175 | $this->headers .= "Reply-To: {$this->get_from_address()}\r\n"; |
| 176 | $this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n"; |
| 177 | } |
| 178 | |
| 179 | return apply_filters( 'give_email_headers', $this->headers, $this ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Retrieve email templates. |
| 184 | * |
| 185 | * @since 1.0 |
| 186 | */ |
| 187 | public function get_templates() { |
| 188 | $templates = array( |
| 189 | 'default' => esc_html__( 'Default Template', 'give' ), |
| 190 | 'none' => esc_html__( 'No template, plain text only', 'give' ), |
| 191 | ); |
| 192 | |
| 193 | return apply_filters( 'give_email_templates', $templates ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the enabled email template. |
| 198 | * |
| 199 | * @since 1.0 |
| 200 | */ |
| 201 | public function get_template() { |
| 202 | if ( ! $this->template ) { |
| 203 | $this->template = give_get_option( 'email_template', 'default' ); |
| 204 | } |
| 205 | |
| 206 | return apply_filters( 'give_email_template', $this->template ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get the header text for the email. |
| 211 | * |
| 212 | * @since 1.0 |
| 213 | */ |
| 214 | public function get_heading() { |
| 215 | return apply_filters( 'give_email_heading', $this->heading ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Parse email template tags. |
| 220 | * |
| 221 | * @param $content |
| 222 | * |
| 223 | * @return mixed |
| 224 | */ |
| 225 | public function parse_tags( $content ) { |
| 226 | return $content; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Build the final email. |
| 231 | * |
| 232 | * @since 1.0 |
| 233 | * |
| 234 | * @param $message |
| 235 | * |
| 236 | * @return string |
| 237 | */ |
| 238 | public function build_email( $message ) { |
| 239 | |
| 240 | if ( false === $this->html ) { |
| 241 | |
| 242 | // Added Replacement check to simply behaviour of anchor tags. |
| 243 | $pattern = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i'; |
| 244 | $message = preg_replace_callback( |
| 245 | $pattern, |
| 246 | function ( $return ) { |
| 247 | if ( $return[1] !== $return[2] ) { |
| 248 | return "{$return[2]} ( {$return[1]} )"; |
| 249 | } |
| 250 | |
| 251 | return trailingslashit( $return[1] ); |
| 252 | }, |
| 253 | $message |
| 254 | ); |
| 255 | |
| 256 | return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this ); |
| 257 | } |
| 258 | |
| 259 | $message = $this->text_to_html( $message ); |
| 260 | |
| 261 | $template = $this->get_template(); |
| 262 | |
| 263 | ob_start(); |
| 264 | |
| 265 | give_get_template_part( 'emails/header', $template, true ); |
| 266 | |
| 267 | /** |
| 268 | * Fires in the email head. |
| 269 | * |
| 270 | * @since 1.0 |
| 271 | * |
| 272 | * @param Give_Emails $this The email object. |
| 273 | */ |
| 274 | do_action( 'give_email_header', $this ); |
| 275 | |
| 276 | if ( has_action( 'give_email_template_' . $template ) ) { |
| 277 | /** |
| 278 | * Fires in a specific email template. |
| 279 | * |
| 280 | * @since 1.0 |
| 281 | */ |
| 282 | do_action( "give_email_template_{$template}" ); |
| 283 | } else { |
| 284 | give_get_template_part( 'emails/body', $template, true ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Fires in the email body. |
| 289 | * |
| 290 | * @since 1.0 |
| 291 | * |
| 292 | * @param Give_Emails $this The email object. |
| 293 | */ |
| 294 | do_action( 'give_email_body', $this ); |
| 295 | |
| 296 | give_get_template_part( 'emails/footer', $template, true ); |
| 297 | |
| 298 | /** |
| 299 | * Fires in the email footer. |
| 300 | * |
| 301 | * @since 1.0 |
| 302 | * |
| 303 | * @param Give_Emails $this The email object. |
| 304 | */ |
| 305 | do_action( 'give_email_footer', $this ); |
| 306 | |
| 307 | $body = ob_get_clean(); |
| 308 | |
| 309 | // Email tag. |
| 310 | $message = str_replace( '{email}', $message, $body ); |
| 311 | |
| 312 | $header_img = Give_Email_Notification_Util::get_email_logo( $this->form_id ); |
| 313 | |
| 314 | if ( ! empty( $header_img ) ) { |
| 315 | $header_img = sprintf( |
| 316 | '<div id="template_header_image"><p style="margin-top:0;"><img style="max-width:450px;" src="%1$s" alt="%2$s" /></p></div>', |
| 317 | esc_url( $header_img ), |
| 318 | get_bloginfo( 'name' ) |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | $message = str_replace( '{email_logo}', $header_img, $message ); |
| 323 | |
| 324 | return apply_filters( 'give_email_message', $message, $this ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Send the email. |
| 329 | * |
| 330 | * @param string $to The To address to send to. |
| 331 | * @param string $subject The subject line of the email to send. |
| 332 | * @param string $message The body of the email to send. |
| 333 | * @param string|array $attachments Attachments to the email in a format supported by wp_mail(). |
| 334 | * |
| 335 | * @return bool |
| 336 | */ |
| 337 | public function send( $to, $subject, $message, $attachments = '' ) { |
| 338 | |
| 339 | if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) { |
| 340 | give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ) ); |
| 341 | |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Fires before sending an email. |
| 347 | * |
| 348 | * @since 1.0 |
| 349 | * |
| 350 | * @param Give_Emails $this The email object. |
| 351 | */ |
| 352 | do_action( 'give_email_send_before', $this ); |
| 353 | |
| 354 | $subject = $this->parse_tags( $subject ); |
| 355 | $message = $this->parse_tags( $message ); |
| 356 | |
| 357 | $message = $this->build_email( $message ); |
| 358 | |
| 359 | $attachments = apply_filters( 'give_email_attachments', $attachments, $this ); |
| 360 | |
| 361 | $sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments ); |
| 362 | |
| 363 | /** |
| 364 | * Fires after sending an email. |
| 365 | * |
| 366 | * @since 1.0 |
| 367 | * |
| 368 | * @param Give_Emails $this The email object. |
| 369 | */ |
| 370 | do_action( 'give_email_send_after', $this ); |
| 371 | |
| 372 | return $sent; |
| 373 | |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Add filters / actions before the email is sent. |
| 378 | * |
| 379 | * @since 1.0 |
| 380 | */ |
| 381 | public function send_before() { |
| 382 | add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 383 | add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 384 | add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Remove filters / actions after the email is sent. |
| 389 | * |
| 390 | * @since 1.0 |
| 391 | */ |
| 392 | public function send_after() { |
| 393 | remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 394 | remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 395 | remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 396 | |
| 397 | // Reset email related params. |
| 398 | $this->heading = ''; |
| 399 | $this->from_name = ''; |
| 400 | $this->from_address = ''; |
| 401 | $this->form_id = 0; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags. |
| 406 | * |
| 407 | * @since 1.0 |
| 408 | * |
| 409 | * @param string $message |
| 410 | * |
| 411 | * @return string |
| 412 | */ |
| 413 | public function text_to_html( $message ) { |
| 414 | /** |
| 415 | * Filter the flag which decide to process email message with wpautop or not. |
| 416 | * |
| 417 | * @since 2.3.0 |
| 418 | */ |
| 419 | $disable_wpautop = apply_filters( 'give_email_message_disable_wpautop', false ); |
| 420 | |
| 421 | if ( |
| 422 | ( 'text/html' == $this->content_type || true === $this->html ) |
| 423 | && ! $disable_wpautop |
| 424 | ) { |
| 425 | $message = wpautop( $message ); |
| 426 | } |
| 427 | |
| 428 | return $message; |
| 429 | } |
| 430 | |
| 431 | } |
| 432 |