/wp-content/plugins/dw-question-answer/inc/Notifications.php

https://github.com/livinglab/openlab · PHP · 536 lines · 428 code · 77 blank · 31 comment · 96 complexity · 93e16bff02ac1067539f979990757691 MD5 · raw file

  1. <?php
  2. class DWQA_Notifications {
  3. private $time_delay = 120;
  4. // private $time_delay = 0;
  5. public function __construct() {
  6. if(get_option('dwqa_enable_email_delay')){
  7. add_action('dwqa_new_question_notify', array( $this, 'new_question_notify' ), 10, 2);
  8. add_action('dwqa_new_answer_notify', array( $this, 'new_answer_notify' ), 10, 2);
  9. add_action('dwqa_new_comment_notify', array( $this, 'new_comment_notify' ), 10, 2);
  10. add_action( 'dwqa_add_question', array( $this, 'dwqa_queue_add_question' ), 10, 2 );
  11. add_action( 'dwqa_add_answer', array( $this, 'dwqa_queue_add_answer' ), 10, 2 );
  12. add_action( 'wp_insert_comment', array( $this, 'dwqa_queue_insert_comment' ), 10, 2 );
  13. }else{
  14. add_action( 'dwqa_add_question', array( $this, 'new_question_notify' ), 10, 2 );
  15. add_action( 'wp_insert_comment', array( $this, 'new_comment_notify' ), 10, 2 );
  16. add_action( 'dwqa_add_answer', array( $this, 'new_answer_notify' ), 10, 2 );
  17. }
  18. // add_action( 'dwqa_add_question', array( $this, 'new_activity' ) );
  19. // add_action( 'dwqa_add_answer', array( $this, 'new_activity' ) );
  20. // add_action( 'dwqa_add_comment', array( $this, 'new_activity' ) );
  21. }
  22. public function dwqa_queue_add_question($question_id, $user_id){
  23. wp_schedule_single_event( time() + 120, 'dwqa_new_question_notify', array($question_id, $user_id) );
  24. }
  25. public function dwqa_queue_add_answer($answer_id, $question_id){
  26. wp_schedule_single_event( time() + 120, 'dwqa_new_answer_notify', array($answer_id, $question_id) );
  27. }
  28. public function dwqa_queue_insert_comment($comment_id, $comment){
  29. wp_schedule_single_event( time() + 120, 'dwqa_new_comment_notify', array($comment_id, $comment) );
  30. }
  31. public function new_question_notify( $question_id, $user_id ) {
  32. // receivers
  33. $admin_email = $this->get_admin_email();
  34. $enabled = get_option( 'dwqa_subscrible_enable_new_question_notification', 1 );
  35. if ( ! $enabled ) {
  36. return false;
  37. }
  38. $question = get_post( $question_id );
  39. if ( ! $question ) {
  40. return false;
  41. }
  42. $subject = get_option( 'dwqa_subscrible_new_question_email_subject' );
  43. if ( ! $subject ) {
  44. $subject = __( 'A new question was posted on {site_name}', 'dw-question-answer' );
  45. }
  46. $subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $subject );
  47. $subject = str_replace( '{question_title}', $question->post_title, $subject );
  48. $subject = str_replace( '{question_id}', $question->ID, $subject );
  49. $subject = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $subject );
  50. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_question_email', 'new-question' );
  51. if ( ! $message ) {
  52. return false;
  53. }
  54. // Replacement
  55. $admin = get_user_by( 'email', $admin_email[0] );
  56. if ( $admin ) {
  57. $message = str_replace( '{admin}', get_the_author_meta( 'display_name', $admin->ID ), $message );
  58. }
  59. //sender
  60. $message = str_replace( '{user_avatar}', get_avatar( $user_id, '60' ), $message );
  61. $message = str_replace( '{user_link}', dwqa_get_author_link( $user_id ), $message );
  62. $message = str_replace( '{username}', get_the_author_meta( 'display_name', $user_id ), $message );
  63. //question
  64. $message = str_replace( '{question_link}', get_permalink( $question_id ), $message );
  65. $message = str_replace( '{question_title}', $question->post_title, $message );
  66. $message = str_replace( '{question_content}', $question->post_content, $message );
  67. // Site info
  68. $logo = get_option( 'dwqa_subscrible_email_logo', '' );
  69. $logo = $logo ? '<img src="' . $logo . '" alt="' . get_bloginfo( 'name' ) . '" style="max-width: 100%; height: auto;" />' : '';
  70. $message = str_replace( '{site_logo}', $logo, $message );
  71. $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message );
  72. $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
  73. $message = str_replace( '{site_url}', site_url(), $message );
  74. $headers = array(
  75. "From: {$this->get_from_name()} <{$this->get_from_address()}>",
  76. "Reply-To: {$this->get_from_address()}",
  77. "Content-Type: {$this->get_content_type()}; charset=utf-8"
  78. );
  79. // start send out email
  80. foreach( $admin_email as $to ) {
  81. if ( is_email( $to ) )
  82. $sended = $this->send( sanitize_email( $to ), $subject, $message, $headers );
  83. }
  84. }
  85. public function new_answer_notify( $answer_id, $question_id ) {
  86. // print_r( $answer_id ); die;
  87. if ( 'dwqa-answer' !== get_post_type( $answer_id ) ) {
  88. return false;
  89. }
  90. if ( 'dwqa-question' !== get_post_type( $question_id ) ) {
  91. return false;
  92. }
  93. // default value
  94. $site_name = get_bloginfo( 'name' );
  95. $question_title = get_the_title( $question_id );
  96. $answer_content = get_post_field( 'post_content', $answer_id );
  97. $question_link = get_permalink( $question_id );
  98. $answer_link = trailingslashit( $question_link ) . '#answer-' . $answer_id;
  99. $site_description = get_bloginfo( 'description' );
  100. $site_url = site_url();
  101. $enable_send_copy = get_option( 'dwqa_subscrible_send_copy_to_admin' );
  102. $admin_email = $this->get_admin_email('answer');
  103. $site_logo = get_option( 'dwqa_subscrible_email_logo', '' );
  104. $site_logo = $site_logo ? '<img src="' . $site_logo . '" alt="' . get_bloginfo( 'name' ) . '" style="max-width: 100%; height: auto;" />' : '';
  105. // for answer
  106. $answer_is_anonymous = dwqa_is_anonymous( $answer_id );
  107. if ( $answer_is_anonymous ) {
  108. $user_answer_id = 0;
  109. $user_answer_display_name = get_post_meta( $answer_id, '_dwqa_anonymous_name', true );
  110. $user_answer_display_name = $user_answer_display_name ? sanitize_text_field( $user_answer_display_name ) : __( 'Anonymous', 'dw-question-answer' );
  111. $user_answer_email = get_post_meta( $answer_id, '_dwqa_anonymous_email', true );
  112. $user_answer_email = $user_answer_email ? sanitize_email( $user_answer_email ) : false;
  113. } else {
  114. $user_answer_id = get_post_field( 'post_author', $answer_id );
  115. $user_answer_display_name = get_the_author_meta( 'display_name', $user_answer_id );
  116. $user_answer_email = get_the_author_meta( 'user_email', $user_answer_id );
  117. }
  118. if ( $user_answer_email ) {
  119. $user_answer_avatar = get_avatar( $user_answer_email, 60 );
  120. } else {
  121. $user_answer_avatar = get_avatar( $user_answer_id, 60 );
  122. }
  123. // for question
  124. $question_is_anonymous = dwqa_is_anonymous( $question_id );
  125. if ( $question_is_anonymous ) {
  126. $user_question_id = 0;
  127. $user_question_display_name = get_post_meta( $question_id, '_dwqa_anonymous_name', true );
  128. $user_question_display_name = $user_question_display_name ? sanitize_text_field( $user_question_display_name ) : __( 'Anonymous', 'dw-question-answer' );
  129. $user_question_email = get_post_meta( $question_id, '_dwqa_anonymous_email', true );
  130. $user_question_email = $user_question_email ? sanitize_email( $user_question_email ) : false;
  131. } else {
  132. $user_question_id = get_post_field( 'post_author', $question_id );
  133. $user_question_display_name = get_the_author_meta( 'display_name', $user_question_id );
  134. $user_question_email = get_the_author_meta( 'user_email', $user_question_id );
  135. }
  136. if ( $user_question_email ) {
  137. $user_question_avatar = get_avatar( $user_question_email, 60 );
  138. } else {
  139. $user_question_avatar = get_avatar( $user_question_id, 60 );
  140. }
  141. // get all follower email lists
  142. $followers = get_post_meta( $question_id, '_dwqa_followers' );
  143. $followers_email = array();
  144. if ( !empty( $followers ) && is_array( $followers ) ) {
  145. foreach( $followers as $follower ) {
  146. if ( is_numeric( $follower ) ) {
  147. // prevent send to answer author and question author
  148. if ( absint( $follower ) == $user_answer_id || absint( $follower ) == $user_question_id ) continue;
  149. // get user email has registered
  150. $followers_email[] = get_the_author_meta( 'user_email', $follower );
  151. } else {
  152. // prevent send to question author and answer author
  153. if ( sanitize_email( $user_answer_email ) == sanitize_email( $follower ) || sanitize_email( $user_question_email ) == sanitize_email( $follower ) ) continue;
  154. // get anonymous email
  155. $followers_email[] = sanitize_email( $follower );
  156. }
  157. }
  158. }
  159. // start send to followers
  160. $answer_notify_enabled = get_option( 'dwqa_subscrible_enable_new_answer_followers_notification', 1 );
  161. if ( $answer_notify_enabled && !empty( $followers_email ) && is_array( $followers_email ) && 'private' !== get_post_status( $answer_id ) ) {
  162. $subject = get_option( 'dwqa_subscrible_new_answer_followers_email_subject', __( '[{site_name}] You have a new answer for your followed question', 'dw-question-answer' ) );
  163. $subject = str_replace( '{site_name}', esc_html( $site_name ), $subject );
  164. $subject = str_replace( '{question_title}', $question_title, $subject );
  165. $subject = str_replace( '{answer_author}', esc_html( $user_answer_display_name ), $subject );
  166. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_answer_followers_email', 'new-answer-followers' );
  167. $message = apply_filters( 'dwqa_get_new_answer_email_to_followers_message', $message, $answer_id, $question_id );
  168. if ( !$message ) {
  169. return false;
  170. }
  171. $message = str_replace( 'Howdy {follower},', '', $message );
  172. $message = str_replace( '{answer_author}', esc_html( $user_answer_display_name ), $message );
  173. $message = str_replace( '{question_link}', esc_url( $question_link ), $message );
  174. $message = str_replace( '{answer_link}', esc_url( $answer_link ), $message );
  175. $message = str_replace( '{question_title}', $question_title, $message );
  176. $message = str_replace( '{answer_content}', wp_kses_post( $answer_content ), $message );
  177. $message = str_replace( '{answer_avatar}', $user_answer_avatar, $message );
  178. $message = str_replace( '{site_logo}', $site_logo, $message );
  179. $message = str_replace( '{site_name}', esc_html( $site_name ), $message );
  180. $message = str_replace( '{site_description}', esc_html( $site_description ), $message );
  181. $message = str_replace( '{site_url}', esc_url( $site_url ), $message );
  182. if ( $enable_send_copy ) {
  183. $followers_email = array_merge( $followers_email, $admin_email );
  184. }
  185. // make sure it is not duplicate email
  186. $followers_email = array_unique( $followers_email );
  187. $headers = array(
  188. "From: {$this->get_from_name()} <{$this->get_from_address()}>",
  189. "Content-Type: {$this->get_content_type()}; charset=utf-8"
  190. );
  191. foreach( $followers_email as $f_email ) {
  192. $headers[] = "Bcc: " . $f_email;
  193. }
  194. $sitename = strtolower( $_SERVER['SERVER_NAME'] );
  195. if ( substr( $sitename, 0, 4 ) === 'www.' ) {
  196. $sitename = substr( $sitename, 4 );
  197. }
  198. $no_reply = 'noreply@' . $sitename;
  199. $sender = $this->send( $no_reply, $subject, $message, $headers );
  200. }
  201. // start send to question author
  202. $answer_notify_for_question_enabled = get_option( 'dwqa_subscrible_enable_new_answer_notification', 1 );
  203. if ( $user_question_email && $answer_notify_for_question_enabled && absint( $user_answer_id ) !== absint( $user_question_id ) ) {
  204. $subject = get_option( 'dwqa_subscrible_new_answer_email_subject', __( '[{site_name}] A new answer for "{question_title}" was posted on {site_name}', 'dw-question-answer' ) );
  205. $subject = str_replace( '{site_name}', esc_html( $site_name ), $subject );
  206. $subject = str_replace( '{question_title}', $question_title, $subject );
  207. $subject = str_replace( '{question_id}', absint( $question_id ), $subject );
  208. $subject = str_replace( '{username}', esc_html( $user_question_display_name ), $subject );
  209. $subject = str_replace( '{answer_author}', esc_html( $user_answer_display_name ), $subject );
  210. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_answer_email', 'new-answer' );
  211. $message = apply_filters( 'dwqa_get_new_answer_email_to_author_message', $message, $question_id, $answer_id );
  212. if ( !$message ) {
  213. return false;
  214. }
  215. $message = str_replace( '{answer_avatar}', $user_answer_avatar, $message );
  216. $message = str_replace( '{answer_author}', esc_html( $user_answer_display_name ), $message );
  217. $message = str_replace( '{question_link}', esc_url( $question_link ), $message );
  218. $message = str_replace( '{question_author}', esc_html( $user_question_display_name ), $message );
  219. $message = str_replace( '{answer_link}', esc_url( $answer_link ), $message );
  220. $message = str_replace( '{question_title}', $question_title, $message );
  221. $message = str_replace( '{answer_content}', wp_kses_post( $answer_content ), $message );
  222. $message = str_replace( '{site_logo}', $site_logo, $message );
  223. $message = str_replace( '{site_name}', esc_html( $site_name ), $message );
  224. $message = str_replace( '{site_description}', esc_html( $site_description ), $message );
  225. $message = str_replace( '{site_url}', esc_url( $site_url ), $message );
  226. $headers = array(
  227. "From: {$this->get_from_name()} <{$this->get_from_address()}>",
  228. "Content-Type: {$this->get_content_type()}; charset=utf-8"
  229. );
  230. if ( $enable_send_copy ) {
  231. foreach( $admin_email as $a_email ) {
  232. $headers[] = "Bcc: " . $a_email;
  233. }
  234. }
  235. $sender = $this->send( $user_question_email, $subject, $message, $headers );
  236. }
  237. }
  238. public function new_comment_notify( $comment_id, $comment ) {
  239. $parent = get_post_type( $comment->comment_post_ID );
  240. //Admin email
  241. $admin_email = get_bloginfo( 'admin_email' );
  242. $enable_send_copy = get_option( 'dwqa_subscrible_send_copy_to_admin' );
  243. if ( 1 == $comment->comment_approved && ( 'dwqa-question' == $parent || 'dwqa-answer' == $parent ) ) {
  244. if ( $parent == 'dwqa-question' ) {
  245. $enabled = get_option( 'dwqa_subscrible_enable_new_comment_question_notification', 1 );
  246. // $admin_email = $this->get_admin_email( 'comment-question' ); //ignore in this time
  247. } elseif ( $parent == 'dwqa-answer' ) {
  248. $enabled = get_option( 'dwqa_subscrible_enable_new_comment_answer_notification', 1 );
  249. // $admin_email = $this->get_admin_email( 'comment-answer' );
  250. }
  251. if ( ! $enabled ) {
  252. return false;
  253. }
  254. $post_parent = get_post( $comment->comment_post_ID );
  255. if ( dwqa_is_anonymous( $comment->comment_post_ID ) ) {
  256. $post_parent_email = get_post_meta( $comment->comment_post_ID, '_dwqa_anonymous_email', true );
  257. if ( ! is_email( $post_parent_email ) ) {
  258. return false;
  259. }
  260. } else {
  261. // if user is not the author of question/answer, add user to followers list
  262. if ( $post_parent->post_author != $comment->user_id ) {
  263. if ( ! dwqa_is_followed( $post_parent->ID, $comment->user_id ) ) {
  264. add_post_meta( $post_parent->ID, '_dwqa_followers', $comment->user_id );
  265. }
  266. }
  267. $post_parent_email = get_the_author_meta( 'user_email', $post_parent->post_author );
  268. }
  269. // To send HTML mail, the Content-type header must be set
  270. $headers = array(
  271. "From: {$this->get_from_name()} <{$this->get_from_address()}>",
  272. "Content-Type: {$this->get_content_type()}; charset=utf-8"
  273. );
  274. if ( $parent == 'dwqa-question' ) {
  275. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_question_email', 'new-comment-question' );
  276. $subject = get_option( 'dwqa_subscrible_new_comment_question_email_subject',__( '[{site_name}] You have a new comment for question {question_title}', 'dw-question-answer' ) );
  277. $message = str_replace( '{question_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message );
  278. $question = $post_parent;
  279. } else {
  280. $message = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_answer_email', 'new-comment-answer' );
  281. $subject = get_option( 'dwqa_subscrible_new_comment_answer_email_subject',__( '[{site_name}] You have a new comment for answer', 'dw-question-answer' ) );
  282. $message = str_replace( '{answer_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message );
  283. $question_id = dwqa_get_post_parent_id( $post_parent->ID );
  284. $question = get_post( $question_id );
  285. }
  286. $subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $subject );
  287. $subject = str_replace( '{question_title}', $question->post_title, $subject );
  288. $subject = str_replace( '{question_id}', $question->ID, $subject );
  289. $subject = str_replace( '{username}',get_the_author_meta( 'display_name', $comment->user_id ), $subject );
  290. if ( ! $message ) {
  291. return false;
  292. }
  293. // logo replace
  294. $logo = get_option( 'dwqa_subscrible_email_logo','' );
  295. $logo = $logo ? '<img src="'.$logo.'" alt="'.get_bloginfo( 'name' ).'" style="max-width: 100%; height: auto;" />' : '';
  296. $subject = str_replace( '{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $subject );
  297. $message = str_replace( '{site_logo}', $logo, $message );
  298. $message = str_replace( '{question_link}', get_permalink( $question->ID ), $message );
  299. $message = str_replace( '{comment_link}', get_permalink( $question->ID ) . '#comment-' . $comment_id, $message );
  300. $message = str_replace( '{question_title}', $question->post_title, $message );
  301. $message = str_replace( '{comment_author_avatar}', get_avatar( $comment->user_id, '60' ), $message );
  302. $message = str_replace( '{comment_author_link}', dwqa_get_author_link( $comment->user_id ), $message );
  303. $message = str_replace( '{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $message );
  304. $message = str_replace( '{comment_content}', $comment->comment_content, $message );
  305. $message = str_replace( '{site_name}', get_bloginfo( 'name' ), $message );
  306. $message = str_replace( '{site_description}', get_bloginfo( 'description' ), $message );
  307. $message = str_replace( '{site_url}', site_url(), $message );
  308. if ( $parent == 'dwqa-question' ) {
  309. $enable_notify = get_option( 'dwqa_subscrible_enable_new_comment_question_followers_notify', true );
  310. } else {
  311. $enable_notify = get_option( 'dwqa_subscrible_enable_new_comment_answer_followers_notification', true );
  312. }
  313. if ( $enable_notify ) {
  314. //Follower email task
  315. $followers = get_post_meta( $post_parent->ID, '_dwqa_followers' );
  316. $comment_email = get_the_author_meta( 'user_email', $comment->user_id );
  317. if ( $parent == 'dwqa-question' ) {
  318. $message_to_follower = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_question_followers_email', 'new-comment-question' );
  319. $follow_subject = get_option( 'dwqa_subscrible_new_comment_question_followers_email_subject',__( '[{site_name}] You have a new comment for question {question_title}', 'dw-question-answer' ) );
  320. $message_to_follower = str_replace( '{question_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message_to_follower );
  321. $question = $post_parent;
  322. } else {
  323. $message_to_follower = dwqa_get_mail_template( 'dwqa_subscrible_new_comment_answer_followers_email', 'new-comment-answer' );
  324. $follow_subject = get_option( 'dwqa_subscrible_new_comment_answer_followers_email_subject',__( '[{site_name}] You have a new comment for answer', 'dw-question-answer' ) );
  325. $message_to_follower = str_replace( '{answer_author}', get_the_author_meta( 'display_name', $post_parent->post_author ), $message_to_follower );
  326. }
  327. $follow_subject = str_replace( '{site_name}', get_bloginfo( 'name' ), $follow_subject );
  328. $follow_subject = str_replace( '{question_title}', $question->post_title, $follow_subject );
  329. $follow_subject = str_replace( '{question_id}', $question->ID, $follow_subject );
  330. $follow_subject = str_replace( '{username}',get_the_author_meta( 'display_name', $comment->user_id ), $follow_subject );
  331. $follow_subject = str_replace( '{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $follow_subject );
  332. $message_to_follower = str_replace( '{site_logo}', $logo, $message_to_follower );
  333. $message_to_follower = str_replace( '{question_link}', get_permalink( $question->ID ), $message_to_follower );
  334. $comment_link = get_permalink( $question->ID ) . '#comment-' . $comment_id;
  335. $message_to_follower = str_replace( '{comment_link}', $comment_link, $message_to_follower );
  336. $message_to_follower = str_replace( '{question_title}', $question->post_title, $message_to_follower );
  337. $message_to_follower = str_replace( '{comment_author_avatar}', get_avatar( $comment->user_id, '60' ), $message_to_follower );
  338. $message_to_follower = str_replace( '{comment_author_link}', dwqa_get_author_link( $comment->user_id ), $message_to_follower );
  339. $message_to_follower = str_replace( '{comment_author}', get_the_author_meta( 'display_name', $comment->user_id ), $message_to_follower );
  340. $message_to_follower = str_replace( '{comment_content}', $comment->comment_content, $message_to_follower );
  341. $message_to_follower = str_replace( '{site_name}', get_bloginfo( 'name' ), $message_to_follower );
  342. $message_to_follower = str_replace( '{site_description}', get_bloginfo( 'description' ), $message_to_follower );
  343. $message_to_follower = str_replace( '{site_url}', site_url(), $message_to_follower );
  344. if ( ! empty( $followers ) && is_array( $followers ) ) {
  345. foreach ( $followers as $follower ) {
  346. $follower = (int) $follower;
  347. $user_data = get_user_by( 'id', $follower );
  348. if ( $user_data ) {
  349. $follow_email = $user_data->user_email;
  350. $follower_name = $user_data->display_name;
  351. if ( $follow_email && absint( $follower ) !== absint( $post_parent->post_author ) && absint( $follower ) !== absint( $comment->user_id ) ) {
  352. $message_to_each_follower = str_replace( '{follower}', $follower_name, $message_to_follower );
  353. $test = $this->send( $follow_email, $follow_subject, $message_to_each_follower, $headers );
  354. if ( $enable_send_copy && $follow_email != $admin_email ) {
  355. $this->send( $admin_email, $follow_subject, $message_to_each_follower, $headers );
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }
  362. if ( $post_parent->post_author != $comment->user_id ) {
  363. $this->send( $post_parent_email, $subject, $message, $headers );
  364. if ( $enable_send_copy && $admin_email != $post_parent_email ) {
  365. $this->send( $admin_email, $subject, $message, $headers );
  366. }
  367. }
  368. }
  369. }
  370. public function get_admin_email( $type = 'question' ){
  371. switch ($type) {
  372. case 'answer':
  373. $admin_email = get_option( 'dwqa_subscrible_new_answer_forward', '' );
  374. break;
  375. case 'comment-question':
  376. $admin_email = get_option( 'dwqa_subscrible_new_comment_question_forward', '' );
  377. break;
  378. case 'comment-answer':
  379. $admin_email = get_option( 'dwqa_subscrible_new_comment_answer_forward', '' );
  380. break;
  381. case 'question':
  382. default:
  383. $admin_email = get_option( 'dwqa_subscrible_sendto_address', '' );
  384. break;
  385. }
  386. $emails = preg_split('/\r\n|\r|\n/', $admin_email );
  387. $emails = array_merge( $emails, array( get_bloginfo( 'admin_email' ) ) );
  388. $emails = array_unique($emails);
  389. return $emails;
  390. }
  391. // Pushover
  392. public function push( $args ) {
  393. if ( !function_exists( 'ckpn_send_notification' ) ) {
  394. if ( class_exists( 'CKPushoverNotifications' ) ) {
  395. $ckpn_core = CKPushoverNotifications::getInstance();
  396. $ckpn_core->ckpn_send_notification( $args );
  397. }
  398. } else {
  399. ckpn_send_notification( $args );
  400. }
  401. }
  402. function new_activity( $post_id = 0 ) {
  403. if ( empty( $post_id ) ) {
  404. return false;
  405. }
  406. $title = false;
  407. if ( 'dwqa_add_comment' == current_action() ) {
  408. $comment = get_comment( $post_id );
  409. $title = __( 'New Comment in: ', 'dwqa-notification' );
  410. $post_id = $comment->comment_post_ID;
  411. }
  412. if ( 'dwqa-answer' == get_post_type( $post_id ) ) {
  413. $question_id = dwqa_get_post_parent_id( $post_id );
  414. if ( !$title ) {
  415. $title = __( 'New Answer in: ', 'dwqa-notification' );
  416. }
  417. } else {
  418. $question_id = $post_id;
  419. if ( !$title ) {
  420. $title = __( 'New Question: ', 'dwqa-notification' );
  421. }
  422. }
  423. $question_title = get_post_field( 'post_title', $question_id );
  424. $question_link = get_permalink( $question_id );
  425. $content = get_post_field( 'post_content', $post_id );
  426. $title = $title . $question_title;
  427. $args = array( 'title' => $title, 'message' => strip_tags( $content ) );
  428. $this->push( $args );
  429. }
  430. public function get_from_address() {
  431. $from_email = get_option( 'dwqa_subscrible_from_address', get_bloginfo( 'admin_email' ) );
  432. if ( empty( $from_email ) ) {
  433. $from_email = get_bloginfo( 'admin_email' );
  434. }
  435. return sanitize_email( $from_email );
  436. }
  437. public function get_from_name() {
  438. $name = get_option( 'dwqa_subscrible_from_name', get_bloginfo( 'name' ) );
  439. if ( empty( $name ) ) {
  440. $name = get_bloginfo( 'name' );
  441. }
  442. return $name;
  443. }
  444. public function get_content_type() {
  445. return apply_filters( 'dwqa_notifications_get_content_type', 'text/html' );
  446. }
  447. public function send( $to, $subject, $message, $headers = '', $attachments = array() ) {
  448. // return ;
  449. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 9999 );
  450. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 9999 );
  451. add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 9999 );
  452. $sended = wp_mail( $to, $subject, $message, $headers, $attachments );
  453. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  454. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  455. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  456. return $sended;
  457. }
  458. }
  459. ?>