Skip to:
Content

BuddyPress.org

Opened 2 years ago

Closed 22 months ago

#7988 closed enhancement (fixed)

Supply new activity id to bp_activity_add action hook

Reported by: drywallbmb Owned by: boonebgorges
Milestone: 4.0 Priority: normal
Severity: normal Version: 3.0.0
Component: Activity Keywords:
Cc:

Description

The bp_activity_add action fired at https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/bp-activity-functions.php#L1925 will only supply the ID of the just-added activity if that ID was present when the function was called — which is to say, it's only present if an activity is actually being edited, not added.

Not knowing the ID of the just-added activity severely limits the utility of this action.

Example use case: I want to add certain meta value to activities as they're added. Hooking into bp_activity_add seems like the right (and possibly only) place to watch for these additions, but when activities are added I need to then call other code to actually fetch the ID of the activity in question in order to give it a meta value. This code is cumbersome to write and results in an unnecessary performance hit as I need to query the DB to get an ID that could otherwise just be provided to me.... the action provides all other useful and relevant information about the new activity, just not its id.

Attachments (1)

patch_7988.diff (727 bytes) - added by drywallbmb 2 years ago.

Download all attachments as: .zip

Change History (11)

#1 @drywallbmb
2 years ago

I can envision two different approaches to addressing this.

One would be to update $r before the action is called:
$r['id'] = $activity->id;

Another would be to pass a second parameter to the action:
do_action( 'bp_activity_add', $r, $activity->id );

I've no idea which would be preferable or more appropriate, but if someone can provide guidance I'd be happy to submit a patch!

#2 @boonebgorges
2 years ago

Hi @drywallbmb - Thanks for the ticket!

Your second option looks more consistent with how we pass IDs elsewhere in BP. Please do write a patch!

#3 follow-up: @r-a-y
2 years ago

You might also want to consider using the 'bp_activity_after_save' hook, which will include the activity ID:
https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/classes/class-bp-activity-activity.php#L304

Last edited 2 years ago by r-a-y (previous) (diff)

#4 in reply to: ↑ 3 ; follow-up: @drywallbmb
2 years ago

Replying to r-a-y:

You might also want to consider using the 'bp_activity_after_save' hook, which will include the activity ID:
https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/classes/class-bp-activity-activity.php#L304

Yep, I'd hoped to use that one! But there's no guarantee that the code invoking bp_activity_add will then also do bp_activity_after_save. (For example, bbPress calls bp_activity_add() directly but then never invokes that after-save hook anywhere.)

#5 @drywallbmb
2 years ago

Just uploaded a patch... Made a wild guess at target release version in the phpdoc. Also, haven't done a patch in SVN in forever, let me know if I borked it and I can attempt to fix.

#6 @espellcaste
2 years ago

There is a typo in the PHPDoc but whoever will merge this can fix it.

#7 in reply to: ↑ 4 @r-a-y
2 years ago

Replying to drywallbmb:

Yep, I'd hoped to use that one! But there's no guarantee that the code invoking bp_activity_add will then also do bp_activity_after_save. (For example, bbPress calls bp_activity_add() directly but then never invokes that after-save hook anywhere.)

You are guaranteed to get the activity ID if you use the 'bp_activity_after_save' hook even for new activity items.

See https://buddypress.trac.wordpress.org/browser/tags/3.2.0/src/bp-activity/classes/class-bp-activity-activity.php#L295

Please try the 'bp_activity_after_save' hook in your custom code.

Last edited 2 years ago by r-a-y (previous) (diff)

#8 @DJPaul
22 months ago

  • Milestone changed from Awaiting Review to 4.0

I see no harm adding the extra parameter to the action.

#9 @DJPaul
22 months ago

  • Component changed from Core to Activity

#10 @boonebgorges
22 months ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 12294:

Pass activity ID to bp_activity_add action.

Props drywallbmb.
Fixes #7988.

Note: See TracTickets for help on using tickets.