This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbp-user-template.php
1520 lines (1297 loc) · 46.1 KB
/
bbp-user-template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* bbPress User Template Tags
*
* @package bbPress
* @subpackage TemplateTags
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/** Users *********************************************************************/
/**
* Output a validated user id
*
* @since bbPress (r2729)
*
* @param int $user_id Optional. User id
* @param bool $displayed_user_fallback Fallback on displayed user?
* @param bool $current_user_fallback Fallback on current user?
* @uses bbp_get_user_id() To get the user id
*/
function bbp_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
echo bbp_get_user_id( $user_id, $displayed_user_fallback, $current_user_fallback );
}
/**
* Return a validated user id
*
* @since bbPress (r2729)
*
* @param int $user_id Optional. User id
* @param bool $displayed_user_fallback Fallback on displayed user?
* @param bool $current_user_fallback Fallback on current user?
* @uses get_query_var() To get the 'bbp_user_id' query var
* @uses apply_filters() Calls 'bbp_get_user_id' with the user id
* @return int Validated user id
*/
function bbp_get_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) {
$bbp = bbpress();
// Easy empty checking
if ( !empty( $user_id ) && is_numeric( $user_id ) )
$bbp_user_id = $user_id;
// Currently viewing or editing a user
elseif ( ( true == $displayed_user_fallback ) && !empty( $bbp->displayed_user->ID ) )
$bbp_user_id = $bbp->displayed_user->ID;
// Maybe fallback on the current_user ID
elseif ( ( true == $current_user_fallback ) && !empty( $bbp->current_user->ID ) )
$bbp_user_id = $bbp->current_user->ID;
// Failsafe
else
$bbp_user_id = get_query_var( 'bbp_user_id' );
return (int) apply_filters( 'bbp_get_user_id', (int) $bbp_user_id, $displayed_user_fallback, $current_user_fallback );
}
/**
* Output ID of current user
*
* @since bbPress (r2574)
*
* @uses bbp_get_current_user_id() To get the current user id
*/
function bbp_current_user_id() {
echo bbp_get_current_user_id();
}
/**
* Return ID of current user
*
* @since bbPress (r2574)
*
* @uses bbp_get_user_id() To get the current user id
* @uses apply_filters() Calls 'bbp_get_current_user_id' with the id
* @return int Current user id
*/
function bbp_get_current_user_id() {
return apply_filters( 'bbp_get_current_user_id', bbp_get_user_id( 0, false, true ) );
}
/**
* Output ID of displayed user
*
* @since bbPress (r2688)
*
* @uses bbp_get_displayed_user_id() To get the displayed user id
*/
function bbp_displayed_user_id() {
echo bbp_get_displayed_user_id();
}
/**
* Return ID of displayed user
*
* @since bbPress (r2688)
*
* @uses bbp_get_user_id() To get the displayed user id
* @uses apply_filters() Calls 'bbp_get_displayed_user_id' with the id
* @return int Displayed user id
*/
function bbp_get_displayed_user_id() {
return apply_filters( 'bbp_get_displayed_user_id', bbp_get_user_id( 0, true, false ) );
}
/**
* Output a sanitized user field value
*
* @since bbPress (r2688)
*
* @param string $field Field to get
* @uses bbp_get_displayed_user_field() To get the field
*/
function bbp_displayed_user_field( $field = '' ) {
echo bbp_get_displayed_user_field( $field );
}
/**
* Return a sanitized user field value
*
* @since bbPress (r2688)
*
* @param string $field Field to get
* @uses sanitize_text_field() To sanitize the field
* @uses esc_attr() To sanitize the field
* @uses apply_filters() Calls 'bbp_get_displayed_user_field' with the value
* @return string|bool Value of the field if it exists, else false
*/
function bbp_get_displayed_user_field( $field = '' ) {
$bbp = bbpress();
$value = false;
// Return field if exists
if ( isset( $bbp->displayed_user->$field ) )
$value = esc_attr( sanitize_text_field( $bbp->displayed_user->$field ) );
// Return empty
return apply_filters( 'bbp_get_displayed_user_field', $value, $field );
}
/**
* Output name of current user
*
* @since bbPress (r2574)
*
* @uses bbp_get_current_user_name() To get the current user name
*/
function bbp_current_user_name() {
echo bbp_get_current_user_name();
}
/**
* Return name of current user
*
* @since bbPress (r2574)
*
* @uses apply_filters() Calls 'bbp_get_current_user_name' with the
* current user name
* @return string
*/
function bbp_get_current_user_name() {
global $user_identity;
$current_user_name = is_user_logged_in() ? $user_identity : __( 'Anonymous', 'bbpress' );
return apply_filters( 'bbp_get_current_user_name', $current_user_name );
}
/**
* Output avatar of current user
*
* @since bbPress (r2574)
*
* @param int $size Size of the avatar. Defaults to 40
* @uses bbp_get_current_user_avatar() To get the current user avatar
*/
function bbp_current_user_avatar( $size = 40 ) {
echo bbp_get_current_user_avatar( $size );
}
/**
* Return avatar of current user
*
* @since bbPress (r2574)
*
* @param int $size Size of the avatar. Defaults to 40
* @uses bbp_get_current_user_id() To get the current user id
* @uses bbp_get_current_anonymous_user_data() To get the current
* anonymous user's email
* @uses get_avatar() To get the avatar
* @uses apply_filters() Calls 'bbp_get_current_user_avatar' with the
* avatar and size
* @return string Current user avatar
*/
function bbp_get_current_user_avatar( $size = 40 ) {
$user = bbp_get_current_user_id();
if ( empty( $user ) )
$user = bbp_get_current_anonymous_user_data( 'email' );
$avatar = get_avatar( $user, $size );
return apply_filters( 'bbp_get_current_user_avatar', $avatar, $size );
}
/**
* Output link to the profile page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_profile_link() To get user profile link
*/
function bbp_user_profile_link( $user_id = 0 ) {
echo bbp_get_user_profile_link( $user_id );
}
/**
* Return link to the profile page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_id() To get user id
* @uses get_userdata() To get user data
* @uses bbp_get_user_profile_url() To get user profile url
* @uses apply_filters() Calls 'bbp_get_user_profile_link' with the user
* profile link and user id
* @return string User profile link
*/
function bbp_get_user_profile_link( $user_id = 0 ) {
// Validate user id
$user_id = bbp_get_user_id( $user_id );
if ( empty( $user_id ) )
return false;
$user = get_userdata( $user_id );
$name = esc_attr( $user->display_name );
$user_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . $name . '">' . $name . '</a>';
return apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id );
}
/**
* Output URL to the profile page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @param string $user_nicename Optional. User nicename
* @uses bbp_get_user_profile_url() To get user profile url
*/
function bbp_user_profile_url( $user_id = 0, $user_nicename = '' ) {
echo bbp_get_user_profile_url( $user_id, $user_nicename );
}
/**
* Return URL to the profile page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @param string $user_nicename Optional. User nicename
* @uses bbp_get_user_id() To get user id
* @uses WP_Rewrite::using_permalinks() To check if the blog is using
* permalinks
* @uses add_query_arg() To add custom args to the url
* @uses home_url() To get blog home url
* @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user
* profile url, user id and user nicename
* @return string User profile url
*/
function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) {
global $wp_rewrite;
// Use displayed user ID if there is one, and one isn't requested
$user_id = bbp_get_user_id( $user_id );
if ( empty( $user_id ) )
return false;
// Allow early overriding of the profile URL to cut down on processing
$early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', (int) $user_id );
if ( is_string( $early_profile_url ) )
return $early_profile_url;
// Pretty permalinks
if ( $wp_rewrite->using_permalinks() ) {
$url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%';
// Get username if not passed
if ( empty( $user_nicename ) ) {
$user = get_userdata( $user_id );
if ( !empty( $user->user_nicename ) ) {
$user_nicename = $user->user_nicename;
}
}
$url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url );
$url = home_url( user_trailingslashit( $url ) );
// Unpretty permalinks
} else {
$url = add_query_arg( array( bbp_get_user_rewrite_id() => $user_id ), home_url( '/' ) );
}
return apply_filters( 'bbp_get_user_profile_url', $url, $user_id, $user_nicename );
}
/**
* Output link to the profile edit page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_profile_edit_link() To get user profile edit link
*/
function bbp_user_profile_edit_link( $user_id = 0 ) {
echo bbp_get_user_profile_edit_link( $user_id );
}
/**
* Return link to the profile edit page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_id() To get user id
* @uses get_userdata() To get user data
* @uses bbp_get_user_profile_edit_url() To get user profile edit url
* @uses apply_filters() Calls 'bbp_get_user_profile_link' with the edit
* link and user id
* @return string User profile edit link
*/
function bbp_get_user_profile_edit_link( $user_id = 0 ) {
// Validate user id
$user_id = bbp_get_user_id( $user_id );
if ( empty( $user_id ) )
return false;
$user = get_userdata( $user_id );
$name = $user->display_name;
$edit_link = '<a href="' . bbp_get_user_profile_url( $user_id ) . '" title="' . esc_attr( $name ) . '">' . $name . '</a>';
return apply_filters( 'bbp_get_user_profile_link', $edit_link, $user_id );
}
/**
* Output URL to the profile edit page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @param string $user_nicename Optional. User nicename
* @uses bbp_get_user_profile_edit_url() To get user profile edit url
*/
function bbp_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
echo bbp_get_user_profile_edit_url( $user_id, $user_nicename );
}
/**
* Return URL to the profile edit page of a user
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @param string $user_nicename Optional. User nicename
* @uses bbp_get_user_id() To get user id
* @uses WP_Rewrite::using_permalinks() To check if the blog is using
* permalinks
* @uses add_query_arg() To add custom args to the url
* @uses home_url() To get blog home url
* @uses apply_filters() Calls 'bbp_get_user_edit_profile_url' with the
* edit profile url, user id and user nicename
* @return string
*/
function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) {
global $wp_rewrite;
$bbp = bbpress();
$user_id = bbp_get_user_id( $user_id );
if ( empty( $user_id ) )
return false;
// Pretty permalinks
if ( $wp_rewrite->using_permalinks() ) {
$url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . $bbp->user_id . '%/' . $bbp->edit_id;
// Get username if not passed
if ( empty( $user_nicename ) ) {
$user = get_userdata( $user_id );
if ( !empty( $user->user_nicename ) ) {
$user_nicename = $user->user_nicename;
}
}
$url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url );
$url = home_url( user_trailingslashit( $url ) );
// Unpretty permalinks
} else {
$url = add_query_arg( array( $bbp->user_id => $user_id, $bbp->edit_id => '1' ), home_url( '/' ) );
}
return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename );
}
/**
* Output a user's main role for display
*
* @since bbPress (r3860)
*
* @param int $user_id
* @uses bbp_get_user_display_role To get the user display role
*/
function bbp_user_display_role( $user_id = 0 ) {
echo bbp_get_user_display_role( $user_id );
}
/**
* Return a user's main role for display
*
* @since bbPress (r3860)
*
* @param int $user_id
* @uses bbp_get_user_role() To get the main user role
* @uses apply_filters() Calls 'bbp_get_user_display_role' with the
* display role, user id, and user role
* @return string
*/
function bbp_get_user_display_role( $user_id = 0 ) {
// Validate user id
$user_id = bbp_get_user_id( $user_id, false, false );
$user_role = bbp_get_user_role( $user_id );
// Capes earn Vinz Clortho status
if ( is_super_admin( $user_id ) ) {
$role = __( 'Key Master', 'bbpress' );
// User is not registered
} elseif ( empty( $user_id ) ) {
$role = __( 'Guest', 'bbpress' );
// Not the keymaster of Gozer
} else {
// Get the user's main role for display
switch ( $user_role ) {
case 'administrator' :
case 'editor' :
case 'author' :
case 'contributor' :
case 'subscriber' :
default : // Any other role (plugins, etc...)
global $wp_roles;
// Load roles if not set
if ( !isset( $wp_roles ) )
$wp_roles = new WP_Roles();
// Get a translated role name
if ( !empty( $wp_roles->role_names[$user_role] ) )
$role = translate_user_role( $wp_roles->role_names[$user_role] );
// Fallback for registered user
else
$role = __( 'Member', 'bbpress' );
break;
}
}
return apply_filters( 'bbp_get_user_display_role', $role, $user_id, $user_role );
}
/**
* Output the link to the admin section
*
* @since bbPress (r2827)
*
* @param mixed $args Optional. See {@link bbp_get_admin_link()}
* @uses bbp_get_admin_link() To get the admin link
*/
function bbp_admin_link( $args = '' ) {
echo bbp_get_admin_link( $args );
}
/**
* Return the link to the admin section
*
* @since bbPress (r2827)
*
* @param mixed $args Optional. This function supports these arguments:
* - text: The text
* - before: Before the lnk
* - after: After the link
* @uses current_user_can() To check if the current user can moderate
* @uses admin_url() To get the admin url
* @uses apply_filters() Calls 'bbp_get_admin_link' with the link & args
* @return The link
*/
function bbp_get_admin_link( $args = '' ) {
if ( !current_user_can( 'moderate' ) )
return;
if ( !empty( $args ) && is_string( $args ) && ( false === strpos( $args, '=' ) ) )
$args = array( 'text' => $args );
$defaults = array(
'text' => __( 'Admin', 'bbpress' ),
'before' => '',
'after' => ''
);
$args = bbp_parse_args( $args, $defaults, 'get_admin_link' );
extract( $args, EXTR_SKIP );
$uri = admin_url();
$retval = $before . '<a href="' . $uri . '">' . $text . '</a>' . $after;
return apply_filters( 'bbp_get_admin_link', $retval, $args );
}
/** User IP *******************************************************************/
/**
* Output the author IP address of a post
*
* @since bbPress (r3120)
*
* @param mixed $args Optional. If it is an integer, it is used as post id.
* @uses bbp_get_author_ip() To get the post author link
*/
function bbp_author_ip( $args = '' ) {
echo bbp_get_author_ip( $args );
}
/**
* Return the author IP address of a post
*
* @since bbPress (r3120)
*
* @param mixed $args Optional. If an integer, it is used as reply id.
* @uses get_post_meta() To check if it's a topic page
* @return string Author link of reply
*/
function bbp_get_author_ip( $args = '' ) {
// Default arguments
$defaults = array(
'post_id' => 0,
'before' => '<span class="bbp-author-ip">(',
'after' => ')</span>'
);
$r = bbp_parse_args( $args, $defaults, 'get_author_ip' );
extract( $r );
// Used as post id
if ( is_numeric( $args ) )
$post_id = $args;
// Get the author IP meta value
$author_ip = get_post_meta( $post_id, '_bbp_author_ip', true );
if ( !empty( $author_ip ) ) {
$author_ip = $before . $author_ip . $after;
// No IP address
} else {
$author_ip = '';
}
return apply_filters( 'bbp_get_author_ip', $author_ip, $args );
}
/** Favorites *****************************************************************/
/**
* Output the link to the user's favorites page (profile page)
*
* @since bbPress (r2652)
*
* @param int $user_id Optional. User id
* @uses bbp_get_favorites_permalink() To get the favorites permalink
*/
function bbp_favorites_permalink( $user_id = 0 ) {
echo bbp_get_favorites_permalink( $user_id );
}
/**
* Return the link to the user's favorites page (profile page)
*
* @since bbPress (r2652)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_profile_url() To get the user profile url
* @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the
* user profile url and user id
* @return string Permanent link to user profile page
*/
function bbp_get_favorites_permalink( $user_id = 0 ) {
return apply_filters( 'bbp_get_favorites_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
}
/**
* Output the link to make a topic favorite/remove a topic from favorites
*
* @since bbPress (r2652)
*
* @param array $add Optional. Add to favorites args
* @param array $rem Optional. Remove from favorites args
* @param int $user_id Optional. User id
* @param int $topic_id Optional. Topic id
* @uses bbp_get_user_favorites_link() To get the user favorites link
*/
function bbp_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
echo bbp_get_user_favorites_link( $add, $rem, $user_id, $topic_id );
}
/**
* User favorites link
*
* Return the link to make a topic favorite/remove a topic from
* favorites
*
* @since bbPress (r2652)
*
* @param array $add Optional. Add to favorites args
* @param array $rem Optional. Remove from favorites args
* @param int $user_id Optional. User id
* @param int $topic_id Optional. Topic id
* @uses bbp_get_user_id() To get the user id
* @uses current_user_can() If the current user can edit the user
* @uses bbp_get_topic_id() To get the topic id
* @uses bbp_is_user_favorite() To check if the topic is user's favorite
* @uses bbp_get_favorites_permalink() To get the favorites permalink
* @uses bbp_get_topic_permalink() To get the topic permalink
* @uses bbp_is_favorites() Is it the favorites page?
* @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the
* html, add args, remove args, user & topic id
* @return string User favorites link
*/
function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
if ( !bbp_is_favorites_active() )
return false;
// Validate user and topic ID's
$user_id = bbp_get_user_id( $user_id, true, true );
$topic_id = bbp_get_topic_id( $topic_id );
if ( empty( $user_id ) || empty( $topic_id ) )
return false;
if ( !current_user_can( 'edit_user', (int) $user_id ) )
return false;
if ( empty( $add ) || !is_array( $add ) ) {
$add = array(
'mid' => __( 'Add this topic to your favorites', 'bbpress' ),
'post' => __( ' (%?%)', 'bbpress' )
);
}
if ( empty( $rem ) || !is_array( $rem ) ) {
$rem = array(
'pre' => __( 'This topic is one of your %favorites% [', 'bbpress' ),
'mid' => __( '×', 'bbpress' ),
'post' => __( ']', 'bbpress' )
);
}
$is_fav = bbp_is_user_favorite( $user_id, $topic_id );
if ( !empty( $is_fav ) ) {
$url = esc_url( bbp_get_favorites_permalink( $user_id ) );
$rem = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
$favs = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
$pre = ( is_array( $rem ) && isset( $rem['pre'] ) ) ? $rem['pre'] : '';
$mid = ( is_array( $rem ) && isset( $rem['mid'] ) ) ? $rem['mid'] : ( is_string( $rem ) ? $rem : '' );
$_post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
} else {
$url = esc_url( bbp_get_favorites_permalink( $user_id ) );
$add = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
$favs = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
$pre = ( is_array( $add ) && isset( $add['pre'] ) ) ? $add['pre'] : '';
$mid = ( is_array( $add ) && isset( $add['mid'] ) ) ? $add['mid'] : ( is_string( $add ) ? $add : '' );
$_post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
}
// Create the link based where the user is and if the topic is
// already the user's favorite
if ( bbp_is_favorites() ) {
$permalink = bbp_get_favorites_permalink( $user_id );
} elseif ( is_singular( bbp_get_topic_post_type() ) ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} elseif ( is_singular( bbp_get_reply_post_type() ) ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
$permalink = get_permalink();
}
$url = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
$is_fav = $is_fav ? 'is-favorite' : '';
$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>';
// Return the link
return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id );
}
/** Subscriptions *************************************************************/
/**
* Output the link to the user's subscriptions page (profile page)
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_subscriptions_permalink() To get the subscriptions link
*/
function bbp_subscriptions_permalink( $user_id = 0 ) {
echo bbp_get_subscriptions_permalink( $user_id );
}
/**
* Return the link to the user's subscriptions page (profile page)
*
* @since bbPress (r2688)
*
* @param int $user_id Optional. User id
* @uses bbp_get_user_profile_url() To get the user profile url
* @uses apply_filters() Calls 'bbp_get_subscriptions_permalink' with
* the user profile url and user id
* @return string Permanent link to user subscriptions page
*/
function bbp_get_subscriptions_permalink( $user_id = 0 ) {
return apply_filters( 'bbp_get_subscriptions_permalink', bbp_get_user_profile_url( $user_id ), $user_id );
}
/**
* Output the link to subscribe/unsubscribe from a topic
*
* @since bbPress (r2668)
*
* @param mixed $args See {@link bbp_get_user_subscribe_link()}
* @uses bbp_get_user_subscribe_link() To get the subscribe link
*/
function bbp_user_subscribe_link( $args = '' ) {
echo bbp_get_user_subscribe_link( $args );
}
/**
* Return the link to subscribe/unsubscribe from a topic
*
* @since bbPress (r2668)
*
* @param mixed $args This function supports these arguments:
* - subscribe: Subscribe text
* - unsubscribe: Unsubscribe text
* - user_id: User id
* - topic_id: Topic id
* - before: Before the link
* - after: After the link
* @param int $user_id Optional. User id
* @uses bbp_get_user_id() To get the user id
* @uses current_user_can() To check if the current user can edit user
* @uses bbp_get_topic_id() To get the topic id
* @uses bbp_is_user_subscribed() To check if the user is subscribed
* @uses bbp_is_subscriptions() To check if it's the subscriptions page
* @uses bbp_get_subscriptions_permalink() To get subscriptions link
* @uses bbp_get_topic_permalink() To get topic link
* @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the
* link, args, user id & topic id
* @return string Permanent link to topic
*/
function bbp_get_user_subscribe_link( $args = '', $user_id = 0 ) {
if ( !bbp_is_subscriptions_active() )
return;
$defaults = array (
'subscribe' => __( 'Subscribe', 'bbpress' ),
'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),
'user_id' => 0,
'topic_id' => 0,
'before' => ' | ',
'after' => ''
);
$args = bbp_parse_args( $args, $defaults, 'get_user_subscribe_link' );
extract( $args );
// Validate user and topic ID's
$user_id = bbp_get_user_id( $user_id, true, true );
$topic_id = bbp_get_topic_id( $topic_id );
if ( empty( $user_id ) || empty( $topic_id ) ) {
return false;
}
// No link if you can't edit yourself
if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
return false;
}
// Decine which link to show
$is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id );
if ( !empty( $is_subscribed ) ) {
$text = $unsubscribe;
$query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
} else {
$text = $subscribe;
$query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
}
// Create the link based where the user is and if the user is
// subscribed already
if ( bbp_is_subscriptions() ) {
$permalink = bbp_get_subscriptions_permalink( $user_id );
} elseif ( is_singular( bbp_get_topic_post_type() ) ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} elseif ( is_singular( bbp_get_reply_post_type() ) ) {
$permalink = bbp_get_topic_permalink( $topic_id );
} elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
$permalink = get_permalink();
}
$url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
$is_subscribed = $is_subscribed ? 'is-subscribed' : '';
$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>';
// Return the link
return apply_filters( 'bbp_get_user_subscribe_link', $html, $args, $user_id, $topic_id );
}
/** Edit User *****************************************************************/
/**
* Edit profile success message
*
* @since bbPress (r2688)
*
* @uses bbp_is_single_user() To check if it's the profile page
* @uses bbp_is_single_user_edit() To check if it's the profile edit page
*/
function bbp_notice_edit_user_success() {
if ( isset( $_GET['updated'] ) && ( bbp_is_single_user() || bbp_is_single_user_edit() ) ) : ?>
<div class="bbp-template-notice updated">
<p><?php _e( 'User updated.', 'bbpress' ); ?></p>
</div>
<?php endif;
}
/**
* Super admin privileges notice
*
* @since bbPress (r2688)
*
* @uses is_multisite() To check if the blog is multisite
* @uses bbp_is_single_user() To check if it's the profile page
* @uses bbp_is_single_user_edit() To check if it's the profile edit page
* @uses current_user_can() To check if the current user can manage network
* options
* @uses bbp_get_displayed_user_id() To get the displayed user id
* @uses is_super_admin() To check if the user is super admin
* @uses bbp_is_user_home() To check if it's the user home
* @uses bbp_is_user_home_edit() To check if it's the user home edit
*/
function bbp_notice_edit_user_is_super_admin() {
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() ) ) : ?>
<div class="bbp-template-notice important">
<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>
</div>
<?php endif;
}
/**
* Drop down for selecting the user's display name
*
* @since bbPress (r2688)
*/
function bbp_edit_user_display_name() {
$bbp = bbpress();
$public_display = array();
$public_display['display_username'] = $bbp->displayed_user->user_login;
if ( !empty( $bbp->displayed_user->nickname ) )
$public_display['display_nickname'] = $bbp->displayed_user->nickname;
if ( !empty( $bbp->displayed_user->first_name ) )
$public_display['display_firstname'] = $bbp->displayed_user->first_name;
if ( !empty( $bbp->displayed_user->last_name ) )
$public_display['display_lastname'] = $bbp->displayed_user->last_name;
if ( !empty( $bbp->displayed_user->first_name ) && !empty( $bbp->displayed_user->last_name ) ) {
$public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name;
$public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name;
}
if ( !in_array( $bbp->displayed_user->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
$public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display;
$public_display = array_map( 'trim', $public_display );
$public_display = array_unique( $public_display ); ?>
<select name="display_name" id="display_name">
<?php foreach ( $public_display as $id => $item ) : ?>
<option id="<?php echo $id; ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo $item; ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Output role selector (for user edit)
*
* @since bbPress (r2688)
*/
function bbp_edit_user_role() {
// Return if no user is displayed
if ( !isset( bbpress()->displayed_user ) )
return;
// Local variables
$p = $r = '';
// print the 'no role' option. Make it selected if the user has no role yet.
$user_role = array_shift( bbpress()->displayed_user->roles );
if ( empty( $user_role ) )
$r .= '<option value="">' . __( '— No role for this site —', 'bbpress' ) . '</option>';
// Loop through roles
foreach ( get_editable_roles() as $role => $details ) {
$name = translate_user_role( $details['name'] );
// Make default first in list
if ( $user_role == $role ) {
$p = "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>{$name}</option>";
} else {
$r .= "\n\t<option value='" . esc_attr( $role ) . "'>{$name}</option>";
}
}
// Output result
echo '<select name="role" id="role">' . $p . $r . '</select>';
}
/**
* Return user contact methods Selectbox
*
* @since bbPress (r2688)
*
* @uses _wp_get_user_contactmethods() To get the contact methods
* @uses apply_filters() Calls 'bbp_edit_user_contact_methods' with the methods
* @return string User contact methods
*/
function bbp_edit_user_contact_methods() {
// Get the core WordPress contact methods
$contact_methods = _wp_get_user_contactmethods( bbpress()->displayed_user );
return apply_filters( 'bbp_edit_user_contact_methods', $contact_methods );
}
/** Login *********************************************************************/
/**
* Handle the login and registration template notices
*
* @since bbPress (r2970)
*
* @uses WP_Error bbPress::errors::add() To add an error or message
*/
function bbp_login_notices() {
// loggedout was passed
if ( !empty( $_GET['loggedout'] ) && ( true == $_GET['loggedout'] ) ) {
bbp_add_error( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' );
// registration is disabled
} elseif ( !empty( $_GET['registration'] ) && ( 'disabled' == $_GET['registration'] ) ) {
bbp_add_error( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) );
// Prompt user to check their email
} elseif ( !empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ) ) ) {
switch ( $_GET['checkemail'] ) {
// Email needs confirmation
case 'confirm' :
bbp_add_error( 'confirm', __( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' );
break;
// User requested a new password
case 'newpass' :
bbp_add_error( 'newpass', __( 'Check your e-mail for your new password.', 'bbpress' ), 'message' );
break;
// User is newly registered
case 'registered' :
bbp_add_error( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' );
break;
}