PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 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 All 72 releases
bbpress / includes / extend / buddypress / activity.php

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

644 lines 17.1 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 defined( 'ABSPATH' ) || exit;
12
13 if ( ! class_exists( 'BBP_BuddyPress_Activity' ) ) :
14 /**
15 * Loads BuddyPress Activity extension
16 *
17 * @since 2.0.0 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 Activity Action
35 *
36 * @var string
37 */
38 private $forum_create = '';
39
40 /**
41 * Topic Create Activity Action
42 *
43 * @var string
44 */
45 private $topic_create = '';
46
47 /**
48 * Topic Close Activity Action
49 *
50 * @var string
51 */
52 private $topic_close = '';
53
54 /**
55 * Topic Edit Activity Action
56 *
57 * @var string
58 */
59 private $topic_edit = '';
60
61 /**
62 * Topic Open Activity Action
63 *
64 * @var string
65 */
66 private $topic_open = '';
67
68 /**
69 * Reply Create Activity Action
70 *
71 * @var string
72 */
73 private $reply_create = '';
74
75 /**
76 * Reply Edit Activity 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 2.0.0 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 2.0.0 bbPress (r3395)
100 *
101 * @access private
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 2.0.0 bbPress (r3395)
126 *
127 * @access private
128 */
129 private function setup_actions() {
130
131 // Register the activity stream actions
132 add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
133
134 // Hook into topic and reply creation
135 add_action( 'bbp_new_topic', array( $this, 'topic_create' ), 10, 4 );
136 add_action( 'bbp_new_reply', array( $this, 'reply_create' ), 10, 5 );
137
138 // Hook into topic and reply status changes
139 add_action( 'edit_post', array( $this, 'topic_update' ), 10, 2 );
140 add_action( 'edit_post', array( $this, 'reply_update' ), 10, 2 );
141
142 // Hook into topic and reply deletion
143 add_action( 'bbp_delete_topic', array( $this, 'topic_delete' ), 10, 1 );
144 add_action( 'bbp_delete_reply', array( $this, 'reply_delete' ), 10, 1 );
145 }
146
147 /**
148 * Setup the filters
149 *
150 * @since 2.0.0 bbPress (r3395)
151 *
152 * @access private
153 */
154 private function setup_filters() {
155
156 // Obey BuddyPress commenting rules
157 add_filter( 'bp_activity_can_comment', array( $this, 'activity_can_comment' ) );
158
159 // Link directly to the topic or reply
160 add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
161 }
162
163 /**
164 * Allow the variables, actions, and filters to be modified by third party
165 * plugins and themes.
166 *
167 * @since 2.1.0 bbPress (r3902)
168 */
169 private function fully_loaded() {
170 do_action_ref_array( 'bbp_buddypress_activity_loaded', array( $this ) );
171 }
172
173 /** Methods ***************************************************************/
174
175 /**
176 * Register our activity actions with BuddyPress
177 *
178 * @since 2.0.0 bbPress (r3395)
179 */
180 public function register_activity_actions() {
181
182 // Sitewide topic
183 bp_activity_set_action(
184 $this->component,
185 $this->topic_create,
186 esc_html__( 'New forum topic', 'bbpress' ),
187 'bbp_format_activity_action_new_topic',
188 esc_html__( 'Topics', 'bbpress' ),
189 array( 'activity', 'member', 'member_groups', 'group' )
190 );
191
192 // Sitewide reply
193 bp_activity_set_action(
194 $this->component,
195 $this->reply_create,
196 esc_html__( 'New forum reply', 'bbpress' ),
197 'bbp_format_activity_action_new_reply',
198 esc_html__( 'Replies', 'bbpress' ),
199 array( 'activity', 'member', 'member_groups', 'group' )
200 );
201 }
202
203 /**
204 * Wrapper for recoding bbPress actions to the BuddyPress activity stream
205 *
206 * @since 2.0.0 bbPress (r3395)
207 *
208 * @param array $args Array of arguments for bp_activity_add()
209 *
210 * @return int Activity ID if successful, false if not
211 */
212 private function record_activity( $args = array() ) {
213
214 // Default activity args
215 $activity = bbp_parse_args(
216 $args,
217 array(
218 'id' => null,
219 'user_id' => bbp_get_current_user_id(),
220 'type' => '',
221 'action' => '',
222 'item_id' => '',
223 'secondary_item_id' => '',
224 'content' => '',
225 'primary_link' => '',
226 'component' => $this->component,
227 'recorded_time' => bp_core_current_time(),
228 'hide_sitewide' => false
229 ),
230 'record_activity'
231 );
232
233 // Add the activity
234 return bp_activity_add( $activity );
235 }
236
237 /**
238 * Wrapper for deleting bbPress actions from BuddyPress activity stream
239 *
240 * @since 2.0.0 bbPress (r3395)
241 *
242 * @param array $args Array of arguments for bp_activity_add()
243 *
244 * @return int Activity ID if successful, false if not
245 */
246 public function delete_activity( $args = array() ) {
247
248 // Default activity args
249 $activity = bbp_parse_args(
250 $args,
251 array(
252 'item_id' => false,
253 'component' => $this->component,
254 'type' => false,
255 'user_id' => false,
256 'secondary_item_id' => false
257 ),
258 'delete_activity'
259 );
260
261 // Delete the activity
262 bp_activity_delete_by_item_id( $activity );
263 }
264
265 /**
266 * Check for an existing activity stream entry for a given post_id
267 *
268 * @param int $post_id ID of the topic or reply
269 * @return int if an activity id is verified, false if not
270 */
271 private static function get_activity_id( $post_id = 0 ) {
272
273 // Try to get the activity ID of the post
274 $activity_id = (int) get_post_meta( $post_id, '_bbp_activity_id', true );
275
276 // Bail if no activity ID is in post meta
277 if ( empty( $activity_id ) ) {
278 return null;
279 }
280
281 // Get the activity stream item, bail if it doesn't exist
282 $existing = new BP_Activity_Activity( $activity_id );
283 if ( empty( $existing->component ) ) {
284 return null;
285 }
286
287 // Return the activity ID since we've verified the connection
288 return $activity_id;
289 }
290
291 /**
292 * Maybe disable activity stream comments on select actions
293 *
294 * @since 2.0.0 bbPress (r3399)
295 *
296 * @global BP_Activity_Template $activities_template
297 * @param boolean $can_comment
298 * @return boolean
299 */
300 public function activity_can_comment( $can_comment = true ) {
301 global $activities_template;
302
303 // Already forced off, so comply
304 if ( false === $can_comment ) {
305 return $can_comment;
306 }
307
308 // Check if blog & forum activity stream commenting is off
309 if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
310
311 // Get the current action name
312 $action_name = bp_get_activity_action_name();
313
314 // Setup the array of possibly disabled actions
315 $disabled_actions = array(
316 $this->topic_create,
317 $this->reply_create
318 );
319
320 // Check if this activity stream action is disabled
321 if ( in_array( $action_name, $disabled_actions, true ) ) {
322 $can_comment = false;
323 }
324 }
325
326 return $can_comment;
327 }
328
329 /**
330 * Maybe link directly to topics and replies in activity stream entries
331 *
332 * @since 2.0.0 bbPress (r3399)
333 *
334 * @param string $link
335 * @param mixed $activity_object
336 * @return string The link to the activity stream item
337 */
338 public function activity_get_permalink( $link = '', $activity_object = false ) {
339
340 // Setup the array of actions to link directly to
341 $disabled_actions = array(
342 $this->topic_create,
343 $this->reply_create
344 );
345
346 // Check if this activity stream action is directly linked
347 if ( in_array( $activity_object->type, $disabled_actions, true ) ) {
348 $link = $activity_object->primary_link;
349 }
350
351 return $link;
352 }
353
354 /** Topics ****************************************************************/
355
356 /**
357 * Record an activity stream entry when a topic is created or updated
358 *
359 * @since 2.0.0 bbPress (r3395)
360 *
361 * @param int $topic_id
362 * @param int $forum_id
363 * @param array $anonymous_data
364 * @param int $topic_author_id
365 * @return Bail early if topic is by anonymous user
366 */
367 public function topic_create( $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $topic_author_id = 0 ) {
368
369 // Bail early if topic is by anonymous user
370 if ( ! empty( $anonymous_data ) ) {
371 return;
372 }
373
374 // Bail if site is private
375 if ( ! bbp_is_site_public() ) {
376 return;
377 }
378
379 // Validate activity data
380 $user_id = bbp_get_user_id( $topic_author_id );
381 $topic_id = bbp_get_topic_id( $topic_id );
382 $forum_id = bbp_get_forum_id( $forum_id );
383
384 // Bail if user is not active
385 if ( bbp_is_user_inactive( $user_id ) ) {
386 return;
387 }
388
389 // Bail if topic is not published
390 if ( ! bbp_is_topic_published( $topic_id ) ) {
391 return;
392 }
393
394 // User link for topic author
395 $user_link = bbp_get_user_profile_link( $user_id );
396
397 // Topic
398 $topic_permalink = bbp_get_topic_permalink( $topic_id );
399 $topic_title = get_post_field( 'post_title', $topic_id, 'raw' );
400 $topic_content = get_post_field( 'post_content', $topic_id, 'raw' );
401 $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
402
403 // Forum
404 $forum_permalink = bbp_get_forum_permalink( $forum_id );
405 $forum_title = get_post_field( 'post_title', $forum_id, 'raw' );
406 $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
407
408 // Activity action & text
409 $activity_text = sprintf(
410 /* translators: 1: User linked profile, 2: Topic linked title, 3: Forum linked title */
411 esc_html__( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ),
412 $user_link,
413 $topic_link,
414 $forum_link
415 );
416 $activity_action = apply_filters( 'bbp_activity_topic_create', $activity_text, $user_id, $topic_id, $forum_id );
417 $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', $topic_content );
418
419 // Compile and record the activity stream results
420 $activity_id = $this->record_activity(
421 array(
422 'id' => $this->get_activity_id( $topic_id ),
423 'user_id' => $user_id,
424 'action' => $activity_action,
425 'content' => $activity_content,
426 'primary_link' => $topic_permalink,
427 'type' => $this->topic_create,
428 'item_id' => $topic_id,
429 'secondary_item_id' => $forum_id,
430 'recorded_time' => get_post_time( 'Y-m-d H:i:s', true, $topic_id ),
431 'hide_sitewide' => ! bbp_is_forum_public( $forum_id, false )
432 )
433 );
434
435 // Add the activity entry ID as a meta value to the topic
436 if ( ! empty( $activity_id ) ) {
437 update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
438 }
439 }
440
441 /**
442 * Delete the activity stream entry when a topic is spammed, trashed, or deleted
443 *
444 * @param int $topic_id
445 */
446 public function topic_delete( $topic_id = 0 ) {
447
448 // Get activity ID, bail if it doesn't exist
449 $activity_id = $this->get_activity_id( $topic_id );
450 if ( ! empty( $activity_id ) ) {
451 return bp_activity_delete( array( 'id' => $activity_id ) );
452 }
453
454 return false;
455 }
456
457 /**
458 * Update the activity stream entry when a topic status changes
459 *
460 * @param int $topic_id
461 * @param obj $post
462 * @return Bail early if not a topic, or topic is by anonymous user
463 */
464 public function topic_update( $topic_id = 0, $post = null ) {
465
466 // Bail early if not a topic
467 if ( get_post_type( $post ) !== bbp_get_topic_post_type() ) {
468 return;
469 }
470
471 // Bail early if revisions are off
472 if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
473 return;
474 }
475
476 $topic_id = bbp_get_topic_id( $topic_id );
477
478 // Bail early if topic is by anonymous user
479 if ( bbp_is_topic_anonymous( $topic_id ) ) {
480 return;
481 }
482
483 // Action based on new status
484 if ( bbp_is_topic_public( $post->ID ) ) {
485
486 // Validate topic data
487 $forum_id = bbp_get_topic_forum_id( $topic_id );
488 $topic_author_id = bbp_get_topic_author_id( $topic_id );
489
490 $this->topic_create( $topic_id, $forum_id, array(), $topic_author_id );
491 } else {
492 $this->topic_delete( $topic_id );
493 }
494 }
495
496 /** Replies ***************************************************************/
497
498 /**
499 * Record an activity stream entry when a reply is created
500 *
501 * @since 2.0.0 bbPress (r3395)
502 *
503 * @param int $topic_id
504 * @param int $forum_id
505 * @param array $anonymous_data
506 * @param int $topic_author_id
507 * @return Bail early if topic is by anonymous user
508 */
509 public function reply_create( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = array(), $reply_author_id = 0 ) {
510
511 // Do not log activity of anonymous users
512 if ( ! empty( $anonymous_data ) ) {
513 return;
514 }
515
516 // Bail if site is private
517 if ( ! bbp_is_site_public() ) {
518 return;
519 }
520
521 // Validate activity data
522 $user_id = bbp_get_user_id( $reply_author_id );
523 $reply_id = bbp_get_reply_id( $reply_id );
524 $topic_id = bbp_get_topic_id( $topic_id );
525 $forum_id = bbp_get_forum_id( $forum_id );
526
527 // Bail if user is not active
528 if ( bbp_is_user_inactive( $user_id ) ) {
529 return;
530 }
531
532 // Bail if reply is not published
533 if ( ! bbp_is_reply_published( $reply_id ) ) {
534 return;
535 }
536
537 // Setup links for activity stream
538 $user_link = bbp_get_user_profile_link( $user_id );
539
540 // Reply
541 $reply_url = bbp_get_reply_url( $reply_id );
542 $reply_content = get_post_field( 'post_content', $reply_id, 'raw' );
543
544 // Topic
545 $topic_permalink = bbp_get_topic_permalink( $topic_id );
546 $topic_title = get_post_field( 'post_title', $topic_id, 'raw' );
547 $topic_link = '<a href="' . $topic_permalink . '">' . $topic_title . '</a>';
548
549 // Forum
550 $forum_permalink = bbp_get_forum_permalink( $forum_id );
551 $forum_title = get_post_field( 'post_title', $forum_id, 'raw' );
552 $forum_link = '<a href="' . $forum_permalink . '">' . $forum_title . '</a>';
553
554 // Activity action & text
555 $activity_text = sprintf(
556 /* translators: 1: User linked profile, 2: Topic linked title, 3: Forum linked title */
557 esc_html__( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ),
558 $user_link,
559 $topic_link,
560 $forum_link
561 );
562 $activity_action = apply_filters( 'bbp_activity_reply_create', $activity_text, $user_id, $reply_id, $topic_id );
563 $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', $reply_content );
564
565 // Compile and record the activity stream results
566 $activity_id = $this->record_activity(
567 array(
568 'id' => $this->get_activity_id( $reply_id ),
569 'user_id' => $user_id,
570 'action' => $activity_action,
571 'content' => $activity_content,
572 'primary_link' => $reply_url,
573 'type' => $this->reply_create,
574 'item_id' => $reply_id,
575 'secondary_item_id' => $topic_id,
576 'recorded_time' => get_post_time( 'Y-m-d H:i:s', true, $reply_id ),
577 'hide_sitewide' => ! bbp_is_forum_public( $forum_id, false )
578 )
579 );
580
581 // Add the activity entry ID as a meta value to the reply
582 if ( ! empty( $activity_id ) ) {
583 update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
584 }
585 }
586
587 /**
588 * Delete the activity stream entry when a reply is spammed, trashed, or deleted
589 *
590 * @param int $reply_id
591 */
592 public function reply_delete( $reply_id ) {
593
594 // Get activity ID, bail if it doesn't exist
595 $activity_id = $this->get_activity_id( $reply_id );
596 if ( ! empty( $activity_id ) ) {
597 return bp_activity_delete( array( 'id' => $activity_id ) );
598 }
599
600 return false;
601 }
602
603 /**
604 * Update the activity stream entry when a reply status changes
605 *
606 * @param int $reply_id
607 * @param obj $post
608 * @return Bail early if not a reply, or reply is by anonymous user
609 */
610 public function reply_update( $reply_id, $post ) {
611
612 // Bail early if not a reply
613 if ( get_post_type( $post ) !== bbp_get_reply_post_type() ) {
614 return;
615 }
616
617 // Bail early if revisions are off
618 if ( ! bbp_allow_revisions() || ! post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) {
619 return;
620 }
621
622 $reply_id = bbp_get_reply_id( $reply_id );
623
624 // Bail early if reply is by anonymous user
625 if ( bbp_is_reply_anonymous( $reply_id ) ) {
626 return;
627 }
628
629 // Action based on new status
630 if ( bbp_get_public_status_id() === $post->post_status ) {
631
632 // Validate reply data
633 $topic_id = bbp_get_reply_topic_id( $reply_id );
634 $forum_id = bbp_get_reply_forum_id( $reply_id );
635 $reply_author_id = bbp_get_reply_author_id( $reply_id );
636
637 $this->reply_create( $reply_id, $topic_id, $forum_id, array(), $reply_author_id );
638 } else {
639 $this->reply_delete( $reply_id );
640 }
641 }
642 }
643 endif;
644