Skip to:
Content

BuddyPress.org

Changeset 10895


Ignore:
Timestamp:
06/22/2016 09:17:23 PM (9 years ago)
Author:
r-a-y
Message:

Activity: Fine-tune activity reply screen notifications when activity stream commenting is enabled.

Previously, if "activity stream commenting on blog and forum posts" is
enabled and a reply was created from the Activity Directory page for a
blog post activity item, clicking on the activity reply notification
would not mark the notification as read.

Also, if a member decides to post a comment from the blog post permalink,
a screen notification would not be added.

This commit fixes these issues and adds a unit test.

Props r-a-y, dcavins, imath.

Fixes #7135.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-notifications.php

    r10825 r10895  
    209209 * @param int    $total_items       The total number of notifications to format.
    210210 * @param string $format            'string' to get a BuddyBar-compatible notification, 'array' otherwise.
     211 * @param int    $id                Optional. The notification ID.
    211212 * @return string $return Formatted @mention notification.
    212213 */
    213 function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     214function bp_activity_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) {
    214215    $action_filter = $action;
    215216    $return        = false;
     
    226227
    227228            if ( (int) $total_items > 1 ) {
    228                 $text = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
     229                $text   = sprintf( __( 'You have %1$d new mentions', 'buddypress' ), (int) $total_items );
    229230                $amount = 'multiple';
    230231            } else {
    231                 $text =  sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
     232                $text = sprintf( __( '%1$s mentioned you', 'buddypress' ), $user_fullname );
    232233            }
    233234        break;
     
    243244                $amount = 'multiple';
    244245            } else {
    245                 $link   = bp_activity_get_permalink( $activity_id );
    246                 $text   = sprintf( __( '%1$s commented on one of your updates', 'buddypress' ), $user_fullname );
     246                $link = add_query_arg( 'nid', (int) $id, bp_activity_get_permalink( $activity_id ) );
     247                $text = sprintf( __( '%1$s commented on one of your updates', 'buddypress' ), $user_fullname );
    247248            }
    248249        break;
     
    258259                $amount = 'multiple';
    259260            } else {
    260                 $link   = bp_activity_get_permalink( $activity_id );
    261                 $text   = sprintf( __( '%1$s replied to one your activity comments', 'buddypress' ), $user_fullname );
     261                $link = add_query_arg( 'nid', (int) $id, bp_activity_get_permalink( $activity_id ) );
     262                $text = sprintf( __( '%1$s replied to one your activity comments', 'buddypress' ), $user_fullname );
    262263            }
    263264        break;
     
    366367        bp_notifications_add_notification( array(
    367368            'user_id'           => $activity->user_id,
    368             'item_id'           => $activity->id,
     369            'item_id'           => $comment_id,
    369370            'secondary_item_id' => $commenter_id,
    370371            'component_name'    => buddypress()->activity->id,
     
    390391        bp_notifications_add_notification( array(
    391392            'user_id'           => $activity_comment->user_id,
    392             'item_id'           => $activity_comment->item_id,
     393            'item_id'           => $comment_id,
    393394            'secondary_item_id' => $commenter_id,
    394395            'component_name'    => buddypress()->activity->id,
     
    427428 *
    428429 * @since 2.0.0
    429  * @since 2.6.0 Mark notifications for 'update_reply' and 'comment_reply' actions
    430430 *
    431431 * @param BP_Activity_Activity $activity Activity object.
     
    440440    }
    441441
    442     /**
    443      * Filter here to add the notification actions to mark as read
    444      * when the single activity is displayed.
    445      *
    446      * @since 2.6.0
    447      *
    448      * @param array $value List of notification actions to mark as read.
    449      */
    450     $notification_actions = apply_filters( 'bp_activity_notification_actions_single_activity', array(
    451         'new_at_mention',
    452         'update_reply',
    453         'comment_reply',
    454     ) );
    455 
    456     $user_id   = bp_loggedin_user_id();
    457     $component = buddypress()->activity->id;
    458 
    459     foreach ( $notification_actions as $action ) {
    460         // Mark as read any notifications for the current user related to this activity item.
    461         bp_notifications_mark_notifications_by_item_id( $user_id, $activity->id, $component, $action );
    462     }
     442    // Mark as read any notifications for the current user related to this activity item.
     443    bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $activity->id, buddypress()->activity->id, 'new_at_mention' );
    463444}
    464445add_action( 'bp_activity_screen_single_activity_permalink', 'bp_activity_remove_screen_notifications_single_activity_permalink' );
     446
     447/**
     448 * Mark non-mention notifications as read when user visits our read permalink.
     449 *
     450 * In particular, 'update_reply' and 'comment_reply' notifications are handled
     451 * here. See {@link bp_activity_format_notifications()} for more info.
     452 *
     453 * @since 2.6.0
     454 */
     455function bp_activity_remove_screen_notifications_for_non_mentions() {
     456    if ( false === bp_is_active( 'notifications' ) || false === is_singular() || false === is_user_logged_in() || empty( $_GET['nid'] ) ) {
     457        return;
     458    }
     459
     460    // Mark notification as read.
     461    BP_Notifications_Notification::update(
     462        array(
     463            'is_new'  => false
     464        ),
     465        array(
     466            'user_id' => bp_loggedin_user_id(),
     467            'id'      => (int) $_GET['nid']
     468        )
     469    );
     470}
     471add_action( 'bp_screens', 'bp_activity_remove_screen_notifications_for_non_mentions' );
    465472
    466473/**
     
    481488}
    482489add_action( 'bp_activity_deleted_activities', 'bp_activity_at_mention_delete_notification', 10 );
     490
     491/**
     492 * Add a notification for post comments to the post author or post commenter.
     493 *
     494 * Requires "activity stream commenting on blog and forum posts" to be enabled.
     495 *
     496 * @since 2.6.0
     497 *
     498 * @param int        $activity_id          The activity comment ID.
     499 * @param WP_Comment $post_type_comment    WP Comment object.
     500 * @param array      $activity_args        Activity comment arguments.
     501 * @param object     $activity_post_object The post type tracking args object.
     502 */
     503function bp_activity_add_notification_for_synced_blog_comment( $activity_id, $post_type_comment, $activity_args, $activity_post_object ) {
     504    // If activity comments are disabled for WP posts, stop now!
     505    if ( bp_disable_blogforum_comments() || empty( $activity_id ) || false === bp_is_active( 'notifications' ) ) {
     506        return;
     507    }
     508
     509    // Send a notification to the blog post author.
     510    if ( (int) $post_type_comment->post->post_author !== (int) $activity_args['user_id'] ) {
     511        // Only add a notification if comment author is a registered user.
     512        // @todo Should we remove this restriction?
     513        if ( ! empty( $post_type_comment->user_id ) ) {
     514            bp_notifications_add_notification( array(
     515                'user_id'           => $post_type_comment->post->post_author,
     516                'item_id'           => $activity_id,
     517                'secondary_item_id' => $post_type_comment->user_id,
     518                'component_name'    => buddypress()->activity->id,
     519                'component_action'  => 'update_reply',
     520                'date_notified'     => $post_type_comment->comment_date_gmt,
     521                'is_new'            => 1,
     522            ) );
     523        }
     524    }
     525
     526    // Send a notification to the parent comment author for follow-up comments.
     527    if ( ! empty( $post_type_comment->comment_parent ) ) {
     528        $parent_comment = get_comment( $post_type_comment->comment_parent );
     529
     530        if ( ! empty( $parent_comment->user_id ) && (int) $parent_comment->user_id !== (int) $activity_args['user_id'] ) {
     531            bp_notifications_add_notification( array(
     532                'user_id'           => $parent_comment->user_id,
     533                'item_id'           => $activity_id,
     534                'secondary_item_id' => $post_type_comment->user_id,
     535                'component_name'    => buddypress()->activity->id,
     536                'component_action'  => 'comment_reply',
     537                'date_notified'     => $post_type_comment->comment_date_gmt,
     538                'is_new'            => 1,
     539            ) );
     540        }
     541    }
     542}
     543add_action( 'bp_blogs_comment_sync_activity_comment', 'bp_activity_add_notification_for_synced_blog_comment', 10, 4 );
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r10825 r10895  
    716716                }
    717717            }
     718
     719            /**
     720             * Fires after an activity comment is added from a WP post comment.
     721             *
     722             * @since 2.6.0
     723             *
     724             * @param int        $activity_id          The activity comment ID.
     725             * @param WP_Comment $post_type_comment    WP Comment object.
     726             * @param array      $activity_args        Activity comment arguments.
     727             * @param object     $activity_post_object The post type tracking args object.
     728             */
     729            do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object );
    718730        }
    719731    }
  • trunk/tests/phpunit/testcases/activity/notifications.php

    r10715 r10895  
    375375        $this->assertEquals( $expected_commenter, wp_list_pluck( $u2_notifications, 'secondary_item_id' ) );
    376376    }
     377
     378    /**
     379     * @ticket BP7135
     380     */
     381    public function test_activity_reply_notifications_for_blog_comment_to_activity_comment_sync() {
     382        $old_user = get_current_user_id();
     383        $u1 = $this->factory->user->create();
     384        $u2 = $this->factory->user->create();
     385        $u3 = $this->factory->user->create();
     386
     387        $this->set_current_user( $u1 );
     388        $userdata = get_userdata( $u1 );
     389
     390        // let's use activity comments instead of single "new_blog_comment" activity items
     391        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     392
     393        // Silence comment flood errors.
     394        add_filter( 'comment_flood_filter', '__return_false' );
     395
     396        // create the blog post
     397        $post_id = $this->factory->post->create( array(
     398            'post_status' => 'publish',
     399            'post_type'   => 'post',
     400            'post_title'  => 'Test post',
     401        ) );
     402
     403        $this->set_current_user( $u2 );
     404        $userdata = get_userdata( $u2 );
     405
     406        $c1 = wp_new_comment( array(
     407            'comment_post_ID'      => $post_id,
     408            'comment_author'       => $userdata->user_nicename,
     409            'comment_author_url'   => 'http://buddypress.org',
     410            'comment_author_email' => $userdata->user_email,
     411            'comment_content'      => 'this is a blog comment',
     412            'comment_type'         => '',
     413            'comment_parent'       => 0,
     414            'user_id'              => $u2,
     415        ) );
     416        // Approve the comment
     417        $this->factory->comment->update_object( $c1, array( 'comment_approved' => 1 ) );
     418
     419        $this->set_current_user( $u3 );
     420        $userdata = get_userdata( $u3 );
     421
     422        $c2 = wp_new_comment( array(
     423            'comment_post_ID'      => $post_id,
     424            'comment_author'       => $userdata->user_nicename,
     425            'comment_author_url'   => 'http://buddypress.org',
     426            'comment_author_email' => $userdata->user_email,
     427            'comment_content'      => 'this is a blog comment',
     428            'comment_type'         => '',
     429            'comment_parent'       => $c1,
     430            'user_id'              => $u3,
     431        ) );
     432        // Approve the comment
     433        $this->factory->comment->update_object( $c2, array( 'comment_approved' => 1 ) );
     434
     435        // Get activity IDs.
     436        $ac1 = get_comment_meta( $c1, 'bp_activity_comment_id', true );
     437        $ac2 = get_comment_meta( $c2, 'bp_activity_comment_id', true );
     438
     439        // Check if notifications exists for user 1.
     440        $n1 = BP_Notifications_Notification::get( array(
     441            'component_name' => 'activity',
     442            'user_id'        => $u1
     443        ) );
     444        $this->assertEquals( 2, count( $n1 ) );
     445        $this->assertEquals(
     446            array( $ac1, $ac2 ),
     447            wp_list_pluck( $n1, 'item_id' )
     448        );
     449
     450        // Check if notification exists for user 2.
     451        $n2 = BP_Notifications_Notification::get( array(
     452            'component_action' => 'comment_reply',
     453            'item_id'          => $ac2,
     454            'user_id'          => $u2
     455        ) );
     456        $this->assertNotEmpty( $n2 );
     457
     458        // Reset.
     459        $this->set_current_user( $old_user );
     460        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     461        remove_filter( 'comment_flood_filter', '__return_false' );
     462    }
     463
    377464}
Note: See TracChangeset for help on using the changeset viewer.