forked from annecamille/rede_saude_cultura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddy_boss_wall.php
622 lines (496 loc) · 16.5 KB
/
buddy_boss_wall.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
<?php
/**
* buddy_boss_wall is a BuddyPress plugin combining user activity feeds into a Facebook-like wall.
*
* @author BuddyBoss
* @credits Brajesh Singh for his tutorial on in profile posting
* @since BuddyBoss 2.0
*/
// Indicate whether the Wall module is enabled
// Check if the friends module is enabled if not disable the wall
if (function_exists('friends_get_alphabetically'))
{
$option = get_option("buddy_boss_wall_on");
define(BUDDY_BOSS_WALL_ENABLED, $option);
}
else
{
define(BUDDY_BOSS_WALL_ENABLED, FALSE);
}
// Indicate whether to show debug msgs on screen
define(BUDDY_BOSS_DEBUG, false);
// a variable to hold the event log
$buddy_boss_debug_log = "";
// DEFAULT CONFIGURATION OPTIONS
$buddy_boss_wall_defaults = array(
"POST_IN_WIRE_OPTIONS" => array(),
"UPDATE_MENUS" => TRUE,
"PERSONAL_TAB_NAME" => __( 'News Feed' , 'buddyboss' ),
"FAV_TAB_NAME" => __( 'My Likes' , 'buddyboss' ),
"MENU_NAME" => __( 'Wall' , 'buddyboss' )
);
class BuddyBoss_Wall
{
/**
* BUDDYPRESS ACTIVITIES
*
* @since BuddyBoss 1.5
*/
public $activities;
public $activity_count = 0;
public $filter_qs = false;
/**
* OPTIONS
*
* @since BuddyBoss 1.5
*/
private $options;
/**
* STORAGE
*
* @since BuddyBoss 2.0
*/
public $cache;
/**
* LIKES
*
* @since BuddyBoss 2.0
*/
public $likes_store = array();
/**
* INITIALIZE CLASS
*
* @since BuddyBoss 1.5
*/
function __construct($options = null)
{
global $buddy_boss_wall_defaults, $activity_template;
if (isset($options) && $options !=null)
{
$this->options = $options;
}
else
{
$this->options = $buddy_boss_wall_defaults;
buddy_boss_log("WALL Using default config");
}
// Log
buddy_boss_log($this->options);
// Update menu text
if (isset($this->options['UPDATE_MENUS']) && $this->options['UPDATE_MENUS'] == true)
{
add_action( 'bp_setup_nav', array($this, 'update_menus'), 98 );
add_action( 'bp_setup_nav', array($this, 'bbg_remove_activity_friends_subnav'), 99 );
add_filter( 'bp_get_displayed_user_nav_activity', array($this, 'bbg_replace_activity_link') );
}
// Add body class
add_filter( 'body_class', array($this, 'add_body_class') );
// Caching
$this->cache = get_transient('bbwall_cacher');
add_action( 'wp_shutdown', array($this, 'shutdown') );
return $this;
}
/**
* SAVES CACHE @ WP SHUTDOWN
*
* @since BuddyBoss 1.5
*/
function shutdown()
{
set_transient('bbwall_cacher', $this->cache);
}
/**
* GET OPTION
*
* @since BuddyBoss 1.5
*/
function get_option($name)
{
if (isset($this->options[$name])) return $this->options[$name];
return false;
}
/**
* Add active wall class
*
* @since BuddyBoss 2.0
*/
function add_body_class( $classes )
{
$classes[] = 'buddyboss-active-wall';
return $classes;
}
/**
* RENAME ACTIVITY LINK ON PROFILE SIDEBAR MENU
*
* @since BuddyBoss 1.5
*/
function bbg_replace_activity_link($v)
{
return str_replace('Activity', $this->options["MENU_NAME"], $v);
}
/**
* REMOVE TABS FROM PROFILE HEADER
*
* @since BuddyBoss 1.5
*/
function bbg_remove_activity_friends_subnav() {
global $bp;
// For now we hack the $bp global to remove a nav item without
// removing the funcitonality. This is a bug in Buddypress
if ( isset( $bp->bp_options_nav ) && isset ( $bp->bp_options_nav['activity'] ) )
{
if ( isset( $bp->bp_options_nav['activity']['friends'] ) )
unset ( $bp->bp_options_nav['activity']['friends'] );
if ( isset( $bp->bp_options_nav['activity']['mentions'] ) )
unset ( $bp->bp_options_nav['activity']['mentions'] );
if ( isset( $bp->bp_options_nav['activity']['groups'] ) )
unset ( $bp->bp_options_nav['activity']['groups'] );
if ( !bp_is_home() && isset( $bp->bp_options_nav['activity']['favorites'] ) )
unset ( $bp->bp_options_nav['activity']['favorites'] );
}
/**
* This code won't work until a bugfix in Buddypress is applied.
*
bp_core_remove_subnav_item( 'activity', 'friends' );
bp_core_remove_subnav_item( 'activity', 'mentions' );
bp_core_remove_subnav_item( 'activity', 'groups' );
if ( !bp_is_home() )
bp_core_remove_subnav_item( 'activity', 'favorites' );
*/
}
/**
* RENAME MENU TABS ON PROFILE
*/
function update_menus()
{
buddy_boss_log('Updating Menus');
global $bp;
$domain = (!empty($bp->displayed_user->id)) ? $bp->displayed_user->domain : $bp->loggedin_user->domain;
$profile_link = $domain . $bp->activity->slug . '/';
// RENAME PERSONAL TAB
bp_core_new_subnav_item( array(
'name' => $this->options["PERSONAL_TAB_NAME"],
'slug' => 'just-me',
'parent_url' => $profile_link,
'parent_slug' => $bp->activity->slug,
'screen_function' =>
'bp_activity_screen_my_activity' ,
"position" => 10
) );
// RENAME FAVORITES TAB
bp_core_new_subnav_item( array(
'name' => $this->options["FAV_TAB_NAME"],
'slug' => 'favorites',
'parent_url' => $profile_link,
'parent_slug' => $bp->activity->slug,
'screen_function' => 'bp_activity_screen_favorites',
'position' => 10
) );
}
/**
* WRAPPER FUNCTION, WILL BE DEPRECATED
*/
function is_friend($id)
{
return buddyboss_is_my_friend($id);
}
/**
* GET WALL ACTIVITES
*/
function get_wall_activities($page = 0, $per_page=20){
global $bp,$wpdb;
$min = ($page>0)? ($page-1) * $per_page : 0;
$max = ($page+1) * $per_page;
$per_page = bp_get_activity_per_page();
buddy_boss_log(" per page $per_page");
if (isset($bp->loggedin_user) && isset($bp->loggedin_user->id) && $bp->displayed_user->id == $bp->loggedin_user->id)
{
$myprofile = true;
}
else {
$myprofile = false;
}
$wpdb->show_errors = BUDDY_BOSS_DEBUG;
$user_id = $bp->displayed_user->id;
buddy_boss_log("Looking at $user_id" );
$filter = addslashes($bp->displayed_user->fullname);
$friend_ids = friends_get_friend_user_ids($user_id);
$admin_ids = array(); //buddyboss_users_by_role( 'administrator' );
$friend_ids = array_merge($friend_ids,$admin_ids);
// var_dump($friend_ids);
buddy_boss_log($friend_ids);
if (!empty($friend_ids)) $friend_id_list = implode(",", ($friend_ids));
buddy_boss_log($friend_id_list);
$table = $wpdb->prefix."bp_activity";
$table2 = $wpdb->prefix."bp_activity_meta";
// Group Display code
$groups = BP_Groups_Member::get_group_ids($user_id);
//var_dump($groups);
$valid_groups=array();
if (!empty($groups)) {
foreach ($groups['groups'] as $id)
{
$group = new BP_Groups_Group( $id);
//var_dump($group);
if ("public" == $group->status) {
//echo $group->slug;
$valid_groups[]=$id;
}
$user_group[]=$group->slug;
}
}
$group_modifier = "OR ( $table.user_id = $user_id AND $table.component = 'groups' ) ";
$group_mentions = "OR ( $table.component = 'groups' AND ($table.content LIKE '%@$mentions_filter%'))";
$friendships = "OR ( $table.user_id = $user_id AND $table.component = 'friends' )";
$mentions_modifier = "OR ( $table.component = 'activity' AND $table.action LIKE '%@$mentions_filter%' ) ";
//$friends_modifier = "OR ( $table.user_id IN ($friend_id_list) AND ($table2.meta_value LIKE '%$filter%') ) ";
$friends_modifier = "OR ( $table.user_id IN ($friend_id_list) AND $table.type!='activity_comment' AND ($table2.meta_value LIKE '%@$mentions_filter%') ) ";
$qry = "SELECT $table.id FROM $table LEFT JOIN $table2 ON $table.id=$table2.activity_id
WHERE ( $table.user_id = $user_id AND $table.type!='activity_comment' AND $table.type!='friends' )
$friends_modifier
$group_modifier
$group_mentions
$friendships
$mentions_modifier
ORDER BY date_recorded DESC LIMIT $min, 30";
//echo $qry;
$activities = $wpdb->get_results($qry,ARRAY_A);
//var_dump($wpdb->print_error());
buddy_boss_log($qry);
buddy_boss_log($activities);
if (empty($activities )) return null;
$tmp = array();
foreach ($activities as $a)
{
$tmp[] = $a["id"];
}
$activity_list = implode(",", $tmp);
return $activity_list;
}
/**
* Retrive likes for current activity (within activity loop)
*
* @since 2.0
*/
function has_likes( $actid = null )
{
if ( $actid === null ) $actid = bp_get_activity_id();
return bp_activity_get_meta( $actid, 'favorite_count' );
}
} // end of BUDDY_BOSS_WALL class
/**
* ACTIVATION AND DEACTIVATION CODE
*/
function buddy_boss_wall_on_activate()
{
return 'The Profile Wall was activated successfully';
}
/**
* Deregister the BuddyBoss Wall Component
*
* @since BuddyBoss 2.0
*/
function buddy_boss_wall_on_deactivate()
{
return '';
}
/* FILTERS */
if (BUDDY_BOSS_WALL_ENABLED)
{
add_action('bp_before_activity_comment', 'wall_add_like_action');
add_filter('bp_get_activity_action', 'wall_read_filter');
add_filter('bp_activity_after_save', 'wall_input_filter' );
add_filter('bp_ajax_querystring', 'wall_qs_filter');
}
/**
* This adds how many people liked an item
*
* @since BuddyBoss 2.0
*/
function wall_add_like_action()
{
global $buddy_boss_wall;
if ( isset($_POST['action']) && $_POST['action'] == 'new_activity_comment' )
return false;
$actid = (int) bp_get_activity_id();
if ( $actid === 0 )
return false;
if ( isset( $buddy_boss_wall->likes_store[$actid] ) )
return false;
$count = (int) bp_activity_get_meta( $actid, 'favorite_count' );
$buddy_boss_wall->likes_store[$actid] = 1;
if ( $count === 0 )
return false;
$subject = ($count == 1) ? __( 'person' , 'buddyboss' ) : __( 'people' , 'buddyboss' );
$verb = ($count > 1) ? __( 'like' , 'buddyboss' ) : __( 'likes' , 'buddyboss' );
$like_html = "<li class=\"activity-like-count\">$count $subject $verb " . __( 'this' , 'buddyboss' ) . ".</li>";
echo $like_html;
}
/**
* This adds how many people liked an item
*
* @since BuddyBoss 2.0
*/
function wall_add_likes_comments()
{
$actid = (int) bp_get_activity_id();
if ( $actid === 0 )
return false;
$count = (int) bp_activity_get_meta( $actid, 'favorite_count' );
if ( $count === 0 )
return false;
$subject = ($count == 1) ? __( 'person' , 'buddyboss' ) : __( 'people' , 'buddyboss' );
$verb = ($count > 1) ? __( 'like' , 'buddyboss' ) : __( 'likes' , 'buddyboss' );
$like_html = "<ul><li class=\"activity-like-count\">$count $subject $verb " . __( 'this' , 'buddyboss' ) . ".</li></ul>";
echo $like_html;
}
/**
* This filters wall actions, when reading an item it will convert it to use wall language
*
* @since BuddyBoss 2.0
*/
function wall_read_filter($action)
{
global $bbwall, $activities_template;
$curr_id = $activities_template->current_activity;
$act_id = $activities_template->activities[$curr_id]->id;
$bbwall_action = bp_activity_get_meta( $act_id, 'bboss_wall_action' );
return ($bbwall_action) ? $bbwall_action : $action;
}
/**
* This will save wall related data to the activity meta table when a new wall post happens
*
* @since BuddyBoss 2.0
*/
function wall_input_filter(&$activity)
{
global $bp, $buddy_boss_wall;
$user = $bp->loggedin_user;
$tgt = $bp->displayed_user;
$new_action = false;
// If we're on an activity page (user's own profile or a friends), check for a target ID
if ( $bp->current_action == 'just-me' && (!isset($tgt->id) || $tgt->id == 0) ) return;
// It's either an @ mention, status update, or forum post.
if ( ($bp->current_action == 'just-me' && $user->id == $tgt->id) || $bp->current_action == 'forum' )
{
if (!empty($activity->content))
{
$mentioned = bp_activity_find_mentions($activity->content);
$uids = array();
$usernames = array();
// Get all the mentions and store valid usernames in a new array
foreach( (array)$mentioned as $mention ) {
if ( bp_is_username_compatibility_mode() )
$user_id = username_exists( $mention );
else
$user_id = bp_core_get_userid_from_nicename( $mention );
if ( empty( $user_id ) )
continue;
$uids[] = $user_id;
$usernames[] = $mention;
}
$len = count($uids);
$mentions_action = '';
// It's mentioning one person
if($len == 1)
{
$user_id =
$tgt = bp_core_get_core_userdata( (int) $uids[0] );
$user_url = '<a href="'.$user->domain.'">'.$user->fullname.'</a>';
$tgt_url = '<a href="'.bp_core_get_userlink( $uids[0], false, true ).'">@'.$tgt->user_login.'</a>';
$mentions_action = " " . __( 'mentioned' , 'buddyboss' ) ." ". $tgt_url;
}
// It's mentioning multiple people
elseif($len > 1)
{
$user_url = '<a href="'.$user->domain.'">'.$user->fullname.'</a>';
$un = '@'.join(',@', $usernames);
$mentions_action = $user_url. " " . __( 'mentioned' , 'buddyboss' ) ." ".$len." " . __( 'people' , 'buddyboss' );
}
// If it's a forum post let's define some forum topic text
if ( $bp->current_action == 'forum' )
{
$new_action = str_replace( ' replied to the forum topic', $mentions_action.' in the forum topic', $activity->action);
}
// If it's a plublic message let's define that text as well
elseif ($len > 0) {
$new_action = $user_url. " " .$mentions_action.' ' . __( 'in a public message' , 'buddyboss' );
}
// Otherwise it's a normal status update
else {
$new_action = false;
}
}
}
// It's a normal wall post because the displayed ID doesn't match the logged in ID
// And we're on activity page
elseif ( $bp->current_action == 'just-me' && $user->id != $tgt->id ) {
$user_url = '<a href="'.$user->domain.'">'.$user->fullname.'</a>';
$tgt_url = '<a href="'.$tgt->domain.'">'.$tgt->fullname.'\'s</a>';
// if a user is on his own page it is an update
$new_action = sprintf( __( '%s wrote on %s Wall', 'buddyboss' ), $user_url , $tgt_url );
}
if ( $new_action )
{
bp_activity_update_meta( $activity->id, 'bboss_wall_action', $new_action );
}
}
function wall_qs_filter($qs)
{
global $bp, $buddy_boss_wall, $buddy_boss_wall_defaults;
//echo $qs;
$action = $bp->current_action;
if ( $action != "just-me")
{
// if we're on a different page than wall pass qs as is
return $qs;
}
// else modify it to include wall activities
// see if we have a page string
$page_str = preg_match("/page=\d+/", $qs, $m);
$page= intval(str_replace("page=", "", $m[0])); // if so grab the number
$activities = $buddy_boss_wall->get_wall_activities($page); // load activities for this page
$nqs = "include=$activities";
return $nqs;
}
// POST IN WIRE by Brajesh Singh
// AJAX update posting
function bp_mytheme_post_update() {
global $bp;
// Check the nonce
check_admin_referer( 'post_update', '_wpnonce_post_update' );
if ( !is_user_logged_in() ) {
echo '-1';
return false;
}
if ( empty( $_POST['content'] ) ) {
echo '-1<div id="message" class="error"><p>' . __( 'Please enter some content to post.', 'buddypress' ) . '</p></div>';
return false;
}
if ( empty( $_POST['object'] ) && function_exists( 'bp_activity_post_update' ) ) {
if(!bp_is_home()&&bp_is_member())
$content="@". bp_get_displayed_user_username()." ".$_POST['content'];
else
$content=$_POST['content'];
$activity_id = bp_activity_post_update( array( 'content' => $content ) );
} elseif ( $_POST['object'] == 'groups' ) {
if ( !empty( $_POST['item_id'] ) && function_exists( 'groups_post_update' ) )
$activity_id = groups_post_update( array( 'content' => $_POST['content'], 'group_id' => $_POST['item_id'] ) );
} else
$activity_id = apply_filters( 'bp_activity_custom_update', $_POST['object'], $_POST['item_id'], $_POST['content'] );
if ( !$activity_id ) {
echo '-1<div id="message" class="error"><p>' . __( 'There was a problem posting your update, please try again.', 'buddypress' ) . '</p></div>';
return false;
}
if ( bp_has_activities ( 'include=' . $activity_id ) ) : ?>
<?php while ( bp_activities() ) : bp_the_activity(); ?>
<?php locate_template( array( 'activity/entry-wall.php' ), true ) ?>
<?php endwhile; ?>
<?php endif;
}
add_action( 'wp_ajax_post_update', 'bp_mytheme_post_update' );
add_action("init","mytheme_remove_original_update_func",5);
function mytheme_remove_original_update_func(){
remove_action( 'wp_ajax_post_update', 'bp_dtheme_post_update' );
}
?>