bp_core_maybe_hook_new_subnav_screen_function( array $subnav_item, string $component = 'members' )

For a given subnav item, either hook the screen function or generate redirect arguments, as necessary.


Parameters Parameters

$subnav_item

(array) (Required) The subnav array added to the secondary navigation of the component in bp_core_new_subnav_item().

$component

(string) (Optional) The component the navigation is attached to. Defaults to 'members'.

Default value: 'members'


Top ↑

Return Return

(array)


Source Source

File: bp-core/bp-core-buddybar.php

function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item, $component = 'members' ) {
	$retval = array(
		'status' => '',
	);

	// Is this accessible by site admins only?
	$site_admin_restricted = false;
	if ( ! empty( $subnav_item['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
		$site_admin_restricted = true;
	}

	// User has access, so let's try to hook the display callback.
	if ( ! empty( $subnav_item['user_has_access'] ) && ! $site_admin_restricted ) {

		// Screen function is invalid.
		if ( ! is_callable( $subnav_item['screen_function'] ) ) {
			$retval['status'] = 'failure';

		// Success - hook to bp_screens.
		} else {
			add_action( 'bp_screens', $subnav_item['screen_function'], 3 );
			$retval['status'] = 'success';
		}

	// User doesn't have access. Determine redirect arguments based on
	// user status.
	} else {
		$retval['status'] = 'failure';

		if ( is_user_logged_in() ) {

			$bp = buddypress();

			// If a redirect URL has been passed to the subnav item, respect it.
			if ( ! empty( $subnav_item['no_access_url'] ) ) {
				$message     = __( 'You do not have access to that page.', 'buddypress' );
				$redirect_to = trailingslashit( $subnav_item['no_access_url'] );

			// In the case of a user page, we try to assume a
			// redirect URL.
			} elseif ( bp_is_user() ) {

				$parent_nav_default = $bp->{$component}->nav->get_primary( array( 'slug' => $bp->default_component ), false );
				if ( $parent_nav_default ) {
					$parent_nav_default_item = reset( $parent_nav_default );
				}

				// Redirect to the displayed user's default
				// component, as long as that component is
				// publicly accessible.
				if ( bp_is_my_profile() || ( isset( $parent_nav_default_item ) && $parent_nav_default_item->show_for_displayed_user ) ) {
					$message     = __( 'You do not have access to that page.', 'buddypress' );
					$redirect_to = bp_displayed_user_domain();

				// In some cases, the default tab is not accessible to
				// the logged-in user. So we fall back on a tab that we
				// know will be accessible.
				} else {
					// Try 'activity' first.
					if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
						$redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
					// Then try 'profile'.
					} else {
						$redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
					}

					$message     = '';
				}

			// Fall back to the home page.
			} else {
				$message     = __( 'You do not have access to this page.', 'buddypress' );
				$redirect_to = bp_get_root_domain();
			}

			$retval['redirect_args'] = array(
				'message'  => $message,
				'root'     => $redirect_to,
				'redirect' => false,
				'mode'     => 1
			);

		} else {
			// When the user is logged out, pass an empty array
			// This indicates that the default arguments should be
			// used in bp_core_no_access().
			$retval['redirect_args'] = array();
		}
	}

	return $retval;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced the $component parameter.
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.