PluginProbe
bbPress / 2.0-beta-3
bbPress v2.0-beta-3
2.6.17 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 All 72 releases
bbpress / bbp-includes / bbp-user-template.php

bbp-user-template.php in bbPress 2.0-beta-3, at bbp-includes/bbp-user-template.php

1,221 lines 37.8 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 global $bbp;
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 apply_filters( 'bbp_get_user_id', (int) $bbp_user_id );
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 * Return a sanitized user field value
110 *
111 * @since bbPress (r2688)
112 *
113 * @param string $field Field to get
114 * @uses sanitize_text_field() To sanitize the field
115 * @uses esc_attr() To sanitize the field
116 * @return string|bool Value of the field if it exists, else false
117 */
118 function bbp_get_displayed_user_field( $field = '' ) {
119 global $bbp;
120
121 // Return field if exists
122 if ( isset( $bbp->displayed_user->$field ) )
123 return esc_attr( sanitize_text_field( $bbp->displayed_user->$field ) );
124
125 // Return empty
126 return false;
127 }
128
129 /**
130 * Output name of current user
131 *
132 * @since bbPress (r2574)
133 *
134 * @uses bbp_get_current_user_name() To get the current user name
135 */
136 function bbp_current_user_name() {
137 echo bbp_get_current_user_name();
138 }
139 /**
140 * Return name of current user
141 *
142 * @since bbPress (r2574)
143 *
144 * @uses apply_filters() Calls 'bbp_get_current_user_name' with the
145 * current user name
146 * @return string
147 */
148 function bbp_get_current_user_name() {
149 global $user_identity;
150
151 $current_user_name = is_user_logged_in() ? $user_identity : __( 'Anonymous', 'bbpress' );
152
153 return apply_filters( 'bbp_get_current_user_name', $current_user_name );
154 }
155
156 /**
157 * Output avatar of current user
158 *
159 * @since bbPress (r2574)
160 *
161 * @param int $size Size of the avatar. Defaults to 40
162 * @uses bbp_get_current_user_avatar() To get the current user avatar
163 */
164 function bbp_current_user_avatar( $size = 40 ) {
165 echo bbp_get_current_user_avatar( $size );
166 }
167
168 /**
169 * Return avatar of current user
170 *
171 * @since bbPress (r2574)
172 *
173 * @param int $size Size of the avatar. Defaults to 40
174 * @uses bbp_get_current_user_id() To get the current user id
175 * @uses bbp_get_current_anonymous_user_data() To get the current
176 * anonymous user's email
177 * @uses get_avatar() To get the avatar
178 * @uses apply_filters() Calls 'bbp_get_current_user_avatar' with the
179 * avatar and size
180 * @return string Current user avatar
181 */
182 function bbp_get_current_user_avatar( $size = 40 ) {
183
184 if ( !$user = bbp_get_current_user_id() )
185 $user = bbp_get_current_anonymous_user_data( 'email' );
186
187 return apply_filters( 'bbp_get_current_user_avatar', get_avatar( $user, $size ), $size );
188 }
189
190 /**
191 * Output link to the profile page of a user
192 *
193 * @since bbPress (r2688)
194 *
195 * @param int $user_id Optional. User id
196 * @uses bbp_get_user_profile_link() To get user profile link
197 */
198 function bbp_user_profile_link( $user_id = 0 ) {
199 echo bbp_get_user_profile_link( $user_id );
200 }
201 /**
202 * Return link to the profile page of a user
203 *
204 * @since bbPress (r2688)
205 *
206 * @param int $user_id Optional. User id
207 * @uses bbp_get_user_id() To get user id
208 * @uses get_userdata() To get user data
209 * @uses bbp_get_user_profile_url() To get user profile url
210 * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the user
211 * profile link and user id
212 * @return string User profile link
213 */
214 function bbp_get_user_profile_link( $user_id = 0 ) {
215 if ( !$user_id = bbp_get_user_id( $user_id ) )
216 return false;
217
218 $user = get_userdata( $user_id );
219 $name = esc_attr( $user->display_name );
220 $user_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . $name . '">' . $name . '</a>';
221
222 return apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id );
223 }
224
225 /**
226 * Output URL to the profile page of a user
227 *
228 * @since bbPress (r2688)
229 *
230 * @param int $user_id Optional. User id
231 * @param string $user_nicename Optional. User nicename
232 * @uses bbp_get_user_profile_url() To get user profile url
233 */
234 function bbp_user_profile_url( $user_id = 0, $user_nicename = '' ) {
235 echo bbp_get_user_profile_url( $user_id );
236 }
237 /**
238 * Return URL to the profile page of a user
239 *
240 * @since bbPress (r2688)
241 *
242 * @param int $user_id Optional. User id
243 * @param string $user_nicename Optional. User nicename
244 * @uses bbp_get_user_id() To get user id
245 * @uses WP_Rewrite::using_permalinks() To check if the blog is using
246 * permalinks
247 * @uses add_query_arg() To add custom args to the url
248 * @uses home_url() To get blog home url
249 * @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user
250 * profile url, user id and user nicename
251 * @return string User profile url
252 */
253 function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) {
254 global $wp_rewrite, $bbp;
255
256 // Use displayed user ID if there is one, and one isn't requested
257 if ( !$user_id = bbp_get_user_id( $user_id ) )
258 return false;
259
260 // Pretty permalinks
261 if ( $wp_rewrite->using_permalinks() ) {
262 $url = $wp_rewrite->root . $bbp->user_slug . '/%' . $bbp->user_id . '%';
263
264 // Get username if not passed
265 if ( empty( $user_nicename ) ) {
266 $user = get_userdata( $user_id );
267 if ( !empty( $user->user_nicename ) ) {
268 $user_nicename = $user->user_nicename;
269 }
270 }
271
272 $url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url );
273 $url = home_url( user_trailingslashit( $url ) );
274
275 // Unpretty permalinks
276 } else {
277 $url = add_query_arg( array( $bbp->user_id => $user_id ), home_url( '/' ) );
278 }
279
280 return apply_filters( 'bbp_get_user_profile_url', $url, $user_id, $user_nicename );
281
282 }
283
284 /**
285 * Output link to the profile edit page of a user
286 *
287 * @since bbPress (r2688)
288 *
289 * @param int $user_id Optional. User id
290 * @uses bbp_get_user_profile_edit_link() To get user profile edit link
291 */
292 function bbp_user_profile_edit_link( $user_id = 0 ) {
293 echo bbp_get_user_profile_edit_link( $user_id );
294 }
295 /**
296 * Return link to the profile edit page of a user
297 *
298 * @since bbPress (r2688)
299 *
300 * @param int $user_id Optional. User id
301 * @uses bbp_get_user_id() To get user id
302 * @uses get_userdata() To get user data
303 * @uses bbp_get_user_profile_edit_url() To get user profile edit url
304 * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the edit
305 * link and user id
306 * @return string User profile edit link
307 */
308 function bbp_get_user_profile_edit_link( $user_id = 0 ) {
309 if ( !$user_id = bbp_get_user_id( $user_id ) )
310 return false;
311
312 $user = get_userdata( $user_id );
313 $name = $user->display_name;
314 $edit_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . esc_attr( $name ) . '">' . $name . '</a>';
315 return apply_filters( 'bbp_get_user_profile_link', $edit_link, $user_id );
316 }
317
318 /**
319 * Output URL to the profile edit page of a user
320 *
321 * @since bbPress (r2688)
322 *
323 * @param int $user_id Optional. User id
324 * @param string $user_nicename Optional. User nicename
325 * @uses bbp_get_user_profile_edit_url() To get user profile edit url
326 */
327 function bbp_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
328 echo bbp_get_user_profile_edit_url( $user_id );
329 }
330 /**
331 * Return URL to the profile edit page of a user
332 *
333 * @since bbPress (r2688)
334 *
335 * @param int $user_id Optional. User id
336 * @param string $user_nicename Optional. User nicename
337 * @uses bbp_get_user_id() To get user id
338 * @uses WP_Rewrite::using_permalinks() To check if the blog is using
339 * permalinks
340 * @uses add_query_arg() To add custom args to the url
341 * @uses home_url() To get blog home url
342 * @uses apply_filters() Calls 'bbp_get_user_edit_profile_url' with the
343 * edit profile url, user id and user nicename
344 * @return string
345 */
346 function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
347 global $wp_rewrite, $bbp;
348
349 if ( !$user_id = bbp_get_user_id( $user_id ) )
350 return;
351
352 // Pretty permalinks
353 if ( $wp_rewrite->using_permalinks() ) {
354 $url = $wp_rewrite->root . $bbp->user_slug . '/%' . $bbp->user_id . '%/' . $bbp->edit_id;
355
356 // Get username if not passed
357 if ( empty( $user_nicename ) ) {
358 $user = get_userdata( $user_id );
359 if ( !empty( $user->user_nicename ) ) {
360 $user_nicename = $user->user_nicename;
361 }
362 }
363
364 $url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url );
365 $url = home_url( user_trailingslashit( $url ) );
366
367 // Unpretty permalinks
368 } else {
369 $url = add_query_arg( array( $bbp->user_id => $user_id, $bbp->edit_id => '1' ), home_url( '/' ) );
370 }
371
372 return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename );
373
374 }
375
376 /**
377 * Output the link to the admin section
378 *
379 * @since bbPress (r2827)
380 *
381 * @param mixed $args Optional. See {@link bbp_get_admin_link()}
382 * @uses bbp_get_admin_link() To get the admin link
383 */
384 function bbp_admin_link( $args = '' ) {
385 echo bbp_get_admin_link( $args );
386 }
387 /**
388 * Return the link to the admin section
389 *
390 * @since bbPress (r2827)
391 *
392 * @param mixed $args Optional. This function supports these arguments:
393 * - text: The text
394 * - before: Before the lnk
395 * - after: After the link
396 * @uses current_user_can() To check if the current user can moderate
397 * @uses admin_url() To get the admin url
398 * @uses apply_filters() Calls 'bbp_get_admin_link' with the link & args
399 * @return The link
400 */
401 function bbp_get_admin_link( $args = '' ) {
402 if ( !current_user_can( 'moderate' ) )
403 return;
404
405 if ( $args && is_string( $args ) && false === strpos( $args, '=' ) )
406 $args = array( 'text' => $args );
407
408 $defaults = array( 'text' => __( 'Admin', 'bbpress' ), 'before' => '', 'after' => '' );
409 $args = wp_parse_args( $args, $defaults );
410 extract( $args, EXTR_SKIP );
411
412 $uri = admin_url();
413
414 return apply_filters( 'bbp_get_admin_link', $before . '<a href="' . $uri . '">' . $text . '</a>' . $after, $args );
415 }
416
417 /** User IP *******************************************************************/
418
419 /**
420 * Output the author IP address of a post
421 *
422 * @since bbPress (r3120)
423 *
424 * @param mixed $args Optional. If it is an integer, it is used as post id.
425 * @uses bbp_get_author_ip() To get the post author link
426 */
427 function bbp_author_ip( $args = '' ) {
428 echo bbp_get_author_ip( $args );
429 }
430 /**
431 * Return the author IP address of a post
432 *
433 * @since bbPress (r3120)
434 *
435 * @param mixed $args Optional. If an integer, it is used as reply id.
436 * @uses get_post_meta() To check if it's a topic page
437 * @return string Author link of reply
438 */
439 function bbp_get_author_ip( $args = '' ) {
440
441 // Default arguments
442 $defaults = array(
443 'post_id' => 0,
444 'before' => '<span class="bbp-author-ip">(',
445 'after' => ')</span>'
446 );
447
448 $r = wp_parse_args( $args, $defaults );
449 extract( $r );
450
451 // Used as post id
452 if ( is_numeric( $args ) )
453 $post_id = $args;
454
455 // Get the author IP meta value
456 if ( $author_ip = get_post_meta( $post_id, '_bbp_author_ip', true ) )
457 $author_ip = $before . $author_ip . $after;
458
459 // No IP address
460 else
461 $author_ip = '';
462
463 return apply_filters( 'bbp_get_author_ip', $author_ip, $args );
464 }
465
466 /** Favorites *****************************************************************/
467
468 /**
469 * Output the link to the user's favorites page (profile page)
470 *
471 * @since bbPress (r2652)
472 *
473 * @param int $user_id Optional. User id
474 * @uses bbp_get_favorites_permalink() To get the favorites permalink
475 */
476 function bbp_favorites_permalink( $user_id = 0 ) {
477 echo bbp_get_favorites_permalink( $user_id );
478 }
479 /**
480 * Return the link to the user's favorites page (profile page)
481 *
482 * @since bbPress (r2652)
483 *
484 * @param int $user_id Optional. User id
485 * @uses bbp_get_user_profile_url() To get the user profile url
486 * @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the
487 * user profile url and user id
488 * @return string Permanent link to user profile page
489 */
490 function bbp_get_favorites_permalink( $user_id = 0 ) {
491 return apply_filters( 'bbp_get_favorites_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
492 }
493
494 /**
495 * Output the link to make a topic favorite/remove a topic from favorites
496 *
497 * @since bbPress (r2652)
498 *
499 * @param array $add Optional. Add to favorites args
500 * @param array $rem Optional. Remove from favorites args
501 * @param int $user_id Optional. User id
502 * @uses bbp_get_user_favorites_link() To get the user favorites link
503 */
504 function bbp_user_favorites_link( $add = array(), $rem = array(), $user_id = 0 ) {
505 echo bbp_get_user_favorites_link( $add, $rem, $user_id );
506 }
507 /**
508 * User favorites link
509 *
510 * Return the link to make a topic favorite/remove a topic from
511 * favorites
512 *
513 * @since bbPress (r2652)
514 *
515 * @param array $add Optional. Add to favorites args
516 * @param array $rem Optional. Remove from favorites args
517 * @param int $user_id Optional. User id
518 * @uses bbp_get_user_id() To get the user id
519 * @uses current_user_can() If the current user can edit the user
520 * @uses bbp_get_topic_id() To get the topic id
521 * @uses bbp_is_user_favorite() To check if the topic is user's favorite
522 * @uses bbp_get_favorites_permalink() To get the favorites permalink
523 * @uses bbp_get_topic_permalink() To get the topic permalink
524 * @uses bbp_is_favorites() Is it the favorites page?
525 * @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the
526 * html, add args, remove args, user & topic id
527 * @return string User favorites link
528 */
529 function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0 ) {
530 if ( !$user_id = bbp_get_user_id( $user_id, true, true ) )
531 return false;
532
533 if ( !current_user_can( 'edit_user', (int) $user_id ) )
534 return false;
535
536 if ( !$topic_id = bbp_get_topic_id() )
537 return false;
538
539 if ( empty( $add ) || !is_array( $add ) ) {
540 $add = array(
541 'mid' => __( 'Add this topic to your favorites', 'bbpress' ),
542 'post' => __( ' (%?%)', 'bbpress' )
543 );
544 }
545
546 if ( empty( $rem ) || !is_array( $rem ) ) {
547 $rem = array(
548 'pre' => __( 'This topic is one of your %favorites% [', 'bbpress' ),
549 'mid' => __( '&times;', 'bbpress' ),
550 'post' => __( ']', 'bbpress' )
551 );
552 }
553
554 if ( $is_fav = bbp_is_user_favorite( $user_id, $topic_id ) ) {
555 $url = esc_url( bbp_get_favorites_permalink( $user_id ) );
556 $rem = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
557 $favs = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
558 $pre = ( is_array( $rem ) && isset( $rem['pre'] ) ) ? $rem['pre'] : '';
559 $mid = ( is_array( $rem ) && isset( $rem['mid'] ) ) ? $rem['mid'] : ( is_string( $rem ) ? $rem : '' );
560 $post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
561 } else {
562 $url = esc_url( bbp_get_topic_permalink( $topic_id ) );
563 $add = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
564 $favs = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
565 $pre = ( is_array( $add ) && isset( $add['pre'] ) ) ? $add['pre'] : '';
566 $mid = ( is_array( $add ) && isset( $add['mid'] ) ) ? $add['mid'] : ( is_string( $add ) ? $add : '' );
567 $post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
568 }
569
570 // Create the link based where the user is and if the topic is already the user's favorite
571 $permalink = bbp_is_favorites() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
572 $url = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
573 $is_fav = $is_fav ? 'is-favorite' : '';
574 $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>';
575
576 // Return the link
577 return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id );
578 }
579
580 /** Subscriptions *************************************************************/
581
582 /**
583 * Output the link to the user's subscriptions page (profile page)
584 *
585 * @since bbPress (r2688)
586 *
587 * @param int $user_id Optional. User id
588 * @uses bbp_get_subscriptions_permalink() To get the subscriptions link
589 */
590 function bbp_subscriptions_permalink( $user_id = 0 ) {
591 echo bbp_get_subscriptions_permalink( $user_id );
592 }
593 /**
594 * Return the link to the user's subscriptions page (profile page)
595 *
596 * @since bbPress (r2688)
597 *
598 * @param int $user_id Optional. User id
599 * @uses bbp_get_user_profile_url() To get the user profile url
600 * @uses apply_filters() Calls 'bbp_get_subscriptions_permalink' with
601 * the user profile url and user id
602 * @return string Permanent link to user subscriptions page
603 */
604 function bbp_get_subscriptions_permalink( $user_id = 0 ) {
605 return apply_filters( 'bbp_get_subscriptions_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
606 }
607
608 /**
609 * Output the link to subscribe/unsubscribe from a topic
610 *
611 * @since bbPress (r2668)
612 *
613 * @param mixed $args See {@link bbp_get_user_subscribe_link()}
614 * @uses bbp_get_user_subscribe_link() To get the subscribe link
615 */
616 function bbp_user_subscribe_link( $args = '' ) {
617 echo bbp_get_user_subscribe_link( $args );
618 }
619 /**
620 * Return the link to subscribe/unsubscribe from a topic
621 *
622 * @since bbPress (r2668)
623 *
624 * @param mixed $args This function supports these arguments:
625 * - subscribe: Subscribe text
626 * - unsubscribe: Unsubscribe text
627 * - user_id: User id
628 * - topic_id: Topic id
629 * - before: Before the link
630 * - after: After the link
631 * @param int $user_id Optional. User id
632 * @uses bbp_get_user_id() To get the user id
633 * @uses current_user_can() To check if the current user can edit user
634 * @uses bbp_get_topic_id() To get the topic id
635 * @uses bbp_is_user_subscribed() To check if the user is subscribed
636 * @uses bbp_is_subscriptions() To check if it's the subscriptions page
637 * @uses bbp_get_subscriptions_permalink() To get subscriptions link
638 * @uses bbp_get_topic_permalink() To get topic link
639 * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the
640 * link, args, user id & topic id
641 * @return string Permanent link to topic
642 */
643 function bbp_get_user_subscribe_link( $args = '', $user_id = 0 ) {
644 if ( !bbp_is_subscriptions_active() )
645 return;
646
647 $defaults = array (
648 'subscribe' => __( 'Subscribe', 'bbpress' ),
649 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
650 'user_id' => 0,
651 'topic_id' => 0,
652 'before' => '&nbsp;|&nbsp;',
653 'after' => ''
654 );
655
656 $args = wp_parse_args( $args, $defaults );
657 extract( $args );
658
659 // Try to get a user_id
660 if ( !$user_id = bbp_get_user_id( $user_id, true, true ) )
661 return false;
662
663 // No link if you can't edit yourself
664 if ( !current_user_can( 'edit_user', (int) $user_id ) )
665 return false;
666
667 // No link if not viewing a topic
668 if ( !$topic_id = bbp_get_topic_id( $topic_id ) )
669 return false;
670
671 // Decine which link to show
672 if ( $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id ) ) {
673 $text = $unsubscribe;
674 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
675 } else {
676 $text = $subscribe;
677 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
678 }
679
680 // Create the link based where the user is and if the user is subscribed already
681 $permalink = bbp_is_subscriptions() ? bbp_get_subscriptions_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
682 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
683 $is_subscribed = $is_subscribed ? 'is-subscribed' : '';
684 $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>';
685
686 // Return the link
687 return apply_filters( 'bbp_get_user_subscribe_link', $html, $args, $user_id, $topic_id );
688 }
689
690
691 /** Edit User *****************************************************************/
692
693 /**
694 * Edit profile success message
695 *
696 * @since bbPress (r2688)
697 *
698 * @uses bbp_is_single_user() To check if it's the profile page
699 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
700 */
701 function bbp_notice_edit_user_success() {
702 if ( isset( $_GET['updated'] ) && ( bbp_is_single_user() || bbp_is_single_user_edit() ) ) : ?>
703
704 <div class="bbp-template-notice updated">
705 <p><?php _e( 'User updated.', 'bbpress' ); ?></p>
706 </div>
707
708 <?php endif;
709 }
710
711 /**
712 * Super admin privileges notice
713 *
714 * @since bbPress (r2688)
715 *
716 * @uses is_multisite() To check if the blog is multisite
717 * @uses bbp_is_single_user() To check if it's the profile page
718 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
719 * @uses current_user_can() To check if the current user can manage network
720 * options
721 * @uses bbp_get_displayed_user_id() To get the displayed user id
722 * @uses is_super_admin() To check if the user is super admin
723 * @uses bbp_is_user_home() To check if it's the user home
724 */
725 function bbp_notice_edit_user_is_super_admin() {
726 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() ) ) : ?>
727
728 <div class="bbp-template-notice important">
729 <p><?php bbp_is_user_home() ? _e( 'You have super admin privileges.', 'bbpress' ) : _e( 'This user has super admin privileges.', 'bbpress' ); ?></p>
730 </div>
731
732 <?php endif;
733 }
734
735 /**
736 * Drop down for selecting the user's display name
737 *
738 * @since bbPress (r2688)
739 */
740 function bbp_edit_user_display_name() {
741 global $bbp;
742
743 $public_display = array();
744 $public_display['display_username'] = $bbp->displayed_user->user_login;
745 $public_display['display_nickname'] = $bbp->displayed_user->nickname;
746
747 if ( !empty( $bbp->displayed_user->first_name ) )
748 $public_display['display_firstname'] = $bbp->displayed_user->first_name;
749
750 if ( !empty( $bbp->displayed_user->last_name ) )
751 $public_display['display_lastname'] = $bbp->displayed_user->last_name;
752
753 if ( !empty( $bbp->displayed_user->first_name ) && !empty( $bbp->displayed_user->last_name ) ) {
754 $public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name;
755 $public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name;
756 }
757
758 if ( !in_array( $bbp->displayed_user->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
759 $public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display;
760
761 $public_display = array_map( 'trim', $public_display );
762 $public_display = array_unique( $public_display ); ?>
763
764 <select name="display_name" id="display_name">
765
766 <?php foreach ( $public_display as $id => $item ) : ?>
767
768 <option id="<?php echo $id; ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo $item; ?></option>
769
770 <?php endforeach; ?>
771
772 </select>
773
774 <?php
775 }
776
777 /**
778 * Output role selector (for user edit)
779 *
780 * @since bbPress (r2688)
781 */
782 function bbp_edit_user_role() {
783 global $bbp;
784
785 // Return if no user is displayed
786 if ( !isset( $bbp->displayed_user ) )
787 return;
788
789 // Local variables
790 $p = $r = '';
791
792 // print the 'no role' option. Make it selected if the user has no role yet.
793 if ( !$user_role = array_shift( $bbp->displayed_user->roles ) )
794 $r .= '<option value="">' . __( '&mdash; No role for this site &mdash;', 'bbpress' ) . '</option>';
795
796 // Loop through roles
797 foreach ( get_editable_roles() as $role => $details ) {
798 $name = translate_user_role( $details['name'] );
799
800 // Make default first in list
801 if ( $user_role == $role )
802 $p = "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>{$name}</option>";
803 else
804 $r .= "\n\t<option value='" . esc_attr( $role ) . "'>{$name}</option>";
805 }
806
807 // Output result
808 echo '<select name="role" id="role">' . $p . $r . '</select>';
809 }
810
811 /**
812 * Return user contact methods Selectbox
813 *
814 * @since bbPress (r2688)
815 *
816 * @uses _wp_get_user_contactmethods() To get the contact methods
817 * @uses apply_filters() Calls 'bbp_edit_user_contact_methods' with the methods
818 * @return string User contact methods
819 */
820 function bbp_edit_user_contact_methods() {
821 global $bbp;
822
823 // Get the core WordPress contact methods
824 $contact_methods = _wp_get_user_contactmethods( $bbp->displayed_user );
825
826 return apply_filters( 'bbp_edit_user_contact_methods', $contact_methods );
827 }
828
829 /** Login *********************************************************************/
830
831 /**
832 * Handle the login and registration template notices
833 *
834 * @since bbPress (r2970)
835 *
836 * @uses WP_Error bbPress::errors::add() To add an error or message
837 */
838 function bbp_login_notices() {
839 global $bbp;
840
841 // loggedout was passed
842 if ( !empty( $_GET['loggedout'] ) && ( true == $_GET['loggedout'] ) ) {
843 $bbp->errors->add( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' );
844
845 // registration is disabled
846 } elseif ( !empty( $_GET['registration'] ) && ( 'disabled' == $_GET['registration'] ) ) {
847 $bbp->errors->add( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) );
848
849 // Prompt user to check their email
850 } elseif ( !empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ) ) ) {
851
852 switch ( $_GET['checkemail'] ) {
853
854 // Email needs confirmation
855 case 'confirm' :
856 $bbp->errors->add( 'confirm', __( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' );
857 break;
858
859 // User requested a new password
860 case 'newpass' :
861 $bbp->errors->add( 'newpass', __( 'Check your e-mail for your new password.', 'bbpress' ), 'message' );
862 break;
863
864 // User is newly registered
865 case 'registered' :
866 $bbp->errors->add( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
867 break;
868 }
869 }
870 }
871
872 /**
873 * Redirect a user back to their profile if they are already logged in.
874 *
875 * This should be used before {@link get_header()} is called in template files
876 * where the user should never have access to the contents of that file.
877 *
878 * @since bbPress (r2815)
879 *
880 * @param string $url The URL to redirect to
881 * @uses is_user_logged_in() Check if user is logged in
882 * @uses wp_safe_redirect() To safely redirect
883 * @uses bbp_get_user_profile_url() To get the profile url of the user
884 * @uses bbp_get_current_user_id() To get the current user id
885 */
886 function bbp_logged_in_redirect( $url = '' ) {
887
888 // Bail if user is not logged in
889 if ( !is_user_logged_in() )
890 return;
891
892 // Setup the profile page to redirect to
893 $redirect_to = !empty( $url ) ? $url : bbp_get_user_profile_url( bbp_get_current_user_id() );
894
895 // Do a safe redirect and exit
896 wp_safe_redirect( $redirect_to );
897 exit;
898 }
899
900 /**
901 * Output the required hidden fields when logging in
902 *
903 * @since bbPress (r2815)
904 *
905 * @uses bbp_redirect_to_field() To output the hidden request url field
906 * @uses wp_nonce_field() To generate hidden nonce fields
907 */
908 function bbp_user_login_fields() {
909 ?>
910
911 <input type="hidden" name="user-cookie" value="1" />
912
913 <?php bbp_redirect_to_field(); ?>
914
915 <?php wp_nonce_field( 'bbp-user-login' );
916 }
917
918 /** Register ******************************************************************/
919
920 /**
921 * Output the required hidden fields when registering
922 *
923 * @since bbPress (r2815)
924 *
925 * @uses add_query_arg() To add query args
926 * @uses bbp_login_url() To get the login url
927 * @uses bbp_redirect_to_field() To output the redirect to field
928 * @uses wp_nonce_field() To generate hidden nonce fields
929 */
930 function bbp_user_register_fields() {
931 ?>
932
933 <input type="hidden" name="action" value="register" />
934 <input type="hidden" name="user-cookie" value="1" />
935
936 <?php bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'registered' ), '' ) ); ?>
937
938 <?php wp_nonce_field( 'bbp-user-register' );
939 }
940
941 /** Lost Password *************************************************************/
942
943 /**
944 * Output the required hidden fields when user lost password
945 *
946 * @since bbPress (r2815)
947 *
948 * @uses wp_referer_field() Set referer
949 * @uses wp_nonce_field() To generate hidden nonce fields
950 */
951 function bbp_user_lost_pass_fields() {
952 ?>
953
954 <input type="hidden" name="user-cookie" value="1" />
955
956 <?php bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'confirm' ), get_permalink() ) ); ?>
957
958 <?php wp_nonce_field( 'bbp-user-lost-pass' );
959 }
960
961 /** Author Avatar *************************************************************/
962
963 /**
964 * Output the author link of a post
965 *
966 * @since bbPress (r2875)
967 *
968 * @param mixed $args Optional. If it is an integer, it is used as post id.
969 * @uses bbp_get_author_link() To get the post author link
970 */
971 function bbp_author_link( $args = '' ) {
972 echo bbp_get_author_link( $args );
973 }
974 /**
975 * Return the author link of the post
976 *
977 * @since bbPress (r2875)
978 *
979 * @param mixed $args Optional. If an integer, it is used as reply id.
980 * @uses bbp_is_topic() To check if it's a topic page
981 * @uses bbp_get_topic_author_link() To get the topic author link
982 * @uses bbp_is_reply() To check if it's a reply page
983 * @uses bbp_get_reply_author_link() To get the reply author link
984 * @uses get_post_field() To get the post author
985 * @uses bbp_is_reply_anonymous() To check if the reply is by an
986 * anonymous user
987 * @uses get_the_author_meta() To get the author name
988 * @uses bbp_get_user_profile_url() To get the author profile url
989 * @uses get_avatar() To get the author avatar
990 * @uses apply_filters() Calls 'bbp_get_reply_author_link' with the
991 * author link and args
992 * @return string Author link of reply
993 */
994 function bbp_get_author_link( $args = '' ) {
995 $defaults = array (
996 'post_id' => 0,
997 'link_title' => '',
998 'type' => 'both',
999 'size' => 80
1000 );
1001
1002 $r = wp_parse_args( $args, $defaults );
1003 extract( $r );
1004
1005 // Used as reply_id
1006 if ( is_numeric( $args ) )
1007 $post_id = $args;
1008
1009 if ( bbp_is_topic( $post_id ) )
1010 return bbp_get_topic_author_link( $args );
1011 elseif ( bbp_is_reply( $post_id ) )
1012 return bbp_get_reply_author_link( $args );
1013 else
1014 $user_id = get_post_field( 'post_author', $post_id );
1015
1016 // Neither a reply nor a topic, so could be a revision
1017 if ( !empty( $post_id ) ) {
1018 if ( empty( $link_title ) )
1019 $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 ) );
1020
1021 $link_title = !empty( $link_title ) ? ' title="' . $link_title . '"' : '';
1022 $author_url = bbp_get_user_profile_url( $user_id );
1023 $anonymous = bbp_is_reply_anonymous( $post_id );
1024
1025 // Get avatar
1026 if ( 'avatar' == $type || 'both' == $type )
1027 $author_links[] = get_avatar( $user_id, $size );
1028
1029 // Get display name
1030 if ( 'name' == $type || 'both' == $type )
1031 $author_links[] = get_the_author_meta( 'display_name', $user_id );
1032
1033 // Add links if not anonymous
1034 if ( empty( $anonymous ) ) {
1035 foreach ( $author_links as $link_text ) {
1036 $author_link[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $author_url, $link_title, $link_text );
1037 }
1038 $author_link = join( '&nbsp;', $author_link );
1039
1040 // No links if anonymous
1041 } else {
1042 $author_link = join( '&nbsp;', $author_links );
1043 }
1044
1045 // No post so link is empty
1046 } else {
1047 $author_link = '';
1048 }
1049
1050 return apply_filters( 'bbp_get_author_link', $author_link, $args );
1051 }
1052
1053 /** Capabilities **************************************************************/
1054
1055 function bbp_user_can_view_forum( $args = '' ) {
1056
1057 $defaults = array(
1058 'user_id' => bbp_get_current_user_id(),
1059 'forum_id' => bbp_get_forum_id(),
1060 'check_ancestors' => false
1061 );
1062 $r = wp_parse_args( $args, $defaults );
1063 extract( $r );
1064
1065 // Validate parsed values
1066 $user_id = bbp_get_user_id ( $user_id, false, false );
1067 $forum_id = bbp_get_forum_id( $forum_id );
1068 $retval = false;
1069
1070 // User is a super admin
1071 if ( is_super_admin() )
1072 $retval = true;
1073
1074 // Forum is public, and user can read forums or is not logged in
1075 elseif ( bbp_is_forum_public ( $forum_id, $check_ancestors ) )
1076 $retval = true;
1077
1078 // Forum is private, and user can see it
1079 elseif ( bbp_is_forum_private( $forum_id, $check_ancestors ) && current_user_can( 'read_private_forums' ) )
1080 $retval = true;
1081
1082 // Forum is hidden, and user can see it
1083 elseif ( bbp_is_forum_hidden ( $forum_id, $check_ancestors ) && current_user_can( 'read_hidden_forums' ) )
1084 $retval = true;
1085
1086 return apply_filters( 'bbp_user_can_view_forum', $retval, $forum_id, $user_id );
1087 }
1088
1089 /** Forms *********************************************************************/
1090
1091 /**
1092 *
1093 * @since bbPress (r3127)
1094 *
1095 * @uses bbp_get_forum_post_type()
1096 * @uses get_posts()
1097 *
1098 * @param type $args
1099 * @return type
1100 */
1101 function bbp_get_forums_for_current_user( $args = array() ) {
1102
1103 // Setup arrays
1104 $private = $hidden = $post__not_in = array();
1105
1106 // Private forums
1107 if ( !current_user_can( 'read_private_forums' ) )
1108 $private = bbp_get_private_forum_ids();
1109
1110 // Hidden forums
1111 if ( !current_user_can( 'read_hidden_forums' ) )
1112 $hidden = bbp_get_hidden_forum_ids();
1113
1114 // Merge private and hidden forums together and remove any empties
1115 $forum_ids = (array) array_filter( array_merge( $private, $hidden ) );
1116
1117 // There are forums that need to be ex
1118 if ( !empty( $forum_ids ) )
1119 $post__not_in = implode( ',', $forum_ids );
1120
1121 $defaults = array(
1122 'post_type' => bbp_get_forum_post_type(),
1123 'post_status' => 'publish',
1124 'numberposts' => -1,
1125 'exclude' => $post__not_in
1126 );
1127 $r = wp_parse_args( $args, $defaults );
1128
1129 // Get the forums
1130 $forums = get_posts( $r );
1131
1132 // No availabe forums
1133 if ( empty( $forums ) )
1134 $forums = false;
1135
1136 return apply_filters( 'bbp_get_forums_for_current_user', $forums );
1137 }
1138
1139 /**
1140 * Performs a series of checks to ensure the current user can create topics.
1141 *
1142 * @since bbPress (r3127)
1143 *
1144 * @uses bbp_is_topic_edit()
1145 * @uses current_user_can()
1146 * @uses bbp_get_topic_id()
1147 * @uses bbp_allow_anonymous()
1148 * @uses is_user_logged_in()
1149 *
1150 * @return bool
1151 */
1152 function bbp_current_user_can_access_create_topic_form() {
1153
1154 // Always allow super admins
1155 if ( is_super_admin() )
1156 return true;
1157
1158 // Users need to earn access
1159 $retval = false;
1160
1161 // Looking at a single forum
1162 if ( bbp_is_forum() ) {
1163
1164 // Forum is open
1165 if ( bbp_is_forum_open() ) {
1166
1167 // What is the visibility of this forum
1168 switch ( bbp_get_forum_visibility() ) {
1169
1170 // Public
1171 case 'publish' :
1172
1173 // User cannot publish topics
1174 if ( current_user_can( 'publish_topics' ) )
1175 $retval = true;
1176
1177 // Anonymous posting is allowed
1178 if ( bbp_allow_anonymous() && !is_user_logged_in() )
1179 $retval = true;
1180
1181 break;
1182
1183 // Private forums
1184 case 'private' :
1185 if ( current_user_can( 'read_private_forums' ) )
1186 $retval = true;
1187
1188 break;
1189
1190 // Hidden forums
1191 case 'hidden' :
1192 if ( current_user_can( 'read_hidden_forums' ) )
1193 $retval = true;
1194
1195 break;
1196 }
1197 }
1198
1199 // Editing a single topic
1200 } elseif ( bbp_is_topic_edit() ) {
1201
1202 // User can edit edit this topic
1203 if ( current_user_can( 'edit_topic', bbp_get_topic_id() ) )
1204 $retval = true;
1205
1206 // Fallback for shortcodes
1207 } elseif ( is_page() || is_single() ) {
1208
1209 // Page or single post, and user can publish topics or anonymous
1210 // posting is allowed.
1211 if ( current_user_can( 'publish_topics' ) || ( !is_user_logged_in() && bbp_allow_anonymous() ) )
1212 $retval = true;
1213
1214 }
1215
1216 // Allow access to be filtered
1217 return apply_filters( 'bbp_current_user_can_access_create_topic_form', $retval );
1218 }
1219
1220 ?>
1221