/
bp-usernames-only.php
525 lines (420 loc) · 14.7 KB
/
bp-usernames-only.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
<?php
/**
* BP Usernames Only Core
*
* @package BP-Usernames-Only
* @subpackage Core
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
// Set 'BP_SHOW_DISPLAYNAME_ON_PROFILE' constant if not already defined
if( ! defined( 'BP_SHOW_DISPLAYNAME_ON_PROFILE' ) ) {
define( 'BP_SHOW_DISPLAYNAME_ON_PROFILE', true );
}
if( (bool) BP_SHOW_DISPLAYNAME_ON_PROFILE === true ) {
// only show the display name for the <h1> tag on a member profile
add_action( 'bp_before_member_header', function() {
remove_filter( 'bp_displayed_user_fullname', 'ray_bp_displayed_user_fullname' );
}, 99 );
// add back the username filter
add_action( 'bp_before_member_header_meta', function() {
add_filter( 'bp_displayed_user_fullname', 'ray_bp_displayed_user_fullname' );
}, 0 );
// support for BP Lists
if ( function_exists( 'bp_is_lists_component' ) && bp_is_lists_component() && bp_is_single_item() )
add_filter( 'bp_displayed_user_fullname', 'ray_bp_displayed_user_fullname' );
} else {
add_filter( 'bp_displayed_user_fullname', 'ray_bp_displayed_user_fullname' );
}
/* UTILITY *********************************************************/
/**
* Returns either the username or user nicename depending on the BuddyPress
* username compatibility mode.
*
* @since 0.6
*
* @param obj $userdata Object The userdata that includes the user_login and user_nicename.
* @mixed String on success; boolean false on failure
*/
function ray_bp_username_compatibility( $userdata ) {
if ( empty( $userdata ) )
return false;
if ( bp_is_username_compatibility_mode() )
return $userdata->user_login;
return $userdata->user_nicename;
}
if ( ! function_exists( 'str_replace_first' ) ) :
/**
* Replaces the first match in a target string.
*
* @since 0.6
*
* @param string $search The text we want to get rid of
* @param string $replace The text we want to replace with
* @param string $subject The string we want to do the replacing with
*/
function str_replace_first( $search, $replace, $subject ) {
$pos = strpos( $subject, $search );
if ( $pos !== false ) {
$subject = substr_replace( $subject, $replace, $pos, strlen( $search ) );
}
return $subject;
}
endif;
/**
* Replaces the first occurence of anchor text in a string.
*
* @since 0.6
*
* @param string $subject The string we want to replace the first occurence of anchor text for.
* @param string $replacement The anchor text we want to replace with
* @return string
*/
function ray_replace_first_anchor_text( $subject, $replacement ) {
$before_anchor_text = strpos( $subject, '">' ) + 2;
$after_anchor_text = strpos( $subject, '</a' );
return substr_replace( $subject, $replacement, $before_anchor_text, $after_anchor_text - $before_anchor_text );
}
/**
* Fix first instance of anchor element to use double quotations instead of
* single quotations in anchor tag.
*
* This is not perfect.
*
* @since 0.6
*
* @param string $content
* @return string
*/
function ray_fix_first_anchor( $content ) {
$content = str_replace_first( "href='", 'href="', $content );
$content = str_replace_first( "'>", '">', $content );
return $content;
}
/* CORE OVERRIDES **************************************************/
function ray_bp_core_get_userlink( $link, $user_id ) {
global $bp;
if ( bp_loggedin_user_id() == $user_id ) {
$displayed_user = $bp->loggedin_user->userdata;
} elseif ( bp_displayed_user_id() == $user_id ) {
$displayed_user = $bp->displayed_user->userdata;
} else {
if ( empty( $bp->usernames_only->userdata ) ) {
$bp->usernames_only = new stdClass;
$bp->usernames_only->userdata = array();
}
$displayed_user = false;
// try to get locally-cached value first
if ( ! empty( $bp->usernames_only->userdata[$user_id] ) ) {
$displayed_user = $bp->usernames_only->userdata[$user_id];
}
// no cached value, so query for it
if ( $displayed_user === false ) {
$displayed_user = bp_core_get_core_userdata( $user_id );
// cache it for later use
$bp->usernames_only->userdata[$user_id] = $displayed_user;
}
}
return ray_replace_first_anchor_text( $link, ray_bp_username_compatibility( $displayed_user ) );
}
add_filter( 'bp_core_get_userlink', 'ray_bp_core_get_userlink', 1, 2 );
/**
* Used in member profile header.
*/
function ray_bp_displayed_user_fullname() {
global $bp;
return ray_bp_username_compatibility( $bp->displayed_user->userdata );
}
/**
* Used in private messages (sent between blah and x)
*/
function ray_bp_get_loggedin_user_fullname() {
global $bp;
return ray_bp_username_compatibility( $bp->loggedin_user->userdata );
}
add_filter( 'bp_get_loggedin_user_fullname', 'ray_bp_get_loggedin_user_fullname' );
/**
* Used for group invite friends list
*/
function ray_bp_get_member_name( $name ) {
global $members_template;
return ray_bp_username_compatibility( $members_template->member );
}
add_filter( 'bp_get_member_name' , 'ray_bp_get_member_name' );
/**
* Used in a lot of places
* - email notifications
* - messages subnav (From: x)
* - private messages (sent between x and blah)
*/
function ray_bp_core_get_user_displayname( $name, $user_id ) {
global $bp;
if ( bp_loggedin_user_id() == $user_id ) {
$displayed_user = $bp->loggedin_user->userdata;
} elseif ( bp_displayed_user_id() == $user_id ) {
$displayed_user = $bp->displayed_user->userdata;
} else {
if ( empty( $bp->usernames_only->userdata ) ) {
$bp->usernames_only = new stdClass;
$bp->usernames_only->userdata = array();
}
$displayed_user = false;
// try to get locally-cached value first
if ( ! empty( $bp->usernames_only->userdata[$user_id] ) ) {
$displayed_user = $bp->usernames_only->userdata[$user_id];
}
// no cached value, so query for it
if ( $displayed_user === false ) {
$displayed_user = bp_core_get_core_userdata( $user_id );
// cache it for later use
$bp->usernames_only->userdata[$user_id] = $displayed_user;
}
}
return ray_bp_username_compatibility( $displayed_user );
}
add_filter( 'bp_core_get_user_displayname', 'ray_bp_core_get_user_displayname', 1, 2 );
/**
* Change BP followers members listing
*/
function ray_bp_get_user_firstname( $name ) {
global $members_template;
if( ! empty( $members_template->member->user_login ) )
return ray_bp_username_compatibility( $members_template->member );
if ( bp_displayed_user_id() ) {
global $bp;
return ray_bp_username_compatibility( $bp->displayed_user->userdata );
}
return $name;
}
add_filter( 'bp_get_user_firstname' , 'ray_bp_get_user_firstname' );
/**
* Change "What's new, username" in post form
*
* @since 0.6
*/
function ray_bp_whats_new_firstname( $name ) {
if ( bp_loggedin_user_id() ) {
global $bp;
return ray_bp_username_compatibility( $bp->loggedin_user->userdata );
}
return $name;
}
add_action( 'bp_before_activity_post_form', function() {
add_filter( 'bp_get_user_firstname', 'ray_bp_whats_new_firstname' );
}, 99 );
add_action( 'bp_activity_post_form_options', function() {
remove_filter( 'bp_get_user_firstname', 'ray_bp_whats_new_firstname' );
}, 0 );
/**
* Used in <title> tag
*/
function ray_bp_page_title( $title, $sep ) {
if ( bp_displayed_user_id() ) {
global $bp;
$title = str_replace(
$bp->displayed_user->fullname . ' ' . $sep,
ray_bp_username_compatibility( $bp->displayed_user->userdata ) . ' ' . $sep,
$title
);
}
return $title;
}
add_filter( 'wp_title', 'ray_bp_page_title', 20, 2 );
/* GROUP OVERRIDES *************************************************/
/**
* Used in group member listing
*/
function ray_bp_get_group_member_link() {
global $members_template;
return '<a href="' . bp_core_get_user_domain( $members_template->member->user_id, $members_template->member->user_nicename, $members_template->member->user_login ) . '">' . ray_bp_username_compatibility( $members_template->member ) . '</a>';
}
add_filter( 'bp_get_group_member_link', 'ray_bp_get_group_member_link' );
/* ACTIVITY OVERRIDES **********************************************/
/**
* Fix first instance of activity action to use double quotations instead of
* single quotations in anchor tag.
*
* Runs at priority 0; just before ray_bp_get_activity_action().
*
* @since 0.6
*/
function ray_bp_fix_activity_action( $action ) {
return ray_fix_first_anchor( $action );
}
/**
* Used in parent activity update
*/
function ray_bp_get_activity_action( $action, $activity ) {
if ( empty( $activity->user_id ) ) {
return $action;
}
return ray_replace_first_anchor_text( $action, ray_bp_username_compatibility( $activity ) );
}
// bug in BP 1.7; activity admin page is missing the other filter parameters
// for this hook, so we only add this hook on the frontend or during AJAX.
//
// for the AJAX check, we're checking HTTP_X_REQUESTED_WITH b/c BP 1.5 doesn't
// use admin-ajax.php.
if ( ! is_admin() || ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) ) {
add_filter( 'bp_get_activity_action', 'ray_bp_fix_activity_action', 0 );
add_filter( 'bp_get_activity_action', 'ray_bp_get_activity_action', 1, 2 );
}
/**
* Replaces username in the "In reply to" activity item.
*
* This is new in BuddyPress 1.5.
* See /bp-themes/bp-default/activity/entry.php.
*
* @since 0.6
*/
function ray_bp_get_activity_parent_content( $content ) {
global $activities_template;
$parent_id = $activities_template->activity->item_id;
return ray_replace_first_anchor_text( $content, ray_bp_username_compatibility( $activities_template->activity_parents[$parent_id] ) );
}
add_filter( 'bp_get_activity_parent_content', 'ray_bp_get_activity_parent_content' );
/**
* Used in activity comments
*/
function ray_bp_acomment_name( $name, $comment ) {
return ray_bp_username_compatibility( $comment );
}
add_filter( 'bp_acomment_name' , 'ray_bp_acomment_name', 1, 2 );
/**
* RSS feed title.
*
* We can't use the 'bp_get_activity_feed_title' hook because that filter
* strips the HTML tags before plugins can alter the activity action.
*
* So we have to resort to hacking the $activities_template global before
* bp_get_activity_feed_item_title() is called.
*
* @since 0.6.1
*/
function ray_bp_uso_filter_activity_action_for_feeds( $retval ) {
if ( is_feed() ) {
global $activities_template;
$activities_template->activity->action = ray_replace_first_anchor_text( $activities_template->activity->action, ray_bp_username_compatibility( $activities_template->activity ) );
}
return $retval;
}
add_filter( 'bp_get_activity_thread_permalink', 'ray_bp_uso_filter_activity_action_for_feeds' );
/* FORUM OVERRIDES *************************************************/
/**
* Used in the bbPress plugin
*
* @since 0.6
*/
function ray_get_the_author_display_name( $name, $user_id ) {
// test to see if we're on a BP group forum page or on any bbPress page
if ( function_exists( 'bbpress' ) && is_bbpress() ) {
// cache username queries with static variable
//
// tried stuffing in $bp global but didn't work properly
// probably due to object buffering in bbP
static $bp_uso_data = array();
$name = false;
// try to get locally-cached value first
if ( ! empty( $bp_uso_data[$user_id] ) ) {
$name = $bp_uso_data[$user_id];
}
// no cached value, so query for it
if ( $name === false ) {
$field = bp_is_username_compatibility_mode() ? 'user_login' : 'user_nicename';
$name = get_the_author_meta( $field, $user_id );
// cache it for later use in the loop
$bp_uso_data[$user_id] = $name;
}
}
return $name;
}
add_filter( 'get_the_author_display_name', 'ray_get_the_author_display_name', 10, 2 );
/* MESSAGE OVERRIDES ***********************************************/
/**
* Used in message loop
*/
function ray_bp_get_the_thread_message_sender_name() {
global $bp, $thread_template;
if ( bp_loggedin_user_id() == $thread_template->message->sender_id ) {
$displayed_user = $bp->loggedin_user->userdata;
} elseif ( bp_displayed_user_id() == $thread_template->message->sender_id ) {
$displayed_user = $bp->displayed_user->userdata;
} else {
if ( empty( $bp->usernames_only->userdata ) ) {
$bp->usernames_only = new stdClass;
$bp->usernames_only->userdata = array();
}
$displayed_user = false;
// try to get locally-cached value first
if ( ! empty( $bp->usernames_only->userdata[$thread_template->message->sender_id] ) ) {
$displayed_user = $bp->usernames_only->userdata[$thread_template->message->sender_id];
}
// no cached value, so query for it
if ( $displayed_user === false ) {
$displayed_user = bp_core_get_core_userdata( $thread_template->message->sender_id );
// cache it for later use in the loop
$bp->usernames_only->userdata[$thread_template->message->sender_id] = $displayed_user;
}
}
return ray_bp_username_compatibility( $displayed_user );
}
add_filter( 'bp_get_the_thread_message_sender_name', 'ray_bp_get_the_thread_message_sender_name' );
/**
* Override display name for ajax message reply
*
* Hopefully there aren't any side-effects with doing this.
*/
function ray_bp_message_reply_ajax_sent_name() {
global $bp;
$bp->loggedin_user->fullname = ray_bp_username_compatibility( $bp->loggedin_user->userdata );
}
add_action( 'bp_before_message_meta', 'ray_bp_message_reply_ajax_sent_name' );
/* BLOG OVERRIDES **************************************************/
/**
* Used in comment author link
*
* @todo Locally-cache value. Useful in a comment loop.
*/
function ray_get_comment_author( $author ) {
global $bp, $comment;
if( ! empty( $comment->user_id ) ) {
if ( bp_loggedin_user_id() == $comment->user_id ) {
$displayed_user = $bp->loggedin_user->userdata;
} else {
if ( empty( $bp->usernames_only->userdata ) ) {
$bp->usernames_only = new stdClass;
$bp->usernames_only->userdata = array();
}
$displayed_user = false;
// try to get locally-cached value first
if ( ! empty( $bp->usernames_only->userdata[$comment->user_id] ) ) {
$displayed_user = $bp->usernames_only->userdata[$comment->user_id];
}
// no cached value, so query for it
if ( $displayed_user === false ) {
$displayed_user = bp_core_get_core_userdata( $comment->user_id );
// cache it for later use in the loop
$bp->usernames_only->userdata[$comment->user_id] = $displayed_user;
}
}
return ray_bp_username_compatibility( $displayed_user );
} else {
return $author;
}
}
add_filter( 'get_comment_author', 'ray_get_comment_author' );
/* ADMIN BAR *******************************************************/
/**
* Replaces display name in WP Toolbar. Only takes effect in WP 3.3+.
*
* Hacks the $current_user global before the "My Account" menu is output.
*
* @since 0.6
*/
function ray_wp_toolbar_title( $wp_admin_bar ) {
if ( ! is_user_logged_in() )
return;
global $current_user, $bp;
$current_user->display_name = ray_bp_username_compatibility( $bp->loggedin_user->userdata );
}
add_action( 'admin_bar_menu', 'ray_wp_toolbar_title', 6 );