Skip to:
Content

BuddyPress.org

Changeset 10706


Ignore:
Timestamp:
04/14/2016 08:36:39 PM (10 years ago)
Author:
r-a-y
Message:

Messages: Introduce dynamic filter, "bp_messages_{$action}_notification".

This new filter helps plugin developers filter private message
notifications that are not the 'new_message' action.

For backward compatibility, we apply the existing
"bp_messages_{$amount}_new_message_notification" filter just before
adding our new dynamic filter.

Fixes #6750.

Props imath.

File:
1 edited

Legend:

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

    r10513 r10706  
    104104function messages_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    105105        $total_items = (int) $total_items;
     106        $text        = '';
    106107        $link        = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox' );
    107108        $title       = __( 'Inbox', 'buddypress' );
     
    112113                        $amount = 'multiple';
    113114                        $text   = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items );
     115
    114116                } else {
    115                         $amount = 'single';
    116 
    117117                        // Get message thread ID.
    118118                        $message   = new BP_Messages_Message( $item_id );
     
    128128                        }
    129129                }
    130         }
    131 
    132         if ( 'string' === $format ) {
    133                 if ( ! empty( $link ) ) {
    134                         $retval = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
     130
     131                if ( 'string' === $format ) {
     132                        if ( ! empty( $link ) ) {
     133                                $return = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
     134                        } else {
     135                                $return = esc_html( $text );
     136                        }
     137
     138                        /**
     139                         * Filters the new message notification text before the notification is created.
     140                         *
     141                         * This is a dynamic filter. Possible filter names are:
     142                         *   - 'bp_messages_multiple_new_message_notification'.
     143                         *   - 'bp_messages_single_new_message_notification'.
     144                         *
     145                         * @param string $return            Notification text.
     146                         * @param int    $total_items       Number of messages referred to by the notification.
     147                         * @param string $text              The raw notification test (ie, not wrapped in a link).
     148                         * @param int    $item_id           ID of the associated item.
     149                         * @param int    $secondary_item_id ID of the secondary associated item.
     150                         */
     151                        $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    135152                } else {
    136                         $retval = esc_html( $text );
     153                        /** This filter is documented in bp-messages/bp-messages-notifications.php */
     154                        $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
     155                                'text' => $text,
     156                                'link' => $link
     157                        ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     158                }
     159
     160        // Custom notification action for the Messages component
     161        } else {
     162                if ( 'string' === $format ) {
     163                        $return = $text;
     164                } else {
     165                        $return = array(
     166                                'text' => $text,
     167                                'link' => $link
     168                        );
    137169                }
    138170
    139171                /**
    140                  * Filters the new message notification text before the notification is created.
     172                 * Backcompat for plugins that used to filter bp_messages_single_new_message_notification
     173                 * for their custom actions. These plugins should now use 'bp_messages_' . $action . '_notification'
     174                 */
     175                if ( has_filter( 'bp_messages_single_new_message_notification' ) ) {
     176                        if ( 'string' === $format ) {
     177                                /** This filter is documented in bp-messages/bp-messages-notifications.php */
     178                                $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     179
     180                        // Notice that there are seven parameters instead of six? Ugh...
     181                        } else {
     182                                /** This filter is documented in bp-messages/bp-messages-notifications.php */
     183                                $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     184                        }
     185                }
     186
     187                /**
     188                 * Filters the custom action notification before the notification is created.
    141189                 *
    142                  * This is a dynamic filter. Possible filter names are:
    143                  *   - 'bp_messages_multiple_new_message_notification'.
    144                  *   - 'bp_messages_single_new_message_notification'.
     190                 * This is a dynamic filter based on the message notification action.
    145191                 *
    146                  * @param string $retval            Notification text.
    147                  * @param int    $total_items       Number of messages referred to by the notification.
    148                  * @param string $text              The raw notification test (ie, not wrapped in a link).
     192                 * @since 2.6.0
     193                 *
     194                 * @param array  $value             An associative array containing the text and the link of the notification
    149195                 * @param int    $item_id           ID of the associated item.
    150196                 * @param int    $secondary_item_id ID of the secondary associated item.
     197                 * @param int    $total_items       Number of messages referred to by the notification.
     198                 * @param string $format            Return value format. 'string' for BuddyBar-compatible
     199                 *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
    151200                 */
    152                 $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $retval, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    153         } else {
    154                 /** This filter is documented in bp-messages/bp-messages-notifications.php */
    155                 $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
    156                         'text' => $text,
    157                         'link' => $link
    158                 ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     201                $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format );
    159202        }
    160203
Note: See TracChangeset for help on using the changeset viewer.