PluginProbe
bbPress / 2.1
bbPress v2.1
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-user-template.php

bbp-user-template.php in bbPress 2.1, at bbp-includes/bbp-user-template.php

1,538 lines 46.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress User Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /** Users *********************************************************************/
14
15 /**
16 * Output a validated user id
17 *
18 * @since bbPress (r2729)
19 *
20 * @param int $user_id Optional. User id
21 * @param bool $displayed_user_fallback Fallback on displayed user?
22 * @param bool $current_user_fallback Fallback on current user?
23 * @uses bbp_get_user_id() To get the user id
24 */
25 function bbp_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
26 echo bbp_get_user_id( $user_id, $displayed_user_fallback, $current_user_fallback );
27 }
28 /**
29 * Return a validated user id
30 *
31 * @since bbPress (r2729)
32 *
33 * @param int $user_id Optional. User id
34 * @param bool $displayed_user_fallback Fallback on displayed user?
35 * @param bool $current_user_fallback Fallback on current user?
36 * @uses get_query_var() To get the 'bbp_user_id' query var
37 * @uses apply_filters() Calls 'bbp_get_user_id' with the user id
38 * @return int Validated user id
39 */
40 function bbp_get_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
41 $bbp = bbpress();
42
43 // Easy empty checking
44 if ( !empty( $user_id ) && is_numeric( $user_id ) )
45 $bbp_user_id = $user_id;
46
47 // Currently viewing or editing a user
48 elseif ( ( true == $displayed_user_fallback ) && !empty( $bbp->displayed_user->ID ) )
49 $bbp_user_id = $bbp->displayed_user->ID;
50
51 // Maybe fallback on the current_user ID
52 elseif ( ( true == $current_user_fallback ) && !empty( $bbp->current_user->ID ) )
53 $bbp_user_id = $bbp->current_user->ID;
54
55 // Failsafe
56 else
57 $bbp_user_id = get_query_var( 'bbp_user_id' );
58
59 return (int) apply_filters( 'bbp_get_user_id', (int) $bbp_user_id, $displayed_user_fallback, $current_user_fallback );
60 }
61
62 /**
63 * Output ID of current user
64 *
65 * @since bbPress (r2574)
66 *
67 * @uses bbp_get_current_user_id() To get the current user id
68 */
69 function bbp_current_user_id() {
70 echo bbp_get_current_user_id();
71 }
72 /**
73 * Return ID of current user
74 *
75 * @since bbPress (r2574)
76 *
77 * @uses bbp_get_user_id() To get the current user id
78 * @uses apply_filters() Calls 'bbp_get_current_user_id' with the id
79 * @return int Current user id
80 */
81 function bbp_get_current_user_id() {
82 return apply_filters( 'bbp_get_current_user_id', bbp_get_user_id( 0, false, true ) );
83 }
84
85 /**
86 * Output ID of displayed user
87 *
88 * @since bbPress (r2688)
89 *
90 * @uses bbp_get_displayed_user_id() To get the displayed user id
91 */
92 function bbp_displayed_user_id() {
93 echo bbp_get_displayed_user_id();
94 }
95 /**
96 * Return ID of displayed user
97 *
98 * @since bbPress (r2688)
99 *
100 * @uses bbp_get_user_id() To get the displayed user id
101 * @uses apply_filters() Calls 'bbp_get_displayed_user_id' with the id
102 * @return int Displayed user id
103 */
104 function bbp_get_displayed_user_id() {
105 return apply_filters( 'bbp_get_displayed_user_id', bbp_get_user_id( 0, true, false ) );
106 }
107
108 /**
109 * Output a sanitized user field value
110 *
111 * @since bbPress (r2688)
112 *
113 * @param string $field Field to get
114 * @uses bbp_get_displayed_user_field() To get the field
115 */
116 function bbp_displayed_user_field( $field = '' ) {
117 echo bbp_get_displayed_user_field( $field );
118 }
119 /**
120 * Return a sanitized user field value
121 *
122 * @since bbPress (r2688)
123 *
124 * @param string $field Field to get
125 * @uses sanitize_text_field() To sanitize the field
126 * @uses esc_attr() To sanitize the field
127 * @return string|bool Value of the field if it exists, else false
128 */
129 function bbp_get_displayed_user_field( $field = '' ) {
130 $bbp = bbpress();
131
132 // Return field if exists
133 if ( isset( $bbp->displayed_user->$field ) )
134 return esc_attr( sanitize_text_field( $bbp->displayed_user->$field ) );
135
136 // Return empty
137 return false;
138 }
139
140 /**
141 * Output name of current user
142 *
143 * @since bbPress (r2574)
144 *
145 * @uses bbp_get_current_user_name() To get the current user name
146 */
147 function bbp_current_user_name() {
148 echo bbp_get_current_user_name();
149 }
150 /**
151 * Return name of current user
152 *
153 * @since bbPress (r2574)
154 *
155 * @uses apply_filters() Calls 'bbp_get_current_user_name' with the
156 * current user name
157 * @return string
158 */
159 function bbp_get_current_user_name() {
160 global $user_identity;
161
162 $current_user_name = is_user_logged_in() ? $user_identity : __( 'Anonymous', 'bbpress' );
163
164 return apply_filters( 'bbp_get_current_user_name', $current_user_name );
165 }
166
167 /**
168 * Output avatar of current user
169 *
170 * @since bbPress (r2574)
171 *
172 * @param int $size Size of the avatar. Defaults to 40
173 * @uses bbp_get_current_user_avatar() To get the current user avatar
174 */
175 function bbp_current_user_avatar( $size = 40 ) {
176 echo bbp_get_current_user_avatar( $size );
177 }
178
179 /**
180 * Return avatar of current user
181 *
182 * @since bbPress (r2574)
183 *
184 * @param int $size Size of the avatar. Defaults to 40
185 * @uses bbp_get_current_user_id() To get the current user id
186 * @uses bbp_get_current_anonymous_user_data() To get the current
187 * anonymous user's email
188 * @uses get_avatar() To get the avatar
189 * @uses apply_filters() Calls 'bbp_get_current_user_avatar' with the
190 * avatar and size
191 * @return string Current user avatar
192 */
193 function bbp_get_current_user_avatar( $size = 40 ) {
194
195 $user = bbp_get_current_user_id();
196 if ( empty( $user ) )
197 $user = bbp_get_current_anonymous_user_data( 'email' );
198
199 $avatar = get_avatar( $user, $size );
200
201 return apply_filters( 'bbp_get_current_user_avatar', $avatar, $size );
202 }
203
204 /**
205 * Output link to the profile page of a user
206 *
207 * @since bbPress (r2688)
208 *
209 * @param int $user_id Optional. User id
210 * @uses bbp_get_user_profile_link() To get user profile link
211 */
212 function bbp_user_profile_link( $user_id = 0 ) {
213 echo bbp_get_user_profile_link( $user_id );
214 }
215 /**
216 * Return link to the profile page of a user
217 *
218 * @since bbPress (r2688)
219 *
220 * @param int $user_id Optional. User id
221 * @uses bbp_get_user_id() To get user id
222 * @uses get_userdata() To get user data
223 * @uses bbp_get_user_profile_url() To get user profile url
224 * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the user
225 * profile link and user id
226 * @return string User profile link
227 */
228 function bbp_get_user_profile_link( $user_id = 0 ) {
229
230 // Validate user id
231 $user_id = bbp_get_user_id( $user_id );
232 if ( empty( $user_id ) )
233 return false;
234
235 $user = get_userdata( $user_id );
236 $name = esc_attr( $user->display_name );
237 $user_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . $name . '">' . $name . '</a>';
238
239 return apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id );
240 }
241
242 /**
243 * Output URL to the profile page of a user
244 *
245 * @since bbPress (r2688)
246 *
247 * @param int $user_id Optional. User id
248 * @param string $user_nicename Optional. User nicename
249 * @uses bbp_get_user_profile_url() To get user profile url
250 */
251 function bbp_user_profile_url( $user_id = 0, $user_nicename = '' ) {
252 echo bbp_get_user_profile_url( $user_id, $user_nicename );
253 }
254 /**
255 * Return URL to the profile page of a user
256 *
257 * @since bbPress (r2688)
258 *
259 * @param int $user_id Optional. User id
260 * @param string $user_nicename Optional. User nicename
261 * @uses bbp_get_user_id() To get user id
262 * @uses WP_Rewrite::using_permalinks() To check if the blog is using
263 * permalinks
264 * @uses add_query_arg() To add custom args to the url
265 * @uses home_url() To get blog home url
266 * @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user
267 * profile url, user id and user nicename
268 * @return string User profile url
269 */
270 function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) {
271 global $wp_rewrite;
272
273 // Use displayed user ID if there is one, and one isn't requested
274 $user_id = bbp_get_user_id( $user_id );
275 if ( empty( $user_id ) )
276 return false;
277
278 // Allow early overriding of the profile URL to cut down on processing
279 $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', (int) $user_id );
280 if ( is_string( $early_profile_url ) )
281 return $early_profile_url;
282
283 // Pretty permalinks
284 if ( $wp_rewrite->using_permalinks() ) {
285 $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%';
286
287 // Get username if not passed
288 if ( empty( $user_nicename ) ) {
289 $user = get_userdata( $user_id );
290 if ( !empty( $user->user_nicename ) ) {
291 $user_nicename = $user->user_nicename;
292 }
293 }
294
295 $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
296 $url = home_url( user_trailingslashit( $url ) );
297
298 // Unpretty permalinks
299 } else {
300 $url = add_query_arg( array( bbp_get_user_rewrite_id() => $user_id ), home_url( '/' ) );
301 }
302
303 return apply_filters( 'bbp_get_user_profile_url', $url, $user_id, $user_nicename );
304
305 }
306
307 /**
308 * Output link to the profile edit page of a user
309 *
310 * @since bbPress (r2688)
311 *
312 * @param int $user_id Optional. User id
313 * @uses bbp_get_user_profile_edit_link() To get user profile edit link
314 */
315 function bbp_user_profile_edit_link( $user_id = 0 ) {
316 echo bbp_get_user_profile_edit_link( $user_id );
317 }
318 /**
319 * Return link to the profile edit page of a user
320 *
321 * @since bbPress (r2688)
322 *
323 * @param int $user_id Optional. User id
324 * @uses bbp_get_user_id() To get user id
325 * @uses get_userdata() To get user data
326 * @uses bbp_get_user_profile_edit_url() To get user profile edit url
327 * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the edit
328 * link and user id
329 * @return string User profile edit link
330 */
331 function bbp_get_user_profile_edit_link( $user_id = 0 ) {
332
333 // Validate user id
334 $user_id = bbp_get_user_id( $user_id );
335 if ( empty( $user_id ) )
336 return false;
337
338 $user = get_userdata( $user_id );
339 $name = $user->display_name;
340 $edit_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . esc_attr( $name ) . '">' . $name . '</a>';
341 return apply_filters( 'bbp_get_user_profile_link', $edit_link, $user_id );
342 }
343
344 /**
345 * Output URL to the profile edit page of a user
346 *
347 * @since bbPress (r2688)
348 *
349 * @param int $user_id Optional. User id
350 * @param string $user_nicename Optional. User nicename
351 * @uses bbp_get_user_profile_edit_url() To get user profile edit url
352 */
353 function bbp_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
354 echo bbp_get_user_profile_edit_url( $user_id, $user_nicename );
355 }
356 /**
357 * Return URL to the profile edit page of a user
358 *
359 * @since bbPress (r2688)
360 *
361 * @param int $user_id Optional. User id
362 * @param string $user_nicename Optional. User nicename
363 * @uses bbp_get_user_id() To get user id
364 * @uses WP_Rewrite::using_permalinks() To check if the blog is using
365 * permalinks
366 * @uses add_query_arg() To add custom args to the url
367 * @uses home_url() To get blog home url
368 * @uses apply_filters() Calls 'bbp_get_user_edit_profile_url' with the
369 * edit profile url, user id and user nicename
370 * @return string
371 */
372 function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
373 global $wp_rewrite;
374
375 $bbp = bbpress();
376 $user_id = bbp_get_user_id( $user_id );
377 if ( empty( $user_id ) )
378 return false;
379
380 // Pretty permalinks
381 if ( $wp_rewrite->using_permalinks() ) {
382 $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . $bbp->user_id . '%/' . $bbp->edit_id;
383
384 // Get username if not passed
385 if ( empty( $user_nicename ) ) {
386 $user = get_userdata( $user_id );
387 if ( !empty( $user->user_nicename ) ) {
388 $user_nicename = $user->user_nicename;
389 }
390 }
391
392 $url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url );
393 $url = home_url( user_trailingslashit( $url ) );
394
395 // Unpretty permalinks
396 } else {
397 $url = add_query_arg( array( $bbp->user_id => $user_id, $bbp->edit_id => '1' ), home_url( '/' ) );
398 }
399
400 return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename );
401
402 }
403
404 /**
405 * Output a user's main role for display
406 *
407 * @since bbPress (r3860)
408 *
409 * @param int $user_id
410 * @uses bbp_get_user_display_role To get the user display role
411 */
412 function bbp_user_display_role( $user_id = 0 ) {
413 echo bbp_get_user_display_role( $user_id );
414 }
415 /**
416 * Return a user's main role for display
417 *
418 * @since bbPress (r3860)
419 *
420 * @param int $user_id
421 * @uses bbp_get_user_role() To get the main user role
422 * @uses bbp_get_moderator_role() To get the moderator role
423 * @uses bbp_get_participant_role() To get the participant role
424 * @uses bbp_get_moderator_role() To get the moderator role
425 * @uses apply_filters() Calls 'bbp_get_user_display_role' with the
426 * display role, user id, and user role
427 * @return string
428 */
429 function bbp_get_user_display_role( $user_id = 0 ) {
430
431 // Validate user id
432 $user_id = bbp_get_user_id( $user_id, false, false );
433 $user_role = bbp_get_user_role( $user_id );
434
435 // Capes earn Vinz Clortho status
436 if ( is_super_admin( $user_id ) ) {
437 $role = __( 'Key Master', 'bbpress' );
438
439 // Not the keymaster of Gozer
440 } else {
441
442 // Get the user's main role for display
443 switch ( $user_role ) {
444
445 /** bbPress Roles *********************************************/
446
447 // Anonymous
448 case bbp_get_anonymous_role() :
449 $role = __( 'Guest', 'bbpress' );
450 break;
451
452 // Multisite Participant Role
453 case bbp_get_participant_role() :
454 $role = __( 'Member', 'bbpress' );
455 break;
456
457 // Moderator
458 case bbp_get_moderator_role() :
459 $role = __( 'Moderator', 'bbpress' );
460 break;
461
462 /** WordPress Core Roles **************************************/
463
464 case 'administrator' :
465 case 'editor' :
466 case 'author' :
467 case 'contributor' :
468 case 'subscriber' :
469 default : // Any other role (plugins, etc...)
470 global $wp_roles;
471
472 // Load roles if not set
473 if ( !isset( $wp_roles ) )
474 $wp_roles = new WP_Roles();
475
476 // Get a translated role name
477 if ( !empty( $wp_roles->role_names[$user_role] ) )
478 $role = translate_user_role( $wp_roles->role_names[$user_role] );
479
480 // Fallback for registered user
481 else
482 $role = __( 'Member', 'bbpress' );
483
484 break;
485 }
486 }
487
488 return apply_filters( 'bbp_get_user_display_role', $role, $user_id, $user_role );
489 }
490
491 /**
492 * Output the link to the admin section
493 *
494 * @since bbPress (r2827)
495 *
496 * @param mixed $args Optional. See {@link bbp_get_admin_link()}
497 * @uses bbp_get_admin_link() To get the admin link
498 */
499 function bbp_admin_link( $args = '' ) {
500 echo bbp_get_admin_link( $args );
501 }
502 /**
503 * Return the link to the admin section
504 *
505 * @since bbPress (r2827)
506 *
507 * @param mixed $args Optional. This function supports these arguments:
508 * - text: The text
509 * - before: Before the lnk
510 * - after: After the link
511 * @uses current_user_can() To check if the current user can moderate
512 * @uses admin_url() To get the admin url
513 * @uses apply_filters() Calls 'bbp_get_admin_link' with the link & args
514 * @return The link
515 */
516 function bbp_get_admin_link( $args = '' ) {
517 if ( !current_user_can( 'moderate' ) )
518 return;
519
520 if ( !empty( $args ) && is_string( $args ) && ( false === strpos( $args, '=' ) ) )
521 $args = array( 'text' => $args );
522
523 $defaults = array(
524 'text' => __( 'Admin', 'bbpress' ),
525 'before' => '',
526 'after' => ''
527 );
528 $args = bbp_parse_args( $args, $defaults, 'get_admin_link' );
529 extract( $args, EXTR_SKIP );
530
531 $uri = admin_url();
532 $retval = $before . '<a href="' . $uri . '">' . $text . '</a>' . $after;
533
534 return apply_filters( 'bbp_get_admin_link', $retval, $args );
535 }
536
537 /** User IP *******************************************************************/
538
539 /**
540 * Output the author IP address of a post
541 *
542 * @since bbPress (r3120)
543 *
544 * @param mixed $args Optional. If it is an integer, it is used as post id.
545 * @uses bbp_get_author_ip() To get the post author link
546 */
547 function bbp_author_ip( $args = '' ) {
548 echo bbp_get_author_ip( $args );
549 }
550 /**
551 * Return the author IP address of a post
552 *
553 * @since bbPress (r3120)
554 *
555 * @param mixed $args Optional. If an integer, it is used as reply id.
556 * @uses get_post_meta() To check if it's a topic page
557 * @return string Author link of reply
558 */
559 function bbp_get_author_ip( $args = '' ) {
560
561 // Default arguments
562 $defaults = array(
563 'post_id' => 0,
564 'before' => '<span class="bbp-author-ip">(',
565 'after' => ')</span>'
566 );
567
568 $r = bbp_parse_args( $args, $defaults, 'get_author_ip' );
569 extract( $r );
570
571 // Used as post id
572 if ( is_numeric( $args ) )
573 $post_id = $args;
574
575 // Get the author IP meta value
576 $author_ip = get_post_meta( $post_id, '_bbp_author_ip', true );
577 if ( !empty( $author_ip ) ) {
578 $author_ip = $before . $author_ip . $after;
579
580 // No IP address
581 } else {
582 $author_ip = '';
583 }
584
585 return apply_filters( 'bbp_get_author_ip', $author_ip, $args );
586 }
587
588 /** Favorites *****************************************************************/
589
590 /**
591 * Output the link to the user's favorites page (profile page)
592 *
593 * @since bbPress (r2652)
594 *
595 * @param int $user_id Optional. User id
596 * @uses bbp_get_favorites_permalink() To get the favorites permalink
597 */
598 function bbp_favorites_permalink( $user_id = 0 ) {
599 echo bbp_get_favorites_permalink( $user_id );
600 }
601 /**
602 * Return the link to the user's favorites page (profile page)
603 *
604 * @since bbPress (r2652)
605 *
606 * @param int $user_id Optional. User id
607 * @uses bbp_get_user_profile_url() To get the user profile url
608 * @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the
609 * user profile url and user id
610 * @return string Permanent link to user profile page
611 */
612 function bbp_get_favorites_permalink( $user_id = 0 ) {
613 return apply_filters( 'bbp_get_favorites_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
614 }
615
616 /**
617 * Output the link to make a topic favorite/remove a topic from favorites
618 *
619 * @since bbPress (r2652)
620 *
621 * @param array $add Optional. Add to favorites args
622 * @param array $rem Optional. Remove from favorites args
623 * @param int $user_id Optional. User id
624 * @param int $topic_id Optional. Topic id
625 * @uses bbp_get_user_favorites_link() To get the user favorites link
626 */
627 function bbp_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
628 echo bbp_get_user_favorites_link( $add, $rem, $user_id, $topic_id );
629 }
630 /**
631 * User favorites link
632 *
633 * Return the link to make a topic favorite/remove a topic from
634 * favorites
635 *
636 * @since bbPress (r2652)
637 *
638 * @param array $add Optional. Add to favorites args
639 * @param array $rem Optional. Remove from favorites args
640 * @param int $user_id Optional. User id
641 * @param int $topic_id Optional. Topic id
642 * @uses bbp_get_user_id() To get the user id
643 * @uses current_user_can() If the current user can edit the user
644 * @uses bbp_get_topic_id() To get the topic id
645 * @uses bbp_is_user_favorite() To check if the topic is user's favorite
646 * @uses bbp_get_favorites_permalink() To get the favorites permalink
647 * @uses bbp_get_topic_permalink() To get the topic permalink
648 * @uses bbp_is_favorites() Is it the favorites page?
649 * @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the
650 * html, add args, remove args, user & topic id
651 * @return string User favorites link
652 */
653 function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
654 if ( !bbp_is_favorites_active() )
655 return false;
656
657 // Validate user and topic ID's
658 $user_id = bbp_get_user_id( $user_id, true, true );
659 $topic_id = bbp_get_topic_id( $topic_id );
660 if ( empty( $user_id ) || empty( $topic_id ) )
661 return false;
662
663 if ( !current_user_can( 'edit_user', (int) $user_id ) )
664 return false;
665
666 if ( empty( $add ) || !is_array( $add ) ) {
667 $add = array(
668 'mid' => __( 'Add this topic to your favorites', 'bbpress' ),
669 'post' => __( ' (%?%)', 'bbpress' )
670 );
671 }
672
673 if ( empty( $rem ) || !is_array( $rem ) ) {
674 $rem = array(
675 'pre' => __( 'This topic is one of your %favorites% [', 'bbpress' ),
676 'mid' => __( '&times;', 'bbpress' ),
677 'post' => __( ']', 'bbpress' )
678 );
679 }
680
681 $is_fav = bbp_is_user_favorite( $user_id, $topic_id );
682 if ( !empty( $is_fav ) ) {
683 $url = esc_url( bbp_get_favorites_permalink( $user_id ) );
684 $rem = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
685 $favs = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
686 $pre = ( is_array( $rem ) && isset( $rem['pre'] ) ) ? $rem['pre'] : '';
687 $mid = ( is_array( $rem ) && isset( $rem['mid'] ) ) ? $rem['mid'] : ( is_string( $rem ) ? $rem : '' );
688 $_post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
689 } else {
690 $url = esc_url( bbp_get_favorites_permalink( $user_id ) );
691 $add = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
692 $favs = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
693 $pre = ( is_array( $add ) && isset( $add['pre'] ) ) ? $add['pre'] : '';
694 $mid = ( is_array( $add ) && isset( $add['mid'] ) ) ? $add['mid'] : ( is_string( $add ) ? $add : '' );
695 $_post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
696 }
697
698 // Create the link based where the user is and if the topic is
699 // already the user's favorite
700 if ( bbp_is_favorites() ) {
701 $permalink = bbp_get_favorites_permalink( $user_id );
702 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
703 $permalink = bbp_get_topic_permalink( $topic_id );
704 } elseif ( is_singular( bbp_get_reply_post_type() ) ) {
705 $permalink = bbp_get_topic_permalink( $topic_id );
706 } elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
707 $permalink = get_permalink();
708 }
709
710 $url = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
711 $is_fav = $is_fav ? 'is-favorite' : '';
712 $html = '<span id="favorite-toggle"><span id="favorite-' . $topic_id . '" class="' . $is_fav . '">' . $pre . '<a href="' . $url . '" class="dim:favorite-toggle:favorite-' . $topic_id . ':is-favorite">' . $mid . '</a>' . $_post . '</span></span>';
713
714 // Return the link
715 return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id );
716 }
717
718 /** Subscriptions *************************************************************/
719
720 /**
721 * Output the link to the user's subscriptions page (profile page)
722 *
723 * @since bbPress (r2688)
724 *
725 * @param int $user_id Optional. User id
726 * @uses bbp_get_subscriptions_permalink() To get the subscriptions link
727 */
728 function bbp_subscriptions_permalink( $user_id = 0 ) {
729 echo bbp_get_subscriptions_permalink( $user_id );
730 }
731 /**
732 * Return the link to the user's subscriptions page (profile page)
733 *
734 * @since bbPress (r2688)
735 *
736 * @param int $user_id Optional. User id
737 * @uses bbp_get_user_profile_url() To get the user profile url
738 * @uses apply_filters() Calls 'bbp_get_subscriptions_permalink' with
739 * the user profile url and user id
740 * @return string Permanent link to user subscriptions page
741 */
742 function bbp_get_subscriptions_permalink( $user_id = 0 ) {
743 return apply_filters( 'bbp_get_subscriptions_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
744 }
745
746 /**
747 * Output the link to subscribe/unsubscribe from a topic
748 *
749 * @since bbPress (r2668)
750 *
751 * @param mixed $args See {@link bbp_get_user_subscribe_link()}
752 * @uses bbp_get_user_subscribe_link() To get the subscribe link
753 */
754 function bbp_user_subscribe_link( $args = '' ) {
755 echo bbp_get_user_subscribe_link( $args );
756 }
757 /**
758 * Return the link to subscribe/unsubscribe from a topic
759 *
760 * @since bbPress (r2668)
761 *
762 * @param mixed $args This function supports these arguments:
763 * - subscribe: Subscribe text
764 * - unsubscribe: Unsubscribe text
765 * - user_id: User id
766 * - topic_id: Topic id
767 * - before: Before the link
768 * - after: After the link
769 * @param int $user_id Optional. User id
770 * @uses bbp_get_user_id() To get the user id
771 * @uses current_user_can() To check if the current user can edit user
772 * @uses bbp_get_topic_id() To get the topic id
773 * @uses bbp_is_user_subscribed() To check if the user is subscribed
774 * @uses bbp_is_subscriptions() To check if it's the subscriptions page
775 * @uses bbp_get_subscriptions_permalink() To get subscriptions link
776 * @uses bbp_get_topic_permalink() To get topic link
777 * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the
778 * link, args, user id & topic id
779 * @return string Permanent link to topic
780 */
781 function bbp_get_user_subscribe_link( $args = '', $user_id = 0 ) {
782 if ( !bbp_is_subscriptions_active() )
783 return;
784
785 $defaults = array (
786 'subscribe' => __( 'Subscribe', 'bbpress' ),
787 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
788 'user_id' => 0,
789 'topic_id' => 0,
790 'before' => '&nbsp;|&nbsp;',
791 'after' => ''
792 );
793 $args = bbp_parse_args( $args, $defaults, 'get_user_subscribe_link' );
794 extract( $args );
795
796 // Validate user and topic ID's
797 $user_id = bbp_get_user_id( $user_id, true, true );
798 $topic_id = bbp_get_topic_id( $topic_id );
799 if ( empty( $user_id ) || empty( $topic_id ) ) {
800 return false;
801 }
802
803 // No link if you can't edit yourself
804 if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
805 return false;
806 }
807
808 // Decine which link to show
809 $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id );
810 if ( !empty( $is_subscribed ) ) {
811 $text = $unsubscribe;
812 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
813 } else {
814 $text = $subscribe;
815 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
816 }
817
818 // Create the link based where the user is and if the user is
819 // subscribed already
820 if ( bbp_is_subscriptions() ) {
821 $permalink = bbp_get_subscriptions_permalink( $user_id );
822 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
823 $permalink = bbp_get_topic_permalink( $topic_id );
824 } elseif ( is_singular( bbp_get_reply_post_type() ) ) {
825 $permalink = bbp_get_topic_permalink( $topic_id );
826 } elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
827 $permalink = get_permalink();
828 }
829
830 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
831 $is_subscribed = $is_subscribed ? 'is-subscribed' : '';
832 $html = '<span id="subscription-toggle">' . $before . '<span id="subscribe-' . $topic_id . '" class="' . $is_subscribed . '"><a href="' . $url . '" class="dim:subscription-toggle:subscribe-' . $topic_id . ':is-subscribed">' . $text . '</a></span>' . $after . '</span>';
833
834 // Return the link
835 return apply_filters( 'bbp_get_user_subscribe_link', $html, $args, $user_id, $topic_id );
836 }
837
838
839 /** Edit User *****************************************************************/
840
841 /**
842 * Edit profile success message
843 *
844 * @since bbPress (r2688)
845 *
846 * @uses bbp_is_single_user() To check if it's the profile page
847 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
848 */
849 function bbp_notice_edit_user_success() {
850 if ( isset( $_GET['updated'] ) && ( bbp_is_single_user() || bbp_is_single_user_edit() ) ) : ?>
851
852 <div class="bbp-template-notice updated">
853 <p><?php _e( 'User updated.', 'bbpress' ); ?></p>
854 </div>
855
856 <?php endif;
857 }
858
859 /**
860 * Super admin privileges notice
861 *
862 * @since bbPress (r2688)
863 *
864 * @uses is_multisite() To check if the blog is multisite
865 * @uses bbp_is_single_user() To check if it's the profile page
866 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
867 * @uses current_user_can() To check if the current user can manage network
868 * options
869 * @uses bbp_get_displayed_user_id() To get the displayed user id
870 * @uses is_super_admin() To check if the user is super admin
871 * @uses bbp_is_user_home() To check if it's the user home
872 * @uses bbp_is_user_home_edit() To check if it's the user home edit
873 */
874 function bbp_notice_edit_user_is_super_admin() {
875 if ( is_multisite() && ( bbp_is_single_user() || bbp_is_single_user_edit() ) && current_user_can( 'manage_network_options' ) && is_super_admin( bbp_get_displayed_user_id() ) ) : ?>
876
877 <div class="bbp-template-notice important">
878 <p><?php bbp_is_user_home() || bbp_is_user_home_edit() ? _e( 'You have super admin privileges.', 'bbpress' ) : _e( 'This user has super admin privileges.', 'bbpress' ); ?></p>
879 </div>
880
881 <?php endif;
882 }
883
884 /**
885 * Drop down for selecting the user's display name
886 *
887 * @since bbPress (r2688)
888 */
889 function bbp_edit_user_display_name() {
890 $bbp = bbpress();
891 $public_display = array();
892 $public_display['display_username'] = $bbp->displayed_user->user_login;
893
894 if ( !empty( $bbp->displayed_user->nickname ) )
895 $public_display['display_nickname'] = $bbp->displayed_user->nickname;
896
897 if ( !empty( $bbp->displayed_user->first_name ) )
898 $public_display['display_firstname'] = $bbp->displayed_user->first_name;
899
900 if ( !empty( $bbp->displayed_user->last_name ) )
901 $public_display['display_lastname'] = $bbp->displayed_user->last_name;
902
903 if ( !empty( $bbp->displayed_user->first_name ) && !empty( $bbp->displayed_user->last_name ) ) {
904 $public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name;
905 $public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name;
906 }
907
908 if ( !in_array( $bbp->displayed_user->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
909 $public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display;
910
911 $public_display = array_map( 'trim', $public_display );
912 $public_display = array_unique( $public_display ); ?>
913
914 <select name="display_name" id="display_name">
915
916 <?php foreach ( $public_display as $id => $item ) : ?>
917
918 <option id="<?php echo $id; ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo $item; ?></option>
919
920 <?php endforeach; ?>
921
922 </select>
923
924 <?php
925 }
926
927 /**
928 * Output role selector (for user edit)
929 *
930 * @since bbPress (r2688)
931 */
932 function bbp_edit_user_role() {
933
934 // Return if no user is displayed
935 if ( !isset( bbpress()->displayed_user ) )
936 return;
937
938 // Local variables
939 $p = $r = '';
940
941 // print the 'no role' option. Make it selected if the user has no role yet.
942 $user_role = array_shift( bbpress()->displayed_user->roles );
943 if ( empty( $user_role ) )
944 $r .= '<option value="">' . __( '&mdash; No role for this site &mdash;', 'bbpress' ) . '</option>';
945
946 // Loop through roles
947 foreach ( get_editable_roles() as $role => $details ) {
948 $name = translate_user_role( $details['name'] );
949
950 // Make default first in list
951 if ( $user_role == $role ) {
952 $p = "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>{$name}</option>";
953 } else {
954 $r .= "\n\t<option value='" . esc_attr( $role ) . "'>{$name}</option>";
955 }
956 }
957
958 // Output result
959 echo '<select name="role" id="role">' . $p . $r . '</select>';
960 }
961
962 /**
963 * Return user contact methods Selectbox
964 *
965 * @since bbPress (r2688)
966 *
967 * @uses _wp_get_user_contactmethods() To get the contact methods
968 * @uses apply_filters() Calls 'bbp_edit_user_contact_methods' with the methods
969 * @return string User contact methods
970 */
971 function bbp_edit_user_contact_methods() {
972
973 // Get the core WordPress contact methods
974 $contact_methods = _wp_get_user_contactmethods( bbpress()->displayed_user );
975
976 return apply_filters( 'bbp_edit_user_contact_methods', $contact_methods );
977 }
978
979 /** Login *********************************************************************/
980
981 /**
982 * Handle the login and registration template notices
983 *
984 * @since bbPress (r2970)
985 *
986 * @uses WP_Error bbPress::errors::add() To add an error or message
987 */
988 function bbp_login_notices() {
989
990 // loggedout was passed
991 if ( !empty( $_GET['loggedout'] ) && ( true == $_GET['loggedout'] ) ) {
992 bbp_add_error( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' );
993
994 // registration is disabled
995 } elseif ( !empty( $_GET['registration'] ) && ( 'disabled' == $_GET['registration'] ) ) {
996 bbp_add_error( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) );
997
998 // Prompt user to check their email
999 } elseif ( !empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ) ) ) {
1000
1001 switch ( $_GET['checkemail'] ) {
1002
1003 // Email needs confirmation
1004 case 'confirm' :
1005 bbp_add_error( 'confirm', __( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' );
1006 break;
1007
1008 // User requested a new password
1009 case 'newpass' :
1010 bbp_add_error( 'newpass', __( 'Check your e-mail for your new password.', 'bbpress' ), 'message' );
1011 break;
1012
1013 // User is newly registered
1014 case 'registered' :
1015 bbp_add_error( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
1016 break;
1017 }
1018 }
1019 }
1020
1021 /**
1022 * Redirect a user back to their profile if they are already logged in.
1023 *
1024 * This should be used before {@link get_header()} is called in template files
1025 * where the user should never have access to the contents of that file.
1026 *
1027 * @since bbPress (r2815)
1028 *
1029 * @param string $url The URL to redirect to
1030 * @uses is_user_logged_in() Check if user is logged in
1031 * @uses wp_safe_redirect() To safely redirect
1032 * @uses bbp_get_user_profile_url() To get the profile url of the user
1033 * @uses bbp_get_current_user_id() To get the current user id
1034 */
1035 function bbp_logged_in_redirect( $url = '' ) {
1036
1037 // Bail if user is not logged in
1038 if ( !is_user_logged_in() )
1039 return;
1040
1041 // Setup the profile page to redirect to
1042 $redirect_to = !empty( $url ) ? $url : bbp_get_user_profile_url( bbp_get_current_user_id() );
1043
1044 // Do a safe redirect and exit
1045 wp_safe_redirect( $redirect_to );
1046 exit;
1047 }
1048
1049 /**
1050 * Output the required hidden fields when logging in
1051 *
1052 * @since bbPress (r2815)
1053 *
1054 * @uses apply_filters() To allow custom redirection
1055 * @uses bbp_redirect_to_field() To output the hidden request url field
1056 * @uses wp_nonce_field() To generate hidden nonce fields
1057 */
1058 function bbp_user_login_fields() {
1059 ?>
1060
1061 <input type="hidden" name="user-cookie" value="1" />
1062
1063 <?php
1064
1065 // Allow custom login redirection
1066 $redirect_to = apply_filters( 'bbp_user_login_redirect_to', '' );
1067 bbp_redirect_to_field( $redirect_to );
1068
1069 // Prevent intention hi-jacking of log-in form
1070 wp_nonce_field( 'bbp-user-login' );
1071 }
1072
1073 /** Register ******************************************************************/
1074
1075 /**
1076 * Output the required hidden fields when registering
1077 *
1078 * @since bbPress (r2815)
1079 *
1080 * @uses add_query_arg() To add query args
1081 * @uses bbp_login_url() To get the login url
1082 * @uses apply_filters() To allow custom redirection
1083 * @uses bbp_redirect_to_field() To output the redirect to field
1084 * @uses wp_nonce_field() To generate hidden nonce fields
1085 */
1086 function bbp_user_register_fields() {
1087 ?>
1088
1089 <input type="hidden" name="action" value="register" />
1090 <input type="hidden" name="user-cookie" value="1" />
1091
1092 <?php
1093
1094 // Allow custom registration redirection
1095 $redirect_to = apply_filters( 'bbp_user_register_redirect_to', '' );
1096 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'registered' ), $redirect_to ) );
1097
1098 // Prevent intention hi-jacking of sign-up form
1099 wp_nonce_field( 'bbp-user-register' );
1100 }
1101
1102 /** Lost Password *************************************************************/
1103
1104 /**
1105 * Output the required hidden fields when user lost password
1106 *
1107 * @since bbPress (r2815)
1108 *
1109 * @uses apply_filters() To allow custom redirection
1110 * @uses wp_referer_field() Set referer
1111 * @uses wp_nonce_field() To generate hidden nonce fields
1112 */
1113 function bbp_user_lost_pass_fields() {
1114 ?>
1115
1116 <input type="hidden" name="user-cookie" value="1" />
1117
1118 <?php
1119
1120 // Allow custom lost pass redirection
1121 $redirect_to = apply_filters( 'bbp_user_lost_pass_redirect_to', get_permalink() );
1122 bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'confirm' ), $redirect_to ) );
1123
1124 // Prevent intention hi-jacking of lost pass form
1125 wp_nonce_field( 'bbp-user-lost-pass' );
1126 }
1127
1128 /** Author Avatar *************************************************************/
1129
1130 /**
1131 * Output the author link of a post
1132 *
1133 * @since bbPress (r2875)
1134 *
1135 * @param mixed $args Optional. If it is an integer, it is used as post id.
1136 * @uses bbp_get_author_link() To get the post author link
1137 */
1138 function bbp_author_link( $args = '' ) {
1139 echo bbp_get_author_link( $args );
1140 }
1141 /**
1142 * Return the author link of the post
1143 *
1144 * @since bbPress (r2875)
1145 *
1146 * @param mixed $args Optional. If an integer, it is used as reply id.
1147 * @uses bbp_is_topic() To check if it's a topic page
1148 * @uses bbp_get_topic_author_link() To get the topic author link
1149 * @uses bbp_is_reply() To check if it's a reply page
1150 * @uses bbp_get_reply_author_link() To get the reply author link
1151 * @uses get_post_field() To get the post author
1152 * @uses bbp_is_reply_anonymous() To check if the reply is by an
1153 * anonymous user
1154 * @uses get_the_author_meta() To get the author name
1155 * @uses bbp_get_user_profile_url() To get the author profile url
1156 * @uses get_avatar() To get the author avatar
1157 * @uses apply_filters() Calls 'bbp_get_reply_author_link' with the
1158 * author link and args
1159 * @return string Author link of reply
1160 */
1161 function bbp_get_author_link( $args = '' ) {
1162
1163 // Default arguments
1164 $defaults = array(
1165 'post_id' => 0,
1166 'link_title' => '',
1167 'type' => 'both',
1168 'size' => 80
1169 );
1170 $r = bbp_parse_args( $args, $defaults, 'get_author_link' );
1171 extract( $r );
1172
1173 // Used as reply_id
1174 if ( is_numeric( $args ) )
1175 $post_id = $args;
1176
1177 // Confirmed topic
1178 if ( bbp_is_topic( $post_id ) )
1179 return bbp_get_topic_author_link( $args );
1180
1181 // Confirmed reply
1182 elseif ( bbp_is_reply( $post_id ) )
1183 return bbp_get_reply_author_link( $args );
1184
1185 // Get the post author and proceed
1186 else
1187 $user_id = get_post_field( 'post_author', $post_id );
1188
1189 // Neither a reply nor a topic, so could be a revision
1190 if ( !empty( $post_id ) ) {
1191
1192 // Generate title with the display name of the author
1193 if ( empty( $link_title ) ) {
1194 $link_title = sprintf( !bbp_is_reply_anonymous( $post_id ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), get_the_author_meta( 'display_name', $user_id ) );
1195 }
1196
1197 // Assemble some link bits
1198 $link_title = !empty( $link_title ) ? ' title="' . $link_title . '"' : '';
1199 $author_url = bbp_get_user_profile_url( $user_id );
1200 $anonymous = bbp_is_reply_anonymous( $post_id );
1201
1202 // Get avatar
1203 if ( 'avatar' == $type || 'both' == $type ) {
1204 $author_links[] = get_avatar( $user_id, $size );
1205 }
1206
1207 // Get display name
1208 if ( 'name' == $type || 'both' == $type ) {
1209 $author_links[] = get_the_author_meta( 'display_name', $user_id );
1210 }
1211
1212 // Add links if not anonymous
1213 if ( empty( $anonymous ) ) {
1214 foreach ( $author_links as $link_text ) {
1215 $author_link[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $author_url, $link_title, $link_text );
1216 }
1217 $author_link = join( '&nbsp;', $author_link );
1218
1219 // No links if anonymous
1220 } else {
1221 $author_link = join( '&nbsp;', $author_links );
1222 }
1223
1224 // No post so link is empty
1225 } else {
1226 $author_link = '';
1227 }
1228
1229 return apply_filters( 'bbp_get_author_link', $author_link, $args );
1230 }
1231
1232 /** Capabilities **************************************************************/
1233
1234 /**
1235 * Check if the user can access a specific forum
1236 *
1237 * @since bbPress (r3127)
1238 *
1239 * @uses bbp_get_current_user_id()
1240 * @uses bbp_get_forum_id()
1241 * @uses bbp_allow_anonymous()
1242 * @uses bbp_parse_args()
1243 * @uses bbp_get_user_id()
1244 * @uses current_user_can()
1245 * @uses is_super_admin()
1246 * @uses bbp_is_forum_public()
1247 * @uses bbp_is_forum_private()
1248 * @uses bbp_is_forum_hidden()
1249 * @uses current_user_can()
1250 * @uses apply_filters()
1251 *
1252 * @return bool
1253 */
1254 function bbp_user_can_view_forum( $args = '' ) {
1255
1256 // Default arguments
1257 $defaults = array(
1258 'user_id' => bbp_get_current_user_id(),
1259 'forum_id' => bbp_get_forum_id(),
1260 'check_ancestors' => false
1261 );
1262 $r = bbp_parse_args( $args, $defaults, 'user_can_view_forum' );
1263 extract( $r );
1264
1265 // Validate parsed values
1266 $user_id = bbp_get_user_id ( $user_id, false, false );
1267 $forum_id = bbp_get_forum_id( $forum_id );
1268 $retval = false;
1269
1270 // User is a super admin
1271 if ( is_super_admin() )
1272 $retval = true;
1273
1274 // Forum is public, and user can read forums or is not logged in
1275 elseif ( bbp_is_forum_public ( $forum_id, $check_ancestors ) )
1276 $retval = true;
1277
1278 // Forum is private, and user can see it
1279 elseif ( bbp_is_forum_private( $forum_id, $check_ancestors ) && current_user_can( 'read_private_forums' ) )
1280 $retval = true;
1281
1282 // Forum is hidden, and user can see it
1283 elseif ( bbp_is_forum_hidden ( $forum_id, $check_ancestors ) && current_user_can( 'read_hidden_forums' ) )
1284 $retval = true;
1285
1286 return apply_filters( 'bbp_user_can_view_forum', $retval, $forum_id, $user_id );
1287 }
1288
1289 /**
1290 * Check if the current user can publish topics
1291 *
1292 * @since bbPress (r3127)
1293 *
1294 * @uses is_super_admin()
1295 * @uses is_user_logged_in()
1296 * @uses bbp_allow_anonymous()
1297 * @uses bbp_is_user_active()
1298 * @uses current_user_can()
1299 * @uses apply_filters()
1300 *
1301 * @return bool
1302 */
1303 function bbp_current_user_can_publish_topics() {
1304
1305 // Users need to earn access
1306 $retval = false;
1307
1308 // Always allow super admins
1309 if ( is_super_admin() )
1310 $retval = true;
1311
1312 // Do not allow anonymous if not enabled
1313 elseif ( !is_user_logged_in() && bbp_allow_anonymous() )
1314 $retval = true;
1315
1316 // User is logged in
1317 elseif ( current_user_can( 'publish_topics' ) )
1318 $retval = true;
1319
1320 // Allow access to be filtered
1321 return (bool) apply_filters( 'bbp_current_user_can_publish_forums', $retval );
1322 }
1323
1324 /**
1325 * Check if the current user can publish forums
1326 *
1327 * @since bbPress (r3549)
1328 *
1329 * @uses is_super_admin()
1330 * @uses bbp_is_user_active()
1331 * @uses current_user_can()
1332 * @uses apply_filters()
1333 *
1334 * @return bool
1335 */
1336 function bbp_current_user_can_publish_forums() {
1337
1338 // Users need to earn access
1339 $retval = false;
1340
1341 // Always allow super admins
1342 if ( is_super_admin() )
1343 $retval = true;
1344
1345 // User is logged in
1346 elseif ( current_user_can( 'publish_forums' ) )
1347 $retval = true;
1348
1349 // Allow access to be filtered
1350 return (bool) apply_filters( 'bbp_current_user_can_publish_forums', $retval );
1351 }
1352
1353 /**
1354 * Check if the current user can publish replies
1355 *
1356 * @since bbPress (r3127)
1357 *
1358 * @uses is_super_admin()
1359 * @uses is_user_logged_in()
1360 * @uses bbp_allow_anonymous()
1361 * @uses bbp_is_user_active()
1362 * @uses current_user_can()
1363 * @uses apply_filters()
1364 *
1365 * @return bool
1366 */
1367 function bbp_current_user_can_publish_replies() {
1368
1369 // Users need to earn access
1370 $retval = false;
1371
1372 // Always allow super admins
1373 if ( is_super_admin() )
1374 $retval = true;
1375
1376 // Do not allow anonymous if not enabled
1377 elseif ( !is_user_logged_in() && bbp_allow_anonymous() )
1378 $retval = true;
1379
1380 // User is logged in
1381 elseif ( current_user_can( 'publish_replies' ) )
1382 $retval = true;
1383
1384 // Allow access to be filtered
1385 return (bool) apply_filters( 'bbp_current_user_can_publish_replies', $retval );
1386 }
1387
1388 /** Forms *********************************************************************/
1389
1390 /**
1391 * Get the forums the current user has the ability to see and post to
1392 *
1393 * @since bbPress (r3127)
1394 *
1395 * @uses bbp_get_forum_post_type()
1396 * @uses get_posts()
1397 *
1398 * @param type $args
1399 * @return type
1400 */
1401 function bbp_get_forums_for_current_user( $args = array() ) {
1402
1403 // Setup arrays
1404 $private = $hidden = $post__not_in = array();
1405
1406 // Private forums
1407 if ( !current_user_can( 'read_private_forums' ) )
1408 $private = bbp_get_private_forum_ids();
1409
1410 // Hidden forums
1411 if ( !current_user_can( 'read_hidden_forums' ) )
1412 $hidden = bbp_get_hidden_forum_ids();
1413
1414 // Merge private and hidden forums together and remove any empties
1415 $forum_ids = (array) array_filter( array_merge( $private, $hidden ) );
1416
1417 // There are forums that need to be ex
1418 if ( !empty( $forum_ids ) )
1419 $post__not_in = implode( ',', $forum_ids );
1420
1421 $defaults = array(
1422 'post_type' => bbp_get_forum_post_type(),
1423 'post_status' => bbp_get_public_status_id(),
1424 'numberposts' => -1,
1425 'exclude' => $post__not_in
1426 );
1427 $r = bbp_parse_args( $args, $defaults, 'get_forums_for_current_user' );
1428
1429 // Get the forums
1430 $forums = get_posts( $r );
1431
1432 // No availabe forums
1433 if ( empty( $forums ) )
1434 $forums = false;
1435
1436 return apply_filters( 'bbp_get_forums_for_current_user', $forums );
1437 }
1438
1439 /**
1440 * Performs a series of checks to ensure the current user can create forums.
1441 *
1442 * @since bbPress (r3549)
1443 *
1444 * @uses bbp_is_forum_edit()
1445 * @uses current_user_can()
1446 * @uses bbp_get_forum_id()
1447 *
1448 * @return bool
1449 */
1450 function bbp_current_user_can_access_create_forum_form() {
1451
1452 // Users need to earn access
1453 $retval = false;
1454
1455 // Always allow super admins
1456 if ( is_super_admin() )
1457 $retval = true;
1458
1459 // Looking at a single forum & forum is open
1460 elseif ( ( is_page() || is_single() ) && bbp_is_forum_open() )
1461 $retval = bbp_current_user_can_publish_forums();
1462
1463 // User can edit this topic
1464 elseif ( bbp_is_forum_edit() )
1465 $retval = current_user_can( 'edit_forum', bbp_get_forum_id() );
1466
1467 // Allow access to be filtered
1468 return (bool) apply_filters( 'bbp_current_user_can_access_create_forum_form', (bool) $retval );
1469 }
1470
1471 /**
1472 * Performs a series of checks to ensure the current user can create topics.
1473 *
1474 * @since bbPress (r3127)
1475 *
1476 * @uses bbp_is_topic_edit()
1477 * @uses current_user_can()
1478 * @uses bbp_get_topic_id()
1479 * @uses bbp_allow_anonymous()
1480 * @uses is_user_logged_in()
1481 *
1482 * @return bool
1483 */
1484 function bbp_current_user_can_access_create_topic_form() {
1485
1486 // Users need to earn access
1487 $retval = false;
1488
1489 // Always allow super admins
1490 if ( is_super_admin() )
1491 $retval = true;
1492
1493 // Looking at a single forum & forum is open
1494 elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() )
1495 $retval = bbp_current_user_can_publish_topics();
1496
1497 // User can edit this topic
1498 elseif ( bbp_is_topic_edit() )
1499 $retval = current_user_can( 'edit_topic', bbp_get_topic_id() );
1500
1501 // Allow access to be filtered
1502 return (bool) apply_filters( 'bbp_current_user_can_access_create_topic_form', (bool) $retval );
1503 }
1504
1505 /**
1506 * Performs a series of checks to ensure the current user can create replies.
1507 *
1508 * @since bbPress (r3127)
1509 *
1510 * @uses bbp_is_topic_edit()
1511 * @uses current_user_can()
1512 * @uses bbp_get_topic_id()
1513 * @uses bbp_allow_anonymous()
1514 * @uses is_user_logged_in()
1515 *
1516 * @return bool
1517 */
1518 function bbp_current_user_can_access_create_reply_form() {
1519
1520 // Users need to earn access
1521 $retval = false;
1522
1523 // Always allow super admins
1524 if ( is_super_admin() )
1525 $retval = true;
1526
1527 // Looking at a single topic, topic is open, and forum is open
1528 elseif ( ( bbp_is_single_topic() || is_page() || is_single() ) && bbp_is_topic_open() && bbp_is_forum_open() )
1529 $retval = bbp_current_user_can_publish_replies();
1530
1531 // User can edit this topic
1532 elseif ( bbp_is_reply_edit() )
1533 $retval = current_user_can( 'edit_reply', bbp_get_reply_id() );
1534
1535 // Allow access to be filtered
1536 return (bool) apply_filters( 'bbp_current_user_can_access_create_reply_form', (bool) $retval );
1537 }
1538