Skip to:
Content

bbPress.org

Changeset 7511


Ignore:
Timestamp:
09/14/2026 07:40:20 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.

In trunk, for 2.7.

Props rafiem.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.md

    r7509 r7511  
    5151### Security
    5252
     53- Validated and filtered topic titles when converting replies or splitting
     54  topics.
    5355- Prevented subscription emails from disclosing content to users who can no
    5456  longer access a restricted forum.
  • trunk/src/includes/replies/functions.php

    r7509 r7511  
    14061406                                        }
    14071407
     1408                                        // Filter the new topic title
     1409                                        $destination_topic_title = apply_filters( 'bbp_new_topic_pre_title', $destination_topic_title );
     1410
     1411                                        // Title cannot be empty
     1412                                        if ( empty( $destination_topic_title ) ) {
     1413                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
     1414                                        }
     1415
     1416                                        // Title too long
     1417                                        if ( bbp_is_title_too_long( $destination_topic_title ) ) {
     1418                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
     1419                                        }
     1420
     1421                                        // Bail before converting the reply if there are errors
     1422                                        if ( bbp_has_errors() ) {
     1423                                                break;
     1424                                        }
     1425
    14081426                                        // Update the topic
    14091427                                        $destination_topic_id = wp_update_post(
  • trunk/src/includes/topics/functions.php

    r7509 r7511  
    15151515                                        }
    15161516
     1517                                        // Filter the new topic title
     1518                                        $destination_topic_title = apply_filters( 'bbp_new_topic_pre_title', $destination_topic_title );
     1519
     1520                                        // Title cannot be empty
     1521                                        if ( empty( $destination_topic_title ) ) {
     1522                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your topic needs a title.', 'bbpress' ) );
     1523                                        }
     1524
     1525                                        // Title too long
     1526                                        if ( bbp_is_title_too_long( $destination_topic_title ) ) {
     1527                                                bbp_add_error( 'bbp_topic_title', __( '<strong>Error</strong>: Your title is too long.', 'bbpress' ) );
     1528                                        }
     1529
     1530                                        // Bail before converting the reply if there are errors
     1531                                        if ( bbp_has_errors() ) {
     1532                                                break;
     1533                                        }
     1534
    15171535                                        // Update the topic
    15181536                                        $destination_topic_id = wp_update_post(
  • trunk/tests/phpunit/testcases/replies/functions/permissions.php

    r7509 r7511  
    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
  • trunk/tests/phpunit/testcases/topics/functions/permissions.php

    r7487 r7511  
    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.