-
Notifications
You must be signed in to change notification settings - Fork 3
/
buddypress-functions.php
1769 lines (1460 loc) · 57.3 KB
/
buddypress-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
/**
* Functions of BuddyPress's Legacy theme
*
* @package BuddyPress
* @subpackage BP_Theme_Compat
* @since BuddyPress (1.7)
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/** Theme Setup ***************************************************************/
if ( !class_exists( 'BP_Legacy' ) ) :
/**
* Loads BuddyPress Legacy Theme functionality
*
* This is not a real theme by WordPress standards, and is instead used as the
* fallback for any WordPress theme that does not have BuddyPress templates in it.
*
* To make your custom theme BuddyPress compatible and customize the templates, you
* can copy these files into your theme without needing to merge anything
* together; BuddyPress should safely handle the rest.
*
* See @link BP_Theme_Compat() for more.
*
* @since BuddyPress (1.7)
*
* @package BuddyPress
* @subpackage BP_Theme_Compat
*/
class BP_Legacy extends BP_Theme_Compat {
/** Functions *************************************************************/
/**
* The main BuddyPress (Legacy) Loader
*
* @since BuddyPress (1.7)
*
* @uses BP_Legacy::setup_globals()
* @uses BP_Legacy::setup_actions()
*/
public function __construct() {
parent::start();
}
/**
* Component global variables
*
* You'll want to customize the values in here, so they match whatever your
* needs are.
*
* @since BuddyPress (1.7)
* @access private
*/
protected function setup_globals() {
$bp = buddypress();
$this->id = 'legacy';
$this->name = __( 'BuddyPress Legacy', 'buddypress' );
$this->version = bp_get_version();
$this->dir = trailingslashit( $bp->themes_dir . '/bp-legacy' );
$this->url = trailingslashit( $bp->themes_url . '/bp-legacy' );
}
/**
* Setup the theme hooks
*
* @since BuddyPress (1.7)
* @access private
*
* @uses add_filter() To add various filters
* @uses add_action() To add various actions
*/
protected function setup_actions() {
// Template Output
add_filter( 'bp_get_activity_action_pre_meta', array( $this, 'secondary_avatars' ), 10, 2 );
// Filter BuddyPress template hierarchy and look for page templates
add_filter( 'bp_get_buddypress_template', array( $this, 'theme_compat_page_templates' ), 10, 1 );
/** Scripts ***********************************************************/
add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS
add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS
add_filter( 'bp_enqueue_scripts', array( $this, 'localize_scripts' ) ); // Enqueue theme script localization
add_action( 'bp_head', array( $this, 'head_scripts' ) ); // Output some extra JS in the <head>
/** Body no-js Class **************************************************/
add_filter( 'body_class', array( $this, 'add_nojs_body_class' ), 20, 1 );
/** Buttons ***********************************************************/
if ( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
// Register buttons for the relevant component templates
// Friends button
if ( bp_is_active( 'friends' ) )
add_action( 'bp_member_header_actions', 'bp_add_friend_button', 5 );
// Activity button
if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() )
add_action( 'bp_member_header_actions', 'bp_send_public_message_button', 20 );
// Messages button
if ( bp_is_active( 'messages' ) )
add_action( 'bp_member_header_actions', 'bp_send_private_message_button', 20 );
// Group buttons
if ( bp_is_active( 'groups' ) ) {
add_action( 'bp_group_header_actions', 'bp_group_join_button', 5 );
add_action( 'bp_group_header_actions', 'bp_group_new_topic_button', 20 );
add_action( 'bp_directory_groups_actions', 'bp_group_join_button' );
add_action( 'bp_groups_directory_group_filter', 'bp_legacy_theme_group_create_nav', 999 );
}
// Blog button
if ( bp_is_active( 'blogs' ) ) {
add_action( 'bp_directory_blogs_actions', 'bp_blogs_visit_blog_button' );
add_action( 'bp_blogs_directory_blog_types', 'bp_legacy_theme_blog_create_nav', 999 );
}
}
/** Notices ***********************************************************/
// Only hook the 'sitewide_notices' overlay if the Sitewide
// Notices widget is not in use (to avoid duplicate content).
if ( bp_is_active( 'messages' ) && ! is_active_widget( false, false, 'bp_messages_sitewide_notices_widget', true ) ) {
add_action( 'wp_footer', array( $this, 'sitewide_notices' ), 9999 );
}
/** Ajax **************************************************************/
$actions = array(
// Directory filters
'blogs_filter' => 'bp_legacy_theme_object_template_loader',
'forums_filter' => 'bp_legacy_theme_object_template_loader',
'groups_filter' => 'bp_legacy_theme_object_template_loader',
'members_filter' => 'bp_legacy_theme_object_template_loader',
'messages_filter' => 'bp_legacy_theme_messages_template_loader',
'invite_filter' => 'bp_legacy_theme_invite_template_loader',
'requests_filter' => 'bp_legacy_theme_requests_template_loader',
// Friends
'accept_friendship' => 'bp_legacy_theme_ajax_accept_friendship',
'addremove_friend' => 'bp_legacy_theme_ajax_addremove_friend',
'reject_friendship' => 'bp_legacy_theme_ajax_reject_friendship',
// Activity
'activity_get_older_updates' => 'bp_legacy_theme_activity_template_loader',
'activity_mark_fav' => 'bp_legacy_theme_mark_activity_favorite',
'activity_mark_unfav' => 'bp_legacy_theme_unmark_activity_favorite',
'activity_widget_filter' => 'bp_legacy_theme_activity_template_loader',
'delete_activity' => 'bp_legacy_theme_delete_activity',
'delete_activity_comment' => 'bp_legacy_theme_delete_activity_comment',
'get_single_activity_content' => 'bp_legacy_theme_get_single_activity_content',
'new_activity_comment' => 'bp_legacy_theme_new_activity_comment',
'post_update' => 'bp_legacy_theme_post_update',
'bp_spam_activity' => 'bp_legacy_theme_spam_activity',
'bp_spam_activity_comment' => 'bp_legacy_theme_spam_activity',
// Groups
'groups_invite_user' => 'bp_legacy_theme_ajax_invite_user',
'joinleave_group' => 'bp_legacy_theme_ajax_joinleave_group',
// Messages
'messages_autocomplete_results' => 'bp_legacy_theme_ajax_messages_autocomplete_results',
'messages_close_notice' => 'bp_legacy_theme_ajax_close_notice',
'messages_delete' => 'bp_legacy_theme_ajax_messages_delete',
'messages_markread' => 'bp_legacy_theme_ajax_message_markread',
'messages_markunread' => 'bp_legacy_theme_ajax_message_markunread',
'messages_send_reply' => 'bp_legacy_theme_ajax_messages_send_reply',
);
// Conditional actions
if ( bp_is_active( 'messages', 'star' ) ) {
$actions['messages_star'] = 'bp_legacy_theme_ajax_messages_star_handler';
}
/**
* Register all of these AJAX handlers
*
* The "wp_ajax_" action is used for logged in users, and "wp_ajax_nopriv_"
* executes for users that aren't logged in. This is for backpat with BP <1.6.
*/
foreach( $actions as $name => $function ) {
add_action( 'wp_ajax_' . $name, $function );
add_action( 'wp_ajax_nopriv_' . $name, $function );
}
add_filter( 'bp_ajax_querystring', 'bp_legacy_theme_ajax_querystring', 10, 2 );
/** Override **********************************************************/
/**
* Fires after all of the BuddyPress theme compat actions have been added.
*
* @since BuddyPress (1.7.0)
*
* @param BP_Legacy $this Current BP_Legacy instance.
*/
do_action_ref_array( 'bp_theme_compat_actions', array( &$this ) );
}
/**
* Load the theme CSS
*
* @since BuddyPress (1.7)
* @since BuddyPress (2.3.0) Support custom CSS file named after the current theme or parent theme.
*
* @uses wp_enqueue_style() To enqueue the styles
*/
public function enqueue_styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
// Locate the BP stylesheet
$ltr = $this->locate_asset_in_stack( "buddypress{$min}.css", 'css' );
// LTR
if ( ! is_rtl() && isset( $ltr['location'], $ltr['handle'] ) ) {
wp_enqueue_style( $ltr['handle'], $ltr['location'], array(), $this->version, 'screen' );
if ( $min ) {
wp_style_add_data( $ltr['handle'], 'suffix', $min );
}
}
// RTL
if ( is_rtl() ) {
$rtl = $this->locate_asset_in_stack( "buddypress-rtl{$min}.css", 'css' );
if ( isset( $rtl['location'], $rtl['handle'] ) ) {
$rtl['handle'] = str_replace( '-css', '-css-rtl', $rtl['handle'] ); // Backwards compatibility
wp_enqueue_style( $rtl['handle'], $rtl['location'], array(), $this->version, 'screen' );
if ( $min ) {
wp_style_add_data( $rtl['handle'], 'suffix', $min );
}
}
}
// Compatibility stylesheets for specific themes.
$theme = $this->locate_asset_in_stack( get_template() . "{$min}.css", 'css' );
if ( ! is_rtl() && isset( $theme['location'] ) ) {
// use a unique handle
$theme['handle'] = 'bp-' . get_template();
wp_enqueue_style( $theme['handle'], $theme['location'], array(), $this->version, 'screen' );
if ( $min ) {
wp_style_add_data( $theme['handle'], 'suffix', $min );
}
}
// Compatibility stylesheet for specific themes, RTL-version
if ( is_rtl() ) {
$theme_rtl = $this->locate_asset_in_stack( get_template() . "-rtl{$min}.css", 'css' );
if ( isset( $theme_rtl['location'] ) ) {
$theme_rtl['handle'] = $theme['handle'] . '-rtl';
wp_enqueue_style( $theme_rtl['handle'], $theme_rtl['location'], array(), $this->version, 'screen' );
if ( $min ) {
wp_style_add_data( $theme_rtl['handle'], 'suffix', $min );
}
}
}
}
/**
* Enqueue the required JavaScript files
*
* @since BuddyPress (1.7)
*/
public function enqueue_scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
// Locate the BP JS file
$asset = $this->locate_asset_in_stack( "buddypress{$min}.js", 'js' );
// Enqueue the global JS, if found - AJAX will not work
// without it
if ( isset( $asset['location'], $asset['handle'] ) ) {
wp_enqueue_script( $asset['handle'], $asset['location'], bp_core_get_js_dependencies(), $this->version );
}
/**
* Filters core JavaScript strings for internationalization before AJAX usage.
*
* @since BuddyPress (2.0.0)
*
* @param array $value Array of key/value pairs for AJAX usage.
*/
$params = apply_filters( 'bp_core_get_js_strings', array(
'accepted' => __( 'Accepted', 'buddypress' ),
'close' => __( 'Close', 'buddypress' ),
'comments' => __( 'comments', 'buddypress' ),
'leave_group_confirm' => __( 'Are you sure you want to leave this group?', 'buddypress' ),
'mark_as_fav' => __( 'Favorite', 'buddypress' ),
'my_favs' => __( 'My Favorites', 'buddypress' ),
'rejected' => __( 'Rejected', 'buddypress' ),
'remove_fav' => __( 'Remove Favorite', 'buddypress' ),
'show_all' => __( 'Show all', 'buddypress' ),
'show_all_comments' => __( 'Show all comments for this thread', 'buddypress' ),
'show_x_comments' => __( 'Show all %d comments', 'buddypress' ),
'unsaved_changes' => __( 'Your profile has unsaved changes. If you leave the page, the changes will be lost.', 'buddypress' ),
'view' => __( 'View', 'buddypress' ),
) );
wp_localize_script( $asset['handle'], 'BP_DTheme', $params );
// Maybe enqueue comment reply JS
if ( is_singular() && bp_is_blog_page() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
// Maybe enqueue password verify JS (register page or user settings page)
if ( bp_is_register_page() || ( function_exists( 'bp_is_user_settings_general' ) && bp_is_user_settings_general() ) ) {
// Locate the Register Page JS file
$asset = $this->locate_asset_in_stack( "password-verify{$min}.js", 'js', 'bp-legacy-password-verify' );
$dependencies = array_merge( bp_core_get_js_dependencies(), array(
'password-strength-meter',
) );
// Enqueue script
wp_enqueue_script( $asset['handle'] . '-password-verify', $asset['location'], $dependencies, $this->version);
}
// Star private messages
if ( bp_is_active( 'messages', 'star' ) && bp_is_user_messages() ) {
wp_localize_script( $asset['handle'], 'BP_PM_Star', array(
'strings' => array(
'text_unstar' => __( 'Unstar', 'buddypress' ),
'text_star' => __( 'Star', 'buddypress' ),
'title_unstar' => __( 'Starred', 'buddypress' ),
'title_star' => __( 'Not starred', 'buddypress' ),
'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
'title_star_thread' => __( 'Star the first message in this thread', 'buddypress' ),
),
'is_single_thread' => (int) bp_is_messages_conversation(),
'star_counter' => 0,
'unstar_counter' => 0
) );
}
}
/**
* Get the URL and handle of a web-accessible CSS or JS asset
*
* We provide two levels of customizability with respect to where CSS
* and JS files can be stored: (1) the child theme/parent theme/theme
* compat hierarchy, and (2) the "template stack" of /buddypress/css/,
* /community/css/, and /css/. In this way, CSS and JS assets can be
* overloaded, and default versions provided, in exactly the same way
* as corresponding PHP templates.
*
* We are duplicating some of the logic that is currently found in
* bp_locate_template() and the _template_stack() functions. Those
* functions were built with PHP templates in mind, and will require
* refactoring in order to provide "stack" functionality for assets
* that must be accessible both using file_exists() (the file path)
* and at a public URI.
*
* This method is marked private, with the understanding that the
* implementation is subject to change or removal in an upcoming
* release, in favor of a unified _template_stack() system. Plugin
* and theme authors should not attempt to use what follows.
*
* @since BuddyPress (1.8)
* @access private
* @param string $file A filename like buddypress.css
* @param string $type Optional. Either "js" or "css" (the default).
* @param string $script_handle Optional. If set, used as the script name in `wp_enqueue_script`.
* @return array An array of data for the wp_enqueue_* function:
* 'handle' (eg 'bp-child-css') and a 'location' (the URI of the
* asset)
*/
private function locate_asset_in_stack( $file, $type = 'css', $script_handle = '' ) {
$locations = array();
// Ensure the assets can be located when running from /src/.
if ( defined( 'BP_SOURCE_SUBDIRECTORY' ) && BP_SOURCE_SUBDIRECTORY === 'src' ) {
$file = str_replace( '.min', '', $file );
}
// No need to check child if template == stylesheet
if ( is_child_theme() ) {
$locations['bp-child'] = array(
'dir' => get_stylesheet_directory(),
'uri' => get_stylesheet_directory_uri(),
'file' => str_replace( '.min', '', $file ),
);
}
$locations['bp-parent'] = array(
'dir' => get_template_directory(),
'uri' => get_template_directory_uri(),
'file' => str_replace( '.min', '', $file ),
);
$locations['bp-legacy'] = array(
'dir' => bp_get_theme_compat_dir(),
'uri' => bp_get_theme_compat_url(),
'file' => $file,
);
// Subdirectories within the top-level $locations directories
$subdirs = array(
'buddypress/' . $type,
'community/' . $type,
$type,
);
$retval = array();
foreach ( $locations as $location_type => $location ) {
foreach ( $subdirs as $subdir ) {
if ( file_exists( trailingslashit( $location['dir'] ) . trailingslashit( $subdir ) . $location['file'] ) ) {
$retval['location'] = trailingslashit( $location['uri'] ) . trailingslashit( $subdir ) . $location['file'];
$retval['handle'] = ( $script_handle ) ? $script_handle : "{$location_type}-{$type}";
break 2;
}
}
}
return $retval;
}
/**
* Put some scripts in the header, like AJAX url for wp-lists
*
* @since BuddyPress (1.7)
*/
public function head_scripts() {
?>
<script type="text/javascript">
/* <![CDATA[ */
var ajaxurl = '<?php echo bp_core_ajax_url(); ?>';
/* ]]> */
</script>
<?php
}
/**
* Adds the no-js class to the body tag.
*
* This function ensures that the <body> element will have the 'no-js' class by default. If you're
* using JavaScript for some visual functionality in your theme, and you want to provide noscript
* support, apply those styles to body.no-js.
*
* The no-js class is removed by the JavaScript created in buddypress.js.
*
* @since BuddyPress (1.7)
*/
public function add_nojs_body_class( $classes ) {
if ( ! in_array( 'no-js', $classes ) )
$classes[] = 'no-js';
return array_unique( $classes );
}
/**
* Load localizations for topic script
*
* These localizations require information that may not be loaded even by init.
*
* @since BuddyPress (1.7)
*/
public function localize_scripts() {
}
/**
* Outputs sitewide notices markup in the footer.
*
* @since BuddyPress (1.7)
*
* @see https://buddypress.trac.wordpress.org/ticket/4802
*/
public function sitewide_notices() {
// Do not show notices if user is not logged in
if ( ! is_user_logged_in() )
return;
// add a class to determine if the admin bar is on or not
$class = did_action( 'admin_bar_menu' ) ? 'admin-bar-on' : 'admin-bar-off';
echo '<div id="sitewide-notice" class="' . $class . '">';
bp_message_get_notices();
echo '</div>';
}
/**
* Add secondary avatar image to this activity stream's record, if supported.
*
* @since BuddyPress (1.7)
*
* @param string $action The text of this activity
* @param BP_Activity_Activity $activity Activity object
* @package BuddyPress Theme
* @return string
*/
function secondary_avatars( $action, $activity ) {
switch ( $activity->component ) {
case 'groups' :
case 'friends' :
// Only insert avatar if one exists
if ( $secondary_avatar = bp_get_activity_secondary_avatar() ) {
$reverse_content = strrev( $action );
$position = strpos( $reverse_content, 'a<' );
$action = substr_replace( $action, $secondary_avatar, -$position - 2, 0 );
}
break;
}
return $action;
}
/**
* Filter the default theme compatibility root template hierarchy, and prepend
* a page template to the front if it's set.
*
* @see https://buddypress.trac.wordpress.org/ticket/6065
*
* @since BuddyPress (2.2.0)
*
* @param array $templates
* @uses apply_filters() call 'bp_legacy_theme_compat_page_templates_directory_only' and return false
* to use the defined page template for component's directory and its single items
* @return array
*/
public function theme_compat_page_templates( $templates = array() ) {
/**
* Filters whether or not we are looking at a directory to determine if to return early.
*
* @since BuddyPress (2.2.0)
*
* @param bool $value Whether or not we are viewing a directory.
*/
if ( true === (bool) apply_filters( 'bp_legacy_theme_compat_page_templates_directory_only', ! bp_is_directory() ) ) {
return $templates;
}
// No page ID yet
$page_id = 0;
// Get the WordPress Page ID for the current view.
foreach ( (array) buddypress()->pages as $component => $bp_page ) {
// Handles the majority of components.
if ( bp_is_current_component( $component ) ) {
$page_id = (int) $bp_page->id;
}
// Stop if not on a user page.
if ( ! bp_is_user() && ! empty( $page_id ) ) {
break;
}
// The Members component requires an explicit check due to overlapping components.
if ( bp_is_user() && ( 'members' === $component ) ) {
$page_id = (int) $bp_page->id;
break;
}
}
// Bail if no directory page set
if ( 0 === $page_id ) {
return $templates;
}
// Check for page template
$page_template = get_page_template_slug( $page_id );
// Add it to the beginning of the templates array so it takes precedence
// over the default hierarchy.
if ( ! empty( $page_template ) ) {
/**
* Check for existence of template before adding it to template
* stack to avoid accidentally including an unintended file.
*
* @see: https://buddypress.trac.wordpress.org/ticket/6190
*/
if ( '' !== locate_template( $page_template ) ) {
array_unshift( $templates, $page_template );
}
}
return $templates;
}
}
new BP_Legacy();
endif;
/**
* Add the Create a Group button to the Groups directory title.
*
* bp-legacy puts the Create a Group button into the page title, to mimic
* the behavior of bp-default.
*
* @since BuddyPress (2.0.0)
* @todo Deprecate
*
* @param string $title Groups directory title.
* @return string
*/
function bp_legacy_theme_group_create_button( $title ) {
return $title . ' ' . bp_get_group_create_button();
}
/**
* Add the Create a Group nav to the Groups directory navigation.
*
* bp-legacy puts the Create a Group nav at the last position of
* the Groups directory navigation.
*
* @since BuddyPress (2.2.0)
*
* @uses bp_group_create_nav_item() to output the create a Group nav item
* @return string
*/
function bp_legacy_theme_group_create_nav() {
bp_group_create_nav_item();
}
/**
* Add the Create a Site button to the Sites directory title.
*
* bp-legacy puts the Create a Site button into the page title, to mimic
* the behavior of bp-default.
*
* @since BuddyPress (2.0.0)
* @todo Deprecate
*
* @param string $title Sites directory title.
* @return string
*/
function bp_legacy_theme_blog_create_button( $title ) {
return $title . ' ' . bp_get_blog_create_button();
}
/**
* Add the Create a Site nav to the Sites directory navigation.
*
* bp-legacy puts the Create a Site nav at the last position of
* the Sites directory navigation.
*
* @since BuddyPress (2.2.0)
*
* @uses bp_blog_create_nav_item() to output the Create a Site nav item
* @return string
*/
function bp_legacy_theme_blog_create_nav() {
bp_blog_create_nav_item();
}
/**
* This function looks scarier than it actually is. :)
* Each object loop (activity/members/groups/blogs/forums) contains default
* parameters to show specific information based on the page we are currently
* looking at.
*
* The following function will take into account any cookies set in the JS and
* allow us to override the parameters sent. That way we can change the results
* returned without reloading the page.
*
* By using cookies we can also make sure that user settings are retained
* across page loads.
*
* @return string Query string for the component loops
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_ajax_querystring( $query_string, $object ) {
if ( empty( $object ) )
return '';
// Set up the cookies passed on this AJAX request. Store a local var to avoid conflicts
if ( ! empty( $_POST['cookie'] ) ) {
$_BP_COOKIE = wp_parse_args( str_replace( '; ', '&', urldecode( $_POST['cookie'] ) ) );
} else {
$_BP_COOKIE = &$_COOKIE;
}
$qs = array();
/**
* Check if any cookie values are set. If there are then override the
* default params passed to the template loop.
*/
// Activity stream filtering on action
if ( ! empty( $_BP_COOKIE['bp-' . $object . '-filter'] ) && '-1' != $_BP_COOKIE['bp-' . $object . '-filter'] ) {
$qs[] = 'type=' . $_BP_COOKIE['bp-' . $object . '-filter'];
$qs[] = 'action=' . $_BP_COOKIE['bp-' . $object . '-filter'];
}
if ( ! empty( $_BP_COOKIE['bp-' . $object . '-scope'] ) ) {
if ( 'personal' == $_BP_COOKIE['bp-' . $object . '-scope'] ) {
$user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
$qs[] = 'user_id=' . $user_id;
}
// Activity stream scope only on activity directory.
if ( 'all' != $_BP_COOKIE['bp-' . $object . '-scope'] && ! bp_displayed_user_id() && ! bp_is_single_item() )
$qs[] = 'scope=' . $_BP_COOKIE['bp-' . $object . '-scope'];
}
// If page and search_terms have been passed via the AJAX post request, use those.
if ( ! empty( $_POST['page'] ) && '-1' != $_POST['page'] )
$qs[] = 'page=' . absint( $_POST['page'] );
// excludes activity just posted and avoids duplicate ids
if ( ! empty( $_POST['exclude_just_posted'] ) ) {
$just_posted = wp_parse_id_list( $_POST['exclude_just_posted'] );
$qs[] = 'exclude=' . implode( ',', $just_posted );
}
// to get newest activities
if ( ! empty( $_POST['offset'] ) ) {
$qs[] = 'offset=' . intval( $_POST['offset'] );
}
$object_search_text = bp_get_search_default_text( $object );
if ( ! empty( $_POST['search_terms'] ) && $object_search_text != $_POST['search_terms'] && 'false' != $_POST['search_terms'] && 'undefined' != $_POST['search_terms'] )
$qs[] = 'search_terms=' . urlencode( $_POST['search_terms'] );
// Now pass the querystring to override default values.
$query_string = empty( $qs ) ? '' : join( '&', (array) $qs );
$object_filter = '';
if ( isset( $_BP_COOKIE['bp-' . $object . '-filter'] ) )
$object_filter = $_BP_COOKIE['bp-' . $object . '-filter'];
$object_scope = '';
if ( isset( $_BP_COOKIE['bp-' . $object . '-scope'] ) )
$object_scope = $_BP_COOKIE['bp-' . $object . '-scope'];
$object_page = '';
if ( isset( $_BP_COOKIE['bp-' . $object . '-page'] ) )
$object_page = $_BP_COOKIE['bp-' . $object . '-page'];
$object_search_terms = '';
if ( isset( $_BP_COOKIE['bp-' . $object . '-search-terms'] ) )
$object_search_terms = $_BP_COOKIE['bp-' . $object . '-search-terms'];
$object_extras = '';
if ( isset( $_BP_COOKIE['bp-' . $object . '-extras'] ) )
$object_extras = $_BP_COOKIE['bp-' . $object . '-extras'];
/**
* Filters the AJAX query string for the component loops.
*
* @since BuddyPress (1.7.0)
*
* @param string $query_string The query string we are working with.
* @param string $object The type of page we are on.
* @param string $object_filter The current object filter.
* @param string $object_scope The current object scope.
* @param string $object_page The current object page.
* @param string $object_search_terms The current object search terms.
* @param string $object_extras The current object extras.
*/
return apply_filters( 'bp_legacy_theme_ajax_querystring', $query_string, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras );
}
/**
* Load the template loop for the current object.
*
* @return string Prints template loop for the specified object
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_object_template_loader() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;
// Bail if no object passed
if ( empty( $_POST['object'] ) )
return;
// Sanitize the object
$object = sanitize_title( $_POST['object'] );
// Bail if object is not an active component to prevent arbitrary file inclusion
if ( ! bp_is_active( $object ) )
return;
/**
* AJAX requests happen too early to be seen by bp_update_is_directory()
* so we do it manually here to ensure templates load with the correct
* context. Without this check, templates will load the 'single' version
* of themselves rather than the directory version.
*/
if ( ! bp_current_action() )
bp_update_is_directory( true, bp_current_component() );
$template_part = $object . '/' . $object . '-loop';
// The template part can be overridden by the calling JS function
if ( ! empty( $_POST['template'] ) ) {
$template_part = sanitize_option( 'upload_path', $_POST['template'] );
}
// Locate the object template
bp_get_template_part( $template_part );
exit();
}
/**
* Load messages template loop when searched on the private message page
*
* @return string Prints template loop for the Messages component
* @since BuddyPress (1.6)
*/
function bp_legacy_theme_messages_template_loader() {
bp_get_template_part( 'members/single/messages/messages-loop' );
exit();
}
/**
* Load group invitations loop to handle pagination requests sent via AJAX.
*
* @since BuddyPress (2.0.0)
*/
function bp_legacy_theme_invite_template_loader() {
bp_get_template_part( 'groups/single/invites-loop' );
exit();
}
/**
* Load group membership requests loop to handle pagination requests sent via AJAX.
*
* @since BuddyPress (2.0.0)
*/
function bp_legacy_theme_requests_template_loader() {
bp_get_template_part( 'groups/single/requests-loop' );
exit();
}
/**
* Load the activity loop template when activity is requested via AJAX,
*
* @return string JSON object containing 'contents' (output of the template loop
* for the Activity component) and 'feed_url' (URL to the relevant RSS feed).
*
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_activity_template_loader() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;
$scope = '';
if ( ! empty( $_POST['scope'] ) )
$scope = $_POST['scope'];
// We need to calculate and return the feed URL for each scope
switch ( $scope ) {
case 'friends':
$feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/friends/feed/';
break;
case 'groups':
$feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/groups/feed/';
break;
case 'favorites':
$feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/favorites/feed/';
break;
case 'mentions':
$feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/feed/';
bp_activity_clear_new_mentions( bp_loggedin_user_id() );
break;
default:
$feed_url = home_url( bp_get_activity_root_slug() . '/feed/' );
break;
}
// Buffer the loop in the template to a var for JS to spit out.
ob_start();
bp_get_template_part( 'activity/activity-loop' );
$result['contents'] = ob_get_contents();
/**
* Filters the feed URL for when activity is requested via AJAX.
*
* @since BuddyPress (1.7.0)
*
* @param string $feed_url URL for the feed to be used.
* @param string $scope Scope for the activity request.
*/
$result['feed_url'] = apply_filters( 'bp_legacy_theme_activity_feed_url', $feed_url, $scope );
ob_end_clean();
exit( json_encode( $result ) );
}
/**
* Processes Activity updates received via a POST request.
*
* @return string HTML
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_post_update() {
$bp = buddypress();
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;
// Check the nonce
check_admin_referer( 'post_update', '_wpnonce_post_update' );
if ( ! is_user_logged_in() )
exit( '-1' );
if ( empty( $_POST['content'] ) )
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'Please enter some content to post.', 'buddypress' ) . '</p></div>' );
$activity_id = 0;
if ( empty( $_POST['object'] ) && bp_is_active( 'activity' ) ) {
$activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
} elseif ( $_POST['object'] == 'groups' ) {
if ( ! empty( $_POST['item_id'] ) && bp_is_active( 'groups' ) )
$activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $_POST['item_id'] ) );
} else {
/** This filter is documented in bp-activity/bp-activity-actions.php */
$activity_id = apply_filters( 'bp_activity_custom_update', false, $_POST['object'], $_POST['item_id'], $_POST['content'] );
}
if ( empty( $activity_id ) )
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'There was a problem posting your update. Please try again.', 'buddypress' ) . '</p></div>' );
$last_recorded = ! empty( $_POST['since'] ) ? date( 'Y-m-d H:i:s', intval( $_POST['since'] ) ) : 0;
if ( $last_recorded ) {
$activity_args = array( 'since' => $last_recorded );
$bp->activity->last_recorded = $last_recorded;
add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
} else {
$activity_args = array( 'include' => $activity_id );
}
if ( bp_has_activities ( $activity_args ) ) {
while ( bp_activities() ) {
bp_the_activity();
bp_get_template_part( 'activity/entry' );
}
}
if ( ! empty( $last_recorded ) ) {
remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
}
exit;
}
/**
* Posts new Activity comments received via a POST request.
*
* @global BP_Activity_Template $activities_template
* @return string HTML
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_new_activity_comment() {
global $activities_template;
$bp = buddypress();
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
return;
}
// Check the nonce
check_admin_referer( 'new_activity_comment', '_wpnonce_new_activity_comment' );
if ( ! is_user_logged_in() ) {
exit( '-1' );
}
$feedback = __( 'There was an error posting your reply. Please try again.', 'buddypress' );
if ( empty( $_POST['content'] ) ) {
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . esc_html__( 'Please do not leave the comment area blank.', 'buddypress' ) . '</p></div>' );
}
if ( empty( $_POST['form_id'] ) || empty( $_POST['comment_id'] ) || ! is_numeric( $_POST['form_id'] ) || ! is_numeric( $_POST['comment_id'] ) ) {
exit( '-1<div id="message" class="error bp-ajax-message"><p>' . esc_html( $feedback ) . '</p></div>' );
}