PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/notification.php

https://github.com/wpkorea/dw-question-answer
PHP | 393 lines | 313 code | 55 blank | 25 comment | 85 complexity | 807804c440dc2d0d06198429f70f896a MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. function dwqa_get_admin_email(){
  3. $admin_email = get_option( 'dwqa_subscrible_sendto_address' );
  4. if( ! $admin_email ) {
  5. $admin_email = get_bloginfo( 'admin_email' );
  6. }
  7. return explode(',', $admin_email);
  8. }
  9. function dwqa_new_question_notify( $question_id, $user_id ){
  10. // receivers
  11. $admin_email = dwqa_get_admin_email();
  12. $enabled = get_option( 'dwqa_subscrible_enable_new_question_notification', 1);
  13. if( !$enabled ) {
  14. return false;
  15. }
  16. $question = get_post( $question_id );
  17. if( ! $question ) {
  18. return false;
  19. }
  20. $subject = get_option( 'dwqa_subscrible_new_question_email_subject' );
  21. if( ! $subject ) {
  22. $subject = __('A new question was posted on {site_name}', 'dwqa');
  23. }
  24. $subject = str_replace('{site_name}', get_bloginfo( 'name' ), $subject);
  25. $subject = str_replace( '{question_title}', $question->post_title, $subject);
  26. $subject = str_replace( '{question_id}', $question->ID, $subject);
  27. $subject = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $subject);
  28. // To send HTML mail, the Content-type header must be set
  29. $headers = 'MIME-Version: 1.0' . "\r\n";
  30. $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  31. //From email
  32. $from_email = get_option( 'dwqa_subscrible_from_address' );
  33. if( $from_email ) {
  34. $headers .= "From: " . $from_email . "\r\n";
  35. }
  36. //Cc email
  37. $cc_address = get_option( 'dwqa_subscrible_cc_address' );
  38. if( $cc_address ) {
  39. $headers .= "Cc: " . $cc_address . "\r\n";
  40. }
  41. //Bcc email
  42. $bcc_address = get_option( 'dwqa_subscrible_bcc_address' );
  43. if( $bcc_address ) {
  44. $headers .= "Bcc: " . $bcc_address . "\r\n";
  45. }
  46. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_question_email', 'new-question' );
  47. if( ! $message ) {
  48. return false;
  49. }
  50. // Replacement
  51. $admin = get_user_by( 'email', $admin_email[0] );
  52. if( $admin ) {
  53. $message = str_replace( '{admin}', get_the_author_meta( 'display_name', $admin->ID ), $message);
  54. }
  55. //sender
  56. $message = str_replace( '{user_avatar}', get_avatar( $user_id, '60'), $message);
  57. $message = str_replace( '{user_link}', get_author_posts_url( $user_id ), $message);
  58. $message = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $message);
  59. //question
  60. $message = str_replace( '{question_link}', get_permalink( $question_id ), $message);
  61. $message = str_replace( '{question_title}', $question->post_title, $message);
  62. $message = str_replace( '{question_content}', $question->post_content, $message);
  63. // Site info
  64. $logo = get_option( 'dwqa_subscrible_email_logo','' );
  65. $logo = $logo ? '<img src="'.$logo.'" alt="'.get_bloginfo( 'name' ).'" style="max-width: 100%; height: auto;" />' : '';
  66. $message = str_replace( '{site_logo}', $logo, $message );
  67. $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message );
  68. $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
  69. $message = str_replace( '{site_url}', site_url(), $message );
  70. // start send out email
  71. wp_mail( $admin_email, $subject, $message, $headers );
  72. }
  73. add_action( 'dwqa_add_question', 'dwqa_new_question_notify', 10, 2 );
  74. function dwqa_new_answer_nofity( $answer_id ){
  75. $enabled = get_option( 'dwqa_subscrible_enable_new_answer_notification', 1);
  76. if( !$enabled ) {
  77. return false;
  78. }
  79. //Admin email
  80. $admin_email = get_bloginfo( 'admin_email' );;
  81. $enable_send_copy = get_option( 'dwqa_subscrible_send_copy_to_admin' );
  82. $question_id = get_post_meta( $answer_id, '_question', true );
  83. $question = get_post( $question_id );
  84. $answer = get_post( $answer_id );
  85. if( $answer->post_status != 'publish' && $answer->post_status != 'private' ) {
  86. return false;
  87. }
  88. //Send email alert for author of question about this answer
  89. if( dwqa_is_anonymous($question_id) ) {
  90. $email = get_post_meta( $question_id, '_dwqa_anonymous_email', true );
  91. if( ! is_email( $email ) ) {
  92. return false;
  93. }
  94. } else {
  95. // if user is not the author of question/answer, add user to followers list
  96. if( $question->post_author != $answer->post_author ) {
  97. if( ! dwqa_is_followed( $question_id, $answer->post_author ) ) {
  98. add_post_meta( $question_id, '_dwqa_followers', $answer->post_author );
  99. }
  100. }
  101. $email = get_the_author_meta( 'user_email', $question->post_author );
  102. }
  103. $subject = get_option( 'dwqa_subscrible_new_answer_email_subject' );
  104. if( ! $subject ) {
  105. $subject = __('A new answer for "{question_title}" was posted on {site_name}', 'dwqa');
  106. }
  107. $subject = str_replace('{site_name}', get_bloginfo( 'name' ), $subject);
  108. $subject = str_replace( '{question_title}', $question->post_title, $subject);
  109. $subject = str_replace( '{question_id}', $question->ID, $subject);
  110. // To send HTML mail, the Content-type header must be set
  111. $headers = 'MIME-Version: 1.0' . "\r\n";
  112. $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  113. //From email
  114. $from_email = get_option( 'dwqa_subscrible_from_address' );
  115. if( $from_email ) {
  116. $headers .= "From: " . $from_email . "\r\n";
  117. }
  118. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_answer_email', 'new-answer' );
  119. if( ! $message ) {
  120. return false;
  121. }
  122. //Receiver
  123. $message = str_replace( '{question_author}', get_the_author_meta( 'display_name', $question->post_author ), $message );
  124. //Answer
  125. $answer = get_post( $answer_id );
  126. if( dwqa_is_anonymous($answer_id) ) {
  127. $user_id = 0;
  128. $display_name = __('Anonymous','dwqa');
  129. $avatar = get_avatar( $user_id, '60');
  130. $answer_author = __('Anonymous','dwqa');
  131. } else {
  132. $user_id = $answer->post_author;
  133. $display_name = get_the_author_meta( 'display_name', $user_id );
  134. $avatar = get_avatar( $user_id, '60');
  135. $answer_author = '<a href="'.get_author_posts_url( $user_id ).'" >'.get_the_author_meta( 'display_name', $user_id ).'</a>';
  136. }
  137. $subject = str_replace( '{username}', $display_name, $subject);
  138. $subject = str_replace( '{answer_author}', $answer_author, $subject);
  139. $message = str_replace( '{answer_avatar}', $avatar, $message);
  140. $message = str_replace( '{answer_author}', $answer_author, $message);
  141. $message = str_replace( '{question_link}', get_permalink( $question->ID ), $message);
  142. $message = str_replace( '{answer_link}', get_permalink( $question->ID ) . '#answer-' . $answer_id, $message);
  143. $message = str_replace( '{question_title}', $question->post_title, $message);
  144. $message = str_replace( '{answer_content}', $answer->post_content, $message);
  145. // logo replace
  146. $logo = get_option( 'dwqa_subscrible_email_logo','' );
  147. $logo = $logo ? '<img src="'.$logo.'" alt="'.get_bloginfo( 'name' ).'" style="max-width: 100%; height: auto;" />' : '';
  148. $message = str_replace( '{site_logo}', $logo, $message);
  149. $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message);
  150. $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
  151. $message = str_replace( '{site_url}', site_url(), $message);
  152. $enable_notify = get_option( 'dwqa_subscrible_enable_new_answer_followers_notification', true );
  153. if( $enable_notify ) {
  154. $followers = get_post_meta( $question_id, '_dwqa_followers' );
  155. $answer_email = get_the_author_meta( 'user_email', $answer->post_author );
  156. if( ! empty($followers) ) {
  157. $question_link = get_permalink( $question->ID );
  158. $follow_subject = get_option( 'dwqa_subscrible_new_answer_followers_email_subject' );
  159. if( ! $follow_subject ) {
  160. $follow_subject = __('You have a new answer for your followed question','dwqa');
  161. }
  162. $follow_subject = str_replace('{site_name}', get_bloginfo( 'name' ), $follow_subject);
  163. $follow_subject = str_replace( '{question_title}', $question->post_title, $follow_subject);
  164. $follow_subject = str_replace( '{question_id}', $question->ID, $follow_subject);
  165. $follow_subject = str_replace( '{username}', $display_name, $follow_subject);
  166. $follow_subject = str_replace( '{answer_author}', $answer_author, $follow_subject);
  167. $message_to_follower = dwqa_get_mail_template( 'dwqa_subscrible_new_answer_followers_email', 'new-answer-followers' );
  168. $message_to_follower = str_replace( '{answer_avatar}', $avatar, $message_to_follower);
  169. $message_to_follower = str_replace( '{answer_author}', $answer_author, $message_to_follower);
  170. $message_to_follower = str_replace( '{question_link}', get_permalink( $question->ID ), $message_to_follower);
  171. $message_to_follower = str_replace( '{question_title}', $question->post_title, $message_to_follower);
  172. $message_to_follower = str_replace( '{answer_content}', $answer->post_content, $message_to_follower);
  173. $message_to_follower = str_replace( '{site_logo}', $logo, $message_to_follower);
  174. $message_to_follower = str_replace( '{site_name}', get_bloginfo( 'name' ), $message_to_follower);
  175. $message_to_follower = str_replace( '{site_description}', get_bloginfo( 'description' ), $message_to_follower );
  176. $message_to_follower = str_replace( '{site_url}', site_url(), $message_to_follower);
  177. foreach ( $followers as $follower ) {
  178. $follower = (int) $follower;
  179. $user_data = get_userdata( $follower );
  180. if ($user_data) {
  181. $follow_email = $user_data->user_email;
  182. $follower_name = $user_data->display_name;
  183. if( $follow_email && $follow_email != $email && $follow_email != $answer_email ) {
  184. //Send email to follower
  185. $message_to_each_follower = str_replace( '{follower}', $follower_name, $message_to_follower );
  186. wp_mail( $follow_email, $follow_subject, $message_to_each_follower, $headers );
  187. if( $enable_send_copy && $follow_email != $admin_email ) {
  188. wp_mail( $admin_email, $follow_subject, $message_to_each_follower, $headers );
  189. }
  190. }
  191. }
  192. }
  193. }
  194. } // Send email to followers
  195. if( $question->post_author != $answer->post_author ) {
  196. wp_mail( $email, $subject, $message, $headers );
  197. if( $enable_send_copy && $email != $admin_email ) {
  198. wp_mail( $admin_email, $subject, $message, $headers );
  199. }
  200. }
  201. }
  202. add_action( 'dwqa_add_answer', 'dwqa_new_answer_nofity' );
  203. add_action( 'dwqa_update_answer', 'dwqa_new_answer_nofity' );
  204. function dwqa_new_comment_notify( $comment_id, $comment ){
  205. $parent = get_post_type( $comment->comment_post_ID );
  206. //Admin email
  207. $admin_email = get_bloginfo( 'admin_email' );
  208. $enable_send_copy = get_option( 'dwqa_subscrible_send_copy_to_admin' );
  209. if ( 1 == $comment->comment_approved && ( 'dwqa-question' == $parent || 'dwqa-answer' == $parent ) ) {
  210. if( $parent == 'dwqa-question' ) {
  211. $enabled = get_option( 'dwqa_subscrible_enable_new_comment_question_notification', 1);
  212. } elseif ( $parent == 'dwqa-answer' ) {
  213. $enabled = get_option( 'dwqa_subscrible_enable_new_comment_answer_notification', 1);
  214. }
  215. if( !$enabled ) {
  216. return false;
  217. }
  218. $post_parent = get_post( $comment->comment_post_ID );
  219. if( dwqa_is_anonymous( $comment->comment_post_ID ) ) {
  220. $post_parent_email = get_post_meta( $comment->comment_post_ID, '_dwqa_anonymous_email', true );
  221. if( ! is_email( $post_parent_email ) ) {
  222. return false;
  223. }
  224. } else {
  225. // if user is not the author of question/answer, add user to followers list
  226. if( $post_parent->post_author != $comment->user_id ) {
  227. if( ! dwqa_is_followed( $post_parent->ID, $comment->user_id ) ) {
  228. add_post_meta( $post_parent->ID, '_dwqa_followers', $comment->user_id );
  229. }
  230. }
  231. $post_parent_email = get_the_author_meta( 'user_email', $post_parent->post_author );
  232. }
  233. // To send HTML mail, the Content-type header must be set
  234. $headers = 'MIME-Version: 1.0' . "\r\n";
  235. $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
  236. //From email
  237. $from_email = get_option( 'dwqa_subscrible_from_address' );
  238. if( $from_email ) {
  239. $headers .= "From: " . $from_email . "\r\n";
  240. }
  241. if( $parent == 'dwqa-question' ) {
  242. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_question_email', 'new-comment-question' );
  243. $subject = get_option( 'dwqa_subscrible_new_comment_question_email_subject',__('[{site_name}] You have a new comment for question {question_title}', 'dwqa') );
  244. $message = str_replace( '{question_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message);
  245. $question = $post_parent;
  246. } else {
  247. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_answer_email', 'new-comment-answer' );
  248. $subject = get_option( 'dwqa_subscrible_new_comment_answer_email_subject',__('[{site_name}] You have a new comment for answer', 'dwqa') );
  249. $message = str_replace( '{answer_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message);
  250. $question_id = get_post_meta( $post_parent->ID, '_question', true );
  251. $question = get_post( $question_id );
  252. }
  253. $subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $subject);
  254. $subject = str_replace( '{question_title}', $question->post_title, $subject);
  255. $subject = str_replace( '{question_id}', $question->ID, $subject);
  256. $subject = str_replace( '{username}',get_the_author_meta( 'display_name', $comment->user_id ), $subject);
  257. if( ! $message ) {
  258. return false;
  259. }
  260. // logo replace
  261. $logo = get_option( 'dwqa_subscrible_email_logo','' );
  262. $logo = $logo ? '<img src="'.$logo.'" alt="'.get_bloginfo( 'name' ).'" style="max-width: 100%; height: auto;" />' : '';
  263. $subject = str_replace('{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $subject );
  264. $message = str_replace( '{site_logo}', $logo, $message);
  265. $message = str_replace( '{question_link}', get_permalink( $question->ID ), $message);
  266. $message = str_replace( '{comment_link}', get_permalink( $question->ID ) . '#comment-' . $comment_id, $message);
  267. $message = str_replace( '{question_title}', $question->post_title, $message);
  268. $message = str_replace( '{comment_author_avatar}', get_avatar( $comment->user_id, '60'), $message);
  269. $message = str_replace( '{comment_author_link}', get_author_posts_url( $comment->user_id ), $message);
  270. $message = str_replace('{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $message );
  271. $message = str_replace( '{comment_content}', $comment->comment_content, $message);
  272. $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message);
  273. $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
  274. $message = str_replace( '{site_url}', site_url(), $message);
  275. if( $parent == 'dwqa-question' ) {
  276. $enable_notify = get_option( 'dwqa_subscrible_enable_new_comment_question_followers_notify', true );
  277. } else {
  278. $enable_notify = get_option( 'dwqa_subscrible_enable_new_comment_answer_followers_notification', true );
  279. }
  280. if( $enable_notify ) {
  281. //Follower email task
  282. $followers = get_post_meta( $post_parent->ID, '_dwqa_followers' );
  283. $comment_email = get_the_author_meta( 'user_email', $comment->user_id );
  284. if( $parent == 'dwqa-question' ) {
  285. $message_to_follower = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_question_followers_email', 'new-comment-question' );
  286. $follow_subject = get_option( 'dwqa_subscrible_new_comment_question_followers_email_subject',__('[{site_name}] You have a new comment for question {question_title}', 'dwqa') );
  287. $message_to_follower = str_replace( '{question_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message_to_follower);
  288. $question = $post_parent;
  289. } else {
  290. $message_to_follower = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_answer_followers_email', 'new-comment-answer' );
  291. $follow_subject = get_option( 'dwqa_subscrible_new_comment_answer_followers_email_subject',__('[{site_name}] You have a new comment for answer', 'dwqa') );
  292. $message_to_follower = str_replace( '{answer_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message_to_follower);
  293. }
  294. $follow_subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $follow_subject);
  295. $follow_subject = str_replace( '{question_title}', $question->post_title, $follow_subject);
  296. $follow_subject = str_replace( '{question_id}', $question->ID, $follow_subject);
  297. $follow_subject = str_replace( '{username}',get_the_author_meta( 'display_name', $comment->user_id ), $follow_subject);
  298. $follow_subject = str_replace('{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $follow_subject );
  299. $message_to_follower = str_replace( '{site_logo}', $logo, $message_to_follower);
  300. $message_to_follower = str_replace( '{question_link}', get_permalink( $question->ID ), $message_to_follower);
  301. $comment_link = get_permalink( $question->ID ) . '#comment-' . $comment_id;
  302. $message_to_follower = str_replace( '{comment_link}', $comment_link, $message_to_follower);
  303. $message_to_follower = str_replace( '{question_title}', $question->post_title, $message_to_follower);
  304. $message_to_follower = str_replace( '{comment_author_avatar}', get_avatar( $comment->user_id, '60'), $message_to_follower);
  305. $message_to_follower = str_replace( '{comment_author_link}', get_author_posts_url( $comment->user_id ), $message_to_follower);
  306. $message_to_follower = str_replace('{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $message_to_follower );
  307. $message_to_follower = str_replace( '{comment_content}', $comment->comment_content, $message_to_follower);
  308. $message_to_follower = str_replace( '{site_name}', get_bloginfo( 'name' ), $message_to_follower);
  309. $message_to_follower = str_replace( '{site_description}', get_bloginfo( 'description' ), $message_to_follower );
  310. $message_to_follower = str_replace( '{site_url}', site_url(), $message_to_follower);
  311. if( ! empty($followers) ) {
  312. foreach ( $followers as $follower ) {
  313. $follower = (int) $follower;
  314. $user_data = get_userdata( $follower );
  315. if ($user_data) {
  316. $follow_email = $user_data->user_email;
  317. $follower_name = $user_data->display_name;
  318. if( $follow_email && $follow_email != $post_parent_email && $follow_email != $comment_email ) {
  319. $message_to_each_follower = str_replace( '{follower}', $follower_name, $message_to_follower );
  320. wp_mail( $follow_email, $follow_subject, $message_to_each_follower, $headers );
  321. if( $enable_send_copy && $follow_email != $admin_email ) {
  322. wp_mail( $admin_email, $follow_subject, $message_to_each_follower, $headers );
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. if( $post_parent->post_author != $comment->user_id ) {
  330. wp_mail( $post_parent_email, $subject, $message, $headers );
  331. if( $enable_send_copy && $admin_email != $post_parent_email ) {
  332. wp_mail( $admin_email, $subject, $message, $headers );
  333. }
  334. }
  335. }
  336. }
  337. add_action( 'wp_insert_comment', 'dwqa_new_comment_notify',10,2 );
  338. ?>