This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
class_commentpress_mu_bp.php
2144 lines (1246 loc) · 44.4 KB
/
class_commentpress_mu_bp.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 /*
===============================================================
Class CommentPressBuddyPress Version 1.0
===============================================================
AUTHOR : Christian Wach <[email protected]>
LAST MODIFIED : 19/03/2012
---------------------------------------------------------------
NOTES
=====
This class encapsulates all BuddyPress compatibility
---------------------------------------------------------------
*/
/*
===============================================================
Class Name
===============================================================
*/
class CommentPressBuddyPress {
/*
===============================================================
Properties
===============================================================
*/
// parent object reference
var $parent_obj;
/**
* @description: initialises this object
* @param object $parent_obj a reference to the parent object
* @return object
* @todo:
*
*/
function __construct( $parent_obj = null ) {
// store reference to "parent" (calling obj, not OOP parent)
$this->parent_obj = $parent_obj;
// init
$this->_init();
// --<
return $this;
}
/**
* PHP 4 constructor
*/
function CommentPressBuddyPress( $parent_obj = null ) {
// is this php5?
if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
// call php5 constructor
$this->__construct( $parent_obj );
}
// --<
return $this;
}
/**
* @description: set up all items associated with this object
* @todo:
*
*/
function initialise() {
}
/**
* @description: if needed, destroys all items associated with this object
* @todo:
*
*/
function destroy() {
}
//#################################################################
/*
===============================================================
PUBLIC METHODS
===============================================================
*/
/*
--------------------------------------------------------------------------------
BuddyPress Compatibility
--------------------------------------------------------------------------------
*/
/**
* @description: add an admin page for this plugin
* @todo:
*
*/
function add_admin_menu() {
// we must be network admin
if ( !is_super_admin() ) { return false; }
// for BP1.6+ (1.6 introduced 'bp_get_version')
if ( function_exists( 'bp_get_version' ) ) {
// always add the admin page to the Settings menu
$page = add_submenu_page(
'settings.php',
__( 'Commentpress for BuddyPress', 'cp-multisite' ),
__( 'Commentpress for BuddyPress', 'cp-multisite' ),
'manage_options',
'cpmu_admin_page_bp',
array( &$this, 'admin_page' )
);
} else {
// Add the admin page to the BuddyPress menu
$page = add_submenu_page(
'bp-general-settings',
__( 'CommentPress Setup', 'cp-multisite' ),
__( 'CommentPress Setup', 'cp-multisite' ),
'manage_options',
'cpmu_admin_page_bp',
array( &$this, 'admin_page' )
);
}
// add styles only on bp-groupblog admin page, see:
// http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
add_action( 'admin_print_styles-'.$page, array( &$this, 'add_admin_styles' ) );
}
/**
* @description: enqueue any styles and scripts needed by our admin page
* @todo:
*
*/
function add_admin_styles() {
/*
// EXAMPLES:
// add css
wp_enqueue_style('cpbp-admin-style', CPBP_PLUGIN_URL . 'css/admin.css');
// add javascripts
wp_enqueue_script( 'cpbp-admin-js', CPBP_PLUGIN_URL . 'js/admin.js' );
*/
}
/**
* @description: show our admin page
* @todo:
*
*/
function admin_page() {
// only allow network admins through
if( is_super_admin() == false ) {
// disallow
wp_die( __( 'You do not have permission to access this page.', 'cp-multisite' ) );
}
// sanitise admin page url
$url = $_SERVER['REQUEST_URI'];
$url_array = explode( '&', $url );
if ( is_array( $url_array ) ) { $url = $url_array[0]; }
// check for BuddyPress installation
// assume not installed
$bp = '<p class="alert">Please install BuddyPress correctly</p>';
// is it installed?
if ( isset( $GLOBALS['bp'] ) ) {
// we've got it
$bp = '<p>BuddyPress installed</p>';
}
// check for group blog installation
// assume not installed
$grp = '<p class="alert">Please install BP Group Blog correctly</p>';
// try and get a bp-groupblog option
$groupblog = get_site_option( 'bp_groupblog_blog_defaults_options', array() );
//print_r( $groupblog ); die();
// is it installed?
if ( !empty( $groupblog ) ) {
// we've got it
$grp = '<p>BP Group Blog installed</p>';
}
// define admin page - needs translation cap
$admin_page = '
<div class="icon32" id="icon-options-general"><br/></div>
<h2>Commentpress for BuddyPress Settings</h2>
<form method="post" action="'.htmlentities($url.'&updated=true').'">
'.wp_nonce_field( 'cpmu_admin_action', 'cpmu_nonce', true, false ).'
'.wp_referer_field( false ).'
<p style="padding-top: 30px;">Checking environment...</p>
'.$bp.'
'.$grp.'
<p class="submit">
<input type="submit" name="cpmu_submit" value="Save Changes" class="button-primary" />
</p>
</form>'."\n\n\n\n";
// done
echo $admin_page;
}
/**
* @description: enqueue any styles and scripts needed by our public page
* @todo:
*
*/
function add_frontend_styles() {
/*
// EXAMPLES:
// add css
wp_enqueue_style('cpbp-admin-style', CPBP_PLUGIN_URL . 'css/admin.css');
// add javascripts
wp_enqueue_script( 'cpbp-admin-js', CPBP_PLUGIN_URL . 'js/admin.js' );
*/
// dequeue BP Tempate Pack CSS, even if queued
wp_dequeue_style( 'bp' );
}
/*
--------------------------------------------------------------------------------
BP Groupblog Compatibility
--------------------------------------------------------------------------------
*/
/**
* Allow HTML comments and content in Multisite blogs
*/
function allow_html_content() {
// using publish_posts for now - means author+
if ( current_user_can( 'publish_posts' ) ) {
// remove html filtering on content. Note - this has possible consequences...
// see: http://wordpress.org/extend/plugins/unfiltered-mu/
kses_remove_filters();
}
}
/**
* Allow HTML in Activity items
*/
function activity_allowed_tags( $activity_allowedtags ) {
// pretty pointless not to allow p tags when we encourage the use of TinyMCE!
$activity_allowedtags['p'] = array();
// --<
return $activity_allowedtags;
}
/**
* @description: add capability to edit own comments
* @todo:
* @see: http://scribu.net/wordpress/prevent-blog-authors-from-editing-comments.html
*/
function enable_comment_editing( $caps, $cap, $user_id, $args ) {
// only apply this to queries for edit_comment cap
if ( 'edit_comment' == $cap ) {
// get comment
$comment = get_comment( $args[0] );
//print_r( array( 'comment' => $comment, 'user' => $user_id ) ); die();
//print_r( array( 'caps' => $caps, 'cap' => $cap ) ); die();
// is the user the same as the comment author?
if ( $comment->user_id == $user_id ) {
//$caps[] = 'moderate_comments';
$caps = array('edit_posts');
}
}
// --<
return $caps;
}
/**
* @description: override capability to comment based on group membership.
* @todo:
*
*/
function pre_comment_approved( $approved, $commentdata ) {
//print_r( $commentdata ); die();
global $wpdb;
$blog_id = (int)$wpdb->blogid;
// do we have groupblogs?
if ( function_exists( 'get_groupblog_group_id' ) ) {
// check if this blog is a group blog...
$group_id = get_groupblog_group_id( $blog_id );
}
// when this blog is a groupblog
if ( isset( $group_id ) AND is_numeric( $group_id ) ) {
// is this user a member?
if ( groups_is_user_member( $commentdata['user_id'], $group_id ) ) {
// allow un-moderated commenting
return 1;
}
}
// pass through
return $approved;
}
/*
// a nicer way?
add_action( 'preprocess_comment', 'my_check_comment', 1 );
function my_check_comment( $commentdata ) {
// Get the user ID of the comment author.
$user_id = absint( $commentdata['user_ID'] );
// If comment author is a registered user, approve the comment.
if ( 0 < $user_id )
add_filter( 'pre_comment_approved', 'my_approve_comment' );
else
add_filter( 'pre_comment_approved', 'my_moderate_comment' );
return $commentdata;
}
function my_approve_comment( $approved ) {
$approved = 1;
return $approved;
}
function my_moderate_comment( $approved ) {
if ( 'spam' !== $approved )
$approved = 0;
return $approved;
}
*/
/**
* @description: override "publicness" of groupblogs so that we can set the hide_sitewide
* property of the activity item (post or comment) depending on the group's setting.
* @todo: test if they are CP-enabled?
*
*/
function is_blog_public( $blog_public_option ) {
global $wpdb;
$blog_id = (int)$wpdb->blogid;
// do we have groupblogs?
if ( function_exists( 'get_groupblog_group_id' ) ) {
// check if this blog is a group blog...
$group_id = get_groupblog_group_id( $blog_id );
}
// when this blog is a groupblog
if ( isset( $group_id ) AND is_numeric( $group_id ) ) {
// always true - so that activities are registered
return 1;
} else {
return $blog_public_option;
}
}
/**
* groupblog_set_group_to_post_activity ( $activity )
*
* Record the blog activity for the group - amended from bp_groupblog_set_group_to_post_activity
*/
function groupblog_custom_comment_activity( $activity ) {
//print_r( array( 'a1' => $activity ) );// die();
// only deal with comments
if ( ( $activity->type != 'new_blog_comment' ) ) return;
// only do this on CP-enabled groupblogs
if ( ( false === $this->_is_commentpress_groupblog() ) ) return;
// get the group
$blog_id = $activity->item_id;
$group_id = get_groupblog_group_id( $blog_id );
if ( !$group_id ) return;
$group = groups_get_group( array( 'group_id' => $group_id ) );
//print_r( $group ); die();
// see if we already have the modified activity for this blog post
$id = bp_activity_get_activity_id( array(
'user_id' => $activity->user_id,
'type' => 'new_groupblog_comment',
'item_id' => $group_id,
'secondary_item_id' => $activity->secondary_item_id
) );
// if we don't find a modified item...
if ( !$id ) {
// see if we have an unmodified activity item
$id = bp_activity_get_activity_id( array(
'user_id' => $activity->user_id,
'type' => $activity->type,
'item_id' => $activity->item_id,
'secondary_item_id' => $activity->secondary_item_id
) );
}
// If we found an activity for this blog comment then overwrite that to avoid having
// multiple activities for every blog comment edit
if ( $id ) $activity->id = $id;
// get the comment
$comment = get_comment( $activity->secondary_item_id );
//print_r( $comment ); //die();
// get the post
$post = get_post( $comment->comment_post_ID );
//print_r( $post ); die();
// was it a registered user?
if ($comment->user_id != '0') {
// get user details
$user = get_userdata( $comment->user_id );
// construct user link
$user_link = bp_core_get_userlink( $activity->user_id );
} else {
// show anonymous user
$user_link = '<span class="anon-commenter">'.__( 'Anonymous', 'cp-multisite' ).'</span>';
}
// allow plugins to override the name of the activity item
$activity_name = apply_filters(
'cp_activity_post_name',
__( 'blog post', 'cp-multisite' )
);
// set key
$key = '_cp_comment_page';
// if the custom field has a value, we have a subpage comment...
if ( get_comment_meta( $comment->comment_ID, $key, true ) != '' ) {
// get comment's page from meta
$page_num = get_comment_meta( $comment->comment_ID, $key, true );
// get the url for the comment
$link = cp_get_post_multipage_url( $page_num ).'#comment-'.$comment->comment_ID;
// amend the primary link
$activity->primary_link = $link;
// init target link
$target_post_link = '<a href="' . cp_get_post_multipage_url( $page_num, $post ) .'">' . esc_html( $post->post_title ) . '</a>';
} else {
// init target link
$target_post_link = '<a href="' . get_permalink( $post->ID ) .'">' . esc_html( $post->post_title ) . '</a>';
}
// Replace the necessary values to display in group activity stream
$activity->action = sprintf(
__( '%s left a %s on a %s %s in the group %s:', 'cp-multisite' ),
$user_link,
'<a href="' . $activity->primary_link .'">' . __( 'comment', 'cp-multisite' ) . '</a>',
$activity_name,
$target_post_link,
'<a href="' . bp_get_group_permalink( $group ) . '">' . esc_html( $group->name ) . '</a>'
);
// apply group id
$activity->item_id = (int)$group_id;
// change to groups component
$activity->component = 'groups';
// having marked all groupblogs as public, we need to hide activity from them if the group is private
// or hidden, so they don't show up in sitewide activity feeds.
if ( 'public' != $group->status ) {
$activity->hide_sitewide = true;
} else {
$activity->hide_sitewide = false;
}
// set unique type
$activity->type = 'new_groupblog_comment';
// note: BP seemingly runs content through wp_filter_kses (sad face)
// prevent from firing again
remove_action( 'bp_activity_before_save', array( &$this, 'groupblog_custom_comment_activity' ) );
// --<
return $activity;
}
/**
* @description: add some meta for the activity item - bp_activity_after_save doesn't seem to fire
* @todo:
*
*/
function groupblog_custom_comment_meta( $activity ) {
print_r( array( 'a' => $activity ) );
// only deal with comments
if ( ( $activity->type != 'new_groupblog_comment' ) ) return;
// only do this on CP-enabled groupblogs
if ( ( false === $this->_is_commentpress_groupblog() ) ) return;
// set a meta value for the blog type of the post
$meta_value = $this->_get_groupblog_type();
print_r( array( 'a' => $activity ) );
print_r( array( 'm' => $meta_value ) );
$result = bp_activity_update_meta( $activity->id, 'groupblogtype', 'groupblogtype-'.$meta_value );
print_r( array( 'r' => ( ( $result === true ) ? 't' : 'f' ) ) ); die();
// prevent from firing again
remove_action( 'bp_activity_after_save', array( &$this, 'groupblog_custom_comment_meta' ) );
// --<
return $activity;
}
/**
* see: bp_groupblog_set_group_to_post_activity ( $activity )
*
* Record the blog post activity for the group - by Luiz Armesto
*/
function groupblog_custom_post_activity( $activity ) {
// only on new blog posts
if ( ( $activity->type != 'new_blog_post' ) ) return;
// only on CP-enabled groupblogs
if ( ( false === $this->_is_commentpress_groupblog() ) ) return;
//print_r( array( 'a1' => $activity ) ); //die();
// clarify data
$blog_id = $activity->item_id;
$post_id = $activity->secondary_item_id;
$post = get_post( $post_id );
// get group id
$group_id = get_groupblog_group_id( $blog_id );
if ( !$group_id ) return;
// get group
$group = groups_get_group( array( 'group_id' => $group_id ) );
// see if we already have the modified activity for this blog post
$id = bp_activity_get_activity_id( array(
'user_id' => $activity->user_id,
'type' => 'new_groupblog_post',
'item_id' => $group_id,
'secondary_item_id' => $activity->secondary_item_id
) );
// if we don't find a modified item...
if ( !$id ) {
// see if we have an unmodified activity item
$id = bp_activity_get_activity_id( array(
'user_id' => $activity->user_id,
'type' => $activity->type,
'item_id' => $activity->item_id,
'secondary_item_id' => $activity->secondary_item_id
) );
}
// If we found an activity for this blog post then overwrite that to avoid
// having multiple activities for every blog post edit
if ( $id ) {
$activity->id = $id;
}
// allow plugins to override the name of the activity item
$activity_name = apply_filters(
'cp_activity_post_name',
__( 'blog post', 'cp-multisite' )
);
// default to standard BP author
$activity_author = bp_core_get_userlink( $post->post_author );
// compat with Co-Authors Plus
if ( function_exists( 'get_coauthors' ) ) {
// get multiple authors
$authors = get_coauthors();
//print_r( $authors ); die();
// if we get some
if ( !empty( $authors ) ) {
// we only want to override if we have more than one...
if ( count( $authors ) > 1 ) {
// use the Co-Authors format of "name, name, name and name"
$activity_author = '';
// init counter
$n = 1;
// find out how many author we have
$author_count = count( $authors );
// loop
foreach( $authors AS $author ) {
// default to comma
$sep = ', ';
// if we're on the penultimate
if ( $n == ($author_count - 1) ) {
// use ampersand
$sep = __( ' & ', 'cp-multisite' );
}
// if we're on the last, don't add
if ( $n == $author_count ) { $sep = ''; }
// add name
$activity_author .= bp_core_get_userlink( $author->ID );
// and separator
$activity_author .= $sep;
// increment
$n++;
}
}
}
}
// if we're replacing an item, show different message...
if ( $id ) {
// replace the necessary values to display in group activity stream
$activity->action = sprintf(
__( '%s updated a %s %s in the group %s:', 'cp-multisite' ),
$activity_author,
$activity_name,
'<a href="' . get_permalink( $post->ID ) .'">' . esc_attr( $post->post_title ) . '</a>',
'<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>'
);
} else {
// replace the necessary values to display in group activity stream
$activity->action = sprintf(
__( '%s wrote a new %s %s in the group %s:', 'cp-multisite' ),
$activity_author,
$activity_name,
'<a href="' . get_permalink( $post->ID ) .'">' . esc_attr( $post->post_title ) . '</a>',
'<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>'
);
}
$activity->item_id = (int)$group_id;
$activity->component = 'groups';
// having marked all groupblogs as public, we need to hide activity from them if the group is private
// or hidden, so they don't show up in sitewide activity feeds.
if ( 'public' != $group->status ) {
$activity->hide_sitewide = true;
} else {
$activity->hide_sitewide = false;
}
// CMW: assume groupblog_post is intended
$activity->type = 'new_groupblog_post';
//print_r( array( 'a2' => $activity ) ); die();
// prevent from firing again
remove_action( 'bp_activity_before_save', array( &$this, 'groupblog_custom_post_activity' ) );
// --<
return $activity;
}
/**
* @description: add some meta for the activity item
* @todo:
*
*/
function groupblog_custom_post_meta( $activity ) {
// only on new blog posts
if ( ( $activity->type != 'new_groupblog_post' ) ) return;
// only on CP-enabled groupblogs
if ( ( false === $this->_is_commentpress_groupblog() ) ) return;
// set a meta value for the blog type of the post
$meta_value = $this->_get_groupblog_type();
bp_activity_update_meta( $activity->id, 'groupblogtype', 'groupblogtype-'.$meta_value );
// --<
return $activity;
}
/**
* Add a filter option to the filter select box on group activity pages.
*/
function groupblog_comments_filter_option() {
// default name
$comment_name = __( 'Commentpress Comments', 'cp-multisite' );
// allow plugins to override the name of the option
$comment_name = apply_filters( 'cp_groupblog_comment_name', $comment_name );
// construct option
$option = '<option value="new_groupblog_comment">'.$comment_name.'</option>'."\n";
// print
echo $option;
}
/**