Skip to:
Content

BuddyPress.org

Ticket #8546: 8546.patch

File 8546.patch, 10.9 KB (added by imath, 3 years ago)
  • src/bp-messages/bp-messages-notifications.php

    diff --git src/bp-messages/bp-messages-notifications.php src/bp-messages/bp-messages-notifications.php
    index 1645de3cb..6f010b5f9 100644
    function messages_format_notifications( $action, $item_id, $secondary_item_id, $ 
    3434                if ( $total_items > 1 ) {
    3535                        $amount = 'multiple';
    3636
    37                         /* translators: %s: number of new messages */
     37                        /* translators: %d: number of new messages */
    3838                        $text = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items );
    3939
    4040                } else {
    4141                        // Get message thread ID.
    4242                        $message   = new BP_Messages_Message( $item_id );
    4343                        $thread_id = $message->thread_id;
    44                         $link      = ( ! empty( $thread_id ) )
    45                                 ? bp_get_message_thread_view_link( $thread_id )
    46                                 : false;
     44                        $link      = '';
     45
     46                        if ( ! empty( $thread_id ) ) {
     47                                $link = bp_get_message_thread_view_link( $thread_id );
     48                        }
    4749
    4850                        if ( ! empty( $secondary_item_id ) ) {
    4951                                /* translators: %s: member name */
    function messages_format_notifications( $action, $item_id, $secondary_item_id, $ 
    5658
    5759                if ( 'string' === $format ) {
    5860                        if ( ! empty( $link ) ) {
    59                                 $return = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
     61                                $retval = '<a href="' . esc_url( $link ) . '">' . esc_html( $text ) . '</a>';
    6062                        } else {
    61                                 $return = esc_html( $text );
     63                                $retval = esc_html( $text );
    6264                        }
    6365
    64                         /**
    65                          * Filters the new message notification text before the notification is created.
    66                          *
    67                          * This is a dynamic filter. Possible filter names are:
    68                          *   - 'bp_messages_multiple_new_message_notification'.
    69                          *   - 'bp_messages_single_new_message_notification'.
    70                          *
    71                          * @param string $return            Notification text.
    72                          * @param int    $total_items       Number of messages referred to by the notification.
    73                          * @param string $text              The raw notification test (ie, not wrapped in a link).
    74                          * @param int    $item_id           ID of the associated item.
    75                          * @param int    $secondary_item_id ID of the secondary associated item.
    76                          */
    77                         $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     66                        /** This filter is documented in wp-includes/deprecated.php */
     67                        $retval = apply_filters_deprecated(
     68                                'bp_messages_' . $amount . '_new_message_notification',
     69                                array( $retval, $total_items, $text, $link, $item_id, $secondary_item_id ),
     70                                '10.0.0',
     71                                'bp_messages_' . $amount . '_new_message_' . $format . '_notification'
     72                        );
    7873                } else {
    79                         /** This filter is documented in bp-messages/bp-messages-notifications.php */
    80                         $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
     74                        $retval = array(
    8175                                'text' => $text,
    82                                 'link' => $link
    83                         ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     76                                'link' => $link,
     77                        );
     78
     79                        /** This filter is documented in wp-includes/deprecated.php */
     80                        $retval = apply_filters_deprecated(
     81                                'bp_messages_' . $amount . '_new_message_notification',
     82                                array(
     83                                        $retval,
     84                                        $link, // This extra `$link` variable is the reason why we deprecated the filter.
     85                                        $total_items,
     86                                        $text,
     87                                        $link,
     88                                        $item_id,
     89                                        $secondary_item_id,
     90                                ),
     91                                '10.0.0',
     92                                'bp_messages_' . $amount . '_new_message_' . $format . '_notification'
     93                        );
    8494                }
    8595
     96                /**
     97                 * Filters the new message notification text before the notification is created.
     98                 *
     99                 * This is a dynamic filter. Possible filter names are:
     100                 *   - 'bp_messages_multiple_new_message_string_notification'.
     101                 *   - 'bp_messages_single_new_message_string_notification'.
     102                 *   - 'bp_messages_multiple_new_message_array_notification'.
     103                 *   - 'bp_messages_single_new_message_array_notification'.
     104                 *
     105                 * @param array|string $retval            An array containing the text and the link of the Notification or simply its text.
     106                 * @param int          $total_items       Number of messages referred to by the notification.
     107                 * @param string       $text              The raw notification text (ie, not wrapped in a link).
     108                 * @param string       $link              The link of the notification.
     109                 * @param int          $item_id           ID of the associated item.
     110                 * @param int          $secondary_item_id ID of the secondary associated item.
     111                 */
     112                $retval = apply_filters(
     113                        'bp_messages_' . $amount . '_new_message_' . $format . '_notification',
     114                        $retval,
     115                        $total_items,
     116                        $text,
     117                        $link,
     118                        $item_id,
     119                        $secondary_item_id
     120                );
     121
    86122        // Custom notification action for the Messages component
    87123        } else {
    88124                if ( 'string' === $format ) {
    89                         $return = $text;
     125                        $retval = $text;
    90126                } else {
    91                         $return = array(
     127                        $retval = array(
    92128                                'text' => $text,
    93129                                'link' => $link
    94130                        );
    function messages_format_notifications( $action, $item_id, $secondary_item_id, $ 
    100136                 */
    101137                if ( has_filter( 'bp_messages_single_new_message_notification' ) ) {
    102138                        if ( 'string' === $format ) {
    103                                 /** This filter is documented in bp-messages/bp-messages-notifications.php */
    104                                 $return = apply_filters( 'bp_messages_single_new_message_notification', $return, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     139                                /** This filter is documented in wp-includes/deprecated.php */
     140                                $retval = apply_filters_deprecated(
     141                                        'bp_messages_' . $amount . '_new_message_notification',
     142                                        array( $retval, $total_items, $text, $link, $item_id, $secondary_item_id ),
     143                                        '10.0.0',
     144                                        "bp_messages_{$action}_notification"
     145                                );
    105146
    106                         // Notice that there are seven parameters instead of six? Ugh...
    107147                        } else {
    108                                 /** This filter is documented in bp-messages/bp-messages-notifications.php */
    109                                 $return = apply_filters( 'bp_messages_single_new_message_notification', $return, $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     148                                /** This filter is documented in wp-includes/deprecated.php */
     149                                $retval = apply_filters_deprecated(
     150                                        'bp_messages_' . $amount . '_new_message_notification',
     151                                        array( $retval, $link, $total_items, $text, $link, $item_id, $secondary_item_id ),
     152                                        '10.0.0',
     153                                        "bp_messages_{$action}_notification"
     154                                );
    110155                        }
    111156                }
    112157
    function messages_format_notifications( $action, $item_id, $secondary_item_id, $ 
    124169                 * @param string $format            Return value format. 'string' for BuddyBar-compatible
    125170                 *                                  notifications; 'array' for WP Toolbar. Default: 'string'.
    126171                 */
    127                 $return = apply_filters( "bp_messages_{$action}_notification", $return, $item_id, $secondary_item_id, $total_items, $format );
     172                $retval = apply_filters( "bp_messages_{$action}_notification", $retval, $item_id, $secondary_item_id, $total_items, $format );
    128173        }
    129174
    130175        /**
    function messages_format_notifications( $action, $item_id, $secondary_item_id, $ 
    139184         */
    140185        do_action( 'messages_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    141186
    142         return $return;
     187        return $retval;
    143188}
    144189
    145190/**
  • tests/phpunit/testcases/messages/notifications.php

    diff --git tests/phpunit/testcases/messages/notifications.php tests/phpunit/testcases/messages/notifications.php
    index 077e89a0d..8e348a830 100644
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    3131                        'component_action' => 'new_message',
    3232                ) );
    3333
    34                 add_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     34                add_filter( 'bp_messages_multiple_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
    3535                $n = messages_format_notifications( 'new_message', $n, '', 2, 'array' );
    36                 remove_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     36                remove_filter( 'bp_messages_multiple_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
    3737
    38                 $this->assertSame( 'bp_messages_multiple_new_message_notification', $this->filter_fired );
     38                $this->assertSame( 'bp_messages_multiple_new_message_array_notification', $this->filter_fired );
    3939        }
    4040
    4141        /**
    4242         * @group messages_format_notifications
     43         * @group imath
    4344         */
    4445        public function test_friends_format_notifications_bp_messages_single_new_message_notification_nonstring_filter() {
    4546                // Dummy thread ID
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    5556                        'component_action' => 'new_message',
    5657                ) );
    5758
    58                 add_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     59                add_filter( 'bp_messages_single_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
    5960                $n = messages_format_notifications( 'new_message', $n, '', 1, 'array' );
    60                 remove_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     61                remove_filter( 'bp_messages_single_new_message_array_notification', array( $this, 'notification_filter_callback' ) );
    6162
    62                 $this->assertSame( 'bp_messages_single_new_message_notification', $this->filter_fired );
     63                $this->assertSame( 'bp_messages_single_new_message_array_notification', $this->filter_fired );
    6364        }
    6465
    6566        /**
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    7980                        'component_action' => 'new_message',
    8081                ) );
    8182
    82                 add_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     83                add_filter( 'bp_messages_multiple_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
    8384                $n = messages_format_notifications( 'new_message', $n, '', 2 );
    84                 remove_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     85                remove_filter( 'bp_messages_multiple_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
    8586
    86                 $this->assertSame( 'bp_messages_multiple_new_message_notification', $this->filter_fired );
     87                $this->assertSame( 'bp_messages_multiple_new_message_string_notification', $this->filter_fired );
    8788        }
    8889
    8990        /**
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    103104                        'component_action' => 'new_message',
    104105                ) );
    105106
    106                 add_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     107                add_filter( 'bp_messages_single_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
    107108                $n = messages_format_notifications( 'new_message', $n, '', 1 );
    108                 remove_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     109                remove_filter( 'bp_messages_single_new_message_string_notification', array( $this, 'notification_filter_callback' ) );
    109110
    110                 $this->assertSame( 'bp_messages_single_new_message_notification', $this->filter_fired );
     111                $this->assertSame( 'bp_messages_single_new_message_string_notification', $this->filter_fired );
    111112        }
    112113
    113114        /**