bp_core_maybe_hook_new_subnav_screen_function

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

Description

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

Returns (array)

Parameters (2)

0. $subnav_item (array)
The subnav array added to the secondary navigation of the component in bp_core_new_subnav_item().
1. $component — Optional. (string) => 'members'
The component the navigation is attached to. Defaults to members..

Usage

  1. if ( !function_exists( 'bp_core_maybe_hook_new_subnav_screen_function' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-buddybar.php'; 
  3.  
  4. // The subnav array added to the secondary navigation of 
  5. // the component in bp_core_new_subnav_item(). 
  6. $subnav_item = array(); 
  7.  
  8. // The component the navigation is attached to. Defaults to 'members'. 
  9. $component = 'members'; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = bp_core_maybe_hook_new_subnav_screen_function($subnav_item, $component); 
  13.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-buddybar.php  
  1. function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item, $component = 'members' ) { 
  2. $retval = array( 
  3. 'status' => '',  
  4. ); 
  5.  
  6. // Is this accessible by site admins only? 
  7. $site_admin_restricted = false; 
  8. if ( ! empty( $subnav_item['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) { 
  9. $site_admin_restricted = true; 
  10.  
  11. // User has access, so let's try to hook the display callback. 
  12. if ( ! empty( $subnav_item['user_has_access'] ) && ! $site_admin_restricted ) { 
  13.  
  14. // Screen function is invalid. 
  15. if ( ! is_callable( $subnav_item['screen_function'] ) ) { 
  16. $retval['status'] = 'failure'; 
  17.  
  18. // Success - hook to bp_screens. 
  19. } else { 
  20. add_action( 'bp_screens', $subnav_item['screen_function'], 3 ); 
  21. $retval['status'] = 'success'; 
  22.  
  23. // User doesn't have access. Determine redirect arguments based on 
  24. // user status. 
  25. } else { 
  26. $retval['status'] = 'failure'; 
  27.  
  28. if ( is_user_logged_in() ) { 
  29.  
  30. $bp =buddypress); 
  31.  
  32. // If a redirect URL has been passed to the subnav 
  33. // item, respect it. 
  34. if ( ! empty( $subnav_item['no_access_url'] ) ) { 
  35. $message = __( 'You do not have access to this page.', buddypress ); 
  36. $redirect_to = trailingslashit( $subnav_item['no_access_url'] ); 
  37.  
  38. // In the case of a user page, we try to assume a 
  39. // redirect URL. 
  40. } elseif ( bp_is_user() ) { 
  41.  
  42. $parent_nav_default = $bp->{$component}->nav->get_primary( array( 'slug' => $bp->default_component ), false ); 
  43. if ( $parent_nav_default ) { 
  44. $parent_nav_default_item = reset( $parent_nav_default ); 
  45.  
  46. // Redirect to the displayed user's default 
  47. // component, as long as that component is 
  48. // publicly accessible. 
  49. if ( bp_is_my_profile() || ( isset( $parent_nav_default_item ) && $parent_nav_default_item->show_for_displayed_user ) ) { 
  50. $message = __( 'You do not have access to this page.', buddypress ); 
  51. $redirect_to = bp_displayed_user_domain(); 
  52.  
  53. // In some cases, the default tab is not accessible to 
  54. // the logged-in user. So we fall back on a tab that we 
  55. // know will be accessible. 
  56. } else { 
  57. // Try 'activity' first. 
  58. if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) { 
  59. // Then try 'profile'. 
  60. } else { 
  61. $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) ); 
  62.  
  63. $message = ''; 
  64.  
  65. // Fall back to the home page. 
  66. } else { 
  67. $message = __( 'You do not have access to this page.', buddypress ); 
  68. $redirect_to = bp_get_root_domain(); 
  69.  
  70. $retval['redirect_args'] = array( 
  71. 'message' => $message,  
  72. 'root' => $redirect_to,  
  73. 'redirect' => false,  
  74. ); 
  75.  
  76. } else { 
  77. // When the user is logged out, pass an empty array 
  78. // This indicates that the default arguments should be 
  79. // used in bp_core_no_access(). 
  80. $retval['redirect_args'] = array(); 
  81.  
  82. return $retval;