PluginProbe
bbPress / 2.2
bbPress v2.2
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / includes / extend / buddypress / activity.php

activity.php in bbPress 2.2, at includes/extend/buddypress/activity.php

709 lines 20.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress BuddyPress Activity Class
5 *
6 * @package bbPress
7 * @subpackage BuddyPress
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 if ( !class_exists( 'BBP_BuddyPress_Activity' ) ) :
14 /**
15 * Loads BuddyPress Activity extension
16 *
17 * @since bbPress (r3395)
18 *
19 * @package bbPress
20 * @subpackage BuddyPress
21 */
22 class BBP_BuddyPress_Activity {
23
24 /** Variables *************************************************************/
25
26 /**
27 * The name of the BuddyPress component, used in activity streams
28 *
29 * @var string
30 */
31 private $component = '';
32
33 /**
34 * Forum Create Activty Action
35 *
36 * @var string
37 */
38 private $forum_create = '';
39
40 /**
41 * Topic Create Activty Action
42 *
43 * @var string
44 */
45 private $topic_create = '';
46
47 /**
48 * Topic Close Activty Action
49 *
50 * @var string
51 */
52 private $topic_close = '';
53
54 /**
55 * Topic Edit Activty Action
56 *
57 * @var string
58 */
59 private $topic_edit = '';
60
61 /**
62 * Topic Open Activty Action
63 *
64 * @var string
65 */
66 private $topic_open = '';
67
68 /**
69 * Reply Create Activty Action
70 *
71 * @var string
72 */
73 private $reply_create = '';
74
75 /**
76 * Reply Edit Activty Action
77 *
78 * @var string
79 */
80 private $reply_edit = '';
81
82 /** Setup Methods *********************************************************/
83
84 /**
85 * The bbPress BuddyPress Activity loader
86 *
87 * @since bbPress (r3395)
88 */
89 public function __construct() {
90 $this->setup_globals();
91 $this->setup_actions();
92 $this->setup_filters();
93 $this->fully_loaded();
94 }
95
96 /**
97 * Extension variables
98 *
99 * @since bbPress (r3395)
100 * @access private
101 * @uses apply_filters() Calls various filters
102 */
103 private function setup_globals() {
104
105 // The name of the BuddyPress component, used in activity streams
106 $this->component = 'bbpress';
107
108 // Forums
109 $this->forum_create = 'bbp_forum_create';
110
111 // Topics
112 $this->topic_create = 'bbp_topic_create';
113 $this->topic_edit = 'bbp_topic_edit';
114 $this->topic_close = 'bbp_topic_close';
115 $this->topic_open = 'bbp_topic_open';
116
117 // Replies
118 $this->reply_create = 'bbp_reply_create';
119 $this->reply_edit = 'bbp_reply_edit';
120 }
121
122 /**
123 * Setup the actions
124 *
125 * @since bbPress (r3395)
126 * @access private
127 * @uses add_filter() To add various filters
128 * @uses add_action() To add various actions
129 */
130 private function setup_actions() {
131
132 // Register the activity stream actions
133 add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
134
135 // Hook into topic and reply creation
136 add_action( 'bbp_new_topic', array( $this, 'topic_create' ), 10, 4 );
137 add_action( 'bbp_new_reply', array( $this, 'reply_create' ), 10, 5 );
138
139 // Hook into topic and reply status changes
140 add_action( 'wp_insert_post', array( $this, 'topic_update' ), 10, 2 );
141 add_action( 'wp_insert_post', array( $this, 'reply_update' ), 10, 2 );
142
143 // Hook into topic and reply deletion
144 add_action( 'bbp_delete_topic', array( $this, 'topic_delete' ), 10, 1 );
145 add_action( 'bbp_delete_reply', array( $this, 'reply_delete' ), 10, 1 );
146
147 // Append forum filters in site wide activity streams
148 add_action( 'bp_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
149
150 // Append forum filters in single member activity streams
151 add_action( 'bp_member_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
152
153 // Append forum filters in single group activity streams
154 add_action( 'bp_group_activity_filter_options', array( $this, 'activity_filter_options' ), 10 );
155 }
156
157 /**
158 * Setup the filters
159 *
160 * @since bbPress (r3395)
161 * @access private
162 * @uses add_filter() To add various filters
163 * @uses add_action() To add various actions
164 */
165 private function setup_filters() {
166
167 /** Activity **********************************************************/
168
169 // Obey BuddyPress commenting rules
170 add_filter( 'bp_activity_can_comment', array( $this, 'activity_can_comment' ) );
171
172 // Link directly to the topic or reply
173 add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
174
175 /** Mentions **********************************************************/
176
177 // Convert mentions into links on create
178 add_filter( 'bbp_new_topic_pre_content', 'bp_activity_at_name_filter' );
179 add_filter( 'bbp_new_reply_pre_content', 'bp_activity_at_name_filter' );
180
181 // Convert mentions into links on edit
182 add_filter( 'bbp_edit_topic_pre_content', 'bp_activity_at_name_filter' );
183 add_filter( 'bbp_edit_reply_pre_content', 'bp_activity_at_name_filter' );
184
185 // Revert links into text on edit
186 add_filter( 'bbp_get_form_topic_content', array( $this, 'strip_mentions_on_edit' ) );
187 add_filter( 'bbp_get_form_reply_content', array( $this, 'strip_mentions_on_edit' ) );
188 }
189
190 /**
191 * Allow the variables, actions, and filters to be modified by third party
192 * plugins and themes.
193 *
194 * @since bbPress (r3902)
195 */
196 private function fully_loaded() {
197 do_action_ref_array( 'bbp_buddypress_activity_loaded', array( $this ) );
198 }
199
200 /** Methods ***************************************************************/
201
202 /**
203 * Strip out BuddyPress activity at-name HTML on topic/reply edit
204 *
205 * Copied from bp_forums_strip_mentions_on_post_edit() in case forums
206 * component is not active or is not loaded in yet.
207 *
208 * @since bbPress (r3475)
209 * @param type $content Optional
210 * @uses bp_get_root_domain()
211 * @uses bp_get_members_root_slug()
212 * @return string
213 */
214 public function strip_mentions_on_edit( $content = '' ) {
215
216 // Backwards compat for members root slug
217 if ( function_exists( 'bp_get_members_root_slug' ) ) {
218 $members_root = bp_get_members_root_slug();
219 } elseif ( defined( 'BP_MEMBERS_SLUG' ) ) {
220 $members_root = BP_MEMBERS_SLUG;
221 } else {
222 $members_root = 'members';
223 }
224
225 $pattern = "|<a href=&#039;" . bp_get_root_domain() . "/" . $members_root . "/[A-Za-z0-9-_\.]+/&#039; rel=&#039;nofollow&#039;>(@[A-Za-z0-9-_\.@]+)</a>|";
226 $content = preg_replace( $pattern, "$1", htmlspecialchars_decode( $content ) );
227
228 return $content;
229 }
230
231 /**
232 * Register our activity actions with BuddyPress
233 *
234 * @since bbPress (r3395)
235 * @uses bp_activity_set_action()
236 */
237 public function register_activity_actions() {
238 bp_activity_set_action( $this->component, $this->topic_create, __( 'New topic created', 'bbpress' ) );
239 bp_activity_set_action( $this->component, $this->reply_create, __( 'New reply created', 'bbpress' ) );
240 }
241
242 /**
243 * Wrapper for recoding bbPress actions to the BuddyPress activity stream
244 *
245 * @since bbPress (r3395)
246 * @param type $args Array of arguments for bp_activity_add()
247 * @uses bbp_get_current_user_id()
248 * @uses bp_core_current_time()
249 * @uses bbp_parse_args()
250 * @uses aplly_filters()
251 * @uses bp_activity_add()
252 * @return type Activity ID if successful, false if not
253 */
254 private function record_activity( $args = '' ) {
255
256 // Default activity args
257 $defaults = array (
258 'id' => null,
259 'user_id' => bbp_get_current_user_id(),
260 'type' => '',
261 'action' => '',
262 'item_id' => '',
263 'secondary_item_id' => '',
264 'content' => '',
265 'primary_link' => '',
266 'component' => $this->component,
267 'recorded_time' => bp_core_current_time(),
268 'hide_sitewide' => false
269 );
270 $activity = bbp_parse_args( $args, $defaults, 'record_activity' );
271
272 // Add the activity
273 return bp_activity_add( $activity );
274 }
275
276 /**
277 * Wrapper for deleting bbPress actions from BuddyPress activity stream
278 *
279 * @since bbPress (r3395)
280 * @param type $args Array of arguments for bp_activity_add()
281 * @uses bbp_get_current_user_id()
282 * @uses bp_core_current_time()
283 * @uses bbp_parse_args()
284 * @uses aplly_filters()
285 * @uses bp_activity_add()
286 * @return type Activity ID if successful, false if not
287 */
288 public function delete_activity( $args = '' ) {
289
290 // Default activity args
291 $defaults = array(
292 'item_id' => false,
293 'component' => $this->component,
294 'type' => false,
295 'user_id' => false,
296 'secondary_item_id' => false
297 );
298 $activity = bbp_parse_args( $args, $defaults, 'delete_activity' );
299
300 // Delete the activity
301 bp_activity_delete_by_item_id( $activity );
302 }
303
304 /**
305 * Check for an existing activity stream entry for a given post_id
306 *
307 * @param int $post_id ID of the topic or reply
308 * @uses get_post_meta()
309 * @uses bp_activity_get_specific()
310 * @return int if an activity id is verified, false if not
311 */
312 private static function get_activity_id( $post_id = 0 ) {
313
314 // Try to get the activity ID of the post
315 $activity_id = (int) get_post_meta( $post_id, '_bbp_activity_id', true );
316
317 // Bail if no activity ID is in post meta
318 if ( empty( $activity_id ) )
319 return null;
320
321 // Get the activity stream item, bail if it doesn't exist
322 $existing = bp_activity_get_specific( array( 'activity_ids' => $activity_id, 'show_hidden' => true, 'spam' => 'all', ) );
323 if ( empty( $existing['total'] ) || ( 1 != $existing['total'] ) )
324 return null;
325
326 // Return the activity ID since we've verified the connection
327 return $activity_id;
328 }
329
330 /**
331 * Maybe disable activity stream comments on select actions
332 *
333 * @since bbPress (r3399)
334 * @global BP_Activity_Template $activities_template
335 * @param boolean $can_comment
336 * @uses bp_get_activity_action_name()
337 * @return boolean
338 */
339 public function activity_can_comment( $can_comment = true ) {
340 global $activities_template;
341
342 // Already forced off, so comply
343 if ( false === $can_comment )
344 return $can_comment;
345
346 // Check if blog & forum activity stream commenting is off
347 if ( ( false === $activities_template->disable_blogforum_replies ) || (int) $activities_template->disable_blogforum_replies ) {
348
349 // Get the current action name
350 $action_name = bp_get_activity_action_name();
351
352 // Setup the array of possibly disabled actions
353 $disabled_actions = array(
354 $this->topic_create,
355 $this->reply_create
356 );
357
358 // Check if this activity stream action is disabled
359 if ( in_array( $action_name, $disabled_actions ) ) {
360 $can_comment = false;
361 }
362 }
363
364 return $can_comment;
365 }
366
367 /**
368 * Maybe link directly to topics and replies in activity stream entries
369 *
370 * @since bbPress (r3399)
371 * @param string $link
372 * @param mixed $activity_object
373 * @return string The link to the activity stream item
374 */
375 public function activity_get_permalink( $link = '', $activity_object = false ) {
376
377 // Setup the array of actions to link directly to
378 $disabled_actions = array(
379 $this->topic_create,
380 $this->reply_create
381 );
382
383 // Check if this activity stream action is directly linked
384 if ( in_array( $activity_object->type, $disabled_actions ) ) {
385 $link = $activity_object->primary_link;
386 }
387
388 return $link;
389 }
390
391 /**
392 * Append forum options to activity filter select box
393 *
394 * @since bbPress (r3653)
395 */
396 function activity_filter_options() {
397 ?>
398
399 <option value="<?php echo $this->topic_create; ?>"><?php _e( 'Topics', 'bbpress' ); ?></option>
400 <option value="<?php echo $this->reply_create; ?>"><?php _e( 'Replies', 'bbpress' ); ?></option>
401
402 <?php
403 }
404
405 /** Topics ****************************************************************/
406
407 /**
408 * Record an activity stream entry when a topic is created or updated
409 *
410 * @since bbPress (r3395)
411 * @param int $topic_id
412 * @param int $forum_id
413 * @param array $anonymous_data
414 * @param int $topic_author_id
415 * @uses bbp_get_topic_id()
416 * @uses bbp_get_forum_id()
417 * @uses bbp_get_user_profile_link()
418 * @uses bbp_get_topic_permalink()
419 * @uses bbp_get_topic_title()
420 * @uses bbp_get_topic_content()
421 * @uses bbp_get_forum_permalink()
422 * @uses bbp_get_forum_title()
423 * @uses bp_create_excerpt()
424 * @uses apply_filters()
425 * @return Bail early if topic is by anonymous user
426 */
427 public function topic_create( $topic_id, $forum_id, $anonymous_data, $topic_author_id ) {
428
429 // Bail early if topic is by anonymous user
430 if ( !empty( $anonymous_data ) )
431 return;
432
433 // Bail if site is private
434 if ( !bbp_is_site_public() )
435 return;
436
437 // Validate activity data
438 $user_id = $topic_author_id;
439 $topic_id = bbp_get_topic_id( $topic_id );
440 $forum_id = bbp_get_forum_id( $forum_id );
441
442 // Bail if user is not active
443 if ( bbp_is_user_inactive( $user_id ) )
444 return;
445
446 // Bail if topic is not published
447 if ( !bbp_is_topic_published( $topic_id ) )
448 return;
449
450 // Bail if forum is not public
451 if ( !bbp_is_forum_public( $forum_id, false ) )
452 return;
453
454 // User link for topic author
455 $user_link = bbp_get_user_profile_link( $user_id );
456
457 // Topic
458 $topic_permalink = bbp_get_topic_permalink( $topic_id );
459 $topic_title = bbp_get_topic_title ( $topic_id );
460 $topic_content = bbp_get_topic_content ( $topic_id );
461 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
462
463 // Forum
464 $forum_permalink = bbp_get_forum_permalink( $forum_id );
465 $forum_title = bbp_get_forum_title ( $forum_id );
466 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
467
468 // Activity action & text
469 $activity_text = sprintf( __( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
470 $activity_action = apply_filters( 'bbp_activity_topic_create', $activity_text, $user_id, $topic_id, $forum_id );
471 $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', bp_create_excerpt( $topic_content ), $topic_content );
472
473 // Compile the activity stream results
474 $activity = array(
475 'id' => $this->get_activity_id( $topic_id ),
476 'user_id' => $user_id,
477 'action' => $activity_action,
478 'content' => $activity_content,
479 'primary_link' => $topic_permalink,
480 'type' => $this->topic_create,
481 'item_id' => $topic_id,
482 'secondary_item_id' => $forum_id,
483 'recorded_time' => get_post_time( 'Y-m-d H:i:s', true, $topic_id ),
484 );
485
486 // Record the activity
487 $activity_id = $this->record_activity( $activity );
488
489 // Add the activity entry ID as a meta value to the topic
490 if ( !empty( $activity_id ) ) {
491 update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
492 }
493 }
494
495 /**
496 * Delete the activity stream entry when a topic is spammed, trashed, or deleted
497 *
498 * @param int $topic_id
499 * @uses bp_activity_delete()
500 */
501 public function topic_delete( $topic_id ) {
502
503 // Get activity ID, bail if it doesn't exist
504 if ( $activity_id = $this->get_activity_id( $topic_id ) )
505 return bp_activity_delete( array( 'id' => $activity_id ) );
506
507 return false;
508 }
509
510 /**
511 * Update the activity stream entry when a topic status changes
512 *
513 * @param int $post_id
514 * @param obj $post
515 * @uses get_post_type()
516 * @uses bbp_get_topic_post_type()
517 * @uses bbp_get_topic_id()
518 * @uses bbp_is_topic_anonymous()
519 * @uses bbp_get_public_status_id()
520 * @uses bbp_get_closed_status_id()
521 * @uses bbp_get_topic_forum_id()
522 * @uses bbp_get_topic_author_id()
523 * @return Bail early if not a topic, or topic is by anonymous user
524 */
525 public function topic_update( $topic_id, $post ) {
526
527 // Bail early if not a topic
528 if ( get_post_type( $post ) != bbp_get_topic_post_type() )
529 return;
530
531 $topic_id = bbp_get_topic_id( $topic_id );
532
533 // Bail early if topic is by anonymous user
534 if ( bbp_is_topic_anonymous( $topic_id ) )
535 return;
536
537 $anonymous_data = array();
538
539 // Action based on new status
540 if ( in_array( $post->post_status, array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ) ) {
541
542 // Validate topic data
543 $forum_id = bbp_get_topic_forum_id( $topic_id );
544 $topic_author_id = bbp_get_topic_author_id( $topic_id );
545
546 $this->topic_create( $topic_id, $forum_id, $anonymous_data, $topic_author_id );
547 } else {
548 $this->topic_delete( $topic_id );
549 }
550 }
551
552 /** Replies ***************************************************************/
553
554 /**
555 * Record an activity stream entry when a reply is created
556 *
557 * @since bbPress (r3395)
558 * @param int $topic_id
559 * @param int $forum_id
560 * @param array $anonymous_data
561 * @param int $topic_author_id
562 * @uses bbp_get_reply_id()
563 * @uses bbp_get_topic_id()
564 * @uses bbp_get_forum_id()
565 * @uses bbp_get_user_profile_link()
566 * @uses bbp_get_reply_url()
567 * @uses bbp_get_reply_content()
568 * @uses bbp_get_topic_permalink()
569 * @uses bbp_get_topic_title()
570 * @uses bbp_get_forum_permalink()
571 * @uses bbp_get_forum_title()
572 * @uses bp_create_excerpt()
573 * @uses apply_filters()
574 * @return Bail early if topic is by anonywous user
575 */
576 public function reply_create( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author_id ) {
577
578 // Do not log activity of anonymous users
579 if ( !empty( $anonymous_data ) )
580 return;
581
582 // Bail if site is private
583 if ( !bbp_is_site_public() )
584 return;
585
586 // Validate activity data
587 $user_id = $reply_author_id;
588 $reply_id = bbp_get_reply_id( $reply_id );
589 $topic_id = bbp_get_topic_id( $topic_id );
590 $forum_id = bbp_get_forum_id( $forum_id );
591
592 // Bail if user is not active
593 if ( bbp_is_user_inactive( $user_id ) )
594 return;
595
596 // Bail if reply is not published
597 if ( !bbp_is_reply_published( $reply_id ) )
598 return;
599
600 // Bail if forum is not public
601 if ( !bbp_is_forum_public( $forum_id, false ) )
602 return;
603
604 // Setup links for activity stream
605 $user_link = bbp_get_user_profile_link( $user_id );
606
607 // Reply
608 $reply_url = bbp_get_reply_url ( $reply_id );
609 $reply_content = bbp_get_reply_content( $reply_id );
610
611 // Topic
612 $topic_permalink = bbp_get_topic_permalink( $topic_id );
613 $topic_title = bbp_get_topic_title ( $topic_id );
614 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
615
616 // Forum
617 $forum_permalink = bbp_get_forum_permalink( $forum_id );
618 $forum_title = bbp_get_forum_title ( $forum_id );
619 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
620
621 // Activity action & text
622 $activity_text = sprintf( __( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
623 $activity_action = apply_filters( 'bbp_activity_reply_create', $activity_text, $user_id, $reply_id, $topic_id );
624 $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', bp_create_excerpt( $reply_content ), $reply_content );
625
626 // Compile the activity stream results
627 $activity = array(
628 'id' => $this->get_activity_id( $reply_id ),
629 'user_id' => $user_id,
630 'action' => $activity_action,
631 'content' => $activity_content,
632 'primary_link' => $reply_url,
633 'type' => $this->reply_create,
634 'item_id' => $reply_id,
635 'secondary_item_id' => $topic_id,
636 'recorded_time' => get_post_time( 'Y-m-d H:i:s', true, $reply_id ),
637 );
638
639 // Record the activity
640 $activity_id = $this->record_activity( $activity );
641
642 // Add the activity entry ID as a meta value to the reply
643 if ( !empty( $activity_id ) ) {
644 update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
645 }
646 }
647
648 /**
649 * Delete the activity stream entry when a reply is spammed, trashed, or deleted
650 *
651 * @param int $reply_id
652 * @uses get_post_meta()
653 * @uses bp_activity_delete()
654 */
655 public function reply_delete( $reply_id ) {
656
657 // Get activity ID, bail if it doesn't exist
658 if ( $activity_id = $this->get_activity_id( $reply_id ) )
659 return bp_activity_delete( array( 'id' => $activity_id ) );
660
661 return false;
662 }
663
664 /**
665 * Update the activity stream entry when a reply status changes
666 *
667 * @param int $post_id
668 * @param obj $post
669 * @uses get_post_type()
670 * @uses bbp_get_reply_post_type()
671 * @uses bbp_get_reply_id()
672 * @uses bbp_is_reply_anonymous()
673 * @uses bbp_get_public_status_id()
674 * @uses bbp_get_closed_status_id()
675 * @uses bbp_get_reply_topic_id()
676 * @uses bbp_get_reply_forum_id()
677 * @uses bbp_get_reply_author_id()
678 * @return Bail early if not a reply, or reply is by anonymous user
679 */
680 public function reply_update( $reply_id, $post ) {
681
682 // Bail early if not a reply
683 if ( get_post_type( $post ) != bbp_get_reply_post_type() )
684 return;
685
686 $reply_id = bbp_get_reply_id( $reply_id );
687
688 // Bail early if reply is by anonymous user
689 if ( bbp_is_reply_anonymous( $reply_id ) )
690 return;
691
692 $anonymous_data = array();
693
694 // Action based on new status
695 if ( $post->post_status == bbp_get_public_status_id() ) {
696
697 // Validate reply data
698 $topic_id = bbp_get_reply_topic_id( $reply_id );
699 $forum_id = bbp_get_reply_forum_id( $reply_id );
700 $reply_author_id = bbp_get_reply_author_id( $reply_id );
701
702 $this->reply_create( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author_id );
703 } else {
704 $this->reply_delete( $reply_id );
705 }
706 }
707 }
708 endif;
709