PluginProbe
bbPress / 2.0-rc-3
bbPress v2.0-rc-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-rc-3, at bbp-includes/bbp-user-template.php

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