/
bp-core-template.php
3925 lines (3436 loc) · 107 KB
/
bp-core-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
/**
* Core component template tag functions.
*
* @package BuddyPress
* @subpackage TemplateFunctions
* @since 1.5.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Output the "options nav", the secondary-level single item navigation menu.
*
* Uses the component's nav global to render out the sub navigation for the
* current component. Each component adds to its sub navigation array within
* its own setup_nav() function.
*
* This sub navigation array is the secondary level navigation, so for profile
* it contains:
* [Public, Edit Profile, Change Avatar]
*
* The function will also analyze the current action for the current component
* to determine whether or not to highlight a particular sub nav item.
*
* @since 1.0.0
*
* viewed user.
*
* @param string $parent_slug Options nav slug.
* @return string
*/
function bp_get_options_nav( $parent_slug = '' ) {
$bp = buddypress();
// If we are looking at a member profile, then the we can use the current
// component as an index. Otherwise we need to use the component's root_slug.
$component_index = !empty( $bp->displayed_user ) ? bp_current_component() : bp_get_root_slug( bp_current_component() );
$selected_item = bp_current_action();
// Default to the Members nav.
if ( ! bp_is_single_item() ) {
// Set the parent slug, if not provided.
if ( empty( $parent_slug ) ) {
$parent_slug = $component_index;
}
$secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => $parent_slug ) );
if ( ! $secondary_nav_items ) {
return false;
}
// For a single item, try to use the component's nav.
} else {
$current_item = bp_current_item();
$single_item_component = bp_current_component();
// Adjust the selected nav item for the current single item if needed.
if ( ! empty( $parent_slug ) ) {
$current_item = $parent_slug;
$selected_item = bp_action_variable( 0 );
}
// If the nav is not defined by the parent component, look in the Members nav.
if ( ! isset( $bp->{$single_item_component}->nav ) ) {
$secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => $current_item ) );
} else {
$secondary_nav_items = $bp->{$single_item_component}->nav->get_secondary( array( 'parent_slug' => $current_item ) );
}
if ( ! $secondary_nav_items ) {
return false;
}
}
// Loop through each navigation item.
foreach ( $secondary_nav_items as $subnav_item ) {
if ( empty( $subnav_item->user_has_access ) ) {
continue;
}
// If the current action or an action variable matches the nav item id, then add a highlight CSS class.
if ( $subnav_item->slug === $selected_item ) {
$selected = ' class="current selected"';
} else {
$selected = '';
}
// List type depends on our current component.
$list_type = bp_is_group() ? 'groups' : 'personal';
/**
* Filters the "options nav", the secondary-level single item navigation menu.
*
* This is a dynamic filter that is dependent on the provided css_id value.
*
* @since 1.1.0
*
* @param string $value HTML list item for the submenu item.
* @param array $subnav_item Submenu array item being displayed.
* @param string $selected_item Current action.
*/
echo apply_filters( 'bp_get_options_nav_' . $subnav_item->css_id, '<li id="' . esc_attr( $subnav_item->css_id . '-' . $list_type . '-li' ) . '" ' . $selected . '><a id="' . esc_attr( $subnav_item->css_id ) . '" href="' . esc_url( $subnav_item->link ) . '">' . $subnav_item->name . '</a></li>', $subnav_item, $selected_item );
}
}
/**
* Get the 'bp_options_title' property from the BP global.
*
* Not currently used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_get_options_title() {
$bp = buddypress();
if ( empty( $bp->bp_options_title ) ) {
$bp->bp_options_title = __( 'Options', 'buddypress' );
}
echo apply_filters( 'bp_get_options_title', esc_attr( $bp->bp_options_title ) );
}
/**
* Get the directory title for a component.
*
* Used for the <title> element and the page header on the component directory
* page.
*
* @since 2.0.0
*
* @param string $component Component to get directory title for.
* @return string
*/
function bp_get_directory_title( $component = '' ) {
$title = '';
// Use the string provided by the component.
if ( isset( buddypress()->{$component}->directory_title ) && buddypress()->{$component}->directory_title ) {
$title = buddypress()->{$component}->directory_title;
// If none is found, concatenate.
} elseif ( isset( buddypress()->{$component}->name ) ) {
/* translators: %s: Name of the BuddyPress component */
$title = sprintf( __( '%s Directory', 'buddypress' ), buddypress()->{$component}->name );
}
/**
* Filters the directory title for a component.
*
* @since 2.0.0
*
* @param string $title Text to be used in <title> tag.
* @param string $component Current component being displayed.
*/
return apply_filters( 'bp_get_directory_title', $title, $component );
}
/** Avatars *******************************************************************/
/**
* Check to see if there is an options avatar.
*
* An options avatar is an avatar for something like a group, or a friend.
* Basically an avatar that appears in the sub nav options bar.
*
* Not currently used in BuddyPress.
*
* @return bool $value Returns true if an options avatar has been set, otherwise false.
*/
function bp_has_options_avatar() {
return (bool) buddypress()->bp_options_avatar;
}
/**
* Output the options avatar.
*
* Not currently used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_get_options_avatar() {
echo apply_filters( 'bp_get_options_avatar', buddypress()->bp_options_avatar );
}
/**
* Output a comment author's avatar.
*
* Not currently used in BuddyPress.
*/
function bp_comment_author_avatar() {
global $comment;
echo apply_filters( 'bp_comment_author_avatar', bp_core_fetch_avatar( array(
'item_id' => $comment->user_id,
'type' => 'thumb',
'alt' => sprintf(
/* translators: %s: member name */
__( 'Profile photo of %s', 'buddypress' ),
bp_core_get_user_displayname( $comment->user_id )
),
) ) );
}
/**
* Output a post author's avatar.
*
* Not currently used in BuddyPress.
*/
function bp_post_author_avatar() {
global $post;
echo apply_filters( 'bp_post_author_avatar', bp_core_fetch_avatar( array(
'item_id' => $post->post_author,
'type' => 'thumb',
'alt' => sprintf(
/* translators: %s: member name */
__( 'Profile photo of %s', 'buddypress' ),
bp_core_get_user_displayname( $post->post_author )
),
) ) );
}
/**
* Output the current avatar upload step.
*
* @since 1.1.0
*/
function bp_avatar_admin_step() {
echo bp_get_avatar_admin_step();
}
/**
* Return the current avatar upload step.
*
* @since 1.1.0
*
* @return string The current avatar upload step. Returns 'upload-image'
* if none is found.
*/
function bp_get_avatar_admin_step() {
$bp = buddypress();
$step = isset( $bp->avatar_admin->step )
? $step = $bp->avatar_admin->step
: 'upload-image';
/**
* Filters the current avatar upload step.
*
* @since 1.1.0
*
* @param string $step The current avatar upload step.
*/
return apply_filters( 'bp_get_avatar_admin_step', $step );
}
/**
* Output the URL of the avatar to crop.
*
* @since 1.1.0
*/
function bp_avatar_to_crop() {
echo bp_get_avatar_to_crop();
}
/**
* Return the URL of the avatar to crop.
*
* @since 1.1.0
*
* @return string URL of the avatar awaiting cropping.
*/
function bp_get_avatar_to_crop() {
$bp = buddypress();
$url = isset( $bp->avatar_admin->image->url )
? $bp->avatar_admin->image->url
: '';
/**
* Filters the URL of the avatar to crop.
*
* @since 1.1.0
*
* @param string $url URL for the avatar.
*/
return apply_filters( 'bp_get_avatar_to_crop', $url );
}
/**
* Output the relative file path to the avatar to crop.
*
* @since 1.1.0
*/
function bp_avatar_to_crop_src() {
echo bp_get_avatar_to_crop_src();
}
/**
* Return the relative file path to the avatar to crop.
*
* @since 1.1.0
*
* @return string Relative file path to the avatar.
*/
function bp_get_avatar_to_crop_src() {
$bp = buddypress();
$src = isset( $bp->avatar_admin->image->dir )
? str_replace( WP_CONTENT_DIR, '', $bp->avatar_admin->image->dir )
: '';
/**
* Filters the relative file path to the avatar to crop.
*
* @since 1.1.0
*
* @param string $src Relative file path for the avatar.
*/
return apply_filters( 'bp_get_avatar_to_crop_src', $src );
}
/**
* Output the avatar cropper <img> markup.
*
* No longer used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_avatar_cropper() {
?>
<img id="avatar-to-crop" class="avatar" src="<?php echo esc_url( buddypress()->avatar_admin->image ); ?>" />
<?php
}
/**
* Output the name of the BP site. Used in RSS headers.
*
* @since 1.0.0
*/
function bp_site_name() {
echo bp_get_site_name();
}
/**
* Returns the name of the BP site. Used in RSS headers.
*
* @since 1.6.0
*
* @return string
*/
function bp_get_site_name() {
/**
* Filters the name of the BP site. Used in RSS headers.
*
* @since 1.0.0
*
* @param string $value Current BP site name.
*/
return apply_filters( 'bp_site_name', get_bloginfo( 'name', 'display' ) );
}
/**
* Format a date based on a UNIX timestamp.
*
* This function can be used to turn a UNIX timestamp into a properly formatted
* (and possibly localized) string, useful for outputting the date & time an
* action took place.
*
* Not to be confused with `bp_core_time_since()`, this function is best used
* for displaying a more exact date and time vs. a human-readable time.
*
* Note: This function may be improved or removed at a later date, as it is
* hardly used and adds an additional layer of complexity to calculating dates
* and times together with timezone offsets and i18n.
*
* @since 1.1.0
*
* @param int|string $time The UNIX timestamp to be formatted.
* @param bool $exclude_time Optional. True to return only the month + day, false
* to return month, day, and time. Default: false.
* @param bool $gmt Optional. True to display in local time, false to
* leave in GMT. Default: true.
* @return mixed A string representation of $time, in the format
* "March 18, 2014 at 2:00 pm" (or whatever your
* 'date_format' and 'time_format' settings are
* on your root blog). False on failure.
*/
function bp_format_time( $time = '', $exclude_time = false, $gmt = true ) {
// Bail if time is empty or not numeric
// @todo We should output something smarter here.
if ( empty( $time ) || ! is_numeric( $time ) ) {
return false;
}
// Get GMT offset from root blog.
if ( true === $gmt ) {
// Use Timezone string if set.
$timezone_string = bp_get_option( 'timezone_string' );
if ( ! empty( $timezone_string ) ) {
$timezone_object = timezone_open( $timezone_string );
$datetime_object = date_create( "@{$time}" );
$timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
// Fall back on less reliable gmt_offset.
} else {
$timezone_offset = bp_get_option( 'gmt_offset' );
}
// Calculate time based on the offset.
$calculated_time = $time + ( $timezone_offset * HOUR_IN_SECONDS );
// No localizing, so just use the time that was submitted.
} else {
$calculated_time = $time;
}
// Formatted date: "March 18, 2014".
$formatted_date = date_i18n( bp_get_option( 'date_format' ), $calculated_time, $gmt );
// Should we show the time also?
if ( true !== $exclude_time ) {
// Formatted time: "2:00 pm".
$formatted_time = date_i18n( bp_get_option( 'time_format' ), $calculated_time, $gmt );
// Return string formatted with date and time.
$formatted_date = sprintf( esc_html__( '%1$s at %2$s', 'buddypress' ), $formatted_date, $formatted_time );
}
/**
* Filters the date based on a UNIX timestamp.
*
* @since 1.0.0
*
* @param string $formatted_date Formatted date from the timestamp.
*/
return apply_filters( 'bp_format_time', $formatted_date );
}
/**
* Select between two dynamic strings, according to context.
*
* This function can be used in cases where a phrase used in a template will
* differ for a user looking at his own profile and a user looking at another
* user's profile (eg, "My Friends" and "Joe's Friends"). Pass both versions
* of the phrase, and bp_word_or_name() will detect which is appropriate, and
* do the necessary argument swapping for dynamic phrases.
*
* @since 1.0.0
*
* @param string $youtext The "you" version of the phrase (eg "Your Friends").
* @param string $nametext The other-user version of the phrase. Should be in
* a format appropriate for sprintf() - use %s in place of the displayed
* user's name (eg "%'s Friends").
* @param bool $capitalize Optional. Force into title case. Default: true.
* @param bool $echo Optional. True to echo the results, false to return them.
* Default: true.
* @return string|null $nametext If ! $echo, returns the appropriate string.
*/
function bp_word_or_name( $youtext, $nametext, $capitalize = true, $echo = true ) {
if ( ! empty( $capitalize ) ) {
$youtext = bp_core_ucfirst( $youtext );
}
if ( bp_displayed_user_id() == bp_loggedin_user_id() ) {
if ( true == $echo ) {
/**
* Filters the text used based on context of own profile or someone else's profile.
*
* @since 1.0.0
*
* @param string $youtext Context-determined string to display.
*/
echo apply_filters( 'bp_word_or_name', $youtext );
} else {
/** This filter is documented in bp-core/bp-core-template.php */
return apply_filters( 'bp_word_or_name', $youtext );
}
} else {
$fullname = bp_get_displayed_user_fullname();
$fullname = (array) explode( ' ', $fullname );
$nametext = sprintf( $nametext, $fullname[0] );
if ( true == $echo ) {
/** This filter is documented in bp-core/bp-core-template.php */
echo apply_filters( 'bp_word_or_name', $nametext );
} else {
/** This filter is documented in bp-core/bp-core-template.php */
return apply_filters( 'bp_word_or_name', $nametext );
}
}
}
/**
* Do the 'bp_styles' action, and call wp_print_styles().
*
* No longer used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_styles() {
do_action( 'bp_styles' );
wp_print_styles();
}
/** Search Form ***************************************************************/
/**
* Return the "action" attribute for search forms.
*
* @since 1.0.0
*
* @return string URL action attribute for search forms, eg example.com/search/.
*/
function bp_search_form_action() {
/**
* Filters the "action" attribute for search forms.
*
* @since 1.0.0
*
* @param string $value Search form action url.
*/
return apply_filters( 'bp_search_form_action', trailingslashit( bp_get_root_domain() . '/' . bp_get_search_slug() ) );
}
/**
* Generate the basic search form as used in BP-Default's header.
*
* @since 1.0.0
*
* @return string HTML <select> element.
*/
function bp_search_form_type_select() {
$options = array();
if ( bp_is_active( 'xprofile' ) ) {
$options['members'] = _x( 'Members', 'search form', 'buddypress' );
}
if ( bp_is_active( 'groups' ) ) {
$options['groups'] = _x( 'Groups', 'search form', 'buddypress' );
}
if ( bp_is_active( 'blogs' ) && is_multisite() ) {
$options['blogs'] = _x( 'Blogs', 'search form', 'buddypress' );
}
$options['posts'] = _x( 'Posts', 'search form', 'buddypress' );
// Eventually this won't be needed and a page will be built to integrate all search results.
$selection_box = '<label for="search-which" class="accessibly-hidden">' . _x( 'Search these:', 'search form', 'buddypress' ) . '</label>';
$selection_box .= '<select name="search-which" id="search-which" style="width: auto">';
/**
* Filters all of the component options available for search scope.
*
* @since 1.5.0
*
* @param array $options Array of options to add to select field.
*/
$options = apply_filters( 'bp_search_form_type_select_options', $options );
foreach( (array) $options as $option_value => $option_title ) {
$selection_box .= sprintf( '<option value="%s">%s</option>', $option_value, $option_title );
}
$selection_box .= '</select>';
/**
* Filters the complete <select> input used for search scope.
*
* @since 1.0.0
*
* @param string $selection_box <select> input for selecting search scope.
*/
return apply_filters( 'bp_search_form_type_select', $selection_box );
}
/**
* Output the 'name' attribute for search form input element.
*
* @since 2.7.0
*
* @param string $component See bp_get_search_input_name().
*/
function bp_search_input_name( $component = '' ) {
echo esc_attr( bp_get_search_input_name( $component ) );
}
/**
* Get the 'name' attribute for the search form input element.
*
* @since 2.7.0
*
* @param string $component Component name. Defaults to current component.
* @return string Text for the 'name' attribute.
*/
function bp_get_search_input_name( $component = '' ) {
if ( ! $component ) {
$component = bp_current_component();
}
$bp = buddypress();
$name = '';
if ( isset( $bp->{$component}->id ) ) {
$name = $bp->{$component}->id . '_search';
}
return $name;
}
/**
* Output the placeholder text for the search box for a given component.
*
* @since 2.7.0
*
* @param string $component See bp_get_search_placeholder().
*/
function bp_search_placeholder( $component = '' ) {
echo esc_attr( bp_get_search_placeholder( $component ) );
}
/**
* Get the placeholder text for the search box for a given component.
*
* @since 2.7.0
*
* @param string $component Component name. Defaults to current component.
* @return string Placeholder text for the search field.
*/
function bp_get_search_placeholder( $component = '' ) {
$query_arg = bp_core_get_component_search_query_arg( $component );
if ( $query_arg && ! empty( $_REQUEST[ $query_arg ] ) ) {
$placeholder = wp_unslash( $_REQUEST[ $query_arg ] );
} else {
$placeholder = bp_get_search_default_text( $component );
}
return $placeholder;
}
/**
* Output the default text for the search box for a given component.
*
* @since 1.5.0
*
* @see bp_get_search_default_text()
*
* @param string $component See {@link bp_get_search_default_text()}.
*/
function bp_search_default_text( $component = '' ) {
echo bp_get_search_default_text( $component );
}
/**
* Return the default text for the search box for a given component.
*
* @since 1.5.0
*
* @param string $component Component name. Default: current component.
* @return string Placeholder text for search field.
*/
function bp_get_search_default_text( $component = '' ) {
$bp = buddypress();
if ( empty( $component ) ) {
$component = bp_current_component();
}
$default_text = __( 'Search anything...', 'buddypress' );
// Most of the time, $component will be the actual component ID.
if ( !empty( $component ) ) {
if ( !empty( $bp->{$component}->search_string ) ) {
$default_text = $bp->{$component}->search_string;
} else {
// When the request comes through AJAX, we need to get the component
// name out of $bp->pages.
if ( !empty( $bp->pages->{$component}->slug ) ) {
$key = $bp->pages->{$component}->slug;
if ( !empty( $bp->{$key}->search_string ) ) {
$default_text = $bp->{$key}->search_string;
}
}
}
}
/**
* Filters the default text for the search box for a given component.
*
* @since 1.5.0
*
* @param string $default_text Default text for search box.
* @param string $component Current component displayed.
*/
return apply_filters( 'bp_get_search_default_text', $default_text, $component );
}
/**
* Fire the 'bp_custom_profile_boxes' action.
*
* No longer used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_custom_profile_boxes() {
do_action( 'bp_custom_profile_boxes' );
}
/**
* Fire the 'bp_custom_profile_sidebar_boxes' action.
*
* No longer used in BuddyPress.
*
* @todo Deprecate.
*/
function bp_custom_profile_sidebar_boxes() {
do_action( 'bp_custom_profile_sidebar_boxes' );
}
/**
* Output the attributes for a form field.
*
* @since 2.2.0
*
* @param string $name The field name to output attributes for.
* @param array $attributes Array of existing attributes to add.
*/
function bp_form_field_attributes( $name = '', $attributes = array() ) {
echo bp_get_form_field_attributes( $name, $attributes );
}
/**
* Get the attributes for a form field.
*
* Primarily to add better support for touchscreen devices, but plugin devs
* can use the 'bp_get_form_field_extra_attributes' filter for further
* manipulation.
*
* @since 2.2.0
*
* @param string $name The field name to get attributes for.
* @param array $attributes Array of existing attributes to add.
* @return string
*/
function bp_get_form_field_attributes( $name = '', $attributes = array() ) {
$retval = '';
if ( empty( $attributes ) ) {
$attributes = array();
}
$name = strtolower( $name );
switch ( $name ) {
case 'username' :
case 'blogname' :
$attributes['autocomplete'] = 'off';
$attributes['autocapitalize'] = 'none';
break;
case 'email' :
if ( wp_is_mobile() ) {
$attributes['autocapitalize'] = 'none';
}
break;
case 'password' :
$attributes['spellcheck'] = 'false';
$attributes['autocomplete'] = 'off';
if ( wp_is_mobile() ) {
$attributes['autocorrect'] = 'false';
$attributes['autocapitalize'] = 'none';
}
break;
}
/**
* Filter the attributes for a field before rendering output.
*
* @since 2.2.0
*
* @param array $attributes The field attributes.
* @param string $name The field name.
*/
$attributes = (array) apply_filters( 'bp_get_form_field_attributes', $attributes, $name );
foreach( $attributes as $attr => $value ) {
// Numeric keyed array.
if (is_numeric( $attr ) ) {
$retval .= sprintf( ' %s', esc_attr( $value ) );
// Associative keyed array.
} else {
$retval .= sprintf( ' %s="%s"', sanitize_key( $attr ), esc_attr( $value ) );
}
}
return $retval;
}
/**
* Create and output a button.
*
* @since 1.2.6
*
* @see bp_get_button()
*
* @param array|string $args See {@link BP_Button}.
*/
function bp_button( $args = '' ) {
echo bp_get_button( $args );
}
/**
* Create and return a button.
*
* @since 1.2.6
*
* @see BP_Button for a description of arguments and return value.
*
* @param array|string $args See {@link BP_Button}.
* @return string HTML markup for the button.
*/
function bp_get_button( $args = '' ) {
$button = new BP_Button( $args );
/**
* Filters the requested button output.
*
* @since 1.2.6
*
* @param string $contents Button context to be used.
* @param array $args Array of args for the button.
* @param BP_Button $button BP_Button object.
*/
return apply_filters( 'bp_get_button', $button->contents, $args, $button );
}
/**
* Truncate text.
*
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
*
* This function is borrowed from CakePHP v2.0, under the MIT license. See
* http://book.cakephp.org/view/1469/Text#truncate-1625
*
* @since 1.0.0
* @since 2.6.0 Added 'strip_tags' and 'remove_links' as $options args.
*
* @param string $text String to truncate.
* @param int $length Optional. Length of returned string, including ellipsis.
* Default: 225.
* @param array $options {
* An array of HTML attributes and options. Each item is optional.
* @type string $ending The string used after truncation.
* Default: ' […]'.
* @type bool $exact If true, $text will be trimmed to exactly $length.
* If false, $text will not be cut mid-word. Default: false.
* @type bool $html If true, don't include HTML tags when calculating
* excerpt length. Default: true.
* @type bool $filter_shortcodes If true, shortcodes will be stripped.
* Default: true.
* @type bool $strip_tags If true, HTML tags will be stripped. Default: false.
* Only applicable if $html is set to false.
* @type bool $remove_links If true, URLs will be stripped. Default: false.
* Only applicable if $html is set to false.
* }
* @return string Trimmed string.
*/
function bp_create_excerpt( $text, $length = 225, $options = array() ) {
// Backward compatibility. The third argument used to be a boolean $filter_shortcodes.
$filter_shortcodes_default = is_bool( $options ) ? $options : true;
$r = bp_parse_args(
$options,
array(
'ending' => __( ' […]', 'buddypress' ),
'exact' => false,
'html' => true,
'filter_shortcodes' => $filter_shortcodes_default,
'strip_tags' => false,
'remove_links' => false,
),
'create_excerpt'
);
// Save the original text, to be passed along to the filter.
$original_text = $text;
/**
* Filters the excerpt length to trim text to.
*
* @since 1.5.0
*
* @param int $length Length of returned string, including ellipsis.
*/
$length = apply_filters( 'bp_excerpt_length', $length );
/**
* Filters the excerpt appended text value.
*
* @since 1.5.0
*
* @param string $value Text to append to the end of the excerpt.
*/
$ending = apply_filters( 'bp_excerpt_append_text', $r['ending'] );
// Remove shortcodes if necessary.
if ( ! empty( $r['filter_shortcodes'] ) ) {
$text = strip_shortcodes( $text );
}
// When $html is true, the excerpt should be created without including HTML tags in the
// excerpt length.
if ( ! empty( $r['html'] ) ) {
// The text is short enough. No need to truncate.
if ( mb_strlen( preg_replace( '/<.*?>/', '', $text ) ) <= $length ) {
return $text;
}
$totalLength = mb_strlen( strip_tags( $ending ) );
$openTags = array();
$truncate = '';
// Find all the tags and HTML comments and put them in a stack for later use.
preg_match_all( '/(<\/?([\w+!]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER );
foreach ( $tags as $tag ) {
// Process tags that need to be closed.
if ( !preg_match( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2] ) ) {
if ( preg_match( '/<[\w]+[^>]*>/s', $tag[0] ) ) {
array_unshift( $openTags, $tag[2] );
} elseif ( preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag ) ) {
$pos = array_search( $closeTag[1], $openTags );
if ( $pos !== false ) {
array_splice( $openTags, $pos, 1 );
}
}
}
$truncate .= $tag[1];
$contentLength = mb_strlen( preg_replace( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3] ) );
if ( $contentLength + $totalLength > $length ) {
$left = $length - $totalLength;
$entitiesLength = 0;
if ( preg_match_all( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE ) ) {
foreach ( $entities[0] as $entity ) {
if ( $entity[1] + 1 - $entitiesLength <= $left ) {
$left--;
$entitiesLength += mb_strlen( $entity[0] );
} else {
break;
}
}
}
$truncate .= mb_substr( $tag[3], 0 , $left + $entitiesLength );
break;
} else {
$truncate .= $tag[3];
$totalLength += $contentLength;
}
if ( $totalLength >= $length ) {
break;
}
}
} else {
// Strip HTML tags if necessary.
if ( ! empty( $r['strip_tags'] ) ) {
$text = strip_tags( $text );
}
// Remove links if necessary.
if ( ! empty( $r['remove_links'] ) ) {
$text = preg_replace( '#^\s*(https?://[^\s"]+)\s*$#im', '', $text );
}
if ( mb_strlen( $text ) <= $length ) {
/**
* Filters the final generated excerpt.
*
* @since 1.1.0
*
* @param string $text Generated excerpt.
* @param string $original_text Original text provided.
* @param int $length Length of returned string, including ellipsis.
* @param array $options Array of HTML attributes and options.
*/
return apply_filters( 'bp_create_excerpt', $text, $original_text, $length, $options );