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