Skip to:
Content

bbPress.org

Ticket #2794: 2794.05.patch

File 2794.05.patch, 16.9 KB (added by thebrandonallen, 8 years ago)
  • src/includes/extend/buddypress/activity.php

    diff --git src/includes/extend/buddypress/activity.php src/includes/extend/buddypress/activity.php
    index c704beb..5248a99 100644
    class BBP_BuddyPress_Activity { 
    3131        private $component = '';
    3232
    3333        /**
     34         * The name of the BuddyPress groups component, used in activity streams
     35         *
     36         * @var string
     37         */
     38        private $groups_component = '';
     39
     40        /**
    3441         * Forum Create Activity Action
    3542         *
    3643         * @var string
    class BBP_BuddyPress_Activity { 
    103110         */
    104111        private function setup_globals() {
    105112
     113                // The name of the BuddyPress groups component, used in activity streams.
     114                if ( bp_is_active( 'groups' ) ) {
     115                        $this->groups_component = buddypress()->groups->id;
     116                }
     117
    106118                // The name of the BuddyPress component, used in activity streams
    107119                $this->component    = 'bbpress';
    108120
    class BBP_BuddyPress_Activity { 
    195207         */
    196208        public function register_activity_actions() {
    197209
    198                 // Sitewide activity stream items
    199                 bp_activity_set_action( $this->component, $this->topic_create, esc_html__( 'New forum topic', 'bbpress' ) );
    200                 bp_activity_set_action( $this->component, $this->reply_create, esc_html__( 'New forum reply', 'bbpress' ) );
     210                // Sitewide activity stream items.
     211                bp_activity_set_action(
     212                        $this->component,
     213                        $this->topic_create,
     214                        esc_html__( 'New forum topic', 'bbpress' ),
     215                        'bbp_bp_format_activity_action_new_topic',
     216                        __( 'Topics', 'bbpress' ),
     217                        array( 'activity', 'member', 'member_groups', 'group' )
     218                );
     219
     220                bp_activity_set_action(
     221                        $this->component,
     222                        $this->reply_create,
     223                        esc_html__( 'New forum reply', 'bbpress' ),
     224                        'bbp_bp_format_activity_action_new_reply',
     225                        __( 'Replies', 'bbpress' ),
     226                        array( 'activity', 'member', 'member_groups', 'group' )
     227                );
     228
     229                // Group forum activity stream items.
     230                if ( bp_is_active( 'groups' ) ) {
     231                        bp_activity_set_action(
     232                                $this->groups_component,
     233                                $this->topic_create,
     234                                esc_html__( 'New forum topic', 'bbpress' ),
     235                                'bbp_bp_format_activity_action_new_topic',
     236                                __( 'Topics', 'bbpress' ),
     237                                array( 'activity', 'member', 'member_groups', 'group' )
     238                        );
     239
     240                        bp_activity_set_action(
     241                                $this->groups_component,
     242                                $this->reply_create,
     243                                esc_html__( 'New forum reply', 'bbpress' ),
     244                                'bbp_bp_format_activity_action_new_reply',
     245                                __( 'Replies', 'bbpress' ),
     246                                array( 'activity', 'member', 'member_groups', 'group' )
     247                        );
     248                }
    201249        }
    202250
    203251        /**
  • src/includes/extend/buddypress/functions.php

    diff --git src/includes/extend/buddypress/functions.php src/includes/extend/buddypress/functions.php
    index e8731a8..9caaf03 100644
    function bbp_group_is_creator() { 
    712712        // Return the value
    713713        return (bool) $bbp->current_user->is_group_creator;
    714714}
     715
     716/* BuddyPress Activity Action Callbacks ***************************************/
     717
     718/**
     719 * Genereic function to format the dynamic BuddyPress activity action for new
     720 * topics/replies.
     721 *
     722 * @since 2.6.0 bbPress (rXXXX)
     723 *
     724 * @param string               $type     The type of post. Expects `topic` or `reply`.
     725 * @param string               $action   The current action string.
     726 * @param BP_Activity_Activity $activity The BuddyPress activity object.
     727 *
     728 * @return string The formatted activity action.
     729 */
     730function bbp_bp_format_activity_action_new_post( $type = '', $action, $activity ) {
     731
     732        // Bail early if we don't have a valid type.
     733        if ( ! in_array( $type, array( 'topic', 'reply' ), true ) ) {
     734                return $action;
     735        }
     736
     737        // Check if we're on the groups or bbpress component.
     738        if ( 'groups' === $activity->component ) {
     739                if ( 'topic' === $type ) {
     740                        $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
     741                        $forum_id = bbp_get_topic_forum_id( $topic_id );
     742                } else {
     743                        $topic_id = bbp_get_reply_topic_id( $activity->secondary_item_id );
     744                        $forum_id = bbp_get_topic_forum_id( $topic_id );
     745                }
     746        } else {
     747                if ( 'topic' === $type ) {
     748                        $topic_id = bbp_get_topic_id( $activity->item_id );
     749                        $forum_id = bbp_get_forum_id( $activity->secondary_item_id );
     750                } else {
     751                        $topic_id = bbp_get_topic_id( $activity->secondary_item_id );
     752                        $forum_id = bbp_get_topic_forum_id( $topic_id );
     753                }
     754        }
     755
     756        // Setup our post type args.
     757        $actions = array(
     758                'topic' => __( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ),
     759                'reply' => __( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ),
     760        );
     761
     762        // User link for topic author.
     763        $user_link = bbp_get_user_profile_link( $activity->user_id );
     764
     765        // Topic link.
     766        $topic_permalink = bbp_get_topic_permalink( $topic_id );
     767        $topic_title     = get_post_field( 'post_title', $topic_id, 'raw' );
     768        $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
     769
     770        // Forum link.
     771        $forum_permalink = bbp_get_forum_permalink( $forum_id );
     772        $forum_title     = get_post_field( 'post_title', $forum_id, 'raw' );
     773        $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
     774
     775        return sprintf( $actions[ $type ], $user_link, $topic_link, $forum_link );
     776}
     777
     778/**
     779 * Formats the dynamic BuddyPress activity action for new topics.
     780 *
     781 * @since 2.6.0 bbPress (rXXXX)
     782 *
     783 * @param string $action   The current action string.
     784 * @param object $activity The BuddyPress activity object.
     785 *
     786 * @return string The formatted activity action.
     787 */
     788function bbp_bp_format_activity_action_new_topic( $action, $activity ) {
     789
     790        $action = bbp_bp_format_activity_action_new_post( 'topic', $action, $activity );
     791
     792        /**
     793         * Filters the formatted activity action new topic string.
     794         *
     795         * @since 2.6.0 bbPress (rXXXX)
     796         *
     797         * @param string               $action   Activity action string value.
     798         * @param BP_Activity_Activity $activity Activity item object.
     799         */
     800        return apply_filters( 'bbp_bp_format_activity_action_new_topic', $action, $activity );
     801}
     802
     803/**
     804 * Formats the dynamic BuddyPress activity action for new replies.
     805 *
     806 * @since 2.6.0 bbPress (rXXXX)
     807 *
     808 * @param string $action   The current action string.
     809 * @param object $activity The BuddyPress activity object.
     810 *
     811 * @return string The formatted activity action.
     812 */
     813function bbp_bp_format_activity_action_new_reply( $action, $activity ) {
     814
     815        $action = bbp_bp_format_activity_action_new_post( 'reply', $action, $activity );
     816
     817        /**
     818         * Filters the formatted activity action new reply string.
     819         *
     820         * @since 2.6.0 bbPress (rXXXX)
     821         *
     822         * @param string               $action   Activity action string value.
     823         * @param BP_Activity_Activity $activity Activity item object.
     824         */
     825        return apply_filters( 'bbp_bp_format_activity_action_new_reply', $action, $activity );
     826}
  • new file tests/phpunit/testcases/extend/buddypress/activity.php

    diff --git tests/phpunit/testcases/extend/buddypress/activity.php tests/phpunit/testcases/extend/buddypress/activity.php
    new file mode 100644
    index 0000000..c0b747b
    - +  
     1<?php
     2
     3/**
     4 * BuddyPress Extension Activity Tests.
     5 *
     6 * @group extend
     7 * @group buddypress
     8 * @group activity
     9 */
     10class BBP_Tests_Extend_BuddyPress_Activity extends BBP_UnitTestCase {
     11
     12        /**
     13         * Copied from `BBP_Forums_Group_Extension::new_forum()`.
     14         *
     15         * @since x.x.x
     16         *
     17         * @param int $forum_id The forum id.
     18         * @param int $group_id The group id.
     19         */
     20        private function attach_forum_to_group( $forum_id, $group_id ) {
     21                bbp_add_forum_id_to_group( $group_id, $forum_id );
     22                bbp_add_group_id_to_forum( $forum_id, $group_id );
     23        }
     24
     25        /**
     26         * Dynamic activity actions for site-wide forum topics.
     27         *
     28         * @since 2.6.0 bbPress (rXXXX)
     29         *
     30         * @ticket BBP2794
     31         */
     32        public function test_bp_activity_actions_for_site_wide_forum_topic() {
     33                $u = $this->factory->user->create();
     34                $f = $this->factory->forum->create();
     35                $t = $this->factory->topic->create( array(
     36                        'post_parent' => $f,
     37                        'post_author' => $u,
     38                ) );
     39
     40                // Set up our activity text test string.
     41                $user_link       = bbp_get_user_profile_link( $u );
     42                $topic_permalink = bbp_get_topic_permalink( $t );
     43                $topic_title     = get_post_field( 'post_title',   $t, 'raw' );
     44                $topic_content   = get_post_field( 'post_content', $t, 'raw' );
     45                $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
     46                $forum_permalink = bbp_get_forum_permalink( $f );
     47                $forum_title     = get_post_field( 'post_title', $f, 'raw' );
     48                $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
     49                $activity_text   = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     50
     51                // Create the activity.
     52                bbpress()->extend->buddypress->activity->topic_create( $t, $f, array(), $u );
     53
     54                $activity_id = (int) get_post_meta( $t, '_bbp_activity_id', true );
     55                $activity    = new BP_Activity_Activity( $activity_id );
     56
     57                // Test the default generated string.
     58                $this->assertEquals( $activity_text, $activity->action );
     59
     60                // Update a few items for testing.
     61                wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) );
     62                $user_link = bbp_get_user_profile_link( $u );
     63
     64                wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) );
     65                $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>';
     66
     67                wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) );
     68                $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>';
     69
     70                // Set up our new test string.
     71                $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     72
     73                $activity = new BP_Activity_Activity( $activity_id );
     74
     75                // Are we dynamic?
     76                $this->assertEquals( $activity_text, $activity->action );
     77        }
     78
     79        /**
     80         * Dynamic activity actions for replies to site-wide forum topics.
     81         *
     82         * @since 2.6.0 bbPress (rXXXX)
     83         *
     84         * @ticket BBP2794
     85         */
     86        public function test_bp_activity_actions_for_reply_to_site_wide_forum_topic() {
     87                $u = $this->factory->user->create();
     88                $f = $this->factory->forum->create();
     89                $t = $this->factory->topic->create( array(
     90                        'post_parent' => $f,
     91                        'post_author' => $u,
     92                ) );
     93                $r = $this->factory->reply->create( array(
     94                        'post_parent' => $t,
     95                        'post_author' => $u,
     96                ) );
     97
     98                // Set up our activity text test string.
     99                $user_link       = bbp_get_user_profile_link( $u );
     100                $topic_permalink = bbp_get_topic_permalink( $t );
     101                $topic_title     = get_post_field( 'post_title',   $t, 'raw' );
     102                $topic_content   = get_post_field( 'post_content', $t, 'raw' );
     103                $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
     104                $forum_permalink = bbp_get_forum_permalink( $f );
     105                $forum_title     = get_post_field( 'post_title', $f, 'raw' );
     106                $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
     107                $activity_text   = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     108
     109                // Create the activity.
     110                bbpress()->extend->buddypress->activity->reply_create( $r, $t, $f, array(), $u );
     111
     112                $activity_id = (int) get_post_meta( $r, '_bbp_activity_id', true );
     113                $activity    = new BP_Activity_Activity( $activity_id );
     114
     115                // Test the default generated string.
     116                $this->assertEquals( $activity_text, $activity->action );
     117
     118                // Update a few items for testing.
     119                wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) );
     120                $user_link = bbp_get_user_profile_link( $u );
     121
     122                wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) );
     123                $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>';
     124
     125                wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) );
     126                $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>';
     127
     128                // Set up our new test string.
     129                $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     130
     131                $activity = new BP_Activity_Activity( $activity_id );
     132
     133                // Are we dynamic?
     134                $this->assertEquals( $activity_text, $activity->action );
     135        }
     136
     137        /**
     138         * Dynamic activity actions for group forum topics.
     139         *
     140         * @since 2.6.0 bbPress (rXXXX)
     141         *
     142         * @ticket BBP2794
     143         */
     144        public function test_bp_activity_actions_for_group_forum_topic() {
     145                $g = $this->bp_factory->group->create();
     146                $group = groups_get_group( array( 'group_id' => $g ) );
     147                $u = $group->creator_id;
     148                $f = $this->factory->forum->create();
     149                $t = $this->factory->topic->create( array(
     150                        'post_parent' => $f,
     151                        'post_author' => $u,
     152                ) );
     153                $r = $this->factory->reply->create( array(
     154                        'post_parent' => $t,
     155                        'post_author' => $u,
     156                ) );
     157                $this->attach_forum_to_group( $f, $g );
     158                buddypress()->groups->current_group = $group;
     159
     160                // Set up our activity text test string.
     161                $user_link       = bbp_get_user_profile_link( $u );
     162                $topic_permalink = bbp_get_topic_permalink( $t );
     163                $topic_title     = get_post_field( 'post_title', $t, 'raw' );
     164                $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
     165                $forum_permalink = bbp_get_forum_permalink( $f );
     166                $forum_title     = get_post_field( 'post_title', $f, 'raw' );
     167                $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
     168                $activity_text   = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     169
     170                // Create the activity.
     171                bbpress()->extend->buddypress->activity->topic_create( $t, $f, array(), $u );
     172
     173                $activity_id = (int) get_post_meta( $t, '_bbp_activity_id', true );
     174                $activity    = new BP_Activity_Activity( $activity_id );
     175
     176                // Test the default generated string.
     177                $this->assertEquals( $activity_text, $activity->action );
     178
     179                // Update a few items for testing.
     180                wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) );
     181                $user_link = bbp_get_user_profile_link( $u );
     182
     183                wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) );
     184                $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>';
     185
     186                wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) );
     187                $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>';
     188
     189                // Set up our new test string.
     190                $activity_text = sprintf( esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     191
     192                $activity = new BP_Activity_Activity( $activity_id );
     193
     194                // Are we dynamic?
     195                $this->assertEquals( $activity_text, $activity->action );
     196        }
     197
     198        /**
     199         * Dynamic activity actions for replies to group forum topics.
     200         *
     201         * @since 2.6.0 bbPress (rXXXX)
     202         *
     203         * @ticket BBP2794
     204         */
     205        public function test_bp_activity_actions_for_reply_to_group_forum_topic() {
     206                $g = $this->bp_factory->group->create();
     207                $group = groups_get_group( array( 'group_id' => $g ) );
     208                $u = $group->creator_id;
     209                $f = $this->factory->forum->create();
     210                $t = $this->factory->topic->create( array(
     211                        'post_parent' => $f,
     212                        'post_author' => $u,
     213                ) );
     214                $r = $this->factory->reply->create( array(
     215                        'post_parent' => $t,
     216                        'post_author' => $u,
     217                ) );
     218                $this->attach_forum_to_group( $f, $g );
     219                buddypress()->groups->current_group = $group;
     220
     221                // Set up our activity text test string.
     222                $user_link       = bbp_get_user_profile_link( $u );
     223                $topic_permalink = bbp_get_topic_permalink( $t );
     224                $topic_title     = get_post_field( 'post_title',   $t, 'raw' );
     225                $topic_content   = get_post_field( 'post_content', $t, 'raw' );
     226                $topic_link      = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
     227                $forum_permalink = bbp_get_forum_permalink( $f );
     228                $forum_title     = get_post_field( 'post_title', $f, 'raw' );
     229                $forum_link      = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
     230                $activity_text   = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     231
     232                // Create the activity.
     233                bbpress()->extend->buddypress->activity->reply_create( $r, $t, $f, array(), $u );
     234
     235                $activity_id = (int) get_post_meta( $r, '_bbp_activity_id', true );
     236                $activity    = new BP_Activity_Activity( $activity_id );
     237
     238                // Test the default generated string.
     239                $this->assertEquals( $activity_text, $activity->action );
     240
     241                // Update a few items for testing.
     242                wp_update_user( array( 'ID' => $u, 'display_name' => 'New Name' ) );
     243                $user_link = bbp_get_user_profile_link( $u );
     244
     245                wp_update_post( array( 'ID' => $f, 'post_title' => 'New Forum Title' ) );
     246                $forum_link = '<a href="' . $forum_permalink . '">New Forum Title</a>';
     247
     248                wp_update_post( array( 'ID' => $t, 'post_title' => 'New Topic Title' ) );
     249                $topic_link = '<a href="' . $topic_permalink . '">New Topic Title</a>';
     250
     251                // Set up our new test string.
     252                $activity_text = sprintf( esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
     253
     254                $activity = new BP_Activity_Activity( $activity_id );
     255
     256                // Are we dynamic?
     257                $this->assertEquals( $activity_text, $activity->action );
     258        }
     259}