| 241 | * Send email when a blog post receives a comment. |
| 242 | * |
| 243 | * @since BuddyPress (1.9.0) |
| 244 | * |
| 245 | * @uses get_comment() |
| 246 | * @uses get_post() |
| 247 | * @uses get_permalink() |
| 248 | * @uses bp_get_user_meta() |
| 249 | * @uses bp_get_settings_slug() |
| 250 | * @uses bp_core_get_user_domain() |
| 251 | * @uses bp_core_get_core_userdata() |
| 252 | * @uses bp_get_email_subject() |
| 253 | * @uses wp_mail() |
| 254 | * @uses apply_filters() To call the 'bp_blog_post_new_comment_notification_to' hook |
| 255 | * @uses apply_filters() To call the 'bp_blog_post_new_comment_notification_subject' hook |
| 256 | * @uses apply_filters() To call the 'bp_blog_post_new_comment_notification_message' hook |
| 257 | * @uses do_action() To call the 'bp_blog_post_comment_email' hook |
| 258 | * |
| 259 | * @param int $comment_id The comment id. |
| 260 | * @param int $comment_approved 1 (true) if the comment is approved, 0 (false) if not |
| 261 | */ |
| 262 | |
| 263 | function bp_blog_post_new_comment_notification( $comment_ID, $comment_approved ) { |
| 264 | |
| 265 | $comment = get_comment( $comment_ID ); |
| 266 | $post = get_post( $comment->comment_post_ID ); |
| 267 | |
| 268 | if ( $post->post_author != $comment->user_id && 'no' != bp_get_user_meta( $post->post_author, 'notification_blog_post_new_comment', true ) ) { |
| 269 | |
| 270 | $post_link = get_permalink( $post->ID ); |
| 271 | |
| 272 | $settings_slug = function_exists( 'bp_get_settings_slug' ) ? bp_get_settings_slug() : 'settings'; |
| 273 | $settings_link = bp_core_get_user_domain( $post->post_author ) . $settings_slug . '/notifications/'; |
| 274 | |
| 275 | $commenter_name = stripslashes( $comment->comment_author ); |
| 276 | |
| 277 | $content = stripslashes( $comment->comment_content ); |
| 278 | |
| 279 | // Set up and send the message |
| 280 | $ud = bp_core_get_core_userdata( $post->post_author ); |
| 281 | $to = $ud->user_email; |
| 282 | $subject = bp_get_email_subject( array( 'text' => sprintf( __( '%s commented on one of your posts', 'buddypress' ), $commenter_name ) ) ); |
| 283 | $message = sprintf( __( |
| 284 | '%1$s commented on one of your blog posts: |
| 285 | |
| 286 | "%2$s" |
| 287 | |
| 288 | To view your blog post and all comments, log in and visit: %3$s |
| 289 | |
| 290 | --------------------- |
| 291 | ', 'buddypress' ), $commenter_name, $content, $post_link ); |
| 292 | |
| 293 | // Only show the disable notifications line if the settings component is enabled |
| 294 | if ( bp_is_active( 'settings' ) ) { |
| 295 | $message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'buddypress' ), $settings_link ); |
| 296 | } |
| 297 | |
| 298 | /* Send the message */ |
| 299 | $to = apply_filters( 'bp_blog_post_new_comment_notification_to', $to ); |
| 300 | $subject = apply_filters( 'bp_blog_post_new_comment_notification_subject', $subject, $commenter_name ); |
| 301 | $message = apply_filters( 'bp_blog_post_new_comment_notification_message', $message, $commenter_name, $content, $post_link, $settings_link ); |
| 302 | |
| 303 | wp_mail( $to, $subject, $message ); |
| 304 | } |
| 305 | |
| 306 | do_action( 'bp_blog_post_comment_email', $post->post_author, $subject, $message, $comment_ID, $comment->user_id, $post->ID ); |
| 307 | |
| 308 | } |
| 309 | add_action( 'comment_post', 'bp_blog_post_new_comment_notification', 10, 2 ); |
| 310 | |
| 311 | /** |
| 368 | |
| 369 | if ( 'string' == $format ) { |
| 370 | $return = apply_filters( $filter, '<a href="' . esc_url( $at_mention_link ) . '" title="' . esc_attr( $at_mention_title ) . '">' . esc_html( $text ) . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id ); |
| 371 | } else { |
| 372 | $return = apply_filters( $filter, array( |
| 373 | 'text' => $text, |
| 374 | 'link' => $at_mention_link |
| 375 | ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id ); |
| 376 | } |
| 377 | do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); |
| 379 | |
| 380 | case 'new_blog_post_comment': |
| 381 | |
| 382 | $post = get_post( $item_id ); |
| 383 | |
| 384 | $author_url = get_author_posts_url( $post->post_author ); |
| 385 | $post_title = $post->post_title; |
| 386 | |
| 387 | if ( (int) $total_items > 1 ) { |
| 388 | $text = sprintf( __( '%1$d of your posts have new comments', 'buddypress' ), (int) $total_items ); |
| 389 | $filter = 'bp_activity_multiple_blog_post_comments_notification'; |
| 390 | } else { |
| 391 | $user_fullname = bp_core_get_user_displayname( $secondary_item_id ); |
| 392 | $text = sprintf( __( '%1$s commented on one of your posts', 'buddypress' ), $user_fullname ); |
| 393 | $filter = 'bp_activity_single_blog_post_comments_notification'; |
| 394 | } |
| 395 | |
| 396 | if ( 'string' == $format ) { |
| 397 | $return = apply_filters( $filter, '<a href="' . esc_url( $author_url ) . '">' . esc_html( $text ) . '</a>', $author_url, (int) $total_items, $item_id, $secondary_item_id ); |
| 398 | } else { |
| 399 | $return = apply_filters( $filter, array( |
| 400 | 'text' => $text, |
| 401 | 'link' => $author_url |
| 402 | ), $author_url, (int) $total_items, $item_id, $secondary_item_id ); |
| 403 | } |
| 404 | |
| 405 | do_action( 'blog_post_comment_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); |
| 406 | break; |
293 | | if ( 'string' == $format ) { |
294 | | $return = apply_filters( $filter, '<a href="' . esc_url( $at_mention_link ) . '" title="' . esc_attr( $at_mention_title ) . '">' . esc_html( $text ) . '</a>', $at_mention_link, (int) $total_items, $activity_id, $poster_user_id ); |
295 | | } else { |
296 | | $return = apply_filters( $filter, array( |
297 | | 'text' => $text, |
298 | | 'link' => $at_mention_link |
299 | | ), $at_mention_link, (int) $total_items, $activity_id, $poster_user_id ); |
300 | | } |
301 | | |
302 | | do_action( 'activity_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); |
303 | | |
| 459 | /** |
| 460 | * Notify a member when their blog post receives a comment. |
| 461 | * |
| 462 | * Hooked to the 'bp_blog_post_comment_email' action, we piggy back off the |
| 463 | * existing email code for now, since it does the heavy lifting for us. |
| 464 | * |
| 465 | * @since BuddyPress (1.9.0) |
| 466 | * |
| 467 | * @param string $post_author_id |
| 468 | * @param string $subject (not used) |
| 469 | * @param string $message (not used) |
| 470 | * @param string $comment_id (not used) |
| 471 | * @param string $commenter_id |
| 472 | * @param string $post_id |
| 473 | */ |
| 474 | |
| 475 | function bp_blog_post_comment_add_notification( $post_author_id, $subject, $message, $comment_id, $commenter_id, $post_id ) { |
| 476 | if ( bp_is_active( 'notifications' ) ) { |
| 477 | |
| 478 | bp_notifications_add_notification( array( |
| 479 | 'user_id' => $post_author_id, |
| 480 | 'item_id' => $post_id, |
| 481 | 'secondary_item_id' => $commenter_id, |
| 482 | 'component_name' => buddypress()->activity->id, |
| 483 | 'component_action' => 'new_blog_post_comment', |
| 484 | 'date_notified' => bp_core_current_time(), |
| 485 | 'is_new' => 1, |
| 486 | ) ); |
| 487 | } |
| 488 | } |
| 489 | add_action( 'bp_blog_post_comment_email', 'bp_blog_post_comment_add_notification', 10, 6 ); |
| 490 | |
| 491 | /** |
| 492 | * Remove blog post notifications when a user clicks on them. |
| 493 | * |
| 494 | * @since BuddyPress (1.9.0) |
| 495 | * |
| 496 | * @uses bp_notifications_mark_notifications_by_item_id() |
| 497 | * @uses bp_is_active() |
| 498 | * @uses is_author() |
| 499 | * @uses bp_loggedin_user_id() |
| 500 | */ |
| 501 | |
| 502 | function bp_blog_post_comment_remove_screen_notifications() { |
| 503 | if ( ( is_author( bp_loggedin_user_id() ) ) && ( bp_is_active( 'notifications' ) ) ) { |
| 504 | bp_notifications_mark_notifications_by_type( bp_loggedin_user_id(), buddypress()->activity->id, 'new_blog_post_comment' ); |
| 505 | } |
| 506 | } |
| 507 | add_action( 'wp', 'bp_blog_post_comment_remove_screen_notifications' ); |
| 508 | No newline at end of file |