Skip to:
Content

BuddyPress.org

Changeset 9609


Ignore:
Timestamp:
03/10/2015 03:24:04 PM (5 years ago)
Author:
boonebgorges
Message:

bp_core_get_directory_page_ids() should skip bp_is_active() check in save context.

In [9177], a bp_is_active() check was added to bp_core_get_directory_page_ids()
such that the function would not return page mappings for inactive components.
This change caused problems for custom components, as the bp_is_active()
check didn't always pass for components that didn't manually set themselves in
the active_components array. [9553] and [9555] addressed this problem by only
filtering out packaged components. See #6244. But this fix was not truly
general, as it still resulted in the deletion of bp-pages entries related to
third-party deactivated components when saving the page mapping settings.

This changeset introduces a more general fix, by distinguishing between two
different use "contexts" of bp_core_get_directory_page_ids():

  • Read-only - When pulling up page IDs for display or for URI parsing, deactivated components should be ignored.
  • Save - When modifying the bp-pages array stored in the database, we should be working with raw page data, including deactivated components.

The new $status parameter for bp_core_get_directory_page_ids() addresses
this distinction (possible values 'all' and 'active'). We then pass 'all' as
required in BP - namely, when modifying the bp-pages setting. This change
supercedes the fixes from #6244.

Props dtc7240, boonebgorges.
Fixes #6280.

Location:
branches/2.2
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/src/bp-core/admin/bp-core-components.php

    r9553 r9609  
    300300    }
    301301
    302     $current_components       = buddypress()->active_components;
    303     $packaged_components      = array_flip( bp_core_get_packaged_component_ids() );
    304     $custom_active_components = array_diff_key( $current_components, $packaged_components );
     302    $current_components = buddypress()->active_components;
    305303
    306304    switch ( $current_action ) {
     
    310308                if ( ! isset( $submitted[ $retired_component ] ) ) {
    311309                    unset( $current_components[ $retired_component ] );
    312 
    313                     // Make sure custom components does not contain a retired component
    314                     if ( isset( $custom_active_components[ $retired_component ] ) ) {
    315                         unset( $custom_active_components[ $retired_component ] );
    316                     }
    317310                }
    318311            }
     
    330323    }
    331324
    332     // Active components is the list of packaged and custom components
    333     return array_merge( $components, $custom_active_components );
     325    return $components;
    334326}
    335327
  • branches/2.2/src/bp-core/bp-core-cache.php

    r9351 r9609  
    6464    }
    6565
    66     $page_ids = bp_core_get_directory_page_ids();
     66    $page_ids = bp_core_get_directory_page_ids( 'all' );
    6767
    6868    if ( ! in_array( $post_id, (array) $page_ids ) ) {
  • branches/2.2/src/bp-core/bp-core-functions.php

    r9505 r9609  
    419419 * @since BuddyPress (1.5.0)
    420420 *
     421 * @param string $status 'active' to return only pages associated with active components, 'all' to return all saved
     422 *                       pages. When running save routines, use 'all' to avoid removing data related to inactive
     423 *                       components. Default: 'active'.
    421424 * @return array|string An array of page IDs, keyed by component names, or an
    422  *         empty string if the list is not found.
    423  */
    424 function bp_core_get_directory_page_ids() {
     425 *                      empty string if the list is not found.
     426 */
     427function bp_core_get_directory_page_ids( $status = 'active' ) {
    425428    $page_ids = bp_get_option( 'bp-pages' );
    426429
     
    437440            }
    438441
    439             if ( ! bp_is_active( $component_name ) || 'trash' == get_post_status( $page_id ) ) {
     442            // Trashed pages should not appear in results.
     443            if ( 'trash' == get_post_status( $page_id ) ) {
     444                unset( $page_ids[ $component_name ] );
     445
     446            }
     447
     448            // Remove inactive component pages, if required.
     449            if ( 'active' === $status && ! bp_is_active( $component_name ) ) {
    440450                unset( $page_ids[ $component_name ] );
    441451            }
     
    549559    }
    550560
    551     $pages = bp_core_get_directory_page_ids();
     561    $pages = bp_core_get_directory_page_ids( 'all' );
    552562
    553563    // Delete any existing pages
     
    640650    }
    641651
    642     $page_ids       = bp_core_get_directory_page_ids();
     652    $page_ids       = bp_core_get_directory_page_ids( 'all' );
    643653    $component_name = array_search( $post_id, $page_ids );
    644654
     
    746756    }
    747757
    748     $page_ids = array_merge( (array) $new_page_ids, (array) bp_core_get_directory_page_ids() );
     758    $page_ids = array_merge( (array) $new_page_ids, (array) bp_core_get_directory_page_ids( 'all' ) );
    749759    bp_core_update_directory_page_ids( $page_ids );
    750760}
  • branches/2.2/tests/phpunit/testcases/admin/functions.php

    r9554 r9609  
    145145        ) ) );
    146146
    147         $page_ids = array_merge( $new_page_ids, (array) bp_core_get_directory_page_ids() );
     147        $page_ids = array_merge( $new_page_ids, (array) bp_core_get_directory_page_ids( 'all' ) );
    148148        bp_core_update_directory_page_ids( $page_ids );
    149149
     
    151151        bp_core_add_page_mappings( $bp->active_components );
    152152
    153         $this->assertContains( $bp->foo->id, array_keys( bp_core_get_directory_page_ids() ) );
     153        $this->assertContains( $bp->foo->id, array_keys( bp_core_get_directory_page_ids( 'all' ) ) );
    154154
    155155        // Reset buddypress() vars
  • branches/2.2/tests/phpunit/testcases/core/functions.php

    r9510 r9609  
    450450
    451451        $this->assertFalse( wp_cache_get( 'directory_pages', 'bp' ) );
    452     }
    453 
    454     /**
    455      * @group bp_core_get_directory_page_ids
    456      */
    457     public function test_bp_core_get_directory_page_ids_on_directory_page_to_trash() {
    458         $old_page_ids = bp_core_get_directory_page_ids();
    459 
    460         // Grab the and remove the first page.
    461         foreach ( $old_page_ids as $component => $page_id ) {
    462             $p = $page_id;
    463             unset( $old_page_ids[ $component ] );
    464             break;
    465         }
    466 
    467         // Move page to trash.
    468         wp_delete_post( $p, false );
    469 
    470         $new_page_ids = bp_core_get_directory_page_ids();
    471 
    472         $this->assertEquals( $old_page_ids, $new_page_ids );
    473     }
    474 
    475     /**
    476      * @group bp_core_get_directory_page_ids
    477      */
    478     public function test_bp_core_get_directory_page_ids_on_directory_page_delete() {
    479         $old_page_ids = bp_core_get_directory_page_ids();
    480 
    481         // Grab the and remove the first page.
    482         foreach ( $old_page_ids as $component => $page_id ) {
    483             $p = $page_id;
    484             unset( $old_page_ids[ $component ] );
    485             break;
    486         }
    487 
    488         // Force delete page.
    489         wp_delete_post( $p, true );
    490 
    491         $new_page_ids = bp_core_get_directory_page_ids();
    492 
    493         $this->assertEquals( $old_page_ids, $new_page_ids );
    494     }
    495 
    496     /**
    497      * @group bp_core_get_directory_page_ids
    498      */
    499     public function test_bp_core_get_directory_page_ids_on_non_directory_page_delete() {
    500         $old_page_ids = bp_core_get_directory_page_ids();
    501 
    502         $p = $this->factory->post->create( array(
    503             'post_status' => 'publish',
    504             'post_type' => 'page',
    505         ) );
    506 
    507         // Force delete page.
    508         wp_delete_post( $p, true );
    509 
    510         $new_page_ids = bp_core_get_directory_page_ids();
    511 
    512         $this->assertEquals( $old_page_ids, $new_page_ids );
    513     }
    514 
    515     /**
    516      * @group bp_core_get_directory_page_ids
    517      */
    518     public function test_bp_core_get_directory_page_ids_non_active_component() {
    519         $old_page_ids = bp_core_get_directory_page_ids();
    520         $bp = buddypress();
    521 
    522         // Grab the and remove the first page.
    523         foreach ( $old_page_ids as $component => $page_id ) {
    524             $p = $page_id;
    525             $c = $component;
    526             unset( $old_page_ids[ $component ] );
    527             break;
    528         }
    529 
    530         // Deactivate component.
    531         unset( $bp->active_components[ $c ] );
    532 
    533         $new_page_ids = bp_core_get_directory_page_ids();
    534 
    535         // Restore components.
    536         $bp->active_components[ $c ] = 1;
    537 
    538         $this->assertEquals( $old_page_ids, $new_page_ids );
    539     }
    540 
    541     /**
    542      * @group bp_core_get_directory_page_ids
    543      */
    544     public function test_bp_core_get_directory_page_ids_should_contain_register_and_activet_pages_when_registration_is_open() {
    545         add_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    546 
    547         $ac = buddypress()->active_components;
    548         bp_core_add_page_mappings( array_keys( $ac ) );
    549 
    550         $page_ids = bp_core_get_directory_page_ids();
    551         $page_names = array_keys( $page_ids );
    552 
    553         $this->assertContains( 'register', $page_names );
    554         $this->assertContains( 'activate', $page_names );
    555 
    556         remove_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    557     }
    558 
    559     /**
    560      * @group bp_core_get_directory_page_ids
    561      */
    562     public function test_bp_core_get_directory_page_ids_should_not_contain_register_and_activet_pages_when_registration_is_closed() {
    563 
    564         // Make sure the pages exist, to verify they're filtered out.
    565         add_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    566         $ac = buddypress()->active_components;
    567         bp_core_add_page_mappings( array_keys( $ac ) );
    568         remove_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    569 
    570         // Get page ids
    571         $page_ids = bp_core_get_directory_page_ids();
    572 
    573         // Need to delete these pages as previously created.
    574         wp_delete_post( $page_ids['register'], true );
    575         wp_delete_post( $page_ids['activate'], true );
    576 
    577         add_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    578         bp_core_add_page_mappings( array_keys( $ac ) );
    579         $page_ids = bp_core_get_directory_page_ids();
    580         remove_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    581 
    582         $page_names = array_keys( $page_ids );
    583 
    584         $this->assertNotContains( 'register', $page_names );
    585         $this->assertNotContains( 'activate', $page_names );
    586     }
    587 
    588     /**
    589      * @group bp_core_get_directory_pages
    590      */
    591     public function test_bp_core_get_directory_pages_register_activate_page_created_signups_allowed() {
    592         add_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    593 
    594         $ac = buddypress()->active_components;
    595         bp_core_add_page_mappings( array_keys( $ac ) );
    596         $directory_pages = bp_core_get_directory_pages();
    597 
    598         remove_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    599 
    600         $this->assertTrue( isset( $directory_pages->register ) );
    601         $this->assertTrue( isset( $directory_pages->activate ) );
    602 
    603         $r = get_post( $directory_pages->register->id );
    604         $this->assertTrue( 'publish' == $r->post_status );
    605 
    606         $a = get_post( $directory_pages->activate->id );
    607         $this->assertTrue( 'publish' == $a->post_status );
    608     }
    609 
    610     /**
    611      * @group bp_core_get_directory_pages
    612      */
    613     public function test_bp_core_get_directory_pages_register_activate_page_notcreated_signups_allowed() {
    614         add_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    615 
    616         $ac = buddypress()->active_components;
    617         bp_core_add_page_mappings( array_keys( $ac ) );
    618 
    619         remove_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    620 
    621         add_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    622 
    623         $directory_pages = bp_core_get_directory_pages();
    624 
    625         remove_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    626 
    627         $this->assertFalse( isset( $directory_pages->register ) );
    628         $this->assertFalse( isset( $directory_pages->activate ) );
    629     }
    630 
    631     /**
    632      * @group bp_core_get_directory_pages
    633      */
    634     public function test_bp_core_get_directory_pages_register_activate_page_created_signups_notallowed() {
    635         add_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    636 
    637         $ac = buddypress()->active_components;
    638         bp_core_add_page_mappings( array_keys( $ac ) );
    639 
    640         remove_filter( 'bp_get_signup_allowed', '__return_true', 999 );
    641 
    642         add_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    643 
    644         $directory_pages = bp_core_get_directory_pages();
    645 
    646         remove_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    647 
    648         $this->assertTrue( isset( $directory_pages->register ) );
    649         $this->assertTrue( isset( $directory_pages->activate ) );
    650 
    651         $r = get_post( $directory_pages->register->id );
    652         $this->assertTrue( 'publish' == $r->post_status );
    653 
    654         $a = get_post( $directory_pages->activate->id );
    655         $this->assertTrue( 'publish' == $a->post_status );
    656     }
    657 
    658     /**
    659      * @group bp_core_get_directory_pages
    660      */
    661     public function test_bp_core_get_directory_pages_register_activate_page_notcreated_signups_notallowed() {
    662 
    663         add_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    664 
    665         $ac = buddypress()->active_components;
    666         bp_core_add_page_mappings( array_keys( $ac ) );
    667         $directory_pages = bp_core_get_directory_pages();
    668 
    669         remove_filter( 'bp_get_signup_allowed', '__return_false', 999 );
    670 
    671         $this->assertFalse( isset( $directory_pages->register ) );
    672         $this->assertFalse( isset( $directory_pages->activate ) );
    673     }
    674 
    675     /**
    676      * @group bp_core_get_directory_pages
    677      */
    678     public function test_bp_core_get_directory_pages_pages_settings_update() {
    679         // Set the cache
    680         $pages = bp_core_get_directory_pages();
    681 
    682         // Mess with it but put it back
    683         $v = bp_get_option( 'bp-pages' );
    684         bp_update_option( 'bp-pages', 'foo' );
    685 
    686         $this->assertFalse( wp_cache_get( 'directory_pages', 'bp' ) );
    687 
    688         bp_update_option( 'bp-pages', $v );
    689     }
    690 
    691     /**
    692      * @group bp_core_get_directory_pages
    693      */
    694     public function test_bp_core_get_directory_pages_multisite_delete_post_with_same_bp_page_id() {
    695         if ( ! is_multisite() ) {
    696             return;
    697         }
    698 
    699         $dir_pages = bp_core_get_directory_pages();
    700 
    701         // create a blog
    702         $u = $this->factory->user->create();
    703         $b1 = $this->factory->blog->create( array( 'user_id' => $u ) );
    704 
    705         // switch to blog and create some dummy posts until we reach a post ID that
    706         // matches our BP activity page ID
    707         switch_to_blog( $b1 );
    708         $p = $this->factory->post->create();
    709         while( $p <= $dir_pages->activity->id ) {
    710             $p = $this->factory->post->create();
    711         }
    712 
    713         // delete the post that matches the BP activity page ID on this sub-site
    714         wp_delete_post( $dir_pages->activity->id, true );
    715 
    716         // restore blog
    717         restore_current_blog();
    718 
    719         // refetch BP directory pages
    720         $dir_pages = bp_core_get_directory_pages();
    721 
    722         // Now verify that our BP activity page was not wiped out
    723         $this->assertNotEmpty( $dir_pages->activity );
    724452    }
    725453
Note: See TracChangeset for help on using the changeset viewer.