/
rs-bp-activity-refresh.php
497 lines (432 loc) · 14.2 KB
/
rs-bp-activity-refresh.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
<?php
/*
Plugin Name: RS Buddypress Activity Refresh
PLugin URI: http://buddypress.org/community/groups/rs-buddypress-activity-refresh/
Description: This plugin automatically refresh the Buddypress activity stream
Author: Florian Koenig-Heidinger
Requires at least: 2.9.1 / 1.2.4
Tested up to: 3.0.1 / 1.2.6
Tags: buddypress
Version: 1.5
Author URI: http://buddypress.org/community/members/Spitzohr/
Update 1.5: Plugin should work with Buddypress 1.5 again
Update 1.1.4: Reset document title if Activity List is refreshed
Update 1.1.3: Move Admin Page from Settings to BuddyPress
*/
/**
* class rsBuddypressActivityRefresh
*/
class rsBuddypressActivityRefresh
{
/**
* Plugin Folder
* @access protected
* @var string
*/
protected $plugin_folder = null;
/**
* Plugin Directory
* @access protected
* @var string
*/
protected $plugin_dir = null;
/**
* Plugin Url
* @access protected
* @var string
*/
protected $plugin_url = null;
/**
* Plugin File
* @access protected
* @var string
*/
protected $plugin_file = null;
/**
* Default Refresh Rate in seconds
* @access protected
* @var integer
*/
protected $defaultRefreshRate = 10;
/**
* Default Time Format
* Valid values are 'since', 'datetime'
* @access protected
* @var string
*/
protected $defaultTimeFormat = 'since';
/**
* Allowed Values for Time Format
* @access protected
* @var array
*/
protected $allowedValuesTimeFormat = array('since', 'datetime');
/**
* Admin Options Name
* @access protected
* @var string
*/
protected $adminOptionsName = 'rsBuddypressActivityRefresh';
/**
* Options array
* @access protected
* @var array
*/
protected $options = null;
/**
* Constructor
* Do nothing
* @access public
*/
public function __construct()
{
} // function construct()
/**
* Initialising
* @access public
*/
public function init()
{
$this->plugin_folder = substr(dirname(__FILE__), strrpos(dirname(__FILE__), DIRECTORY_SEPARATOR) + 1);
$this->plugin_url = WP_PLUGIN_URL . '/' . $this->plugin_folder;
$this->plugin_dir = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $this->plugin_folder;
$this->plugin_file = $this->plugin_folder . '/' . basename(__FILE__);
load_plugin_textdomain('rs-buddypress-activity-refresh', null, dirname(plugin_basename( __FILE__ )) . '/languages');
// load Options
$this->getOptions();
// only add the JavaScript, if the refreshRate > 0
if ($this->options['refreshRate'] > 0)
{
// adding the refresh javascript file
wp_enqueue_script( 'jquery-timeago-js', $this->plugin_url . '/jquery.timeago.js', array( 'jquery' ) );
wp_enqueue_script( 'rs-bp-activity-refresh-ajax-js', $this->plugin_url . '/refresh.js', array( 'jquery' ) );
// adding JavaScript Refresh Rate to html head
add_action( 'wp_head', array(&$this, 'addJavaScriptRefreshRate'));
// add method ajaxRefresh to action hook wp_ajax_rs_bp_activity_refresh
add_action('wp_ajax_rs_bp_activity_refresh', array(&$this, 'ajaxRefresh'));
}
add_action('admin_menu', array(&$this, 'addAdminMenu'));
add_filter('plugin_action_links', array(&$this, 'addPluginActionLink'), 10, 2 );
add_filter('bp_activity_time_since', array($this, 'replaceActivityTimeSince'), 10, 2);
add_filter('bp_activity_get_comments', array($this, 'getComments'));
add_filter('bp_activity_allowed_tags', array($this, 'updateAllowedTags'));
} // function init()
/**
* Updating allowed tags
*/
public function updateAllowedTags($allowedTags)
{
// Adding the title attribute to <span>, so we can add the timestamp
$allowedTags['span']['title'] = array();
return $allowedTags;
}
/**
* getOptions()
* Load and update options
* @access protected
*/
protected function getOptions()
{
// set default options
$this->options = array(
'refreshRate' => $this->defaultRefreshRate,
'timeFormat' => $this->defaultTimeFormat,
);
// load options
$savedOptions = get_option($this->adminOptionsName);
if (!empty($savedOptions))
{
foreach ($savedOptions as $key => $option)
{
$this->options[$key] = $option;
}
}
// save options
update_option($this->adminOptionsName, $this->options);
return $this->options;
} // getOptions()
/**
* validate and update options ()
* Load and update options
* @var $options array
* @access protected
*/
protected function saveOptions( array $options )
{
if (!empty($options))
{
foreach ($options as $key => $option)
{
switch ($key)
{
case 'refreshRate':
if (is_numeric($option))
{
$this->options[$key] = (int)$option;
}
break;
default:
$this->options[$key] = $option;
break;
}
}
}
// save options
update_option($this->adminOptionsName, $this->options);
} // saveOptions()
/**
* add JavaScript variable refreshRate
* action hook: wp_head
* @access public
*/
public function addJavaScriptRefreshRate()
{
echo '<script type="text/javascript">' . "\n";
echo 'var rsBpActivityRefreshRate = ' . $this->options['refreshRate'] . ';' . "\n";
if ($this->options['timeFormat'] == 'since')
{
echo 'var rsBpActivityRefreshTimeago = true;' . "\n";
echo 'jQuery.timeago.settings.refreshMillis = 0;' . "\n";
}
else
{
echo 'var rsBpActivityRefreshTimeago = false;' . "\n";
}
echo '</script>' . "\n";
}
/**
* method to answer the ajax request
* action hook: wp_ajax_rs_bp_activity_refresh
* @access public
*/
public function ajaxRefresh()
{
global $bp, $activities_template;
$inGroup = !empty( $bp->groups->current_group);
// get last id
if (isset($_POST['last_id']) && is_numeric($_POST['last_id']))
{
$last_id = (int) $_POST['last_id'];
// start the Loop
// show new comments in stream format
if (bp_has_activities( bp_ajax_querystring( 'activity' ) . '&display_comments=stream' ))
{
$activities = $activities_template->activities;
foreach ($activities as $activity)
{
$activities_template->in_the_loop = true;
$activities_template->current_activity++;
$activities_template->activity = $activity;
if ($inGroup)
{
if (bp_get_activity_id() > $last_id)
{
// print the entry
include( locate_template( array( 'activity/entry.php' ), false ) );
}
else if (!empty($activities_template->activity->children))
{
$activities_template->activity_parents[$activity->id] = $activity;
$this->recursiveLoopForGroups($activity->children);
}
}
else
{
// if the current id is less than last_id, break the while
if (bp_get_activity_id() > $last_id)
{
// print the entry
include( locate_template( array( 'activity/entry.php' ), false ) );
}
}
}
}
}
} // function ajaxRefresh()
protected function recursiveLoopForGroups($comments)
{
global $activities_template;
$last_id = (int) $_POST['last_id'];
foreach ($comments as $comment)
{
$activities_template->activity = $comment;
if (bp_get_activity_id() > $last_id)
{
// print the entry
include( locate_template( array( 'activity/entry.php' ), false ) );
}
if (!empty($comment->children))
{
$activities_template->activity_parents[$comment->id] = $comment;
$this->recursiveLoopForGroups($comment->children);
}
}
}
/**
* method to add the admin menu
* action hook: admin_menu
* @access public
*/
public function addAdminMenu()
{
add_submenu_page(
'bp-general-settings',
'Activity Refresh',
'Activity Refresh',
'manage_options',
basename(__FILE__),
array(&$this, 'printAdminPage')
);
} // end function addAdminMenu()
/**
* prints out the admin page
* @access public
*/
public function printAdminPage()
{
if (!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.') );
}
if (isset($_POST['update_rsbpactivityrefresh']))
{
if (isset($_POST['rsbpactivityrefresh_refreshrate']))
{
$options['refreshRate'] = $_POST['rsbpactivityrefresh_refreshrate'];
}
if (isset($_POST['rsbpactivityrefresh_timeformat']) && in_array($_POST['rsbpactivityrefresh_timeformat'], $this->allowedValuesTimeFormat))
{
$options['timeFormat'] = $_POST['rsbpactivityrefresh_timeformat'];
}
$this->saveOptions($options);
?>
<div class="updated"><p><strong><?php _e('Settings Updated.', 'rs-buddypress-activity-refresh');?></strong></p></div>
<?php
}
?>
<div class="wrap">
<form method="post" action="">
<h2>RS Buddypress Activity Refresh</h2>
<h3><?php _e('Refresh Rate', 'rs-buddypress-activity-refresh'); ?></h3>
<p><?php printf( __('This value is the refresh rate in seconds. Default value is %d. If you set this value to 0, the refresh is disabled.', 'rs-buddypress-activity-refresh'), $this->defaultRefreshRate); ?>
<p><label for="rsbpactivityrefresh_refreshrate"><input type="text" id="rsbpactivityrefresh_refreshrate" name="rsbpactivityrefresh_refreshrate" value="<?php echo $this->options['refreshRate']; ?>" /> <?php _e('seconds', 'rs-buddypress-activity-refresh'); ?></label></p>
<h3><?php _e('Time Format', 'rs-buddypress-activity-refresh'); ?></h3>
<p><?php printf( __('Choose between the the <em>since</em>-format: <strong>5 minutes ago</strong> and the <em>datetime</em>-format: <strong>%s</strong>', 'rs-buddypress-activity-refresh'), $this->getFormattedDateTime(date('d-m-y H:i:s', time() - 5 * 60))); ?>
<p><label for="rsbpactivityrefresh_timeformat"><select id="rsbpactivityrefresh_timeformat" name="rsbpactivityrefresh_timeformat">
<option value="since"<?php if ($this->options['timeFormat'] == 'since') echo ' selected="selected"'; ?>>Since</option>
<option value="datetime"<?php if ($this->options['timeFormat'] == 'datetime') echo ' selected="selected"'; ?>>DateTime</option>
</select></p>
<div class="submit">
<input type="submit" name="update_rsbpactivityrefresh" value="<?php _e('Update Settings', 'rs-buddypress-activity-refresh'); ?>" />
</div>
</form>
</div>
<?php
}//End function printAdminPage()
/**
* addPluginActionLink
* Stolen from Welcome Pack - thanks, Paul!
* filter hook: plugin_action_links
* @access public
* @param array $links
* @param string $file
*/
public function addPluginActionLink ($links, $file)
{
if ($this->plugin_file != $file)
{
return $links;
}
$settings_link = '<a href="' . admin_url('admin.php?page=' . basename(__FILE__)) . '">' . __( 'Settings', 'rs-buddypress-activity-refresh' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
} // addPluginActionLink()
/**
* getFormattedDateTime
*/
protected function getFormattedDateTime($datetime)
{
$return = '<span class="timeago" title="' . date('c', strtotime($datetime)) . '">' . date(get_option('date_format') . ' ' . get_option('time_format'), strtotime($datetime) + get_option('gmt_offset') * 60 * 60) . '</span>';
return $return;
}
/**
* replaceActivityTimeSince
*/
public function replaceActivityTimeSince($content, $activity = false)
{
if ($activity !== false)
{
$content = '<span class="time-since">';
$content .= $this->getFormattedDateTime($activity->date_recorded);
$content .= '</span>';
}
return $content;
}
/**
* getComments
* Copy fo the /buddypress/bp-activity/bp-activity-templatetags.php/bp_activity_get_comments() function
* Modification to use my own recursiveComments-function
* @access public
* @params array $args
*/
public function getComments($args = '')
{
global $activities_template, $bp;
if ( !$activities_template->activity->children )
return false;
$comments_html = $this->recursiveComments( $activities_template->activity );
return $comments_html;
}
/**
* recursiveComments
* Copy of the /buddypress/bp-activity/bp-activity-templatetags.php/bp_activity_recurse_comments() function
* Modification to change the "time since"
* @access public
* @param object $comment
*/
protected function recursiveComments($comment)
{
global $activities_template, $bp;
if ( !$comment->children )
return false;
$content .= '<ul>';
foreach ( (array)$comment->children as $comment )
{
if ( !$comment->user_fullname )
{
$comment->user_fullname = $comment->display_name;
}
$content .= '<li id="acomment-' . $comment->id . '">';
$content .= '<div class="acomment-avatar"><a href="' . bp_core_get_user_domain( $comment->user_id, $comment->user_nicename, $comment->user_login ) . '">' . bp_core_fetch_avatar( array( 'item_id' => $comment->user_id, 'width' => 20, 'height' => 20, 'email' => $comment->user_email ) ) . '</a></div>';
$content .= '<div class="acomment-meta"><a href="' . bp_core_get_user_domain( $comment->user_id, $comment->user_nicename, $comment->user_login ) . '">' . apply_filters( 'bp_acomment_name', $comment->user_fullname, $comment ) . '</a> · ' . $this->getFormattedDateTime( $comment->date_recorded );
/* Reply link - the span is so that threaded reply links can be hidden when JS is off. */
if ( is_user_logged_in() )
{
$content .= '<span class="acomment-replylink"> · <a href="#acomment-' . $comment->id . '" class="acomment-reply" id="acomment-reply-' . $activities_template->activity->id . '">' . __( 'Reply', 'buddypress' ) . '</a></span>';
}
/* Delete link */
if ( $bp->loggedin_user->is_super_admin || $bp->loggedin_user->id == $comment->user_id )
{
$content .= ' · <a href="' . wp_nonce_url( $bp->root_domain . '/' . $bp->activity->slug . '/delete/?cid=' . $comment->id, 'bp_activity_delete_link' ) . '" class="delete acomment-delete">' . __( 'Delete', 'buddypress' ) . '</a>';
}
$content .= '</div>';
$content .= '<div class="acomment-content">' . apply_filters( 'bp_get_activity_content', $comment->content ) . '</div>';
$content .= $this->recursiveComments( $comment );
$content .= '</li>';
}
$content .= '</ul>';
return $content;
} // recursiveComments()
} // class rsBuddypressActivityRefresh
/**
* rsBuddypressActivityRefreshInit()
*
* Only load the plugin code if BuddyPress is activated.
*/
function rsBuddypressActivityRefreshInit()
{
$rsBuddypressActivityRefresh = new rsBuddypressActivityRefresh();
$rsBuddypressActivityRefresh->init();
}
add_action('after_setup_theme', 'rsBuddypressActivityRefreshInit');
?>