Skip to:
Content

BuddyPress.org

Changeset 13778


Ignore:
Timestamp:
03/24/2024 07:47:58 AM (2 years ago)
Author:
imath
Message:

Improve the page / buddypress post types slugs unicity checks

To avoid slug conflicts between the page & buddypress post types as both URI Schemas put their slugs right after the site domain, 12.0.0 introduced a filter to extend the WordPress built-in check performed by wp_unique_post_slug().

This filter needed to be improve to :

  • make sure to allow WordPress sub-pages to have the same slugs than the BP Directory ones as in this case the URI Schemas puts the parent page slug before the sub-page one.
  • only request the page or buddypress post types having the same post_name than the page or buddypress post type being inserted.

Props ahegyes, emaralive

Fixes #9086 (branch 12.0)

Location:
branches/12.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/12.0/src/bp-core/bp-core-functions.php

    r13745 r13778  
    999999 */
    10001000function bp_core_set_unique_directory_page_slug( $slug = '', $post_ID = 0, $post_status = '', $post_type = '', $post_parent = 0, $original_slug = '' ) {
    1001     if ( ( 'buddypress' === $post_type || 'page' === $post_type ) && $slug === $original_slug ) {
     1001    if ( ( 'buddypress' === $post_type || 'page' === $post_type ) && $slug === $original_slug && ! $post_parent ) {
    10021002        $pages = get_posts(
    10031003            array(
     
    10051005                'post_status'  => bp_core_get_directory_pages_stati(),
    10061006                'post_type'    => array( 'buddypress', 'page' ),
     1007                'post_parent'  => 0,     // Only get a top level page.
     1008                'name'         => $slug, // Only get the same name page.
    10071009            )
    10081010        );
  • branches/12.0/tests/phpunit/testcases/admin/functions.php

    r13314 r13778  
    245245        }
    246246    }
     247
     248    public function is_active_filter( $is_active, $component ) {
     249        if ( ! $is_active && 'attachments' === $component ) {
     250            $is_active = true;
     251        }
     252
     253        return $is_active;
     254    }
     255
     256    /**
     257     * @group bp_core_set_unique_directory_page_slug
     258     * @ticket BP9086
     259     */
     260    public function test_bp_core_set_unique_directory_page_slug_for_newpage() {
     261        $members_directory_id  = bp_core_get_directory_page_id( 'members' );
     262        $directory_name        = get_post_field( 'post_name', $members_directory_id );
     263        $page_id               = self::factory()->post->create(
     264            array(
     265                'post_type' => 'page',
     266                'post_name' => 'members'
     267            )
     268        );
     269
     270        $page_name = get_post_field( 'post_name', $page_id );
     271
     272        $this->assertNotSame( $page_name, $directory_name );
     273    }
     274
     275    /**
     276     * @group bp_core_set_unique_directory_page_slug
     277     * @ticket BP9086
     278     */
     279    public function test_bp_core_set_unique_directory_page_slug_for_newpage_having_parent() {
     280        $members_directory_id = bp_core_get_directory_page_id( 'members' );
     281        $directory_name       = get_post_field( 'post_name', $members_directory_id );
     282        $parent               = self::factory()->post->create(
     283            array(
     284                'post_type'   => 'page',
     285                'post_name'   => 'parent',
     286            )
     287        );
     288        $page_id              = self::factory()->post->create(
     289            array(
     290                'post_type'   => 'page',
     291                'post_name'   => 'members',
     292                'post_parent' => $parent,
     293            )
     294        );
     295
     296        $page_name = get_post_field( 'post_name', $page_id );
     297
     298        $this->assertSame( $page_name, $directory_name );
     299    }
     300
     301    /**
     302     * @group bp_core_set_unique_directory_page_slug
     303     * @ticket BP9086
     304     */
     305    public function test_bp_core_set_unique_directory_page_slug_for_newcomponent() {
     306        self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
     307        $page_id = self::factory()->post->create(
     308            array(
     309                'post_type' => 'page',
     310                'post_name' => 'bp-attachments',
     311            )
     312        );
     313
     314        $page_name          = get_post_field( 'post_name', $page_id );
     315        $orphaned_component = array(
     316            'attachments' => array(
     317                'name'  => 'bp-attachments',
     318                'title' => 'Community Media',
     319            )
     320        );
     321
     322        add_filter( 'bp_is_active', array( $this, 'is_active_filter' ), 10, 2 );
     323
     324        bp_core_add_page_mappings( $orphaned_component, 'keep', true );
     325
     326        $attachments_directory_id = bp_core_get_directory_page_id( 'attachments' );
     327        $directory_name           = get_post_field( 'post_name', $attachments_directory_id );
     328
     329        $this->assertNotSame( $page_name, $directory_name );
     330
     331        remove_filter( 'bp_is_active', array( $this, 'is_active_filter' ), 10 );
     332    }
     333
     334    /**
     335     * @group bp_core_set_unique_directory_page_slug
     336     * @ticket BP9086
     337     */
     338    public function test_bp_core_set_unique_directory_page_slug_having_parent_for_newcomponent() {
     339        $parent  = self::factory()->post->create(
     340            array(
     341                'post_type'   => 'page',
     342                'post_name'   => 'parent',
     343            )
     344        );
     345        $page_id = self::factory()->post->create(
     346            array(
     347                'post_type'   => 'page',
     348                'post_name'   => 'bp-attachments',
     349                'post_parent' => $parent,
     350            )
     351        );
     352
     353        $page_name          = get_post_field( 'post_name', $page_id );
     354        $orphaned_component = array(
     355            'attachments' => array(
     356                'name'  => 'bp-attachments',
     357                'title' => 'Community Media',
     358            )
     359        );
     360
     361        add_filter( 'bp_is_active', array( $this, 'is_active_filter' ), 10, 2 );
     362
     363        bp_core_add_page_mappings( $orphaned_component, 'keep', true );
     364
     365        $attachments_directory_id = bp_core_get_directory_page_id( 'attachments' );
     366        $directory_name           = get_post_field( 'post_name', $attachments_directory_id );
     367
     368        $this->assertSame( $page_name, $directory_name );
     369
     370        remove_filter( 'bp_is_active', array( $this, 'is_active_filter' ), 10 );
     371    }
    247372}
Note: See TracChangeset for help on using the changeset viewer.