/
bp-core-functions.php
3902 lines (3424 loc) · 125 KB
/
bp-core-functions.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
/**
* BuddyPress Common Functions.
*
* @package BuddyPress
* @subpackage Functions
* @since 1.5.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/** Versions ******************************************************************/
/**
* Output the BuddyPress version.
*
* @since 1.6.0
*
*/
function bp_version() {
echo bp_get_version();
}
/**
* Return the BuddyPress version.
*
* @since 1.6.0
*
* @return string The BuddyPress version.
*/
function bp_get_version() {
return buddypress()->version;
}
/**
* Output the BuddyPress database version.
*
* @since 1.6.0
*
*/
function bp_db_version() {
echo bp_get_db_version();
}
/**
* Return the BuddyPress database version.
*
* @since 1.6.0
*
* @return string The BuddyPress database version.
*/
function bp_get_db_version() {
return buddypress()->db_version;
}
/**
* Output the BuddyPress database version.
*
* @since 1.6.0
*
*/
function bp_db_version_raw() {
echo bp_get_db_version_raw();
}
/**
* Return the BuddyPress database version.
*
* @since 1.6.0
*
* @return string The BuddyPress version direct from the database.
*/
function bp_get_db_version_raw() {
$bp = buddypress();
return !empty( $bp->db_version_raw ) ? $bp->db_version_raw : 0;
}
/** Functions *****************************************************************/
/**
* Get the $wpdb base prefix, run through the 'bp_core_get_table_prefix' filter.
*
* The filter is intended primarily for use in multinetwork installations.
*
* @since 1.2.6
*
* @global object $wpdb WordPress database object.
*
* @return string Filtered database prefix.
*/
function bp_core_get_table_prefix() {
global $wpdb;
/**
* Filters the $wpdb base prefix.
*
* Intended primarily for use in multinetwork installations.
*
* @since 1.2.6
*
* @param string $base_prefix Base prefix to use.
*/
return apply_filters( 'bp_core_get_table_prefix', $wpdb->base_prefix );
}
/**
* Sort an array of objects or arrays by a specific key/property.
*
* The main purpose for this function is so that you can avoid having to create
* your own awkward callback function for usort().
*
* @since 2.2.0
* @since 2.7.0 Added $preserve_keys parameter.
*
* @param array $items The items to be sorted. Its constituent items
* can be either associative arrays or objects.
* @param string|int $key The array index or property name to sort by.
* @param string $type Sort type. 'alpha' for alphabetical, 'num'
* for numeric. Default: 'alpha'.
* @param bool $preserve_keys Whether to keep the keys or not.
*
* @return array $items The sorted array.
*/
function bp_sort_by_key( $items, $key, $type = 'alpha', $preserve_keys = false ) {
$callback = function( $a, $b ) use ( $key, $type ) {
$values = array( 0 => false, 1 => false );
foreach ( func_get_args() as $indexi => $index ) {
if ( isset( $index->{$key} ) ) {
$values[ $indexi ] = $index->{$key};
} elseif ( isset( $index[ $key ] ) ) {
$values[ $indexi ] = $index[ $key ];
}
}
if ( isset( $values[0], $values[1] ) ) {
if ( 'num' === $type ) {
$cmp = $values[0] - $values[1];
} else {
$cmp = strcmp( $values[0], $values[1] );
}
if ( 0 > $cmp ) {
$retval = -1;
} elseif ( 0 < $cmp ) {
$retval = 1;
} else {
$retval = 0;
}
return $retval;
} else {
return 0;
}
};
if ( true === $preserve_keys ) {
uasort( $items, $callback );
} else {
usort( $items, $callback );
}
return $items;
}
/**
* Sort an array of objects or arrays by alphabetically sorting by a specific key/property.
*
* For instance, if you have an array of WordPress post objects, you can sort
* them by post_name as follows:
* $sorted_posts = bp_alpha_sort_by_key( $posts, 'post_name' );
*
* @since 1.9.0
*
* @param array $items The items to be sorted. Its constituent items can be either associative arrays or objects.
* @param string|int $key The array index or property name to sort by.
* @return array $items The sorted array.
*/
function bp_alpha_sort_by_key( $items, $key ) {
return bp_sort_by_key( $items, $key, 'alpha' );
}
/**
* Format numbers the BuddyPress way.
*
* @since 1.2.0
*
* @param int $number The number to be formatted.
* @param bool $decimals Whether to use decimals. See {@link number_format_i18n()}.
* @return string The formatted number.
*/
function bp_core_number_format( $number = 0, $decimals = false ) {
// Force number to 0 if needed.
if ( ! is_numeric( $number ) ) {
$number = 0;
}
/**
* Filters the BuddyPress formatted number.
*
* @since 1.2.4
*
* @param string $value BuddyPress formatted value.
* @param int $number The number to be formatted.
* @param bool $decimals Whether or not to use decimals.
*/
return apply_filters( 'bp_core_number_format', number_format_i18n( $number, $decimals ), $number, $decimals );
}
/**
* A utility for parsing individual function arguments into an array.
*
* The purpose of this function is to help with backward compatibility in cases where
*
* function foo( $bar = 1, $baz = false, $barry = array(), $blip = false ) { // ...
*
* is deprecated in favor of
*
* function foo( $args = array() ) {
* $defaults = array(
* 'bar' => 1,
* 'arg2' => false,
* 'arg3' => array(),
* 'arg4' => false,
* );
* $r = wp_parse_args( $args, $defaults ); // ...
*
* The first argument, $old_args_keys, is an array that matches the parameter positions (keys) to
* the new $args keys (values):
*
* $old_args_keys = array(
* 0 => 'bar', // because $bar was the 0th parameter for foo()
* 1 => 'baz', // because $baz was the 1st parameter for foo()
* 2 => 'barry', // etc
* 3 => 'blip'
* );
*
* For the second argument, $func_args, you should just pass the value of func_get_args().
*
* @since 1.6.0
*
* @param array $old_args_keys Old argument indexs, keyed to their positions.
* @param array $func_args The parameters passed to the originating function.
* @return array $new_args The parsed arguments.
*/
function bp_core_parse_args_array( $old_args_keys, $func_args ) {
$new_args = array();
foreach( $old_args_keys as $arg_num => $arg_key ) {
if ( isset( $func_args[$arg_num] ) ) {
$new_args[$arg_key] = $func_args[$arg_num];
}
}
return $new_args;
}
/**
* Merge user defined arguments into defaults array.
*
* This function is used throughout BuddyPress to allow for either a string or
* array to be merged into another array. It is identical to wp_parse_args()
* except it allows for arguments to be passively or aggressively filtered using
* the optional $filter_key parameter. If no $filter_key is passed, no filters
* are applied.
*
* @since 2.0.0
*
* @param string|array $args Value to merge with $defaults.
* @param array $defaults Array that serves as the defaults.
* @param string $filter_key String to key the filters from.
* @return array Merged user defined values with defaults.
*/
function bp_parse_args( $args, $defaults = array(), $filter_key = '' ) {
// Setup a temporary array from $args.
if ( is_object( $args ) ) {
$r = get_object_vars( $args );
} elseif ( is_array( $args ) ) {
$r =& $args;
} else {
wp_parse_str( $args, $r );
}
// Passively filter the args before the parse.
if ( !empty( $filter_key ) ) {
/**
* Filters the arguments key before parsing if filter key provided.
*
* This is a dynamic filter dependent on the specified key.
*
* @since 2.0.0
*
* @param array $r Array of arguments to use.
*/
$r = apply_filters( 'bp_before_' . $filter_key . '_parse_args', $r );
}
// Parse.
if ( is_array( $defaults ) && !empty( $defaults ) ) {
$r = array_merge( $defaults, $r );
}
// Aggressively filter the args after the parse.
if ( !empty( $filter_key ) ) {
/**
* Filters the arguments key after parsing if filter key provided.
*
* This is a dynamic filter dependent on the specified key.
*
* @since 2.0.0
*
* @param array $r Array of parsed arguments.
*/
$r = apply_filters( 'bp_after_' . $filter_key . '_parse_args', $r );
}
// Return the parsed results.
return $r;
}
/**
* Sanitizes a pagination argument based on both the request override and the
* original value submitted via a query argument, likely to a template class
* responsible for limiting the resultset of a template loop.
*
* @since 2.2.0
*
* @param string $page_arg The $_REQUEST argument to look for.
* @param int $page The original page value to fall back to.
* @return int A sanitized integer value, good for pagination.
*/
function bp_sanitize_pagination_arg( $page_arg = '', $page = 1 ) {
// Check if request overrides exist.
if ( isset( $_REQUEST[ $page_arg ] ) ) {
// Get the absolute integer value of the override.
$int = absint( $_REQUEST[ $page_arg ] );
// If override is 0, do not use it. This prevents unlimited result sets.
// @see https://buddypress.trac.wordpress.org/ticket/5796.
if ( $int ) {
$page = $int;
}
}
return intval( $page );
}
/**
* Sanitize an 'order' parameter for use in building SQL queries.
*
* Strings like 'DESC', 'desc', ' desc' will be interpreted into 'DESC'.
* Everything else becomes 'ASC'.
*
* @since 1.8.0
*
* @param string $order The 'order' string, as passed to the SQL constructor.
* @return string The sanitized value 'DESC' or 'ASC'.
*/
function bp_esc_sql_order( $order = '' ) {
$order = strtoupper( trim( $order ) );
return 'DESC' === $order ? 'DESC' : 'ASC';
}
/**
* Escape special characters in a SQL LIKE clause.
*
* In WordPress 4.0, like_escape() was deprecated, due to incorrect
* documentation and improper sanitization leading to a history of misuse. To
* maintain compatibility with versions of WP before 4.0, we duplicate the
* logic of the replacement, wpdb::esc_like().
*
* @since 2.1.0
*
* @see wpdb::esc_like() for more details on proper use.
*
* @param string $text The raw text to be escaped.
* @return string Text in the form of a LIKE phrase. Not SQL safe. Run through
* wpdb::prepare() before use.
*/
function bp_esc_like( $text ) {
global $wpdb;
if ( method_exists( $wpdb, 'esc_like' ) ) {
return $wpdb->esc_like( $text );
} else {
return addcslashes( $text, '_%\\' );
}
}
/**
* Are we running username compatibility mode?
*
* @since 1.5.0
*
* @todo Move to members component?
*
* @return bool False when compatibility mode is disabled, true when enabled.
* Default: false.
*/
function bp_is_username_compatibility_mode() {
/**
* Filters whether or not to use username compatibility mode.
*
* @since 1.5.0
*
* @param bool $value Whether or not username compatibility mode should be used.
*/
return apply_filters( 'bp_is_username_compatibility_mode', defined( 'BP_ENABLE_USERNAME_COMPATIBILITY_MODE' ) && BP_ENABLE_USERNAME_COMPATIBILITY_MODE );
}
/**
* Should we use the WP Toolbar?
*
* The WP Toolbar, introduced in WP 3.1, is fully supported in BuddyPress as
* of BP 1.5. For BP 1.6, the WP Toolbar is the default.
*
* @since 1.5.0
*
* @return bool Default: true. False when WP Toolbar support is disabled.
*/
function bp_use_wp_admin_bar() {
// Default to true (to avoid loading deprecated BuddyBar code).
$use_admin_bar = true;
// Has the WP Toolbar constant been explicitly opted into?
if ( defined( 'BP_USE_WP_ADMIN_BAR' ) ) {
$use_admin_bar = (bool) BP_USE_WP_ADMIN_BAR;
// ...or is the old BuddyBar being forced back into use?
} elseif ( bp_force_buddybar( false ) ) {
$use_admin_bar = false;
}
/**
* Filters whether or not to use the admin bar.
*
* @since 1.5.0
*
* @param bool $use_admin_bar Whether or not to use the admin bar.
*/
return (bool) apply_filters( 'bp_use_wp_admin_bar', $use_admin_bar );
}
/**
* Return the parent forum ID for the Legacy Forums abstraction layer.
*
* @since 1.5.0
* @since 3.0.0 Supported for compatibility with bbPress 2.
*
* @return int Forum ID.
*/
function bp_forums_parent_forum_id() {
/**
* Filters the parent forum ID for the bbPress abstraction layer.
*
* @since 1.5.0
*
* @param int BP_FORUMS_PARENT_FORUM_ID The Parent forum ID constant.
*/
return apply_filters( 'bp_forums_parent_forum_id', BP_FORUMS_PARENT_FORUM_ID );
}
/** Directory *****************************************************************/
/**
* Returns an array of core component IDs.
*
* @since 2.1.0
*
* @return array
*/
function bp_core_get_packaged_component_ids() {
$components = array(
'activity',
'members',
'groups',
'blogs',
'xprofile',
'friends',
'messages',
'settings',
'notifications',
);
return $components;
}
/**
* Fetch a list of BP directory pages from the appropriate meta table.
*
* @since 1.5.0
*
* @param string $status 'active' to return only pages associated with active components, 'all' to return all saved
* pages. When running save routines, use 'all' to avoid removing data related to inactive
* components. Default: 'active'.
* @return array|string An array of page IDs, keyed by component names, or an
* empty string if the list is not found.
*/
function bp_core_get_directory_page_ids( $status = 'active' ) {
$page_ids = bp_get_option( 'bp-pages', array() );
// Loop through pages
foreach ( $page_ids as $component_name => $page_id ) {
// Ensure that empty indexes are unset. Should only matter in edge cases.
if ( empty( $component_name ) || empty( $page_id ) ) {
unset( $page_ids[ $component_name ] );
}
// Trashed pages should never appear in results.
if ( 'trash' == get_post_status( $page_id ) ) {
unset( $page_ids[ $component_name ] );
}
// 'register' and 'activate' do not have components, but should be whitelisted.
if ( in_array( $component_name, array( 'register', 'activate' ), true ) ) {
continue;
}
// Remove inactive component pages.
if ( ( 'active' === $status ) && ! bp_is_active( $component_name ) ) {
unset( $page_ids[ $component_name ] );
}
}
/**
* Filters the list of BP directory pages from the appropriate meta table.
*
* @since 1.5.0
* @since 2.9.0 Add $status parameter
*
* @param array $page_ids Array of directory pages.
* @param string $status Page status to limit results to
*/
return (array) apply_filters( 'bp_core_get_directory_page_ids', $page_ids, $status );
}
/**
* Get the page ID corresponding to a component directory.
*
* @since 2.6.0
*
* @param string|null $component The slug representing the component. Defaults to the current component.
* @return int|false The ID of the directory page associated with the component. False if none is found.
*/
function bp_core_get_directory_page_id( $component = null ) {
if ( ! $component ) {
$component = bp_current_component();
}
$bp_pages = bp_core_get_directory_page_ids( 'all' );
$page_id = false;
if ( $component && isset( $bp_pages[ $component ] ) ) {
$page_id = (int) $bp_pages[ $component ];
}
return $page_id;
}
/**
* Store the list of BP directory pages in the appropriate meta table.
*
* The bp-pages data is stored in site_options (falls back to options on non-MS),
* in an array keyed by blog_id. This allows you to change your
* bp_get_root_blog_id() and go through the setup process again.
*
* @since 1.5.0
*
* @param array $blog_page_ids The IDs of the WP pages corresponding to BP
* component directories.
*/
function bp_core_update_directory_page_ids( $blog_page_ids ) {
bp_update_option( 'bp-pages', $blog_page_ids );
}
/**
* Get names and slugs for BuddyPress component directory pages.
*
* @since 1.5.0
*
* @return object Page names, IDs, and slugs.
*/
function bp_core_get_directory_pages() {
global $wpdb;
// Look in cache first.
$pages = wp_cache_get( 'directory_pages', 'bp_pages' );
if ( false === $pages ) {
// Set pages as standard class.
$pages = new stdClass;
// Get pages and IDs.
$page_ids = bp_core_get_directory_page_ids();
if ( !empty( $page_ids ) ) {
// Always get page data from the root blog, except on multiblog mode, when it comes
// from the current blog.
$posts_table_name = bp_is_multiblog_mode() ? $wpdb->posts : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'posts';
$page_ids_sql = implode( ',', wp_parse_id_list( $page_ids ) );
$page_names = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_title FROM {$posts_table_name} WHERE ID IN ({$page_ids_sql}) AND post_status = 'publish' " );
foreach ( (array) $page_ids as $component_id => $page_id ) {
foreach ( (array) $page_names as $page_name ) {
if ( $page_name->ID == $page_id ) {
if ( !isset( $pages->{$component_id} ) || !is_object( $pages->{$component_id} ) ) {
$pages->{$component_id} = new stdClass;
}
$pages->{$component_id}->name = $page_name->post_name;
$pages->{$component_id}->id = $page_name->ID;
$pages->{$component_id}->title = $page_name->post_title;
$slug[] = $page_name->post_name;
// Get the slug.
while ( $page_name->post_parent != 0 ) {
$parent = $wpdb->get_results( $wpdb->prepare( "SELECT post_name, post_parent FROM {$posts_table_name} WHERE ID = %d", $page_name->post_parent ) );
$slug[] = $parent[0]->post_name;
$page_name->post_parent = $parent[0]->post_parent;
}
$pages->{$component_id}->slug = implode( '/', array_reverse( (array) $slug ) );
}
unset( $slug );
}
}
}
wp_cache_set( 'directory_pages', $pages, 'bp_pages' );
}
/**
* Filters the names and slugs for BuddyPress component directory pages.
*
* @since 1.5.0
*
* @param object $pages Object holding page names and slugs.
*/
return apply_filters( 'bp_core_get_directory_pages', $pages );
}
/**
* Creates necessary directory pages.
*
* Directory pages are those WordPress pages used by BP components to display
* content (eg, the 'groups' page created by BP).
*
* @since 1.7.0
*
* @param array $components Components to create pages for.
* @param string $existing 'delete' if you want to delete existing page mappings
* and replace with new ones. Otherwise existing page mappings
* are kept, and the gaps filled in with new pages. Default: 'keep'.
*/
function bp_core_add_page_mappings( $components, $existing = 'keep' ) {
// If no value is passed, there's nothing to do.
if ( empty( $components ) ) {
return;
}
// Make sure that the pages are created on the root blog no matter which
// dashboard the setup is being run on.
if ( ! bp_is_root_blog() ) {
switch_to_blog( bp_get_root_blog_id() );
}
$pages = bp_core_get_directory_page_ids( 'all' );
// Delete any existing pages.
if ( 'delete' === $existing ) {
foreach ( $pages as $page_id ) {
wp_delete_post( $page_id, true );
}
$pages = array();
}
$page_titles = bp_core_get_directory_page_default_titles();
$pages_to_create = array();
foreach ( array_keys( $components ) as $component_name ) {
if ( ! isset( $pages[ $component_name ] ) && isset( $page_titles[ $component_name ] ) ) {
$pages_to_create[ $component_name ] = $page_titles[ $component_name ];
}
}
// Register and Activate are not components, but need pages when
// registration is enabled.
if ( bp_get_signup_allowed() ) {
foreach ( array( 'register', 'activate' ) as $slug ) {
if ( ! isset( $pages[ $slug ] ) ) {
$pages_to_create[ $slug ] = $page_titles[ $slug ];
}
}
}
// No need for a Sites directory unless we're on multisite.
if ( ! is_multisite() && isset( $pages_to_create['blogs'] ) ) {
unset( $pages_to_create['blogs'] );
}
// Members must always have a page, no matter what.
if ( ! isset( $pages['members'] ) && ! isset( $pages_to_create['members'] ) ) {
$pages_to_create['members'] = $page_titles['members'];
}
// Create the pages.
foreach ( $pages_to_create as $component_name => $page_name ) {
$exists = get_page_by_path( $component_name );
// If page already exists, use it.
if ( ! empty( $exists ) ) {
$pages[ $component_name ] = $exists->ID;
} else {
$pages[ $component_name ] = wp_insert_post( array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_status' => 'publish',
'post_title' => $page_name,
'post_type' => 'page',
) );
}
}
// Save the page mapping.
bp_update_option( 'bp-pages', $pages );
// If we had to switch_to_blog, go back to the original site.
if ( ! bp_is_root_blog() ) {
restore_current_blog();
}
}
/**
* Get the default page titles for BP directory pages.
*
* @since 2.7.0
*
* @return array
*/
function bp_core_get_directory_page_default_titles() {
$page_default_titles = array(
'activity' => _x( 'Activity', 'Page title for the Activity directory.', 'buddypress' ),
'groups' => _x( 'Groups', 'Page title for the Groups directory.', 'buddypress' ),
'blogs' => _x( 'Sites', 'Page title for the Sites directory.', 'buddypress' ),
'members' => _x( 'Members', 'Page title for the Members directory.', 'buddypress' ),
'activate' => _x( 'Activate', 'Page title for the user activation screen.', 'buddypress' ),
'register' => _x( 'Register', 'Page title for the user registration screen.', 'buddypress' ),
);
/**
* Filters the default page titles array
*
* @since 2.7.0
*
* @param array $page_default_titles the array of default WP (post_title) titles.
*/
return apply_filters( 'bp_core_get_directory_page_default_titles', $page_default_titles );
}
/**
* Remove the entry from bp_pages when the corresponding WP page is deleted.
*
* Bails early on multisite installations when not viewing the root site.
*
* @link https://buddypress.trac.wordpress.org/ticket/6226
*
* @since 2.2.0
*
* @param int $post_id Post ID.
*/
function bp_core_on_directory_page_delete( $post_id ) {
// Stop if we are not on the main BP root blog.
if ( ! bp_is_root_blog() ) {
return;
}
$page_ids = bp_core_get_directory_page_ids( 'all' );
$component_name = array_search( $post_id, $page_ids );
if ( ! empty( $component_name ) ) {
unset( $page_ids[ $component_name ] );
}
bp_core_update_directory_page_ids( $page_ids );
}
add_action( 'delete_post', 'bp_core_on_directory_page_delete' );
/**
* Create a default component slug from a WP page root_slug.
*
* Since 1.5, BP components get their root_slug (the slug used immediately
* following the root domain) from the slug of a corresponding WP page.
*
* E.g. if your BP installation at example.com has its members page at
* example.com/community/people, $bp->members->root_slug will be
* 'community/people'.
*
* By default, this function creates a shorter version of the root_slug for
* use elsewhere in the URL, by returning the content after the final '/'
* in the root_slug ('people' in the example above).
*
* Filter on 'bp_core_component_slug_from_root_slug' to override this method
* in general, or define a specific component slug constant (e.g.
* BP_MEMBERS_SLUG) to override specific component slugs.
*
* @since 1.5.0
*
* @param string $root_slug The root slug, which comes from $bp->pages->[component]->slug.
* @return string The short slug for use in the middle of URLs.
*/
function bp_core_component_slug_from_root_slug( $root_slug ) {
$slug_chunks = explode( '/', $root_slug );
$slug = array_pop( $slug_chunks );
/**
* Filters the default component slug from a WP page root_slug.
*
* @since 1.5.0
*
* @param string $slug Short slug for use in the middle of URLs.
* @param string $root_slug The root slug which comes from $bp->pages-[component]->slug.
*/
return apply_filters( 'bp_core_component_slug_from_root_slug', $slug, $root_slug );
}
/**
* Add support for a top-level ("root") component.
*
* This function originally (pre-1.5) let plugins add support for pages in the
* root of the install. These root level pages are now handled by actual
* WordPress pages and this function is now a convenience for compatibility
* with the new method.
*
* @since 1.0.0
*
* @param string $slug The slug of the component being added to the root list.
*/
function bp_core_add_root_component( $slug ) {
$bp = buddypress();
if ( empty( $bp->pages ) ) {
$bp->pages = bp_core_get_directory_pages();
}
$match = false;
// Check if the slug is registered in the $bp->pages global.
foreach ( (array) $bp->pages as $key => $page ) {
if ( $key == $slug || $page->slug == $slug ) {
$match = true;
}
}
// Maybe create the add_root array.
if ( empty( $bp->add_root ) ) {
$bp->add_root = array();
}
// If there was no match, add a page for this root component.
if ( empty( $match ) ) {
$add_root_items = $bp->add_root;
$add_root_items[] = $slug;
$bp->add_root = $add_root_items;
}
// Make sure that this component is registered as requiring a top-level directory.
if ( isset( $bp->{$slug} ) ) {
$bp->loaded_components[$bp->{$slug}->slug] = $bp->{$slug}->id;
$bp->{$slug}->has_directory = true;
}
}
/**
* Create WordPress pages to be used as BP component directories.
*
* @since 1.5.0
*/
function bp_core_create_root_component_page() {
// Get BuddyPress.
$bp = buddypress();
$new_page_ids = array();
foreach ( (array) $bp->add_root as $slug ) {
$new_page_ids[ $slug ] = wp_insert_post( array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => ucwords( $slug ),
'post_status' => 'publish',
'post_type' => 'page'
) );
}
$page_ids = array_merge( $new_page_ids, bp_core_get_directory_page_ids( 'all' ) );
bp_core_update_directory_page_ids( $page_ids );
}
/**
* Add illegal blog names to WP so that root components will not conflict with blog names on a subdirectory installation.
*
* For example, it would stop someone creating a blog with the slug "groups".
*
* @since 1.0.0
*
* @todo Deprecate?
*/
function bp_core_add_illegal_names() {
update_site_option( 'illegal_names', get_site_option( 'illegal_names' ), array() );
}
/**
* Get the 'search' query argument for a given component.
*
* @since 2.4.0
* @since 2.7.0 The `$component` parameter was made optional, with the current component
* as the fallback value.
*
* @param string|null $component Optional. Component name. Defaults to current component.
* @return string|bool Query argument on success. False on failure.
*/
function bp_core_get_component_search_query_arg( $component = null ) {
if ( ! $component ) {
$component = bp_current_component();
}
$query_arg = false;
if ( isset( buddypress()->{$component}->search_query_arg ) ) {
$query_arg = sanitize_title( buddypress()->{$component}->search_query_arg );
}
/**
* Filters the query arg for a component search string.
*
* @since 2.4.0
*
* @param string $query_arg Query argument.
* @param string $component Component name.
*/
return apply_filters( 'bp_core_get_component_search_query_arg', $query_arg, $component );
}
/**
* Determine whether BuddyPress should register the bp-themes directory.
*
* @since 1.9.0
*
* @return bool True if bp-themes should be registered, false otherwise.
*/
function bp_do_register_theme_directory() {
// If bp-default exists in another theme directory, bail.
// This ensures that the version of bp-default in the regular themes
// directory will always take precedence, as part of a migration away
// from the version packaged with BuddyPress.
foreach ( array_values( (array) $GLOBALS['wp_theme_directories'] ) as $directory ) {
if ( is_dir( $directory . '/bp-default' ) ) {
return false;
}
}
// If the current theme is bp-default (or a bp-default child), BP
// should register its directory.
$register = 'bp-default' === get_stylesheet() || 'bp-default' === get_template();
// Legacy sites continue to have the theme registered.
if ( empty( $register ) && ( 1 == get_site_option( '_bp_retain_bp_default' ) ) ) {
$register = true;
}
/**
* Filters whether BuddyPress should register the bp-themes directory.
*
* @since 1.9.0
*
* @param bool $register If bp-themes should be registered.
*/
return apply_filters( 'bp_do_register_theme_directory', $register );
}
/** URI ***********************************************************************/
/**
* Return the domain for the root blog.
*
* Eg: http://example.com OR https://example.com
*
* @since 1.0.0
*