Skip to:
Content

BuddyPress.org

Changeset 13159


Ignore:
Timestamp:
12/04/2021 02:53:34 PM (3 years ago)
Author:
imath
Message:

Make sure BP directory page IDs are not wrongly unset on multisite

In case a post of a different site of the multisite network has the same ID than one of the BP directory page IDs, the function can wrongly return an array in which the corresponding directory page ID is missing.

We need to make sure this function always run into the BP Root Blog context.

Props thomaslhotta

Fixes #8592

Location:
trunk
Files:
2 edited

Legend:

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

    r13147 r13159  
    503503 *
    504504 * @since 1.5.0
     505 * @since 10.0.0 Eventually switch the current site to BP root's one on multisite configs.
    505506 *
    506507 * @param string $status 'active' to return only pages associated with active components, 'all' to return all saved
     
    512513function bp_core_get_directory_page_ids( $status = 'active' ) {
    513514    $page_ids = bp_get_option( 'bp-pages', array() );
     515    $switched = false;
     516
     517    /*
     518     * Make sure to switch the current site to BP root's one, if needed.
     519     *
     520     * @see https://buddypress.trac.wordpress.org/ticket/8592
     521     */
     522    if ( is_multisite() ) {
     523        $bp_site_id = bp_get_root_blog_id();
     524
     525        if ( $bp_site_id !== get_current_blog_id() ) {
     526            switch_to_blog( $bp_site_id );
     527            $switched = true;
     528        }
     529    }
    514530
    515531    // Loop through pages.
     
    535551            unset( $page_ids[ $component_name ] );
    536552        }
     553    }
     554
     555    if ( true === $switched ) {
     556        restore_current_blog();
    537557    }
    538558
  • trunk/tests/phpunit/testcases/core/functions/bpCoreGetDirectoryPageIds.php

    r13133 r13159  
    286286
    287287    /**
     288     * @ticket BP8592
     289     */
     290    public function test_bp_core_get_directory_pages_ids_ms_non_root_blog_trashed_same_page_id() {
     291        if ( ! is_multisite() ) {
     292            $this->markTestSkipped();
     293        }
     294
     295        // create a blog
     296        $u  = self::factory()->user->create();
     297        $b1 = self::factory()->blog->create(
     298            array(
     299                'user_id' => $u,
     300            )
     301        );
     302
     303        // Switch to blog and create a post with the same BP Members page ID.
     304        switch_to_blog( $b1 );
     305
     306        $dir_page_ids = bp_core_get_directory_page_ids();
     307
     308        $p = self::factory()->post->create(
     309            array(
     310                'import_id' => $dir_page_ids['members'],
     311            )
     312        );
     313
     314        // Trash the post that matches the BP Members page ID on this sub-site
     315        wp_trash_post( $dir_page_ids['members'] );
     316
     317        // refetch BP directory page IDs
     318        $page_ids = bp_core_get_directory_page_ids();
     319
     320        // restore blog
     321        restore_current_blog();
     322
     323        // Now verify that our BP Members page was not wiped out
     324        $this->assertTrue( $dir_page_ids['members'] === $page_ids['members'] );
     325    }
     326
     327    /**
    288328     * @ticket BP7193
    289329     */
Note: See TracChangeset for help on using the changeset viewer.