Skip to:
Content

#7847 closed defect (bug) (fixed)

bp_after_message_content hook not working with BP Nouveau

Reported by: pareshradadiya Owned by: imath
Milestone: 3.1.0 Priority: normal
Severity: normal Version:
Component: Messages Keywords: has-patch commit
Cc:

Description

bp_after_message_content hook does not work when BP Nouveau is active.

I use bp_after_message_content to display some custom stuff after each messages.

So when BP Nouveau is active, my callback function hooked into bp_after_message_content never get call.

I have done something like this:

add_action( 'bp_after_message_content', 'test_message_meta_display' ) );

function test_message_meta_display() {

	$message_id = (int) bp_get_the_thread_message_id();
	$meta = bp_messages_get_meta( $message_id, 'message_meta' );

	ob_start();

	// ..some more custom code

	$content = ob_get_contents();

	ob_end_clean();

	echo str_replace("\n","",$content);
}

Attachments (3)

7847.patch (3.2 KB) - added by imath 10 months ago.
7847.2.patch (4.7 KB) - added by imath 10 months ago.
Improves first patch by making sure bp_is_messages_conversation() is true in AJAX
7847.3.patch (5.3 KB) - added by imath 10 months ago.
Introduces a new function bp_strip_script_and_style_tags() to sanitize hooks buffer

Download all attachments as: .zip

Change History (16)

#1 @DJPaul
10 months ago

  • Component changed from Core to Messages
  • Milestone changed from Awaiting Review to 3.0.1

Another missing action - a similar problem to #7837

#2 @DJPaul
10 months ago

Which is going to be tricky because we'll have to make sure however those templates are loaded in Javascript, that the message content filter is run in the AJAX callback(?).

#3 @imath
10 months ago

I thought this https://buddypress.trac.wordpress.org/browser/trunk/src/bp-templates/bp-nouveau/buddypress/common/js-templates/messages/index.php#L279 was handling this part. I’ll look at the function to understand what’s going wrong. I think the hook is fired into the JS template. So it’s an approach a bit different, I’ll add an example with messages meta.

Last edited 10 months ago by imath (previous) (diff)

#4 @imath
10 months ago

After a deeper look, i confirm bp_after_message_content is fired. But it's unusable the way @pareshradadiya tries to use it, because data is available in JavaScript and not in PHP, see screenshot below:

https://cldup.com/bVqGLlkx53.png

What needs to be done imho:

  1. improve the Ajax functions that are fetching the messages data :
    1. by adding a filter to allow plugins to get meta data
    2. or by populating a metas property with all the meta for the message using bp_messages_get_meta( bp_get_the_thread_message_id() )
  2. Add some documentation about how to use it to display the meta value into the UI.

I doubt 1.a. can be done for the 3.0.1 maintenance release, 1.b. is easier but might cause some performance troubles.

#5 @pareshradadiya
10 months ago

I confirm bp_after_message_content callback function get called. I was returning output instead of echoing it on a page in my old test. However, I cannot fetch message meta inside from callback like this.

$message_id = (int) bp_get_the_thread_message_id();
$meta = bp_messages_get_meta( $message_id, 'message_meta' );
Last edited 10 months ago by pareshradadiya (previous) (diff)

#6 @imath
10 months ago

  • Owner set to imath
  • Status changed from new to assigned

@imath
10 months ago

#7 @imath
10 months ago

  • Keywords has-patch reporter-feedback added

I chose a 3rd approach. The idea is to change as fiew as possible for plugin authors. I think the new Messages UI is a perfect candidate for the BP Rest API, so untill we can count on it and why not deprecate some actions in favor of this API, i think we should use the existing actions and catch their content using the buffer. As this is done within a messages loop, data is accessible via PHP and it doesn't change anything for plugin authors. Here's a screenshot showing injected content using bp_before_message_content & bp_after_message_content :

https://cldup.com/wXf19swDlL.png

@pareshradadiya can you test 7847.patch and tell me if it's fixing the issue for you ?

Last edited 10 months ago by imath (previous) (diff)

#8 @DJPaul
10 months ago

  • Milestone changed from 3.0.1 to 3.1.0

Milestone renamed

@imath
10 months ago

Improves first patch by making sure bp_is_messages_conversation() is true in AJAX

@imath
10 months ago

Introduces a new function bp_strip_script_and_style_tags() to sanitize hooks buffer

#9 @pareshradadiya
10 months ago

@imath I confirm 7847.3.patch fixing issue for me.

#10 @imath
10 months ago

  • Keywords commit added; reporter-feedback removed

Thanks a lot for your feedback 👍 I'll commit it asap.

This ticket was mentioned in Slack in #buddypress by imath. View the logs.


10 months ago

#12 @imath
10 months ago

In 12121:

BP Nouveau: Improve Messages UI extensibility

In BP Nouveau, the Messages UI is a Backbone based one. This means current legacy hooks inserted into the JS templates are a bit more different to use for plugin developers as it requires them to use JavaScript to get the data models. If the current AJAX requests still need a way to be extended to fetch extra data such as specific Messages metas, this commit is a first step to help plugin developers to insert content into the Messages UI without changing their habits about using the PHP Messages template global variables to get data about the Messages loop. This is done by introducing the back compatibility function bp_nouveau_messages_catch_hook_content(). This function is used during the AJAX requests that are fetching messages for a specific thread. As a start, 2 legacy hooks have been moved from the JS Template level to this higher level :

  • bp_before_message_content
  • bp_after_message_content

If actions are attached to these hooks their outputs will be caught using the buffer and will be "transported" as new properties, respectively beforeContent and afterContent, of the JSON reply that is used by the Messages UI. If these 2 properties are set and populated, their content will be output inside the corresponding JS templates.

Props pareshradadiya

See #7847 (Trunk)

#13 @imath
10 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 12122:

BP Nouveau: Improve Messages UI extensibility

In BP Nouveau, the Messages UI is a Backbone based one. This means current legacy hooks inserted into the JS templates are a bit more different to use for plugin developers as it requires them to use JavaScript to get the data models. If the current AJAX requests still need a way to be extended to fetch extra data such as specific Messages metas, this commit is a first step to help plugin developers to insert content into the Messages UI without changing their habits about using the PHP Messages template global variables to get data about the Messages loop. This is done by introducing the back compatibility function bp_nouveau_messages_catch_hook_content(). This function is used during the AJAX requests that are fetching messages for a specific thread. As a start, 2 legacy hooks have been moved from the JS Template level to this higher level :

  • bp_before_message_content
  • bp_after_message_content

If actions are attached to these hooks their outputs will be caught using the buffer and will be "transported" as new properties, respectively beforeContent and afterContent, of the JSON reply that is used by the Messages UI. If these 2 properties are set and populated, their content will be output inside the corresponding JS templates.

Props pareshradadiya

Fixes #7847 (Branch 3.0)

Note: See TracTickets for help on using tickets.