Skip to:
Content

bbPress.org

Changeset 7512


Ignore:
Timestamp:
09/14/2026 07:40:44 PM (2 months ago)
Author:
johnjamesjacoby
Message:

Topics: Validate converted topic titles.

Apply the standard topic-title filter and validation before converting replies into topics through reply moves or topic splits.

This keeps these alternate creation paths consistent with the normal new-topic handler.

This is a backport of [7511] to the 2.6 branch.

In branches/2.6, for 2.6.16.

Props rafiem.

Location:
branches/2.6
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/CHANGELOG.md

    r7510 r7512  
    3535### Security
    3636
     37- Validated and filtered topic titles when converting replies or splitting
     38  topics.
    3739- Prevented subscription emails from disclosing content to users who can no
    3840  longer access a restricted forum.
  • branches/2.6/src/includes/replies/functions.php

    r7510 r7512  
    13731373                                        }
    13741374
     1375                                        // Filter the new topic title
     1376                                        $destination_topic_title = apply_filters( 'bbp_new_topic_pre_title', $destination_topic_title );
     1377
     1378                                        // Title cannot be empty
     1379                                        if ( empty( $destination_topic_title ) ) {
     1380                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
     1381                                        }
     1382
     1383                                        // Title too long
     1384                                        if ( bbp_is_title_too_long( $destination_topic_title ) ) {
     1385                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
     1386                                        }
     1387
     1388                                        // Bail before converting the reply if there are errors
     1389                                        if ( bbp_has_errors() ) {
     1390                                                break;
     1391                                        }
     1392
    13751393                                        // Update the topic
    13761394                                        $destination_topic_id = wp_update_post(
  • branches/2.6/src/includes/topics/functions.php

    r7510 r7512  
    14801480                                        }
    14811481
     1482                                        // Filter the new topic title
     1483                                        $destination_topic_title = apply_filters( 'bbp_new_topic_pre_title', $destination_topic_title );
     1484
     1485                                        // Title cannot be empty
     1486                                        if ( empty( $destination_topic_title ) ) {
     1487                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
     1488                                        }
     1489
     1490                                        // Title too long
     1491                                        if ( bbp_is_title_too_long( $destination_topic_title ) ) {
     1492                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
     1493                                        }
     1494
     1495                                        // Bail before converting the reply if there are errors
     1496                                        if ( bbp_has_errors() ) {
     1497                                                break;
     1498                                        }
     1499
    14821500                                        // Update the topic
    14831501                                        $destination_topic_id = wp_update_post(
  • branches/2.6/tests/phpunit/testcases/replies/functions/permissions.php

    r7510 r7512  
    145145        }
    146146
    147         protected function submit_reply_move( $reply_id, $move_option, $destination_topic_id = 0 ) {
     147        protected function submit_reply_move( $reply_id, $move_option, $destination_topic_id = 0, $destination_title = null ) {
    148148                $home_url             = wp_parse_url( home_url( '/' ) );
    149149                $_SERVER['HTTP_HOST'] = $home_url['host'];
    … …  
    158158                $_POST['bbp_destination_topic']     = $destination_topic_id;
    159159                $_REQUEST['_wpnonce']               = wp_create_nonce( 'bbp-move-reply_' . $reply_id );
     160
     161                if ( null !== $destination_title ) {
     162                        $_POST['bbp_reply_move_destination_title'] = $destination_title;
     163                }
    160164
    161165                $did_redirect     = false;
    … …  
    346350                bbpress()->errors = new WP_Error();
    347351
    348                 $did_redirect = $this->submit_reply_move( $reply_id, 'topic' );
     352                $filter_title = function( $title ) {
     353                        return $title . ' filtered';
     354                };
     355
     356                add_filter( 'bbp_new_topic_pre_title', $filter_title );
     357                $did_redirect = $this->submit_reply_move( $reply_id, 'topic', 0, 'Converted reply' );
     358                remove_filter( 'bbp_new_topic_pre_title', $filter_title );
    349359
    350360                $this->assertSame( array(), bbpress()->errors->get_error_codes() );
    351361                $this->assertSame( bbp_get_topic_post_type(), get_post_type( $reply_id ) );
     362                $this->assertSame( 'Converted reply filtered', get_post_field( 'post_title', $reply_id ) );
    352363                $this->assertSame( $forum_id, wp_get_post_parent_id( $reply_id ) );
    353364                $this->assertSame( $reply_id, (int) get_post_meta( $reply_id, '_bbp_topic_id', true ) );
    354365                $this->assertTrue( $did_redirect );
     366        }
     367
     368        /**
     369         * @covers ::bbp_move_reply_handler
     370         */
     371        public function test_participant_cannot_convert_reply_to_topic_with_long_title() {
     372                $user_id         = $this->factory->user->create( array( 'role' => bbp_get_participant_role() ) );
     373                $forum_id        = $this->factory->forum->create();
     374                $source_topic_id = $this->factory->topic->create(
     375                        array(
     376                                'post_author' => $user_id,
     377                                'post_parent' => $forum_id,
     378                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     379                        )
     380                );
     381                $reply_id        = $this->factory->reply->create(
     382                        array(
     383                                'post_author' => $user_id,
     384                                'post_parent' => $source_topic_id,
     385                                'reply_meta'  => array(
     386                                        'forum_id' => $forum_id,
     387                                        'topic_id' => $source_topic_id,
     388                                ),
     389                        )
     390                );
     391
     392                $this->set_current_user( $user_id );
     393                bbpress()->errors = new WP_Error();
     394
     395                $did_redirect = $this->submit_reply_move( $reply_id, 'topic', 0, str_repeat( 'a', bbp_get_title_max_length() + 1 ) );
     396
     397                $this->assertContains( 'bbp_topic_title', bbpress()->errors->get_error_codes() );
     398                $this->assertSame( bbp_get_reply_post_type(), get_post_type( $reply_id ) );
     399                $this->assertSame( $source_topic_id, wp_get_post_parent_id( $reply_id ) );
     400                $this->assertSame( $source_topic_id, bbp_get_reply_topic_id( $reply_id ) );
     401                $this->assertFalse( $did_redirect );
    355402        }
    356403
  • branches/2.6/tests/phpunit/testcases/topics/functions/permissions.php

    r7488 r7512  
    3333        }
    3434
    35         protected function submit_topic_split( $reply_id, $source_topic_id, $split_option ) {
     35        protected function submit_topic_split( $reply_id, $source_topic_id, $split_option, $destination_title = null ) {
    3636                $home_url             = wp_parse_url( home_url( '/' ) );
    3737                $_SERVER['HTTP_HOST'] = $home_url['host'];
    … …  
    4545                $_POST['bbp_topic_split_option'] = $split_option;
    4646                $_REQUEST['_wpnonce']           = wp_create_nonce( 'bbp-split-topic_' . $source_topic_id );
     47
     48                if ( null !== $destination_title ) {
     49                        $_POST['bbp_topic_split_destination_title'] = $destination_title;
     50                }
    4751
    4852                $did_redirect     = false;
    … …  
    135139                bbpress()->errors = new WP_Error();
    136140
    137                 $did_redirect = $this->submit_topic_split( $reply_id, $source_topic_id, 'reply' );
     141                $filter_title = function( $title ) {
     142                        return $title . ' filtered';
     143                };
     144
     145                add_filter( 'bbp_new_topic_pre_title', $filter_title );
     146                $did_redirect = $this->submit_topic_split( $reply_id, $source_topic_id, 'reply', 'Split topic' );
     147                remove_filter( 'bbp_new_topic_pre_title', $filter_title );
    138148
    139149                $this->assertSame( array(), bbpress()->errors->get_error_codes() );
    140150                $this->assertSame( bbp_get_topic_post_type(), get_post_type( $reply_id ) );
     151                $this->assertSame( 'Split topic filtered', get_post_field( 'post_title', $reply_id ) );
    141152                $this->assertSame( $forum_id, wp_get_post_parent_id( $reply_id ) );
    142153                $this->assertSame( $reply_id, (int) get_post_meta( $reply_id, '_bbp_topic_id', true ) );
    143154                $this->assertTrue( $did_redirect );
    144155        }
     156
     157        /**
     158         * @covers ::bbp_split_topic_handler
     159         */
     160        public function test_moderator_cannot_convert_split_reply_to_topic_with_long_title() {
     161                $user_id         = $this->factory->user->create();
     162                $other_user_id   = $this->factory->user->create( array( 'role' => bbp_get_participant_role() ) );
     163                $forum_id        = $this->factory->forum->create();
     164                $source_topic_id = $this->factory->topic->create(
     165                        array(
     166                                'post_author' => $other_user_id,
     167                                'post_parent' => $forum_id,
     168                                'topic_meta'  => array( 'forum_id' => $forum_id ),
     169                        )
     170                );
     171                $reply_id        = $this->factory->reply->create(
     172                        array(
     173                                'post_author' => $other_user_id,
     174                                'post_parent' => $source_topic_id,
     175                                'reply_meta'  => array(
     176                                        'forum_id' => $forum_id,
     177                                        'topic_id' => $source_topic_id,
     178                                ),
     179                        )
     180                );
     181
     182                bbp_set_user_role( $user_id, bbp_get_moderator_role() );
     183                $this->set_current_user( $user_id );
     184                bbpress()->errors = new WP_Error();
     185
     186                $did_redirect = $this->submit_topic_split( $reply_id, $source_topic_id, 'reply', str_repeat( 'a', bbp_get_title_max_length() + 1 ) );
     187
     188                $this->assertContains( 'bbp_topic_title', bbpress()->errors->get_error_codes() );
     189                $this->assertSame( bbp_get_reply_post_type(), get_post_type( $reply_id ) );
     190                $this->assertSame( $source_topic_id, wp_get_post_parent_id( $reply_id ) );
     191                $this->assertSame( $source_topic_id, bbp_get_reply_topic_id( $reply_id ) );
     192                $this->assertFalse( $did_redirect );
     193        }
    145194}
Note: See TracChangeset for help on using the changeset viewer.