| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Add an item to the main BuddyPress navigation array. |
|---|
| 4 | * |
|---|
| 5 | * @param array $args { |
|---|
| 6 | * Array describing the new nav item. |
|---|
| 7 | * @type string $name Display name for the nav item. |
|---|
| 8 | * @type string $slug Unique URL slug for the nav item. |
|---|
| 9 | * @type bool|string $item_css_id Optional. 'id' attribute for the nav item. Default: the value of `$slug`. |
|---|
| 10 | * @type bool $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a |
|---|
| 11 | * member profile other than your own. Default: true. |
|---|
| 12 | * @type bool $site_admin_only Optional. Whether the nav item should be visible only to site admins |
|---|
| 13 | * (those with the 'bp_moderate' cap). Default: false. |
|---|
| 14 | * @type int $position Optional. Numerical index specifying where the item should appear in |
|---|
| 15 | * the nav array. Default: 99. |
|---|
| 16 | * @type callable $screen_function The callback function that will run when the nav item is clicked. |
|---|
| 17 | * @type bool|string $default_subnav_slug Optional. The slug of the default subnav item to select when the nav |
|---|
| 18 | * item is clicked. |
|---|
| 19 | * } |
|---|
| 20 | * @return bool|null Returns false on failure. |
|---|
| 21 | */ |
|---|
| 22 | function bp_core_new_nav_item( $args = '' ) { |
|---|
| 23 | |
|---|
| 24 | $defaults = array( |
|---|
| 25 | 'name' => false, // Display name for the nav item |
|---|
| 26 | 'slug' => false, // URL slug for the nav item |
|---|
| 27 | 'item_css_id' => false, // The CSS ID to apply to the HTML of the nav item |
|---|
| 28 | 'show_for_displayed_user' => true, // When viewing another user does this nav item show up? |
|---|
| 29 | 'site_admin_only' => false, // Can only site admins see this nav item? |
|---|
| 30 | 'position' => 99, // Index of where this nav item should be positioned |
|---|
| 31 | 'screen_function' => false, // The name of the function to run when clicked |
|---|
| 32 | 'default_subnav_slug' => false // The slug of the default subnav item to select when clicked |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 36 | |
|---|
| 37 | // First, add the nav item link to the bp_nav array. |
|---|
| 38 | $created = bp_core_create_nav_link( $r ); |
|---|
| 39 | |
|---|
| 40 | // To mimic the existing behavior, if bp_core_create_nav_link() |
|---|
| 41 | // returns false, we make an early exit and don't attempt to register |
|---|
| 42 | // the screen function. |
|---|
| 43 | if ( false === $created ) { |
|---|
| 44 | return false; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | // Then, hook the screen function for the added nav item. |
|---|
| 48 | bp_core_register_nav_screen_function( $r ); |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Fires after adding an item to the main BuddyPress navigation array. |
|---|
| 52 | * |
|---|
| 53 | * @since BuddyPress (1.5.0) |
|---|
| 54 | * |
|---|
| 55 | * @param array $r Parsed arguments for the nav item. |
|---|
| 56 | * @param array $args Originally passed in arguments for the nav item. |
|---|
| 57 | * @param array $defaults Default arguments for a nav item. |
|---|
| 58 | */ |
|---|
| 59 | do_action( 'bp_core_new_nav_item', $r, $args, $defaults ); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Add a link to the main BuddyPress navigation array. |
|---|
| 64 | * |
|---|
| 65 | * @since BuddyPress (2.4.0) |
|---|
| 66 | * |
|---|
| 67 | * @param array $r { |
|---|
| 68 | * Array describing the new nav item. |
|---|
| 69 | * @type string $name Display name for the nav item. |
|---|
| 70 | * @type string $slug Unique URL slug for the nav item. |
|---|
| 71 | * @type bool|string $item_css_id Optional. 'id' attribute for the nav item. Default: the value of `$slug`. |
|---|
| 72 | * @type bool $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a |
|---|
| 73 | * member profile other than your own. Default: true. |
|---|
| 74 | * @type bool $site_admin_only Optional. Whether the nav item should be visible only to site admins |
|---|
| 75 | * (those with the 'bp_moderate' cap). Default: false. |
|---|
| 76 | * @type int $position Optional. Numerical index specifying where the item should appear in |
|---|
| 77 | * the nav array. Default: 99. |
|---|
| 78 | * @type callable $screen_function The callback function that will run when the nav item is clicked. |
|---|
| 79 | * @type bool|string $default_subnav_slug Optional. The slug of the default subnav item to select when the nav |
|---|
| 80 | * item is clicked. |
|---|
| 81 | * } |
|---|
| 82 | * @return bool|null Returns false on failure. |
|---|
| 83 | */ |
|---|
| 84 | function bp_core_create_nav_link( $r = '' ) { |
|---|
| 85 | $bp = buddypress(); |
|---|
| 86 | |
|---|
| 87 | // If we don't have the required info we need, don't create this nav item |
|---|
| 88 | if ( empty( $r['name'] ) || empty( $r['slug'] ) ) { |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // If this is for site admins only and the user is not one, don't create the subnav item |
|---|
| 93 | if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) { |
|---|
| 94 | return false; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * If this nav item is hidden for the displayed user, and |
|---|
| 99 | * the logged in user is not the displayed user |
|---|
| 100 | * looking at their own profile, don't create the nav item. |
|---|
| 101 | */ |
|---|
| 102 | if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) { |
|---|
| 103 | return false; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if ( empty( $r['item_css_id'] ) ) { |
|---|
| 107 | $r['item_css_id'] = $r['slug']; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $bp->bp_nav[$r['slug']] = array( |
|---|
| 111 | 'name' => $r['name'], |
|---|
| 112 | 'slug' => $r['slug'], |
|---|
| 113 | 'link' => trailingslashit( bp_loggedin_user_domain() . $r['slug'] ), |
|---|
| 114 | 'css_id' => $r['item_css_id'], |
|---|
| 115 | 'show_for_displayed_user' => $r['show_for_displayed_user'], |
|---|
| 116 | 'position' => $r['position'], |
|---|
| 117 | 'screen_function' => &$r['screen_function'], |
|---|
| 118 | 'default_subnav_slug' => $r['default_subnav_slug'] |
|---|
| 119 | ); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Register a screen function, whether or not a related nav link exists. |
|---|
| 124 | * |
|---|
| 125 | * @since BuddyPress (2.4.0) |
|---|
| 126 | * |
|---|
| 127 | * @param array $r { |
|---|
| 128 | * Array describing the new nav item. |
|---|
| 129 | * @type string $name Display name for the nav item. |
|---|
| 130 | * @type string $slug Unique URL slug for the nav item. |
|---|
| 131 | * @type bool|string $item_css_id Optional. 'id' attribute for the nav item. Default: the value of `$slug`. |
|---|
| 132 | * @type bool $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a |
|---|
| 133 | * member profile other than your own. Default: true. |
|---|
| 134 | * @type bool $site_admin_only Optional. Whether the nav item should be visible only to site admins |
|---|
| 135 | * (those with the 'bp_moderate' cap). Default: false. |
|---|
| 136 | * @type int $position Optional. Numerical index specifying where the item should appear in |
|---|
| 137 | * the nav array. Default: 99. |
|---|
| 138 | * @type callable $screen_function The callback function that will run when the nav item is clicked. |
|---|
| 139 | * @type bool|string $default_subnav_slug Optional. The slug of the default subnav item to select when the nav |
|---|
| 140 | * item is clicked. |
|---|
| 141 | * } |
|---|
| 142 | * @return bool|null Returns false on failure. |
|---|
| 143 | */ |
|---|
| 144 | function bp_core_register_nav_screen_function( $r = '' ) { |
|---|
| 145 | $bp = buddypress(); |
|---|
| 146 | |
|---|
| 147 | // If we don't have the required info we need, don't create this subnav item |
|---|
| 148 | if ( empty( $r['slug'] ) ) { |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | // If this is for site admins only and the user is not one, don't create the subnav item |
|---|
| 153 | if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) { |
|---|
| 154 | return false; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * If this nav item is hidden for the displayed user, and |
|---|
| 159 | * the logged in user is not the displayed user |
|---|
| 160 | * looking at their own profile, don't create the nav item. |
|---|
| 161 | */ |
|---|
| 162 | if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) { |
|---|
| 163 | return false; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | * If the nav item is visible, we are not viewing a user, and this is a root |
|---|
| 168 | * component, don't attach the default subnav function so we can display a |
|---|
| 169 | * directory or something else. |
|---|
| 170 | */ |
|---|
| 171 | if ( ( -1 != $r['position'] ) && bp_is_root_component( $r['slug'] ) && ! bp_displayed_user_id() ) { |
|---|
| 172 | return; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | // Look for current component |
|---|
| 176 | if ( bp_is_current_component( $r['slug'] ) || bp_is_current_item( $r['slug'] ) ) { |
|---|
| 177 | |
|---|
| 178 | // The requested URL has explicitly included the default subnav |
|---|
| 179 | // (eg: http://example.com/members/membername/activity/just-me/) |
|---|
| 180 | // The canonical version will not contain this subnav slug. |
|---|
| 181 | if ( ! empty( $r['default_subnav_slug'] ) && bp_is_current_action( $r['default_subnav_slug'] ) && ! bp_action_variable( 0 ) ) { |
|---|
| 182 | unset( $bp->canonical_stack['action'] ); |
|---|
| 183 | } elseif ( ! bp_current_action() ) { |
|---|
| 184 | |
|---|
| 185 | // Add our screen hook if screen function is callable |
|---|
| 186 | if ( is_callable( $r['screen_function'] ) ) { |
|---|
| 187 | add_action( 'bp_screens', $r['screen_function'], 3 ); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | if ( ! empty( $r['default_subnav_slug'] ) ) { |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * Filters the default component subnav item. |
|---|
| 194 | * |
|---|
| 195 | * @since BuddyPress (1.5.0) |
|---|
| 196 | * |
|---|
| 197 | * @param string $default_subnav_slug The slug of the default subnav item |
|---|
| 198 | * to select when clicked. |
|---|
| 199 | * @param array $r Parsed arguments for the nav item. |
|---|
| 200 | */ |
|---|
| 201 | $bp->current_action = apply_filters( 'bp_default_component_subnav', $r['default_subnav_slug'], $r ); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | } |
|---|