messages_format_notifications( string $action, int $item_id, int $secondary_item_id, int $total_items, string $format = 'string' )

Format notifications for the Messages component.

Description

Parameters

$action

(Required) The kind of notification being rendered.

$item_id

(Required) The primary item id.

$secondary_item_id

(Required) The secondary item id.

$total_items

(Required) The total number of messaging-related notifications waiting for the user.

$format

(Optional) Return value format. 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar. Default: 'string'.

Default value: 'string'

Return

(string|array) Formatted notifications.

Source

File: bp-messages/bp-messages-notifications.php

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    $total_items = (int) $total_items;
    $text        = '';
    $link        = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
    $title       = __( 'Messages', 'buddyboss' );
    $amount      = 'single';
 
    if ( 'new_message' === $action ) {
        if ( $total_items > 1 ) {
            $amount = 'multiple';
            $text   = sprintf( __( 'You have %d new messages', 'buddyboss' ), $total_items );
 
        } else {
            // Get message thread ID.
            $message   = new BP_Messages_Message( $item_id );
            $thread_id = $message->thread_id;
            $link      = ( ! empty( $thread_id ) )
                ? bp_get_message_thread_view_link( $thread_id )
                : false;
 
            if ( ! empty( $secondary_item_id ) ) {
                $text = sprintf( __( '%s sent you a new private message', 'buddyboss' ), bp_core_get_user_displayname( $secondary_item_id ) );
            } else {
                $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddyboss' ), bp_core_number_format( $total_items ) );
            }
        }
 
        if ( 'string' === $format ) {
            if ( ! empty( $link ) ) {
                $return = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
            } else {
                $return = esc_html( $text );
            }
 
            /**
             * Filters the new message notification text before the notification is created.
             *
             * This is a dynamic filter. Possible filter names are:
             *   - 'bp_messages_multiple_new_message_notification'.
             *   - 'bp_messages_single_new_message_notification'.
             *
             * @param string $return            Notification text.
             * @param int    $total_items       Number of messages referred to by the notification.
             * @param string $text              The raw notification test (ie, not wrapped in a link).
             * @param int    $item_id           ID of the associated item.
             * @param int    $secondary_item_id ID of the secondary associated item.
             */
            $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
        } else {
            /** This filter is documented in bp-messages/bp-messages-notifications.php */
            $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
                'text' => $text,
                'link' => $link
            ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
        }
 
    // Custom notification action for the Messages component
    } else {
        if ( 'string' === $format ) {
            $return = $text;
        } else {
            $return = array(
                'text' => $text,
                'link' => $link
            );
        }
 
        /**
         * Backcompat for plugins that used to filter bp_messages_single_new_message_notification
         * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification'
         */
        if ( has_filter( 'bp_messages_single_new_message_notification' ) ) {
            if ( 'string' === $format ) {
                /** This filter is documented in bp-messages/bp-messages-notifications.php */
                $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
 
            // Notice that there are seven parameters instead of six? Ugh...
            } else {
                /** This filter is documented in bp-messages/bp-messages-notifications.php */
                $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
            }
        }
 
        /**
         * Filters the custom action notification before the notification is created.
         *
         * This is a dynamic filter based on the message notification action.
         *
         * @since BuddyPress 2.6.0
         *
         * @param array  $value             An associative array containing the text and the link of the notification
         * @param int    $item_id           ID of the associated item.
         * @param int    $secondary_item_id ID of the secondary associated item.
         * @param int    $total_items       Number of messages referred to by the notification.
         * @param string $format            Return value format. 'string' for BuddyBar-compatible
         *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
         */
        $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format );
    }
 
    /**
     * Fires right before returning the formatted message notifications.
     *
     * @since BuddyPress 1.0.0
     *
     * @param string $action            The type of message notification.
     * @param int    $item_id           The primary item ID.
     * @param int    $secondary_item_id The secondary item ID.
     * @param int    $total_items       Total amount of items to format.
     */
    do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
 
    return $return;
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.