[buddypress-trac] [BuddyPress Trac] #5954: Add a button for users to remove themselves from @mentions

buddypress-trac noreply at wordpress.org
Tue Oct 14 19:58:49 UTC 2014


#5954: Add a button for users to remove themselves from @mentions
-------------------------+-----------------------------
 Reporter:  dtc7240      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Activity     |    Version:
 Severity:  normal       |   Keywords:  has-patch
-------------------------+-----------------------------
 One downside to @mentions is that other users can force things to show up
 on your "mentions" tab (or your "Wall" using some plugins), and you can't
 do anything to remove them.  This could especially be a problem when using
 a "Wall" implementation.

 Here is some code to add a button that allows users to remove themselves
 from any activity in which they were @mentioned.

 First, add the button in perhaps bp-activity/bp-activity-actions.php:

 {{{
 /**
  * Add "Remove Me" button to remove @mentions
  */
 function bp_remove_mentions() {
         if ( bp_activity_user_is_mentioned() ) {
                 $activity_id = bp_get_activity_id();
                 ?>
                 <a data-id="<?php echo $activity_id ?>" class="remove-at-
 mention button bp-secondary-action">Remove Me</a>
                 <?php
 wp_nonce_field('remove_atme','_remove_at_mention_nonce');
         }
 }
 add_action('bp_activity_entry_meta', 'bp_remove_mentions');
 }}}

 Second, add the appropriate javascript to perhaps bp-
 activity/js/mentions.js:

 {{{
         /* When the 'Remove Me' button is clicked to remove an @mentioned
 user from an activity */
         jq('body').on('click', '.remove-at-mention', function(event) {

                 // Stick an are you sure box with explanation here
                 if ( ! confirm('This will remove your tag from this
 activity and remove this activity from your wall (unless you are also the
 author).\n\nWould you like to continue?') ) {
                         return false;
                 }

                 var target = jq(event.target);
                 var activity_id = target.data('id');
                 var nonce =
 target.next('#_remove_at_mention_nonce').val();
                 var activity = jq('li#activity-' + activity_id );

                 jq.ajax({
                         url: ajaxurl,
                         type: "POST",
                         global: false,
                         data: {
                                 action: 'remove_at_mention',
                                 'cookie': bp_get_cookies(),
                                 'id': activity_id,
                                 '_remove_at_mention_nonce': nonce
                         },
                         success: function(response) {
                                 var myprofile = false;
                                 var atmention = response;
                                 if ( response.substring(0,2) == 'my' ) {
                                         myprofile = true;
                                         atmention = response.substring(2);
                                 }
                                 if ( myprofile && document.URL ==
 BuddyBossOptions.user_profile ) {
                                         // Slide up activity
                                         activity.slideUp(600);
                                 } else {
                                         // Remove @mention text from
 activity
                                         activity.find('.activity-inner p
 a').each( function() {
                                                 if ( jq(this).html() ==
 atmention ) {
 jq(this).html('@removed');
                                                 };
                                         });
                                 }
                         },
                         error: function(err) {
                                 alert(err);
                         }
                 });

         });
 }}}
 ''This would require a more generic BuddyPress representation of the
 logged in user's profile base (.../members/scott/) where I've used an easy
 to find variable provided by the BuddyBoss theme.''

 Third, add the Ajax code in perhaps bp-activity/bp-activity-ajax.php:

 {{{
 /**
  * Remove a user's @mention name from an activity
  */
 function bp_ajax_remove_at_mention() {

         // Check the nonce
         if ( !isset($_POST['_remove_at_mention_nonce']) ||
 !wp_verify_nonce($_POST['_remove_at_mention_nonce'], 'remove_atme') ) {
                 exit;
         }

         // Gather data
         $replacement_text = '<i>{@Mention Removed}</i>';
         $activity_id = $_POST['id'];
         $user_id = bp_loggedin_user_id();
         $user_atmention = '@' . bp_activity_get_user_mentionname( $user_id
 );

         // Remove from content
         $activity_lookup = bp_activity_get( array('in'=>$activity_id) );
         $activity = $activity_lookup['activities'][0];
         $content = $activity->content;
         $start_pos_of_atmention = strpos( $content, $user_atmention );
         $end_anchor_tag = strpos( $content, '>', $start_pos_of_atmention
 );
         $begin_anchor_tag = strrpos( substr( $content, 0,
 $start_pos_of_atmention ), '<a' );
         $new_content = substr_replace( $content, $replacement_text,
 $begin_anchor_tag, $end_anchor_tag - $begin_anchor_tag + 1 );
         $activity->content = $new_content;
         $success = bp_activity_add( $activity );

         // Return appropriate values
         if ( $success ) {
                 if ( bp_is_my_profile() ) {
                         echo 'my' . $user_atmention;
                 } else {
                         echo $user_atmention;
                 }
         } else return false;

         die();

 }
 add_action( 'wp_ajax_remove_at_mention', 'bp_ajax_remove_at_mention' );
 }}}

 Thanks,
 Scott

--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/5954>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list