mailer_functions.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. $booked_mailer_actions = apply_filters( 'booked_mailer_actions', array(
  3. 'booked_confirmation_email',
  4. 'booked_admin_confirmation_email',
  5. 'booked_reminder_email',
  6. 'booked_admin_reminder_email',
  7. 'booked_cancellation_email',
  8. 'booked_admin_cancellation_email',
  9. 'booked_approved_email',
  10. 'booked_registration_email'
  11. ));
  12. foreach( $booked_mailer_actions as $action ):
  13. add_action( $action, 'booked_mailer', 10, 5 );
  14. endforeach;
  15. function booked_mailer_tokens(){
  16. return apply_filters( 'booked_mailer_tokens', array(
  17. 'name' => esc_html__( "Display the full name of the customer.","booked" ),
  18. 'email' => esc_html__( "Display the customer's email address.","booked" ),
  19. 'title' => esc_html__( "Display the title of the appointment's time slot.","booked" ),
  20. 'calendar' => esc_html__( "Display the appointment's calendar name (if applicable).","booked" ),
  21. 'date' => esc_html__( "Display the appointment date.","booked" ),
  22. 'time' => esc_html__( "Display the appointment time.","booked" ),
  23. 'customfields' => esc_html__( "Display the appointment's custom field data.","booked" ),
  24. 'id' => esc_html__( "Display the appointment's unique identification number.","booked" ),
  25. ));
  26. }
  27. function booked_user_tokens(){
  28. return apply_filters( 'booked_user_tokens', array(
  29. 'name' => esc_html__( "Display the customer's name.","booked" ),
  30. 'username' => esc_html__( "Display the customer's username.","booked" ),
  31. 'password' => esc_html__( "Display the customer's password.","booked" ),
  32. 'email' => esc_html__( "Display the customer's email address.","booked" )
  33. ));
  34. }
  35. function booked_token_replacement( $content, $replacements, $type = 'appointment' ){
  36. if ( $type == 'appointment' ):
  37. $booked_tokens = booked_mailer_tokens();
  38. elseif ( $type == 'user' ):
  39. $booked_tokens = booked_user_tokens();
  40. else:
  41. return $content;
  42. endif;
  43. $needles = array(); $rep_with = array();
  44. foreach( $booked_tokens as $token => $desc ):
  45. if ( isset($replacements[$token]) ):
  46. $needles[] = '%' . $token . '%';
  47. $rep_with[] = $replacements[$token];
  48. endif;
  49. endforeach;
  50. $content = htmlentities( str_replace( $needles, $rep_with, $content ), ENT_QUOTES | ENT_IGNORE, "UTF-8" );
  51. $content = html_entity_decode( $content, ENT_QUOTES | ENT_IGNORE, "UTF-8" );
  52. return $content;
  53. }
  54. function booked_get_appointment_tokens( $appt_id ){
  55. // Name & Email
  56. // $customer_name
  57. // $email
  58. if ( $first_name = get_post_meta( $appt_id, '_appointment_guest_name', true ) ):
  59. $last_name = get_post_meta( $appt_id, '_appointment_guest_surname', true );
  60. $customer_name = ( $last_name ? $first_name . ' ' . $last_name : $first_name );
  61. $customer_email = get_post_meta( $appt_id, '_appointment_guest_email', true );
  62. else:
  63. $_appt = get_post( $appt_id );
  64. $appt_author = $_appt->post_author;
  65. $_user = get_userdata( $appt_author );
  66. $customer_name = booked_get_name( $appt_author );
  67. $customer_email = $_user->user_email;
  68. endif;
  69. // Calendar Name
  70. // $calendar_name
  71. $calendars = get_the_terms( $appt_id, 'booked_custom_calendars' );
  72. if ( !empty($calendars) ):
  73. foreach( $calendars as $calendar ):
  74. $calendar_id = $calendar->term_id;
  75. $calendar_term = get_term_by( 'id', $calendar_id, 'booked_custom_calendars' );
  76. $calendar_name = $calendar_term->name;
  77. break;
  78. endforeach;
  79. else:
  80. $calendar_name = '';
  81. endif;
  82. // Date
  83. // $date_text
  84. $date_format = get_option( 'date_format' );
  85. $timestamp = get_post_meta( $appt_id, '_appointment_timestamp', true);
  86. $date_text = date_i18n( $date_format,$timestamp );
  87. // Time
  88. // $time_text
  89. $timeslot = get_post_meta( $appt_id, '_appointment_timeslot', true );
  90. $timeslots = explode( '-', $timeslot );
  91. $time_format = get_option( 'time_format' );
  92. $hide_end_times = get_option( 'booked_hide_end_times', false );
  93. $timestamp_start = strtotime( date_i18n( 'Y-m-d', $timestamp) . ' ' . $timeslots[0] );
  94. $timestamp_end = strtotime( date_i18n( 'Y-m-d', $timestamp) . ' ' . $timeslots[1] );
  95. if ($timeslots[0] == '0000' && $timeslots[1] == '2400'):
  96. $time_text = esc_html__( 'All day', 'booked' );
  97. else :
  98. $time_text = date_i18n( $time_format, $timestamp_start ) . ( !$hide_end_times ? '&ndash;' . date_i18n( $time_format, $timestamp_end ) : '' );
  99. endif;
  100. $time_text = apply_filters( 'booked_emailed_timeslot_text', $time_text, $timestamp_start, $timeslot, $calendar_id );
  101. // Custom Fields
  102. // $custom_fields
  103. $custom_fields = get_post_meta( $appt_id, '_cf_meta_value', true);
  104. // Title
  105. // $title
  106. $title = get_post_meta( $appt_id, '_appointment_title', true );
  107. return apply_filters( 'booked_appointment_tokens', array(
  108. 'name' => $customer_name,
  109. 'date' => $date_text,
  110. 'time' => $time_text,
  111. 'customfields' => $custom_fields,
  112. 'calendar' => $calendar_name,
  113. 'email' => $customer_email,
  114. 'title' => $title,
  115. 'id' => $appt_id
  116. ));
  117. }
  118. function booked_mailer( $to = false, $subject, $message, $from_email = false, $from_name = false ){
  119. if ( !$to )
  120. return false;
  121. add_filter( 'wp_mail_content_type', 'booked_set_html_content_type' );
  122. $booked_email_logo = get_option('booked_email_logo');
  123. if ($booked_email_logo):
  124. $logo = apply_filters( 'booked_email_logo_html', '<img src="'.$booked_email_logo.'" style="max-width:100%; height:auto; display:block; margin:10px 0 20px;">' );
  125. else :
  126. $logo = apply_filters( 'booked_email_logo_html', '' );
  127. endif;
  128. $link_color = get_option('booked_button_color','#56C477');
  129. $force_sender = get_option('booked_email_force_sender',false);
  130. $disable_booked_mailer = get_option('booked_emailer_disabled',false);
  131. if ( $disable_booked_mailer ):
  132. $from_email = false;
  133. $from_name = false;
  134. elseif ( $force_sender ):
  135. $admin_email = get_option( 'admin_email' );
  136. $from_email = get_option( 'booked_email_force_sender_from', $admin_email );
  137. $from_name = false;
  138. endif;
  139. if ( file_exists( get_stylesheet_directory() . '/booked/email-template.html' ) ):
  140. $template = file_get_contents( get_stylesheet_directory() . '/booked/email-template.html', true );
  141. elseif ( file_exists( get_template_directory() . '/booked/email-template.html' ) ):
  142. $template = file_get_contents( get_template_directory() . '/booked/email-template.html', true );
  143. else:
  144. $template = file_get_contents( untrailingslashit( BOOKED_PLUGIN_DIR ) . '/includes/email-templates/default.html', true );
  145. endif;
  146. $filter = array('%content%','%logo%','%link_color%');
  147. $replace = array(wpautop($message),$logo,$link_color);
  148. if ( $from_email ):
  149. $headers[] = 'From: ' . ( $from_name ? $from_name . ' <' . $from_email . '>' : $from_email );
  150. endif;
  151. $headers[] = 'Content-Type: text/html; charset=UTF-8';
  152. $message = str_replace($filter, $replace, $template);
  153. wp_mail( $to,$subject,$message,$headers );
  154. remove_filter( 'wp_mail_content_type', 'booked_set_html_content_type' );
  155. }
  156. function booked_set_html_content_type() {
  157. return 'text/html';
  158. }