Skip to:
Content

BuddyPress.org

Ticket #6503: bp_core_new_nav_item_excerpt.03.php

File bp_core_new_nav_item_excerpt.03.php, 11.5 KB (added by dcavins, 11 years ago)
Line 
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 */
22function 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 * Note that, when possible, the more specific action hooks
53 * `bp_core_create_nav_link` or `bp_core_register_nav_screen_function`
54 * should be used.
55 *
56 * @since BuddyPress (1.5.0)
57 *
58 * @param array $r Parsed arguments for the nav item.
59 * @param array $args Originally passed in arguments for the nav item.
60 * @param array $defaults Default arguments for a nav item.
61 */
62 do_action( 'bp_core_new_nav_item', $r, $args, $defaults );
63}
64
65/**
66 * Add a link to the main BuddyPress navigation array.
67 *
68 * @since BuddyPress (2.4.0)
69 *
70 * @param array $args {
71 * Array describing the new nav item.
72 * @type string $name Display name for the nav item.
73 * @type string $slug Unique URL slug for the nav item.
74 * @type bool|string $item_css_id Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
75 * @type bool $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
76 * member profile other than your own. Default: true.
77 * @type bool $site_admin_only Optional. Whether the nav item should be visible only to site admins
78 * (those with the 'bp_moderate' cap). Default: false.
79 * @type int $position Optional. Numerical index specifying where the item should appear in
80 * the nav array. Default: 99.
81 * @type callable $screen_function The callback function that will run when the nav item is clicked.
82 * @type bool|string $default_subnav_slug Optional. The slug of the default subnav item to select when the nav
83 * item is clicked.
84 * }
85 * @return bool|null Returns false on failure.
86 */
87function bp_core_create_nav_link( $args = '' ) {
88 $bp = buddypress();
89
90 $defaults = array(
91 'name' => false, // Display name for the nav item
92 'slug' => false, // URL slug for the nav item
93 'item_css_id' => false, // The CSS ID to apply to the HTML of the nav item
94 'show_for_displayed_user' => true, // When viewing another user does this nav item show up?
95 'site_admin_only' => false, // Can only site admins see this nav item?
96 'position' => 99, // Index of where this nav item should be positioned
97 'screen_function' => false, // The name of the function to run when clicked
98 'default_subnav_slug' => false // The slug of the default subnav item to select when clicked
99 );
100
101 $r = wp_parse_args( $args, $defaults );
102
103 // If we don't have the required info we need, don't create this nav item
104 if ( empty( $r['name'] ) || empty( $r['slug'] ) ) {
105 return false;
106 }
107
108 // If this is for site admins only and the user is not one, don't create the subnav item
109 if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
110 return false;
111 }
112
113 /**
114 * If this nav item is hidden for the displayed user, and
115 * the logged in user is not the displayed user
116 * looking at their own profile, don't create the nav item.
117 */
118 if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
119 return false;
120 }
121
122 if ( empty( $r['item_css_id'] ) ) {
123 $r['item_css_id'] = $r['slug'];
124 }
125
126 $bp->bp_nav[$r['slug']] = array(
127 'name' => $r['name'],
128 'slug' => $r['slug'],
129 'link' => trailingslashit( bp_loggedin_user_domain() . $r['slug'] ),
130 'css_id' => $r['item_css_id'],
131 'show_for_displayed_user' => $r['show_for_displayed_user'],
132 'position' => $r['position'],
133 'screen_function' => &$r['screen_function'],
134 'default_subnav_slug' => $r['default_subnav_slug']
135 );
136
137 /**
138 * Fires after a link is added to the main BuddyPress navigation array.
139 *
140 * @since BuddyPress (2.4.0)
141 *
142 * @param array $r Parsed arguments for the nav item.
143 * @param array $args Originally passed in arguments for the nav item.
144 * @param array $defaults Default arguments for a nav item.
145 */
146 do_action( 'bp_core_create_nav_link', $r, $args, $defaults );
147}
148
149/**
150 * Register a screen function, whether or not a related nav link exists.
151 *
152 * @since BuddyPress (2.4.0)
153 *
154 * @param array $args {
155 * Array describing the new nav item.
156 * @type string $name Display name for the nav item.
157 * @type string $slug Unique URL slug for the nav item.
158 * @type bool|string $item_css_id Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
159 * @type bool $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
160 * member profile other than your own. Default: true.
161 * @type bool $site_admin_only Optional. Whether the nav item should be visible only to site admins
162 * (those with the 'bp_moderate' cap). Default: false.
163 * @type int $position Optional. Numerical index specifying where the item should appear in
164 * the nav array. Default: 99.
165 * @type callable $screen_function The callback function that will run when the nav item is clicked.
166 * @type bool|string $default_subnav_slug Optional. The slug of the default subnav item to select when the nav
167 * item is clicked.
168 * }
169 * @return bool|null Returns false on failure.
170 */
171function bp_core_register_nav_screen_function( $args = '' ) {
172 $bp = buddypress();
173
174 $defaults = array(
175 'name' => false, // Display name for the nav item
176 'slug' => false, // URL slug for the nav item
177 'item_css_id' => false, // The CSS ID to apply to the HTML of the nav item
178 'show_for_displayed_user' => true, // When viewing another user does this nav item show up?
179 'site_admin_only' => false, // Can only site admins see this nav item?
180 'position' => 99, // Index of where this nav item should be positioned
181 'screen_function' => false, // The name of the function to run when clicked
182 'default_subnav_slug' => false // The slug of the default subnav item to select when clicked
183 );
184
185 $r = wp_parse_args( $args, $defaults );
186
187 // If we don't have the required info we need, don't create this subnav item
188 if ( empty( $r['slug'] ) ) {
189 return false;
190 }
191
192 // If this is for site admins only and the user is not one, don't create the subnav item
193 if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
194 return false;
195 }
196
197 /**
198 * If this nav item is hidden for the displayed user, and
199 * the logged in user is not the displayed user
200 * looking at their own profile, don't create the nav item.
201 */
202 if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
203 return false;
204 }
205
206 /**
207 * If the nav item is visible, we are not viewing a user, and this is a root
208 * component, don't attach the default subnav function so we can display a
209 * directory or something else.
210 */
211 if ( ( -1 != $r['position'] ) && bp_is_root_component( $r['slug'] ) && ! bp_displayed_user_id() ) {
212 return;
213 }
214
215 // Look for current component
216 if ( bp_is_current_component( $r['slug'] ) || bp_is_current_item( $r['slug'] ) ) {
217
218 // The requested URL has explicitly included the default subnav
219 // (eg: http://example.com/members/membername/activity/just-me/)
220 // The canonical version will not contain this subnav slug.
221 if ( ! empty( $r['default_subnav_slug'] ) && bp_is_current_action( $r['default_subnav_slug'] ) && ! bp_action_variable( 0 ) ) {
222 unset( $bp->canonical_stack['action'] );
223 } elseif ( ! bp_current_action() ) {
224
225 // Add our screen hook if screen function is callable
226 if ( is_callable( $r['screen_function'] ) ) {
227 add_action( 'bp_screens', $r['screen_function'], 3 );
228 }
229
230 if ( ! empty( $r['default_subnav_slug'] ) ) {
231
232 /**
233 * Filters the default component subnav item.
234 *
235 * @since BuddyPress (1.5.0)
236 *
237 * @param string $default_subnav_slug The slug of the default subnav item
238 * to select when clicked.
239 * @param array $r Parsed arguments for the nav item.
240 */
241 $bp->current_action = apply_filters( 'bp_default_component_subnav', $r['default_subnav_slug'], $r );
242 }
243 }
244 }
245
246 /**
247 * Fires after the screen function for an item in the BuddyPress main
248 * navigation is registered.
249 *
250 * @since BuddyPress (2.4.0)
251 *
252 * @param array $r Parsed arguments for the nav item.
253 * @param array $args Originally passed in arguments for the nav item.
254 * @param array $defaults Default arguments for a nav item.
255 */
256 do_action( 'bp_core_register_nav_screen_function', $r, $args, $defaults );
257}