Skip to:
Content

bbPress.org

Changeset 7500


Ignore:
Timestamp:
09/13/2026 01:14:02 AM (8 days ago)
Author:
johnjamesjacoby
Message:

Canonical: Prevent permalink guessing for bbPress post types.

Prevent WordPress from guessing canonical permalinks for bbPress post types, because forum visibility may restrict access to the matched content. Extend bbp_is_custom_post_type() to accept post-type names and arrays while preserving its existing post object and ID behavior.

In branches/2.6, for 2.6.16.

Props foobar7.

Location:
branches/2.6
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/src/includes/common/functions.php

    r7494 r7500  
    28402840
    28412841/**
     2842 * Prevent WordPress from guessing permalinks for bbPress post types.
     2843 *
     2844 * bbPress post types are excluded from search because their visibility depends
     2845 * on forum access. Older versions of WordPress may otherwise guess a restricted
     2846 * forum or topic permalink from a partial slug and expose its full title.
     2847 *
     2848 * @since 2.6.16 bbPress
     2849 *
     2850 * @param bool $do_redirect_guess Whether to attempt to guess a redirect URL.
     2851 *
     2852 * @return bool Whether to attempt to guess a redirect URL.
     2853 */
     2854function bbp_do_not_guess_404_permalink( $do_redirect_guess = true ) {
     2855        $post_types = (array) get_query_var( 'post_type' );
     2856
     2857        if ( bbp_is_custom_post_type( $post_types ) ) {
     2858                $do_redirect_guess = false;
     2859        }
     2860
     2861        return $do_redirect_guess;
     2862}
     2863
     2864/**
    28422865 * Sets the 404 status.
    28432866 *
  • branches/2.6/src/includes/common/template.php

    r7436 r7500  
    419419
    420420/**
    421  * Check if the current post type is one that comes with bbPress
     421 * Check if the current post type is one that comes with bbPress.
    422422 *
    423423 * @since 2.0.0 bbPress (r3311)
    424  *
    425  * @param mixed $the_post Optional. Post object or post ID.
     424 * @since 2.6.16 bbPress Added support for post-type names and arrays.
     425 *
     426 * @param mixed $post_types Optional. Post object, post ID, post-type name, or
     427 *                          an array of post-type names.
    426428 *
    427429 * @return bool
    428430 */
    429 function bbp_is_custom_post_type( $the_post = false ) {
    430 
    431         // Assume false
    432         $retval = false;
    433 
    434         // Viewing one of the bbPress post types
    435         if ( in_array( get_post_type( $the_post ),
    436                 array(
    437                         bbp_get_forum_post_type(),
    438                         bbp_get_topic_post_type(),
    439                         bbp_get_reply_post_type()
    440                 ),
    441                 true
    442         ) ) {
    443                 $retval = true;
    444         }
    445 
    446         // Filter & return
    447         return (bool) apply_filters( 'bbp_is_custom_post_type', $retval, $the_post );
     431function bbp_is_custom_post_type( $post_types = false ) {
     432        $original_post_types = $post_types;
     433
     434        // Preserve the existing scalar post object or ID behavior
     435        if ( ! is_array( $post_types ) ) {
     436                $post_type = get_post_type( $post_types );
     437
     438                // Use a supplied post-type name if it is not a post object or ID
     439                $post_types = empty( $post_type ) && is_string( $post_types )
     440                        ? array( $post_types )
     441                        : array( $post_type );
     442        }
     443
     444        // Compare post types
     445        $post_types     = array_filter( $post_types, 'is_string' );
     446        $bbp_post_types = bbp_get_post_types();
     447        $retval         = ! empty( array_intersect( $post_types, $bbp_post_types ) );
     448
     449        // Filter & return
     450        return (bool) apply_filters( 'bbp_is_custom_post_type', $retval, $original_post_types );
    448451}
    449452
  • branches/2.6/src/includes/core/filters.php

    r7490 r7500  
    6767
    6868// Avoid queries & 404s
    69 add_filter( 'pre_handle_404',  'bbp_pre_handle_404',  10, 2 );
    70 add_action( 'posts_pre_query', 'bbp_posts_pre_query', 10, 2 );
     69add_filter( 'do_redirect_guess_404_permalink', 'bbp_do_not_guess_404_permalink' );
     70add_filter( 'pre_handle_404',                  'bbp_pre_handle_404',              10, 2 );
     71add_action( 'posts_pre_query',                 'bbp_posts_pre_query',             10, 2 );
    7172
    7273// User Creation
  • branches/2.6/tests/phpunit/testcases/common/functions.php

    r7494 r7500  
    14581458
    14591459        /**
     1460         * @covers ::bbp_is_custom_post_type
     1461         */
     1462        public function test_bbp_is_custom_post_type() {
     1463                $forum_id      = $this->factory->forum->create();
     1464                $forum_object  = (object) get_post( $forum_id )->to_array();
     1465                $original_post = isset( $GLOBALS['post'] )
     1466                        ? $GLOBALS['post']
     1467                        : null;
     1468
     1469                try {
     1470                        $GLOBALS['post'] = get_post( $forum_id );
     1471                        $this->assertTrue( bbp_is_custom_post_type() );
     1472                        $this->assertTrue( bbp_is_custom_post_type( $forum_id ) );
     1473                        $this->assertTrue( bbp_is_custom_post_type( (float) $forum_id ) );
     1474                        $this->assertTrue( bbp_is_custom_post_type( (string) $forum_id ) );
     1475                        $this->assertTrue( bbp_is_custom_post_type( get_post( $forum_id ) ) );
     1476                        $this->assertTrue( bbp_is_custom_post_type( $forum_object ) );
     1477                        $this->assertTrue( bbp_is_custom_post_type( bbp_get_forum_post_type() ) );
     1478                        $this->assertTrue( bbp_is_custom_post_type( array( 'post', bbp_get_topic_post_type() ) ) );
     1479                        $this->assertFalse( bbp_is_custom_post_type( 'post' ) );
     1480                        $this->assertFalse( bbp_is_custom_post_type( array( 'post', 'page' ) ) );
     1481                        $this->assertFalse( bbp_is_custom_post_type( array() ) );
     1482                        $this->assertFalse( bbp_is_custom_post_type( array( true, new stdClass() ) ) );
     1483                        $this->assertTrue( bbp_is_custom_post_type( array( true, new stdClass(), bbp_get_reply_post_type() ) ) );
     1484                } finally {
     1485                        $GLOBALS['post'] = $original_post;
     1486                }
     1487        }
     1488
     1489        /**
     1490         * @covers ::bbp_do_not_guess_404_permalink
     1491         */
     1492        public function test_bbp_do_not_guess_404_permalink() {
     1493                $original_post_type = get_query_var( 'post_type' );
     1494
     1495                set_query_var( 'post_type', 'post' );
     1496                $this->assertTrue( bbp_do_not_guess_404_permalink( true ) );
     1497                $this->assertFalse( bbp_do_not_guess_404_permalink( false ) );
     1498
     1499                foreach ( bbp_get_post_types() as $post_type ) {
     1500                        set_query_var( 'post_type', $post_type );
     1501                        $this->assertFalse( apply_filters( 'do_redirect_guess_404_permalink', true ) );
     1502                }
     1503
     1504                set_query_var( 'post_type', array( 'post', bbp_get_topic_post_type() ) );
     1505                $this->assertFalse( bbp_do_not_guess_404_permalink( true ) );
     1506
     1507                set_query_var( 'post_type', $original_post_type );
     1508        }
     1509
     1510        /**
    14601511         * @covers ::bbp_set_404
    14611512         * @todo   Implement test_bbp_set_404().
Note: See TracChangeset for help on using the changeset viewer.