Skip to:
Content

BuddyPress.org

Changeset 9208


Ignore:
Timestamp:
12/03/2014 03:27:09 PM (12 years ago)
Author:
boonebgorges
Message:

Assemble message notification filters dynamically.

This allows us to centralize the documentation.

Props tw2113.
See #5970.

Location:
trunk
Files:
2 edited

Legend:

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

    r8947 r9208  
    129129        if ( 'new_message' === $action ) {
    130130                if ( $total_items > 1 ) {
     131                        $amount = 'multiple';
    131132                        $text   = sprintf( __( 'You have %d new messages', 'buddypress' ), $total_items );
    132                         $filter = 'bp_messages_multiple_new_message_notification';
    133133                } else {
     134                        $amount = 'single';
     135
    134136                        // get message thread ID
    135137                        $message   = new BP_Messages_Message( $item_id );
     
    144146                                $text = sprintf( _n( 'You have %s new private message', 'You have %s new private messages', $total_items, 'buddypress' ), bp_core_number_format( $total_items ) );
    145147                        }
    146                         $filter = 'bp_messages_single_new_message_notification';
    147148                }
    148149        }
     
    154155                        $retval = esc_html( $text );
    155156                }
    156                 $return = apply_filters( $filter, $retval, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
     157
     158                /**
     159                 * Filters the new message notification text before the notification is created.
     160                 *
     161                 * This is a dynamic filter. Possible filter names are:
     162                 *   - 'bp_messages_multiple_new_message_notification'
     163                 *   - 'bp_messages_single_new_message_notification'
     164                 *
     165                 * @param string $retval            Notification text.
     166                 * @param int    $total_items       Number of messages referred to by the notification.
     167                 * @param string $text              The raw notification test (ie, not wrappen in a link).
     168                 * @param int    $item_id           ID of the associated item.
     169                 * @param int    $secondary_item_id ID of the secondary associated item.
     170                 */
     171                $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', $retval, (int) $total_items, $text, $link, $item_id, $secondary_item_id );
    157172        } else {
    158                 $return = apply_filters( $filter, array(
     173                /** This filter is documented in bp-messages/bp-messages-notifications.php */
     174                $return = apply_filters( 'bp_messages_' . $amount . '_new_message_notification', array(
    159175                        'text' => $text,
    160176                        'link' => $link
  • trunk/tests/phpunit/testcases/messages/notifications.php

    r9139 r9208  
    66 */
    77class BP_Tests_Messages_Notifications extends BP_UnitTestCase {
     8
     9        protected $filter_fired;
     10
     11        public function setUp() {
     12                parent::setUp();
     13
     14                $this->filter_fired = '';
     15        }
    816        /**
    917         * @group bp_messages_message_delete_notifications
     
    4351                $this->set_current_user( $current_user );
    4452        }
     53
     54        /**
     55         * @group messages_format_notifications
     56         */
     57        public function test_friends_format_notifications_bp_messages_multiple_new_message_notification_nonstring_filter() {
     58                // Dummy thread ID
     59                $t = 12;
     60                $u = $this->factory->user->create();
     61                $this->set_current_user( $u );
     62
     63                // Admin
     64                $n = $this->factory->notification->create( array(
     65                        'component_name' => 'messages',
     66                        'user_id' => $u,
     67                        'item_id' => $t,
     68                        'component_action' => 'new_message',
     69                ) );
     70
     71                add_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     72                $n = messages_format_notifications( 'new_message', $n, '', 2, 'array' );
     73                remove_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     74
     75                $this->assertSame( 'bp_messages_multiple_new_message_notification', $this->filter_fired );
     76        }
     77
     78        /**
     79         * @group messages_format_notifications
     80         */
     81        public function test_friends_format_notifications_bp_messages_single_new_message_notification_nonstring_filter() {
     82                // Dummy thread ID
     83                $t = 12;
     84                $u = $this->factory->user->create();
     85                $this->set_current_user( $u );
     86
     87                // Admin
     88                $n = $this->factory->notification->create( array(
     89                        'component_name' => 'messages',
     90                        'user_id' => $u,
     91                        'item_id' => $t,
     92                        'component_action' => 'new_message',
     93                ) );
     94
     95                add_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     96                $n = messages_format_notifications( 'new_message', $n, '', 1, 'array' );
     97                remove_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     98
     99                $this->assertSame( 'bp_messages_single_new_message_notification', $this->filter_fired );
     100        }
     101
     102        /**
     103         * @group messages_format_notifications
     104         */
     105        public function test_friends_format_notifications_bp_messages_multiple_new_message_notification_string_filter() {
     106                // Dummy thread ID
     107                $t = 12;
     108                $u = $this->factory->user->create();
     109                $this->set_current_user( $u );
     110
     111                // Admin
     112                $n = $this->factory->notification->create( array(
     113                        'component_name' => 'messages',
     114                        'user_id' => $u,
     115                        'item_id' => $t,
     116                        'component_action' => 'new_message',
     117                ) );
     118
     119                add_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     120                $n = messages_format_notifications( 'new_message', $n, '', 2 );
     121                remove_filter( 'bp_messages_multiple_new_message_notification', array( $this, 'notification_filter_callback' ) );
     122
     123                $this->assertSame( 'bp_messages_multiple_new_message_notification', $this->filter_fired );
     124        }
     125
     126        /**
     127         * @group messages_format_notifications
     128         */
     129        public function test_friends_format_notifications_bp_messages_single_new_message_notification_string_filter() {
     130                // Dummy thread ID
     131                $t = 12;
     132                $u = $this->factory->user->create();
     133                $this->set_current_user( $u );
     134
     135                // Admin
     136                $n = $this->factory->notification->create( array(
     137                        'component_name' => 'messages',
     138                        'user_id' => $u,
     139                        'item_id' => $t,
     140                        'component_action' => 'new_message',
     141                ) );
     142
     143                add_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     144                $n = messages_format_notifications( 'new_message', $n, '', 1 );
     145                remove_filter( 'bp_messages_single_new_message_notification', array( $this, 'notification_filter_callback' ) );
     146
     147                $this->assertSame( 'bp_messages_single_new_message_notification', $this->filter_fired );
     148        }
     149
     150        public function notification_filter_callback( $value ) {
     151                $this->filter_fired = current_filter();
     152                return $value;
     153        }
    45154}
Note: See TracChangeset for help on using the changeset viewer.