Skip to:
Content

BuddyPress.org

Ticket #5298: 5298.patch

File 5298.patch, 11.2 KB (added by henrywright, 11 years ago)
  • bp-activity-notifications.php

     
    238238}
    239239
    240240/**
     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
     263function 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
     288To 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}
     309add_action( 'comment_post', 'bp_blog_post_new_comment_notification', 10, 2 );
     310
     311/**
    241312 * Helper method to map action arguments to function parameters
    242313 *
    243314 * @since BuddyPress (1.9.0)
     
    254325/**
    255326 * Format notifications related to activity.
    256327 *
    257  * @since BuddyPress (1.5)
     328 * @since BuddyPress (1.9.0)
    258329 *
     330 * @uses get_post()
     331 * @uses get_author_posts_url()
     332 * @uses get_permalink()
    259333 * @uses bp_loggedin_user_domain()
     334 * @uses bp_core_get_user_domain()
    260335 * @uses bp_get_activity_slug()
    261336 * @uses bp_core_get_user_displayname()
    262337 * @uses apply_filters() To call the 'bp_activity_multiple_at_mentions_notification' hook.
    263338 * @uses apply_filters() To call the 'bp_activity_single_at_mentions_notification' hook.
    264  * @uses do_action() To call 'activity_format_notifications' hook.
     339 * @uses apply_filters() To call the 'bp_activity_multiple_blog_post_comment_notification' hook.
     340 * @uses apply_filters() To call the 'bp_activity_single_blog_post_comment_notification' hook.
     341 * @uses do_action() To call 'activity_format_notifications' hook.
     342 * @uses do_action() To call 'blog_post_comment_format_notifications' hook.
    265343 *
    266  * @param string $action The type of activity item. Just 'new_at_mention' for now.
    267  * @param int $item_id The activity ID.
     344 * @param string $action The type of activity item. 'new_at_mention' or 'new_blog_post_comment'.
     345 * @param int $item_id In the case of at-mentions, this is the activity ID. In the case of blog comments, this is the post ID.
    268346 * @param int $secondary_item_id In the case of at-mentions, this is the mentioner's ID.
    269347 * @param int $total_items The total number of notifications to format.
    270348 * @param string $format 'string' to get a BuddyBar-compatible notification, 'array' otherwise.
    271  * @return string $return Formatted @mention notification.
     349 * @return string $return Formatted notification.
    272350 */
    273351function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    274352
     
    287365                                $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
    288366                                $filter = 'bp_activity_single_at_mentions_notification';
    289367                        }
     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 );
    290378                break;
     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;
    291407        }
    292408
    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 
    304409        return $return;
    305410}
    306411
     
    351456add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications' );
    352457add_action( 'bp_activity_screen_mentions',                  'bp_activity_remove_screen_notifications' );
    353458
     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
     475function 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}
     489add_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
     502function 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}
     507add_action( 'wp', 'bp_blog_post_comment_remove_screen_notifications' );
     508 No newline at end of file
  • bp-activity-screens.php

     
    298298}
    299299add_action( 'bp_notification_settings', 'bp_activity_screen_notification_settings', 1 );
    300300
     301/**
     302 * Add blog post comment notifications settings to the notifications settings page.
     303 *
     304 * @since BuddyPress (1.9.0)
     305 *
     306 * @uses bp_get_user_meta()
     307 * @uses bp_core_get_username()
     308 * @uses do_action() To call the 'bp_activity_screen_notification_settings' hook.
     309 */
     310function bp_activity_blog_post_comment_screen_notification_settings() {
     311        global $current_user;
     312
     313        if ( ! $comment = bp_get_user_meta( bp_displayed_user_id(), 'notification_blog_post_new_comment', true ) ) {
     314                $comment = 'yes';
     315        }
     316
     317        ?>
     318        <table class="notification-settings" id="bp-example-notification-settings">
     319
     320                <thead>
     321                <tr>
     322                        <th class="icon"></th>
     323                        <th class="title"><?php _e( 'Blog comments', 'buddypress' ) ?></th>
     324                        <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
     325                        <th class="no"><?php _e( 'No', 'buddypress' )?></th>
     326                </tr>
     327                </thead>
     328
     329                <tbody>
     330                <tr>
     331                        <td></td>
     332                        <td><?php _e( 'A member comments on one of your posts', 'buddypress' ) ?></td>
     333                        <td class="yes"><input type="radio" name="notifications[notification_blog_post_new_comment]" value="yes" <?php checked( $comment, 'yes', true ) ?>/></td>
     334                        <td class="no"><input type="radio" name="notifications[notification_blog_post_new_comment]" value="no" <?php checked( $comment, 'no', true ) ?>/></td>
     335                </tr>
     336
     337                <?php do_action( 'bp_activity_blog_post_comment_notification_settings' ); ?>
     338
     339                </tbody>
     340        </table>
     341<?php }
     342add_action( 'bp_notification_settings', 'bp_activity_blog_post_comment_screen_notification_settings' );
     343
    301344/** Theme Compatability *******************************************************/
    302345
    303346/**