/core/outbound-handling.php

https://bitbucket.org/cyberhobo/easy-subscribe · PHP · 252 lines · 151 code · 51 blank · 50 comment · 31 complexity · 80be9ce0f87bf9a656f8d8cfa497fc60 MD5 · raw file

  1. <?php
  2. /**
  3. * Take care of sending emails.
  4. */
  5. class ES_Outbound_Handling {
  6. /**
  7. * Send post subscribers a new comment notification.
  8. *
  9. * @param object $comment
  10. * @param array $subscriber_ids
  11. * @param string $template
  12. */
  13. public static function send_comment_notifications( $comment, $subscriber_ids, $template ) {
  14. if ( !apply_filters( 'es_send_comment_notifications', true, $comment, $subscriber_ids ) )
  15. return;
  16. // Turn off native comment notifications
  17. add_filter( 'comment_notification_recipients', create_function( '$a', 'return array();' ) );
  18. $comment_author = get_userdata( $comment->user_id );
  19. $comment_post = get_post( $comment->comment_post_ID );
  20. if ( ES_Post_Types::TOPIC == $comment_post->post_type ) {
  21. $group = get_post( $comment_post->post_parent );
  22. $from_name = $group->post_title;
  23. } else {
  24. $group = null;
  25. $from_name = $comment_author->display_name;
  26. }
  27. foreach ( $subscriber_ids as $subscriber_id ) {
  28. if ( $subscriber_id == $comment->user_id )
  29. continue;
  30. $subscriber = get_userdata( $subscriber_id );
  31. if ( !$subscriber )
  32. continue;
  33. $template_data = array(
  34. 'comment_author' => $comment_author,
  35. 'subscriber' => $subscriber,
  36. 'comment' => $comment,
  37. 'comment_post' => $comment_post,
  38. 'group' => $group,
  39. );
  40. $encoded_subject = '[' . get_option( 'blogname' ) . '] New comment on "' . $comment_post->post_title . '"';
  41. $mail = new ES_Email( array(
  42. 'to' => $subscriber->user_email,
  43. 'from_name' => $from_name,
  44. 'reply_email' => EasySubscribe::get_reply_address( $comment_post, $subscriber ),
  45. 'subject' => html_entity_decode( $encoded_subject, ENT_QUOTES, 'UTF-8' ),
  46. 'message' => EasySubscribe::render_template( $template, $template_data, false ),
  47. ) );
  48. do_action( 'es_comment_email', $mail, $template_data );
  49. $mail->send();
  50. }
  51. EasySubscribe::ensure_subscribed( 'post', $comment_author->ID, $comment_post );
  52. }
  53. /**
  54. * Send an email to a user who has had an account created for them.
  55. *
  56. * @param object $user
  57. * @param string $password
  58. * @param string $template
  59. */
  60. public static function send_new_user_notification( $user, $password, $template ) {
  61. $blogname = get_option( 'blogname' );
  62. $template_data = compact( 'blogname', 'user', 'password' );
  63. $encoded_subject = sprintf( __( '[%s] Your username and password', 'EasySubscribe' ), $blogname );
  64. $mail = new ES_Email( array(
  65. 'to' => $user->user_email,
  66. 'subject' => html_entity_decode( $encoded_subject, ENT_QUOTES, 'UTF-8' ),
  67. 'message' => EasySubscribe::render_template( $template, $template_data, false ),
  68. ) );
  69. do_action( 'es_new_user_email', $mail, $template_data );
  70. $mail->send();
  71. }
  72. /**
  73. * Send email notifications for a post.
  74. * @param object $post
  75. * @param array $user_ids
  76. * @param string $template The template file to use for the email.
  77. */
  78. public static function send_post_notifications( $post, $user_ids, $template ) {
  79. if ( !apply_filters( 'es_send_post_notifications', true, $post, $user_ids ) )
  80. return;
  81. $author = get_userdata( $post->post_author );
  82. if ( ES_Post_Types::TOPIC == $post->post_type ) {
  83. $group = get_post( $post->post_parent );
  84. $from_name = $group->post_title;
  85. } else {
  86. $group = null;
  87. $from_name = $author->display_name;
  88. }
  89. foreach ( $user_ids as $user_id ) {
  90. $user = get_userdata( $user_id );
  91. if ( !is_email( $user->user_email ) )
  92. continue;
  93. $template_data = array(
  94. 'author' => $author,
  95. 'recipient' => $user,
  96. 'post' => $post,
  97. 'group' => $group,
  98. );
  99. $encoded_subject = '[' . get_option( 'blogname' ) . '] ' . $post->post_title;
  100. $mail = new ES_Email( array(
  101. 'to' => $user->user_email,
  102. 'subject' => html_entity_decode( $encoded_subject, ENT_QUOTES, 'UTF-8' ),
  103. 'from_name' => $from_name,
  104. 'reply_email' => EasySubscribe::get_reply_address( $post, $user ),
  105. 'message' => EasySubscribe::render_template( $template, $template_data, false ),
  106. ) );
  107. do_action( 'es_post_email', $mail, $template_data );
  108. $mail->send();
  109. }
  110. }
  111. /**
  112. * Send a subscription confirmation email.
  113. *
  114. * @param string $type post or user
  115. * @param int|string|WP_User $subscriber
  116. * @param int|string|WP_Post|WP_User $object
  117. * @param boolean $un True if unsubscribing, default false.
  118. * @return boolean Whether a notification was sent
  119. */
  120. public static function send_subscription_notification( $type, $subscriber, $object, $un = false ) {
  121. if ( !apply_filters( 'es_send_subscription_notifications', true ) )
  122. return false;
  123. $subscriber = EasySubscribe::resolve_object( 'user', $subscriber );
  124. $object = EasySubscribe::resolve_object( $type, $object );
  125. $group = ( ES_Post_Types::GROUP == $object->post_type ) ? new ES_Group( $object ) : null;
  126. $blogname = get_option( 'blogname' );
  127. $subscribed_to = ( 'post' == $type ) ? $object->post_title : $object->display_name;
  128. $submission_address = $group ? $group->submission_address( $subscriber ) : null;
  129. $mail = new ES_Email( array( 'to' => $subscriber->user_email ) );
  130. if ( $un ) {
  131. $mail->subject = sprintf( __( '[%s] You\'re unsubscribed from %s', 'EasySubscribe' ), $blogname, $subscribed_to );
  132. $template = 'unsubscribed-email.php';
  133. $action = 'es_unsubscribed_email';
  134. } else {
  135. $mail->subject = sprintf( __( '[%s] You\'re subscribed to %s', 'EasySubscribe' ), $blogname, $subscribed_to );
  136. $template = 'subscribed-email.php';
  137. $action = 'es_subscribed_email';
  138. }
  139. $template = EasySubscribe::locate_template( $template );
  140. $template_data = compact( 'blogname', 'type', 'subscriber', 'object', 'subscribed_to', 'submission_address' );
  141. $mail->message = EasySubscribe::render_template( $template, $template_data, false );
  142. if ( $submission_address ) {
  143. $vcard = sprintf( "BEGIN:VCARD\r\nN:%s\r\nEMAIL:%s\r\nTITLE:%s\r\nEND:VCARD\r\n",
  144. $object->post_title,
  145. $submission_address,
  146. $blogname
  147. );
  148. $mail->add_string_attachment( $vcard, $object->post_name . '.vcf', '8bit', 'text/x-vcard' );
  149. $mail->reply_email = $submission_address;
  150. }
  151. do_action( $action, $mail, $template_data );
  152. return $mail->send();
  153. }
  154. /**
  155. * Any time a post is published, notify author subscribers.
  156. *
  157. * @param string $new_status
  158. * @param string $old_status
  159. * @param WP_Post $post
  160. */
  161. public static function action_transition_post_status( $new_status, $old_status, $post ) {
  162. if ( empty( $post->post_author ) )
  163. return;
  164. if ( 'publish' == $old_status or 'publish' != $new_status )
  165. return;
  166. $template = EasySubscribe::locate_template( 'new-post-email.php' );
  167. $subscriber_ids = null;
  168. if ( EasySubscribe::$options->get( 'enable_groups' ) ) {
  169. if ( ES_Post_Types::GROUP == $post->post_type ) {
  170. // Always subscribe group authors to the group
  171. EasySubscribe::ensure_subscribed( 'post', $post->post_author, $post );
  172. return;
  173. }
  174. // Subscribers are group members, where the group is the parent post
  175. if ( ES_Post_Types::TOPIC == $post->post_type )
  176. $subscriber_ids = EasySubscribe::get_subscriber_ids( 'post', $post->post_parent );
  177. }
  178. if ( EasySubscribe::$options->get( 'auto_subscribe_authors' ) )
  179. EasySubscribe::ensure_subscribed( 'post', $post->post_author, $post );
  180. if ( !$subscriber_ids ) {
  181. // Subscribers are subscribed to the post author
  182. $subscriber_ids = EasySubscribe::get_subscriber_ids( 'user', $post->post_author );
  183. // Or to post terms
  184. $post_term_subscriber_ids = EasySubscribe::get_post_term_subscriber_ids( $post );
  185. $subscriber_ids = array_unique( array_merge( $subscriber_ids, $post_term_subscriber_ids ) );
  186. }
  187. self::send_post_notifications( $post, $subscriber_ids, $template );
  188. }
  189. /**
  190. * When a comment is published notify subscribers if needed.
  191. *
  192. * @param int $id
  193. * @param object $comment
  194. */
  195. public static function action_wp_insert_comment( $id, $comment ) {
  196. if ( $comment->comment_approved != '1' )
  197. return;
  198. $template = EasySubscribe::locate_template( 'new-comment-email.php' );
  199. self::send_comment_notifications( $comment, EasySubscribe::get_subscriber_ids( 'post', $comment->comment_post_ID ), $template );
  200. }
  201. }