Skip to:
Content

BuddyPress.org

Changeset 6770


Ignore:
Timestamp:
02/06/2013 10:10:49 PM (12 years ago)
Author:
boonebgorges
Message:

Improvements to automatic WP page mapping for BP directory components

Since the wizard was removed, and Members and Activity were set as the default
components, BP has been creating directory pages for Members and Activity
automatically upon installation. This created a jarring disparity for the
Groups component, which also needs a top-level directory; when activated at a
later date, the user would be prompted to associate a page manually.

This changeset moves bp_updater_add_page_mappings() to the core component
(renaming it appropriately). This function is refactored to allow for existing
page mappings to be retained, as you'd want to do when activating new
components (rather than deleting existing mappings and mapped pages, as you'd
want during a fresh installation). Finally, the function is called when saving
the Components admin page, so that components requiring directory pages (like
Groups) get them created automatically upon activation.

Fixes #4805

Location:
trunk/bp-core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/admin/bp-core-components.php

    r6581 r6770  
    301301
    302302        bp_core_install( $bp->active_components );
    303 
     303        bp_core_add_page_mappings( $bp->active_components );
    304304        bp_update_option( 'bp-active-components', $bp->active_components );
    305305    }
  • trunk/bp-core/bp-core-functions.php

    r6579 r6770  
    173173
    174174    return apply_filters( 'bp_core_get_directory_pages', $pages );
     175}
     176
     177/**
     178 * Add the pages for the component mapping. These are most often used by components with directories (e.g. groups, members).
     179 *
     180 * @param array $default_components Optional components to create pages for
     181 * @param string $existing 'delete' if you want to delete existing page
     182 *   mappings and replace with new ones. Otherwise existing page mappings
     183 *   are kept, and the gaps filled in with new pages
     184 * @since BuddyPress (1.7)
     185 */
     186function bp_core_add_page_mappings( $components, $existing = 'keep' ) {
     187
     188    // Make sure that the pages are created on the root blog no matter which Dashboard the setup is being run on
     189    if ( ! bp_is_root_blog() )
     190        switch_to_blog( bp_get_root_blog_id() );
     191
     192    $pages = bp_core_get_directory_page_ids();
     193
     194    // Delete any existing pages
     195    if ( 'delete' == $existing ) {
     196        foreach ( (array) $pages as $page_id ) {
     197            wp_delete_post( $page_id, true );
     198        }
     199
     200        $pages = array();
     201    }
     202
     203    $page_titles = array(
     204        'activity' => _x( 'Activity', 'Page title for the Activity directory.', 'buddypress' ),
     205        'groups'   => _x( 'Groups', 'Page title for the Groups directory.', 'buddypress' ),
     206        'sites'    => _x( 'Sites', 'Page title for the Sites directory.', 'buddypress' ),
     207        'activate' => _x( 'Activate', 'Page title for the user account activation screen.', 'buddypress' ),
     208        'members'  => _x( 'Members', 'Page title for the Members directory.', 'buddypress' ),
     209        'register' => _x( 'Register', 'Page title for the user registration screen.', 'buddypress' ),
     210    );
     211
     212    $pages_to_create = array();
     213    foreach ( array_keys( $components ) as $component_name ) {
     214        if ( ! isset( $pages[ $component_name ] ) && isset( $page_titles[ $component_name ] ) ) {
     215            $pages_to_create[ $component_name ] = $page_titles[ $component_name ];
     216        }
     217    }
     218
     219    // Register and Activate are not components, but need pages when
     220    // registration is enabled
     221    if ( bp_get_signup_allowed() ) {
     222        foreach ( array( 'register', 'activate' ) as $slug ) {
     223            if ( ! isset( $pages[ $slug ] ) ) {
     224                $pages_to_create[ $slug ] = $page_titles[ $slug ];
     225            }
     226        }
     227    }
     228
     229    // No need for a Sites directory unless we're on multisite
     230    if ( ! is_multisite() && isset( $pages_to_create['sites'] ) ) {
     231        unset( $pages_to_create['sites'] );
     232    }
     233
     234    // Members must always have a page, no matter what
     235    if ( ! isset( $pages['members'] ) && ! isset( $pages_to_create['members'] ) ) {
     236        $pages_to_create['members'] = $page_titles['members'];
     237    }
     238
     239    // Create the pages
     240    foreach ( $pages_to_create as $component_name => $page_name ) {
     241        $pages[ $component_name ] = wp_insert_post( array(
     242            'comment_status' => 'closed',
     243            'ping_status'    => 'closed',
     244            'post_status'    => 'publish',
     245            'post_title'     => $page_name,
     246            'post_type'      => 'page',
     247        ) );
     248    }
     249
     250    // Save the page mapping
     251    bp_update_option( 'bp-pages', $pages );
     252
     253    // If we had to switch_to_blog, go back to the original site.
     254    if ( ! bp_is_root_blog() )
     255        restore_current_blog();
    175256}
    176257
  • trunk/bp-core/bp-core-update.php

    r6730 r6770  
    186186        bp_core_install( $default_components );
    187187        bp_update_option( 'bp-active-components', $default_components );
    188         bp_updater_add_page_mappings( $default_components );
     188        bp_core_add_page_mappings( $default_components, 'delete' );
    189189
    190190    // Upgrades
     
    197197        if ( $raw_db_version < 1801 ) {
    198198            bp_update_to_1_5();
    199             bp_updater_add_page_mappings( $default_components );
     199            bp_core_add_page_mappings( $default_components, 'delete' );
    200200        }
    201201
     
    249249
    250250/**
    251  * Add the pages for the component mapping. These are most often used by components with directories (e.g. groups, members).
    252  *
    253  * @param array $default_components Optional components to create pages for
    254  * @since BuddyPress (1.7)
    255  */
    256 function bp_updater_add_page_mappings( $default_components ) {
    257 
    258     // Make sure that the pages are created on the root blog no matter which Dashboard the setup is being run on
    259     if ( ! bp_is_root_blog() )
    260         switch_to_blog( bp_get_root_blog_id() );
    261 
    262     // Delete any existing pages
    263     $pages = bp_core_get_directory_page_ids();
    264     foreach ( (array) $pages as $page_id )
    265         wp_delete_post( $page_id, true );
    266 
    267     $pages = array();
    268     foreach ( array_keys( $default_components ) as $component_name ) {
    269         if ( $component_name == 'activity' ) {
    270             $pages[$component_name] = _x( 'Activity', 'Page title for the Activity directory.', 'buddypress' );
    271 
    272         } elseif ( $component_name == 'groups') {
    273             $pages[$component_name] = _x( 'Groups', 'Page title for the Groups directory.', 'buddypress' );
    274 
    275         // Blogs component only needs a directory page when multisite is enabled
    276         } elseif ( $component_name == 'blogs' && is_multisite() ) {
    277             $pages[$component_name] = _x( 'Sites', 'Page title for the Sites directory.', 'buddypress' );
    278         }
    279     }
    280 
    281     // Mandatory components/pages
    282     $pages['activate'] = _x( 'Activate', 'Page title for the user account activation screen.', 'buddypress' );
    283     $pages['members']  = _x( 'Members',  'Page title for the Members directory.',              'buddypress' );
    284     $pages['register'] = _x( 'Register', 'Page title for the user registration screen.',       'buddypress' );
    285 
    286     // Create the pages
    287     foreach ( $pages as $component_name => $page_name ) {
    288         $pages[$component_name] = wp_insert_post( array(
    289             'comment_status' => 'closed',
    290             'ping_status'    => 'closed',
    291             'post_status'    => 'publish',
    292             'post_title'     => $page_name,
    293             'post_type'      => 'page',
    294         ) );
    295     }
    296 
    297     // Save the page mapping
    298     bp_update_option( 'bp-pages', $pages );
    299 
    300     // If we had to switch_to_blog, go back to the original site.
    301     if ( ! bp_is_root_blog() )
    302         restore_current_blog();
    303 }
    304 
    305 /**
    306251 * Redirect user to BuddyPress's What's New page on activation
    307252 *
Note: See TracChangeset for help on using the changeset viewer.