BBP_BuddyPress_Activity

Loads BuddyPress Activity extension.

Defined (1)

The class is defined in the following location(s).

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