PluginProbe
bbPress / 2.0-rc-4
bbPress v2.0-rc-4
2.6.18 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 All 73 releases
bbpress / bbp-includes / bbp-extend-buddypress.php

bbp-extend-buddypress.php in bbPress 2.0-rc-4, at bbp-includes/bbp-extend-buddypress.php

592 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Main bbPress BuddyPress 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' ) ) :
14 /**
15 * Loads BuddyPress extension
16 *
17 * @since bbPress (r3395)
18 *
19 * @package bbPress
20 * @subpackage BuddyPress
21 */
22 class BBP_BuddyPress {
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 /** Functions *************************************************************/
83
84 /**
85 * The main bbPress BuddyPress loader
86 *
87 * @since bbPress (r3395)
88 */
89 function __construct() {
90 $this->setup_globals();
91 $this->setup_actions();
92 $this->setup_filters();
93 }
94
95 /**
96 * Extension variables
97 *
98 * @since bbPress (r3395)
99 * @access private
100 *
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_topic_create';
119 $this->reply_edit = 'bbp_topic_edit';
120 }
121
122 /**
123 * Setup the actions
124 *
125 * @since bbPress (r3395)
126 * @access private
127 *
128 * @uses add_filter() To add various filters
129 * @uses add_action() To add various actions
130 */
131 private function setup_actions() {
132
133 /** Activity **********************************************************/
134
135 // Register the activity stream actions
136 add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
137
138 // Hook into topic creation
139 add_action( 'bbp_new_topic', array( $this, 'topic_create' ), 10, 4 );
140
141 // Hook into reply creation
142 add_action( 'bbp_new_reply', array( $this, 'reply_create' ), 10, 5 );
143 }
144
145 /**
146 * Setup the filters
147 *
148 * @since bbPress (r3395)
149 * @access private
150 *
151 * @uses add_filter() To add various filters
152 * @uses add_action() To add various actions
153 */
154 private function setup_filters() {
155
156 /** Activity **********************************************************/
157
158 // Obey BuddyPress commenting rules
159 add_filter( 'bp_activity_can_comment', array( $this, 'activity_can_comment' ) );
160
161 // Link directly to the topic or reply
162 add_filter( 'bp_activity_get_permalink', array( $this, 'activity_get_permalink' ), 10, 2 );
163
164 /** Profiles **********************************************************/
165
166 // Override bbPress user profile URL with BuddyPress profile URL
167 add_filter( 'bbp_pre_get_user_profile_url', array( $this, 'user_profile_url' ) );
168
169 /** Mentions **********************************************************/
170
171 // Only link mentions if activity component is active
172 if ( bp_is_active( 'activity' ) ) {
173
174 // Convert mentions into links on create
175 add_filter( 'bbp_new_topic_pre_content', 'bp_activity_at_name_filter' );
176 add_filter( 'bbp_new_reply_pre_content', 'bp_activity_at_name_filter' );
177
178 // Convert mentions into links on edit
179 add_filter( 'bbp_edit_topic_pre_content', 'bp_activity_at_name_filter' );
180 add_filter( 'bbp_edit_reply_pre_content', 'bp_activity_at_name_filter' );
181 }
182
183 // Revert links into text on edit
184 add_filter( 'bbp_get_form_topic_content', array( $this, 'strip_mentions_on_edit' ) );
185 add_filter( 'bbp_get_form_reply_content', array( $this, 'strip_mentions_on_edit' ) );
186 }
187
188 /**
189 * Strip out BuddyPress activity at-name HTML on topic/reply edit
190 *
191 * Copied from bp_forums_strip_mentions_on_post_edit() in case forums
192 * component is not active or is not loaded in yet.
193 *
194 * @since bbPress (r3475)
195 *
196 * @param type $content Optional
197 * @uses bp_get_root_domain()
198 * @uses bp_get_members_root_slug()
199 * @return string
200 */
201 public function strip_mentions_on_edit( $content = '' ) {
202
203 // Backwards compat for members root slug
204 if ( function_exists( 'bp_get_members_root_slug' ) )
205 $members_root = bp_get_members_root_slug();
206 elseif ( defined( 'BP_MEMBERS_SLUG' ) )
207 $members_root = BP_MEMBERS_SLUG;
208 else
209 $members_root = 'members';
210
211 $content = htmlspecialchars_decode( $content );
212 $pattern = "|<a href=&#039;" . bp_get_root_domain() . "/" . $members_root . "/[A-Za-z0-9-_\.]+/&#039; rel=&#039;nofollow&#039;>(@[A-Za-z0-9-_\.@]+)</a>|";
213 $content = preg_replace( $pattern, "$1", $content );
214
215 return $content;
216 }
217
218 /**
219 * Register our activity actions with BuddyPress
220 *
221 * @since bbPress (r3395)
222 *
223 * @uses bp_activity_set_action()
224 */
225 public function register_activity_actions() {
226
227 // Topics
228 bp_activity_set_action( $this->component, $this->topic_create, __( 'New topic created', 'bbpress' ) );
229
230 // Replies
231 bp_activity_set_action( $this->component, $this->reply_create, __( 'New reply created', 'bbpress' ) );
232 }
233
234 /**
235 * Wrapper for recoding bbPress actions to the BuddyPress activity stream
236 *
237 * @since bbPress (r3395)
238 *
239 * @param type $args Array of arguments for bp_activity_add()
240 * @uses bbp_get_current_user_id()
241 * @uses bp_core_current_time()
242 * @uses wp_parse_args()
243 * @uses aplly_filters()
244 * @uses bp_activity_add()
245 *
246 * @return type Activity ID if successful, false if not
247 */
248 private function record_activity( $args = '' ) {
249
250 // Bail if activity is not active
251 if ( !bp_is_active( 'activity' ) )
252 return false;
253
254 // Default activity args
255 $defaults = array (
256 'user_id' => bbp_get_current_user_id(),
257 'type' => '',
258 'action' => '',
259 'item_id' => '',
260 'secondary_item_id' => '',
261 'content' => '',
262 'primary_link' => '',
263 'component' => $this->component,
264 'recorded_time' => bp_core_current_time(),
265 'hide_sitewide' => false
266 );
267
268 // Parse the difference
269 $activity = wp_parse_args( $args, $defaults );
270
271 // Just in-time filtering of activity stream contents
272 $activity = apply_filters( 'bbp_record_activity', $activity );
273
274 // Add the activity
275 return bp_activity_add( $activity );
276 }
277
278 /**
279 * Wrapper for deleting bbPress actions from BuddyPress activity stream
280 *
281 * @since bbPress (r3395)
282 *
283 * @param type $args Array of arguments for bp_activity_add()
284 * @uses bbp_get_current_user_id()
285 * @uses bp_core_current_time()
286 * @uses wp_parse_args()
287 * @uses aplly_filters()
288 * @uses bp_activity_add()
289 *
290 * @return type Activity ID if successful, false if not
291 */
292 public function delete_activity( $args = '' ) {
293
294 // Bail if activity is not active
295 if ( !bp_is_active( 'activity' ) )
296 return;
297
298 // Default activity args
299 $defaults = array(
300 'item_id' => false,
301 'component' => $this->component,
302 'type' => false,
303 'user_id' => false,
304 'secondary_item_id' => false
305 );
306
307 // Parse the differenc
308 $activity = wp_parse_args( $args, $defaults );
309
310 // Just in-time filtering of activity stream contents
311 $activity = apply_filters( 'bbp_delete_activity', $activity );
312
313 // Delete the activity
314 bp_activity_delete_by_item_id( $activity );
315 }
316
317 /**
318 * Maybe disable activity stream comments on select actions
319 *
320 * @since bbPress (r3399)
321 *
322 * @global BP_Activity_Template $activities_template
323 * @global BuddyPress $bp
324 * @param boolean $can_comment
325 * @uses bp_get_activity_action_name()
326 * @return boolean
327 */
328 public function activity_can_comment( $can_comment = true ) {
329 global $activities_template, $bp;
330
331 // Already forced off, so comply
332 if ( false === $can_comment )
333 return $can_comment;
334
335 // Check if blog & forum activity stream commenting is off
336 if ( ( false === $activities_template->disable_blogforum_replies ) || (int) $activities_template->disable_blogforum_replies ) {
337
338 // Get the current action name
339 $action_name = bp_get_activity_action_name();
340
341 // Setup the array of possibly disabled actions
342 $disabled_actions = array(
343 $this->topic_create,
344 $this->reply_create
345 );
346
347 // Check if this activity stream action is disabled
348 if ( in_array( $action_name, $disabled_actions ) ) {
349 $can_comment = false;
350 }
351 }
352
353 return $can_comment;
354 }
355
356 /**
357 * Maybe link directly to topics and replies in activity stream entries
358 *
359 * @since bbPress (r3399)
360 *
361 * @param string $link
362 * @param mixed $activity_object
363 *
364 * @return string The link to the activity stream item
365 */
366 public function activity_get_permalink( $link = '', $activity_object = false ) {
367
368 // Setup the array of actions to link directly to
369 $disabled_actions = array(
370 $this->topic_create,
371 $this->reply_create
372 );
373
374 // Check if this activity stream action is directly linked
375 if ( in_array( $activity_object->type, $disabled_actions ) ) {
376 $link = $activity_object->primary_link;
377 }
378
379 return $link;
380 }
381
382 /**
383 * Override bbPress profile URL with BuddyPress profile URL
384 *
385 * @since bbPress (r3401)
386 *
387 * @param string $url
388 * @param int $user_id
389 * @param string $user_nicename
390 *
391 * @return string
392 */
393 public function user_profile_url( $user_id ) {
394 $profile_url = bp_core_get_user_domain( $user_id );
395
396 return $profile_url;
397 }
398
399 /** Topics ****************************************************************/
400
401 /**
402 * Record an activity stream entry when a topic is created
403 *
404 * @since bbPress (r3395)
405 *
406 * @param int $topic_id
407 * @param int $forum_id
408 * @param array $anonymous_data
409 * @param int $topic_author_id
410 *
411 * @uses bbp_get_topic_id()
412 * @uses bbp_get_forum_id()
413 * @uses bbp_get_user_profile_link()
414 * @uses bbp_get_topic_permalink()
415 * @uses bbp_get_topic_title()
416 * @uses bbp_get_topic_content()
417 * @uses bbp_get_forum_permalink()
418 * @uses bbp_get_forum_title()
419 * @uses bp_create_excerpt()
420 * @uses apply_filters()
421 *
422 * @return Bail early if topic is by anonywous user
423 */
424 public function topic_create( $topic_id, $forum_id, $anonymous_data, $topic_author_id ) {
425
426 // Bail early if topic is by anonywous user
427 if ( !empty( $anonymous_data ) )
428 return;
429
430 // Bail if site is private
431 if ( !bbp_is_site_public() )
432 return;
433
434 // Validate activity data
435 $user_id = $topic_author_id;
436 $topic_id = bbp_get_topic_id( $topic_id );
437 $forum_id = bbp_get_forum_id( $forum_id );
438
439 // Bail if forum is not public
440 if ( !bbp_is_forum_public( $forum_id ) )
441 return;
442
443 // User link for topic author
444 $user_link = bbp_get_user_profile_link( $user_id );
445
446 // Topic
447 $topic_permalink = bbp_get_topic_permalink( $topic_id );
448 $topic_title = bbp_get_topic_title ( $topic_id );
449 $topic_content = bbp_get_topic_content ( $topic_id );
450 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
451
452 // Forum
453 $forum_permalink = bbp_get_forum_permalink( $forum_id );
454 $forum_title = bbp_get_forum_title ( $forum_id );
455 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
456
457 // Activity action & text
458 $activity_text = sprintf( __( '%1$s started the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
459 $activity_action = apply_filters( 'bbp_activity_topic_create', $activity_text, $user_id, $topic_id, $forum_id );
460 $activity_content = apply_filters( 'bbp_activity_topic_create_excerpt', bp_create_excerpt( $topic_content ), $topic_content );
461
462 // Compile the activity stream results
463 $activity = array(
464 'user_id' => $user_id,
465 'action' => $activity_action,
466 'content' => $activity_content,
467 'primary_link' => $topic_permalink,
468 'type' => $this->topic_create,
469 'item_id' => $topic_id,
470 'secondary_item_id' => $forum_id,
471 );
472
473 // Record the activity
474 $activity_id = $this->record_activity( $activity );
475
476 // Add the activity entry ID as a meta value to the topic
477 if ( !empty( $activity_id ) ) {
478 update_post_meta( $topic_id, '_bbp_activity_id', $activity_id );
479 }
480 }
481
482 /** Replies ***************************************************************/
483
484 /**
485 * Record an activity stream entry when a reply is created
486 *
487 * @since bbPress (r3395)
488 *
489 * @param int $topic_id
490 * @param int $forum_id
491 * @param array $anonymous_data
492 * @param int $topic_author_id
493 *
494 * @uses bbp_get_reply_id()
495 * @uses bbp_get_topic_id()
496 * @uses bbp_get_forum_id()
497 * @uses bbp_get_user_profile_link()
498 * @uses bbp_get_reply_url()
499 * @uses bbp_get_reply_content()
500 * @uses bbp_get_topic_permalink()
501 * @uses bbp_get_topic_title()
502 * @uses bbp_get_forum_permalink()
503 * @uses bbp_get_forum_title()
504 * @uses bp_create_excerpt()
505 * @uses apply_filters()
506 *
507 * @return Bail early if topic is by anonywous user
508 */
509 public function reply_create( $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author_id ) {
510
511 // Do not log activity of anonymous users
512 if ( !empty( $anonymous_data ) )
513 return;
514
515 // Bail if site is private
516 if ( !bbp_is_site_public() )
517 return;
518
519 // Validate activity data
520 $user_id = $reply_author_id;
521 $reply_id = bbp_get_reply_id( $reply_id );
522 $topic_id = bbp_get_topic_id( $topic_id );
523 $forum_id = bbp_get_forum_id( $forum_id );
524
525 // Bail if forum is not public
526 if ( !bbp_is_forum_public( $forum_id ) )
527 return;
528
529 // Setup links for activity stream
530 $user_link = bbp_get_user_profile_link( $user_id );
531
532 // Reply
533 $reply_url = bbp_get_reply_url ( $reply_id );
534 $reply_content = bbp_get_reply_content( $reply_id );
535
536 // Topic
537 $topic_permalink = bbp_get_topic_permalink( $topic_id );
538 $topic_title = bbp_get_topic_title ( $topic_id );
539 $topic_link = '<a href="' . $topic_permalink . '" title="' . $topic_title . '">' . $topic_title . '</a>';
540
541 // Forum
542 $forum_permalink = bbp_get_forum_permalink( $forum_id );
543 $forum_title = bbp_get_forum_title ( $forum_id );
544 $forum_link = '<a href="' . $forum_permalink . '" title="' . $forum_title . '">' . $forum_title . '</a>';
545
546 // Activity action & text
547 $activity_text = sprintf( __( '%1$s replied to the topic %2$s in the forum %3$s', 'bbpress' ), $user_link, $topic_link, $forum_link );
548 $activity_action = apply_filters( 'bbp_activity_reply_create', $activity_text, $user_id, $reply_id, $topic_id );
549 $activity_content = apply_filters( 'bbp_activity_reply_create_excerpt', bp_create_excerpt( $reply_content ), $reply_content );
550
551 // Compile the activity stream results
552 $activity = array(
553 'user_id' => $user_id,
554 'action' => $activity_action,
555 'content' => $activity_content,
556 'primary_link' => $reply_url,
557 'type' => $this->reply_create,
558 'item_id' => $reply_id,
559 'secondary_item_id' => $topic_id,
560 );
561
562 // Record the activity
563 $activity_id = $this->record_activity( $activity );
564
565 // Add the activity entry ID as a meta value to the reply
566 if ( !empty( $activity_id ) ) {
567 update_post_meta( $reply_id, '_bbp_activity_id', $activity_id );
568 }
569 }
570 }
571 endif;
572
573 /**
574 * Loads BuddyPress inside the bbPress global class
575 *
576 * @since bbPress (r3395)
577 *
578 * @global bbPress $bbp
579 * @return If bbPress is not active
580 */
581 function bbp_setup_buddypress() {
582 global $bbp, $bp;
583
584 // Bail if no BuddyPress
585 if ( !empty( $bp->maintenance_mode ) || !defined( 'BP_VERSION' ) ) return;
586
587 // Instantiate BuddyPress for bbPress
588 $bbp->extend->buddypress = new BBP_BuddyPress();
589 }
590
591 ?>
592