Skip to:
Content

BuddyPress.org

Changeset 10774


Ignore:
Timestamp:
05/17/2016 03:30:29 PM (10 years ago)
Author:
imath
Message:

Introduce the support for a custom home page template for single members profiles.

  • Themes can now include a front.php template to customize the home page of members.
  • Introduce a template hierarchy for this template so that it is possible to have different front pages according to the ID, the nicename or the member type of the displayed user.

Props hnla, dcavins, DJPaul, imath.

Fixes #6769

Location:
trunk/src
Files:
5 edited

Legend:

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

    r10745 r10774  
    22692269function bp_is_user() {
    22702270        return (bool) bp_displayed_user_id();
     2271}
     2272
     2273/**
     2274 * Is the current page a user custom front page?
     2275 *
     2276 * Will return true anytime there is a custom front page for the displayed user.
     2277 *
     2278 * @since 2.6.0
     2279 *
     2280 * @return bool True if the current page is a user custom front page.
     2281 */
     2282function bp_is_user_front() {
     2283        return (bool) ( bp_is_user() && bp_is_current_component( 'front' ) );
    22712284}
    22722285
  • trunk/src/bp-members/bp-members-functions.php

    r10711 r10774  
    24812481add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' );
    24822482
     2483/**
     2484 * Get the displayed user Object
     2485 *
     2486 * @since 2.6.0
     2487 *
     2488 * @return object The displayed user object, null otherwise.
     2489 */
     2490function bp_get_displayed_user() {
     2491        $bp = buddypress();
     2492
     2493        $displayed_user = null;
     2494        if ( ! empty( $bp->displayed_user->id ) ) {
     2495                $displayed_user = $bp->displayed_user;
     2496        }
     2497
     2498        /**
     2499         * Filters the displayed_user object corresponding to the displayed member.
     2500         *
     2501         * @since 2.6.0
     2502         *
     2503         * @param object $displayed_user The displayed_user object.
     2504         */
     2505        return apply_filters( 'bp_get_displayed_user', $displayed_user );
     2506}
     2507
    24832508/** Member Types *************************************************************/
    24842509
  • trunk/src/bp-members/bp-members-template.php

    r10745 r10774  
    13271327
    13281328/**
     1329 * Output the contents of the current user's home page.
     1330 *
     1331 * @since 2.6.0
     1332 */
     1333function bp_displayed_user_front_template_part() {
     1334        $located = bp_displayed_user_get_front_template();
     1335
     1336        if ( false !== $located ) {
     1337                $slug = str_replace( '.php', '', $located );
     1338                $name = null;
     1339
     1340                /**
     1341                 * Let plugins adding an action to bp_get_template_part get it from here
     1342                 *
     1343                 * @param string $slug Template part slug requested.
     1344                 * @param string $name Template part name requested.
     1345                 */
     1346                do_action( 'get_template_part_' . $slug, $slug, $name );
     1347
     1348                load_template( $located, true );
     1349        }
     1350
     1351        return $located;
     1352}
     1353
     1354/**
     1355 * Locate a custom user front template if it exists.
     1356 *
     1357 * @since 2.6.0
     1358 *
     1359 * @param  object|null $displayed_user Optional. Falls back to current user if not passed.
     1360 * @return string|bool                 Path to front template on success; boolean false on failure.
     1361 */
     1362function bp_displayed_user_get_front_template( $displayed_user = null ) {
     1363        if ( ! is_object( $displayed_user ) || empty( $displayed_user->id ) ) {
     1364                $displayed_user = bp_get_displayed_user();
     1365        }
     1366
     1367        if ( ! isset( $displayed_user->id ) ) {
     1368                return false;
     1369        }
     1370
     1371        if ( isset( $displayed_user->front_template ) ) {
     1372                return $displayed_user->front_template;
     1373        }
     1374
     1375        // Init the hierarchy
     1376        $template_names = array(
     1377                'members/single/front-id-' . sanitize_file_name( $displayed_user->id ) . '.php',
     1378                'members/single/front-nicename-' . sanitize_file_name( $displayed_user->userdata->user_nicename ) . '.php',
     1379        );
     1380
     1381        /**
     1382         * Check for member types and add it to the hierarchy
     1383         *
     1384         * Make sure to register your member
     1385         * type using the hook 'bp_register_member_types'
     1386         */
     1387        if ( bp_get_member_types() ) {
     1388                $displayed_user_member_type = bp_get_member_type( $displayed_user->id );
     1389                if ( ! $displayed_user_member_type ) {
     1390                        $displayed_user_member_type = 'none';
     1391                }
     1392
     1393                $template_names[] = 'members/single/front-member-type-' . sanitize_file_name( $displayed_user_member_type )   . '.php';
     1394        }
     1395
     1396        // Add The generic template to the end of the hierarchy
     1397        $template_names[] = 'members/single/front.php';
     1398
     1399        /**
     1400         * Filters the hierarchy of user front templates corresponding to a specific user.
     1401         *
     1402         * @since 2.6.0
     1403         *
     1404         * @param array  $template_names Array of template paths.
     1405         */
     1406        return bp_locate_template( apply_filters( 'bp_displayed_user_get_front_template', $template_names ), false, true );
     1407}
     1408
     1409/**
     1410 * Check if the displayed user has a custom front template.
     1411 *
     1412 * @since 2.6.0
     1413 */
     1414function bp_displayed_user_has_front_template() {
     1415        $displayed_user = bp_get_displayed_user();
     1416
     1417        return ! empty( $displayed_user->front_template );
     1418}
     1419
     1420/**
    13291421 * Render the navigation markup for the displayed user.
    13301422 *
  • trunk/src/bp-members/classes/class-bp-members-component.php

    r10745 r10774  
    154154                $this->nav = new BP_Core_Nav();
    155155
     156                // If A user is displayed, check if there is a front template
     157                if ( bp_get_displayed_user() ) {
     158                        $bp->displayed_user->front_template = bp_displayed_user_get_front_template();
     159                }
     160
    156161                /** Signup ***********************************************************
    157162                 */
     
    179184                /** Default Profile Component ****************************************
    180185                 */
    181 
    182                 if ( defined( 'BP_DEFAULT_COMPONENT' ) && BP_DEFAULT_COMPONENT ) {
     186                if ( bp_displayed_user_has_front_template() ) {
     187                        $bp->default_component = 'front';
     188                } elseif ( defined( 'BP_DEFAULT_COMPONENT' ) && BP_DEFAULT_COMPONENT ) {
    183189                        $bp->default_component = BP_DEFAULT_COMPONENT;
     190                } elseif ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
     191                        $bp->default_component = bp_get_activity_slug();
    184192                } else {
    185                         if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
    186                                 $bp->default_component = bp_get_activity_slug();
    187                         } else {
    188                                 $bp->default_component = ( 'xprofile' === $bp->profile->id ) ? 'profile' : $bp->profile->id;
    189                         }
     193                        $bp->default_component = ( 'xprofile' === $bp->profile->id ) ? 'profile' : $bp->profile->id;
    190194                }
    191195
     
    243247        public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    244248
    245                 // Bail if XProfile component is active.
    246                 if ( bp_is_active( 'xprofile' ) ) {
    247                         return;
    248                 }
    249 
    250249                // Don't set up navigation if there's no member.
    251250                if ( ! is_user_logged_in() && ! bp_is_user() ) {
     251                        return;
     252                }
     253
     254                $is_xprofile_active = bp_is_active( 'xprofile' );
     255
     256                // Bail if XProfile component is active and there's no custom front page for the user.
     257                if ( ! bp_displayed_user_has_front_template() && $is_xprofile_active ) {
    252258                        return;
    253259                }
     
    262268                }
    263269
    264                 $slug         = bp_get_profile_slug();
    265                 $profile_link = trailingslashit( $user_domain . $slug );
    266 
    267                 // Setup the main navigation.
    268                 $main_nav = array(
    269                         'name'                => _x( 'Profile', 'Member profile main navigation', 'buddypress' ),
    270                         'slug'                => $slug,
    271                         'position'            => 20,
    272                         'screen_function'     => 'bp_members_screen_display_profile',
    273                         'default_subnav_slug' => 'public',
    274                         'item_css_id'         => buddypress()->profile->id
    275                 );
    276 
    277                 // Setup the subnav items for the member profile.
    278                 $sub_nav[] = array(
     270                // Set slug to profile in case the xProfile component is not active
     271                $slug = bp_get_profile_slug();
     272
     273                // Defaults to empty navs
     274                $this->main_nav = array();
     275                $this->sub_nav  = array();
     276
     277                if ( ! $is_xprofile_active ) {
     278                        $this->main_nav = array(
     279                                'name'                => _x( 'Profile', 'Member profile main navigation', 'buddypress' ),
     280                                'slug'                => $slug,
     281                                'position'            => 20,
     282                                'screen_function'     => 'bp_members_screen_display_profile',
     283                                'default_subnav_slug' => 'public',
     284                                'item_css_id'         => buddypress()->profile->id
     285                        );
     286                }
     287
     288                /**
     289                 * Setup the subnav items for the member profile.
     290                 *
     291                 * This is required in case there's a custom front or in case the xprofile component
     292                 * is not active.
     293                 */
     294                $this->sub_nav = array(
    279295                        'name'            => _x( 'View', 'Member profile view', 'buddypress' ),
    280296                        'slug'            => 'public',
    281                         'parent_url'      => $profile_link,
     297                        'parent_url'      => trailingslashit( $user_domain . $slug ),
    282298                        'parent_slug'     => $slug,
    283299                        'screen_function' => 'bp_members_screen_display_profile',
     
    285301                );
    286302
     303                /**
     304                 * If there's a front template the members component nav
     305                 * will be there to display the user's front page.
     306                 */
     307                if ( bp_displayed_user_has_front_template() ) {
     308                        $main_nav = array(
     309                                'name'                => _x( 'Home', 'Member Home page', 'buddypress' ),
     310                                'slug'                => 'front',
     311                                'position'            => 5,
     312                                'screen_function'     => 'bp_members_screen_display_profile',
     313                                'default_subnav_slug' => 'public',
     314                        );
     315
     316                        // We need a dummy subnav for the front page to load.
     317                        $front_subnav = $this->sub_nav;
     318                        $front_subnav['parent_slug'] = 'front';
     319
     320                        // In case the subnav is displayed in the front template
     321                        $front_subnav['parent_url'] = trailingslashit( $user_domain . 'front' );
     322
     323                        // Set the subnav
     324                        $sub_nav[] = $front_subnav;
     325
     326                        /**
     327                         * If the profile component is not active, we need to create a new
     328                         * nav to display the WordPress profile.
     329                         */
     330                        if ( ! $is_xprofile_active ) {
     331                                add_action( 'bp_members_setup_nav', array( $this, 'setup_profile_nav' ) );
     332                        }
     333
     334                /**
     335                 * If there's no front template and xProfile is not active, the members
     336                 * component nav will be there to display the WordPress profile
     337                 */
     338                } else {
     339                        $main_nav  = $this->main_nav;
     340                        $sub_nav[] = $this->sub_nav;
     341                }
     342
     343
    287344                parent::setup_nav( $main_nav, $sub_nav );
     345        }
     346
     347        /**
     348         * Set up a profile nav in case the xProfile
     349         * component is not active and a front template is
     350         * used.
     351         *
     352         * @since 2.6.0
     353         */
     354        public function setup_profile_nav() {
     355                if ( empty( $this->main_nav ) || empty( $this->sub_nav ) ) {
     356                        return;
     357                }
     358
     359                // Add the main nav
     360                bp_core_new_nav_item( $this->main_nav, 'members' );
     361
     362                // Add the sub nav item.
     363                bp_core_new_subnav_item( $this->sub_nav, 'members' );
    288364        }
    289365
  • trunk/src/bp-templates/bp-legacy/buddypress/members/single/home.php

    r10181 r10774  
    6565                do_action( 'bp_before_member_body' );
    6666
    67                 if ( bp_is_user_activity() || !bp_current_component() ) :
     67                if ( bp_is_user_front() ) :
     68                        bp_displayed_user_front_template_part();
     69
     70                elseif ( bp_is_user_activity() ) :
    6871                        bp_get_template_part( 'members/single/activity' );
    6972
Note: See TracChangeset for help on using the changeset viewer.