BP_Activity_Activity

Database interaction class for the BuddyPress activity component.

Defined (1)

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

/bp-activity/classes/class-bp-activity-activity.php  
  1. class BP_Activity_Activity { 
  2.  
  3. /** Properties ************************************************************/ 
  4.  
  5. /** 
  6. * ID of the activity item. 
  7. * @since 1.0.0 
  8. * @var int 
  9. */ 
  10. var $id; 
  11.  
  12. /** 
  13. * ID of the associated item. 
  14. * @since 1.0.0 
  15. * @var int 
  16. */ 
  17. var $item_id; 
  18.  
  19. /** 
  20. * ID of the associated secondary item. 
  21. * @since 1.0.0 
  22. * @var int 
  23. */ 
  24. var $secondary_item_id; 
  25.  
  26. /** 
  27. * ID of user associated with the activity item. 
  28. * @since 1.0.0 
  29. * @var int 
  30. */ 
  31. var $user_id; 
  32.  
  33. /** 
  34. * The primary URL for the activity in RSS feeds. 
  35. * @since 1.0.0 
  36. * @var string 
  37. */ 
  38. var $primary_link; 
  39.  
  40. /** 
  41. * BuddyPress component the activity item relates to. 
  42. * @since 1.2.0 
  43. * @var string 
  44. */ 
  45. var $component; 
  46.  
  47. /** 
  48. * Activity type, eg 'new_blog_post'. 
  49. * @since 1.2.0 
  50. * @var string 
  51. */ 
  52. var $type; 
  53.  
  54. /** 
  55. * Description of the activity, eg 'Alex updated his profile.'. 
  56. * @since 1.2.0 
  57. * @var string 
  58. */ 
  59. var $action; 
  60.  
  61. /** 
  62. * The content of the activity item. 
  63. * @since 1.2.0 
  64. * @var string 
  65. */ 
  66. var $content; 
  67.  
  68. /** 
  69. * The date the activity item was recorded, in 'Y-m-d h:i:s' format. 
  70. * @since 1.0.0 
  71. * @var string 
  72. */ 
  73. var $date_recorded; 
  74.  
  75. /** 
  76. * Whether the item should be hidden in sitewide streams. 
  77. * @since 1.1.0 
  78. * @var int 
  79. */ 
  80. var $hide_sitewide = 0; 
  81.  
  82. /** 
  83. * Node boundary start for activity or activity comment. 
  84. * @since 1.5.0 
  85. * @var int 
  86. */ 
  87. var $mptt_left; 
  88.  
  89. /** 
  90. * Node boundary end for activity or activity comment. 
  91. * @since 1.5.0 
  92. * @var int 
  93. */ 
  94. var $mptt_right; 
  95.  
  96. /** 
  97. * Whether this item is marked as spam. 
  98. * @since 1.6.0 
  99. * @var int 
  100. */ 
  101. var $is_spam; 
  102.  
  103. /** 
  104. * Error holder. 
  105. * @since 2.6.0 
  106. * @var WP_Error 
  107. */ 
  108. public $errors; 
  109.  
  110. /** 
  111. * Error type to return. Either 'bool' or 'wp_error'. 
  112. * @since 2.6.0 
  113. * @var string 
  114. */ 
  115. public $error_type = 'bool'; 
  116.  
  117. /** 
  118. * Constructor method. 
  119. * @since 1.5.0 
  120. * @param int|bool $id Optional. The ID of a specific activity item. 
  121. */ 
  122. public function __construct( $id = false ) { 
  123. // Instantiate errors object. 
  124. $this->errors = new WP_Error; 
  125.  
  126. if ( !empty( $id ) ) { 
  127. $this->id = (int) $id; 
  128. $this->populate(); 
  129.  
  130. /** 
  131. * Populate the object with data about the specific activity item. 
  132. * @since 1.0.0 
  133. */ 
  134. public function populate() { 
  135. global $wpdb; 
  136.  
  137. $row = wp_cache_get( $this->id, 'bp_activity' ); 
  138.  
  139. if ( false === $row ) { 
  140. $bp = buddypress(); 
  141. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE id = %d", $this->id ) ); 
  142.  
  143. wp_cache_set( $this->id, $row, 'bp_activity' ); 
  144.  
  145. if ( empty( $row ) ) { 
  146. $this->id = 0; 
  147. return; 
  148.  
  149. $this->id = (int) $row->id; 
  150. $this->item_id = (int) $row->item_id; 
  151. $this->secondary_item_id = (int) $row->secondary_item_id; 
  152. $this->user_id = (int) $row->user_id; 
  153. $this->primary_link = $row->primary_link; 
  154. $this->component = $row->component; 
  155. $this->type = $row->type; 
  156. $this->action = $row->action; 
  157. $this->content = $row->content; 
  158. $this->date_recorded = $row->date_recorded; 
  159. $this->hide_sitewide = (int) $row->hide_sitewide; 
  160. $this->mptt_left = (int) $row->mptt_left; 
  161. $this->mptt_right = (int) $row->mptt_right; 
  162. $this->is_spam = (int) $row->is_spam; 
  163.  
  164. // Generate dynamic 'action' when possible. 
  165. $action = bp_activity_generate_action_string( $this ); 
  166. if ( false !== $action ) { 
  167. $this->action = $action; 
  168.  
  169. // If no callback is available, use the literal string from 
  170. // the database row. 
  171. } elseif ( ! empty( $row->action ) ) { 
  172. $this->action = $row->action; 
  173.  
  174. // Provide a fallback to avoid PHP notices. 
  175. } else { 
  176. $this->action = ''; 
  177.  
  178. /** 
  179. * Save the activity item to the database. 
  180. * @since 1.0.0 
  181. * @return bool True on success. 
  182. */ 
  183. public function save() { 
  184. global $wpdb; 
  185.  
  186. $bp = buddypress(); 
  187.  
  188. $this->id = apply_filters_ref_array( 'bp_activity_id_before_save', array( $this->id, &$this ) ); 
  189. $this->item_id = apply_filters_ref_array( 'bp_activity_item_id_before_save', array( $this->item_id, &$this ) ); 
  190. $this->secondary_item_id = apply_filters_ref_array( 'bp_activity_secondary_item_id_before_save', array( $this->secondary_item_id, &$this ) ); 
  191. $this->user_id = apply_filters_ref_array( 'bp_activity_user_id_before_save', array( $this->user_id, &$this ) ); 
  192. $this->primary_link = apply_filters_ref_array( 'bp_activity_primary_link_before_save', array( $this->primary_link, &$this ) ); 
  193. $this->component = apply_filters_ref_array( 'bp_activity_component_before_save', array( $this->component, &$this ) ); 
  194. $this->type = apply_filters_ref_array( 'bp_activity_type_before_save', array( $this->type, &$this ) ); 
  195. $this->action = apply_filters_ref_array( 'bp_activity_action_before_save', array( $this->action, &$this ) ); 
  196. $this->content = apply_filters_ref_array( 'bp_activity_content_before_save', array( $this->content, &$this ) ); 
  197. $this->date_recorded = apply_filters_ref_array( 'bp_activity_date_recorded_before_save', array( $this->date_recorded, &$this ) ); 
  198. $this->hide_sitewide = apply_filters_ref_array( 'bp_activity_hide_sitewide_before_save', array( $this->hide_sitewide, &$this ) ); 
  199. $this->mptt_left = apply_filters_ref_array( 'bp_activity_mptt_left_before_save', array( $this->mptt_left, &$this ) ); 
  200. $this->mptt_right = apply_filters_ref_array( 'bp_activity_mptt_right_before_save', array( $this->mptt_right, &$this ) ); 
  201. $this->is_spam = apply_filters_ref_array( 'bp_activity_is_spam_before_save', array( $this->is_spam, &$this ) ); 
  202.  
  203. /** 
  204. * Fires before the current activity item gets saved. 
  205. * Please use this hook to filter the properties above. Each part will be passed in. 
  206. * @since 1.0.0 
  207. * @param BP_Activity_Activity $this Current instance of the activity item being saved. Passed by reference. 
  208. */ 
  209. do_action_ref_array( 'bp_activity_before_save', array( &$this ) ); 
  210.  
  211. if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) { 
  212. return $this->errors; 
  213.  
  214. if ( empty( $this->component ) || empty( $this->type ) ) { 
  215. if ( 'bool' === $this->error_type ) { 
  216. return false; 
  217. } else { 
  218. if ( empty( $this->component ) ) { 
  219. $this->errors->add( 'bp_activity_missing_component' ); 
  220. } else { 
  221. $this->errors->add( 'bp_activity_missing_type' ); 
  222.  
  223. return $this->errors; 
  224.  
  225. if ( empty( $this->primary_link ) ) { 
  226. $this->primary_link = bp_loggedin_user_domain(); 
  227.  
  228. // If we have an existing ID, update the activity item, otherwise insert it. 
  229. if ( ! empty( $this->id ) ) { 
  230. $q = $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET user_id = %d, component = %s, type = %s, action = %s, content = %s, primary_link = %s, date_recorded = %s, item_id = %d, secondary_item_id = %d, hide_sitewide = %d, is_spam = %d WHERE id = %d", $this->user_id, $this->component, $this->type, $this->action, $this->content, $this->primary_link, $this->date_recorded, $this->item_id, $this->secondary_item_id, $this->hide_sitewide, $this->is_spam, $this->id ); 
  231. } else { 
  232. $q = $wpdb->prepare( "INSERT INTO {$bp->activity->table_name} ( user_id, component, type, action, content, primary_link, date_recorded, item_id, secondary_item_id, hide_sitewide, is_spam ) VALUES ( %d, %s, %s, %s, %s, %s, %s, %d, %d, %d, %d )", $this->user_id, $this->component, $this->type, $this->action, $this->content, $this->primary_link, $this->date_recorded, $this->item_id, $this->secondary_item_id, $this->hide_sitewide, $this->is_spam ); 
  233.  
  234. if ( false === $wpdb->query( $q ) ) { 
  235. return false; 
  236.  
  237. // If this is a new activity item, set the $id property. 
  238. if ( empty( $this->id ) ) { 
  239. $this->id = $wpdb->insert_id; 
  240.  
  241. // If an existing activity item, prevent any changes to the content generating new @mention notifications. 
  242. } else { 
  243. add_filter( 'bp_activity_at_name_do_notifications', '__return_false' ); 
  244.  
  245. /** 
  246. * Fires after an activity item has been saved to the database. 
  247. * @since 1.0.0 
  248. * @param BP_Activity_Activity $this Current instance of activity item being saved. Passed by reference. 
  249. */ 
  250. do_action_ref_array( 'bp_activity_after_save', array( &$this ) ); 
  251.  
  252. return true; 
  253.  
  254. /** Static Methods ***************************************************/ 
  255.  
  256. /** 
  257. * Get activity items, as specified by parameters. 
  258. * @since 1.2.0 
  259. * @since 2.4.0 Introduced the `$fields` parameter. 
  260. * @see BP_Activity_Activity::get_filter_sql() for a description of the 
  261. * 'filter' parameter. 
  262. * @see WP_Meta_Query::queries for a description of the 'meta_query' 
  263. * parameter format. 
  264. * @param array $args { 
  265. * An array of arguments. All items are optional. 
  266. * @type int $page Which page of results to fetch. Using page=1 without per_page will result 
  267. * in no pagination. Default: 1. 
  268. * @type int|bool $per_page Number of results per page. Default: 25. 
  269. * @type int|bool $max Maximum number of results to return. Default: false (unlimited). 
  270. * @type string $fields Activity fields to return. Pass 'ids' to get only the activity IDs. 
  271. * 'all' returns full activity objects. 
  272. * @type string $sort ASC or DESC. Default: 'DESC'. 
  273. * @type array $exclude Array of activity IDs to exclude. Default: false. 
  274. * @type array $in Array of ids to limit query by (IN). Default: false. 
  275. * @type array $meta_query Array of meta_query conditions. See WP_Meta_Query::queries. 
  276. * @type array $date_query Array of date_query conditions. See first parameter of 
  277. * WP_Date_Query::__construct(). 
  278. * @type array $filter_query Array of advanced query conditions. See BP_Activity_Query::__construct(). 
  279. * @type string|array $scope Pre-determined set of activity arguments. 
  280. * @type array $filter See BP_Activity_Activity::get_filter_sql(). 
  281. * @type string $search_terms Limit results by a search term. Default: false. 
  282. * @type bool $display_comments Whether to include activity comments. Default: false. 
  283. * @type bool $show_hidden Whether to show items marked hide_sitewide. Default: false. 
  284. * @type string $spam Spam status. Default: 'ham_only'. 
  285. * @type bool $update_meta_cache Whether to pre-fetch metadata for queried activity items. Default: true. 
  286. * @type string|bool $count_total If true, an additional DB query is run to count the total activity items 
  287. * for the query. Default: false. 
  288. * } 
  289. * @return array The array returned has two keys: 
  290. * - 'total' is the count of located activities 
  291. * - 'activities' is an array of the located activities 
  292. */ 
  293. public static function get( $args = array() ) { 
  294. global $wpdb; 
  295.  
  296. // Backward compatibility with old method of passing arguments. 
  297. if ( !is_array( $args ) || func_num_args() > 1 ) { 
  298. _deprecated_argument( __METHOD__, '1.6', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 
  299.  
  300. $old_args_keys = array( 
  301. 0 => 'max',  
  302. 1 => 'page',  
  303. 2 => 'per_page',  
  304. 3 => 'sort',  
  305. 4 => 'search_terms',  
  306. 5 => 'filter',  
  307. 6 => 'display_comments',  
  308. 7 => 'show_hidden',  
  309. 8 => 'exclude',  
  310. 9 => 'in',  
  311. 10 => 'spam' 
  312. ); 
  313.  
  314. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  315.  
  316. $bp = buddypress(); 
  317. $r = wp_parse_args( $args, array( 
  318. 'page' => 1, // The current page. 
  319. 'per_page' => 25, // Activity items per page. 
  320. 'max' => false, // Max number of items to return. 
  321. 'fields' => 'all', // Fields to include. 
  322. 'sort' => 'DESC', // ASC or DESC. 
  323. 'exclude' => false, // Array of ids to exclude. 
  324. 'in' => false, // Array of ids to limit query by (IN). 
  325. 'meta_query' => false, // Filter by activitymeta. 
  326. 'date_query' => false, // Filter by date. 
  327. 'filter_query' => false, // Advanced filtering - see BP_Activity_Query. 
  328. 'filter' => false, // See self::get_filter_sql(). 
  329. 'scope' => false, // Preset activity arguments. 
  330. 'search_terms' => false, // Terms to search by. 
  331. 'display_comments' => false, // Whether to include activity comments. 
  332. 'show_hidden' => false, // Show items marked hide_sitewide. 
  333. 'spam' => 'ham_only', // Spam status. 
  334. 'update_meta_cache' => true, // Whether or not to update meta cache. 
  335. 'count_total' => false, // Whether or not to use count_total. 
  336. ) ); 
  337.  
  338. // Select conditions. 
  339. $select_sql = "SELECT DISTINCT a.id"; 
  340.  
  341. $from_sql = " FROM {$bp->activity->table_name} a"; 
  342.  
  343. $join_sql = ''; 
  344.  
  345. // Where conditions. 
  346. $where_conditions = array(); 
  347.  
  348. // Excluded types. 
  349. $excluded_types = array(); 
  350.  
  351. // Scope takes precedence. 
  352. if ( ! empty( $r['scope'] ) ) { 
  353. $scope_query = self::get_scope_query_sql( $r['scope'], $r ); 
  354.  
  355. // Add our SQL conditions if matches were found. 
  356. if ( ! empty( $scope_query['sql'] ) ) { 
  357. $where_conditions['scope_query_sql'] = $scope_query['sql']; 
  358.  
  359. // Override some arguments if needed. 
  360. if ( ! empty( $scope_query['override'] ) ) { 
  361. $r = array_replace_recursive( $r, $scope_query['override'] ); 
  362.  
  363. // Advanced filtering. 
  364. } elseif ( ! empty( $r['filter_query'] ) ) { 
  365. $filter_query = new BP_Activity_Query( $r['filter_query'] ); 
  366. $sql = $filter_query->get_sql(); 
  367. if ( ! empty( $sql ) ) { 
  368. $where_conditions['filter_query_sql'] = $sql; 
  369.  
  370. // Regular filtering. 
  371. if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) { 
  372. $where_conditions['filter_sql'] = $filter_sql; 
  373.  
  374. // Spam. 
  375. if ( 'ham_only' == $r['spam'] ) { 
  376. $where_conditions['spam_sql'] = 'a.is_spam = 0'; 
  377. } elseif ( 'spam_only' == $r['spam'] ) { 
  378. $where_conditions['spam_sql'] = 'a.is_spam = 1'; 
  379.  
  380. // Searching. 
  381. if ( $r['search_terms'] ) { 
  382. $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%'; 
  383. $where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like ); 
  384.  
  385. // Sorting. 
  386. $sort = $r['sort']; 
  387. if ( $sort != 'ASC' && $sort != 'DESC' ) { 
  388. $sort = 'DESC'; 
  389.  
  390. // Hide Hidden Items? 
  391. if ( ! $r['show_hidden'] ) { 
  392. $where_conditions['hidden_sql'] = "a.hide_sitewide = 0"; 
  393.  
  394. // Exclude specified items. 
  395. if ( ! empty( $r['exclude'] ) ) { 
  396. $exclude = implode( ', ', wp_parse_id_list( $r['exclude'] ) ); 
  397. $where_conditions['exclude'] = "a.id NOT IN ({$exclude})"; 
  398.  
  399. // The specific ids to which you want to limit the query. 
  400. if ( ! empty( $r['in'] ) ) { 
  401. $in = implode( ', ', wp_parse_id_list( $r['in'] ) ); 
  402. $where_conditions['in'] = "a.id IN ({$in})"; 
  403.  
  404. // Process meta_query into SQL. 
  405. $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 
  406.  
  407. if ( ! empty( $meta_query_sql['join'] ) ) { 
  408. $join_sql .= $meta_query_sql['join']; 
  409.  
  410. if ( ! empty( $meta_query_sql['where'] ) ) { 
  411. $where_conditions[] = $meta_query_sql['where']; 
  412.  
  413. // Process date_query into SQL. 
  414. $date_query_sql = self::get_date_query_sql( $r['date_query'] ); 
  415.  
  416. if ( ! empty( $date_query_sql ) ) { 
  417. $where_conditions['date'] = $date_query_sql; 
  418.  
  419. // Alter the query based on whether we want to show activity item 
  420. // comments in the stream like normal comments or threaded below 
  421. // the activity. 
  422. if ( false === $r['display_comments'] || 'threaded' === $r['display_comments'] ) { 
  423. $excluded_types[] = 'activity_comment'; 
  424.  
  425. // Exclude 'last_activity' items unless the 'action' filter has 
  426. // been explicitly set. 
  427. if ( empty( $r['filter']['object'] ) ) { 
  428. $excluded_types[] = 'last_activity'; 
  429.  
  430. // Build the excluded type sql part. 
  431. if ( ! empty( $excluded_types ) ) { 
  432. $not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'"; 
  433. $where_conditions['excluded_types'] = "a.type NOT IN ({$not_in})"; 
  434.  
  435. /** 
  436. * Filters the MySQL WHERE conditions for the Activity items get method. 
  437. * @since 1.9.0 
  438. * @param array $where_conditions Current conditions for MySQL WHERE statement. 
  439. * @param array $r Parsed arguments passed into method. 
  440. * @param string $select_sql Current SELECT MySQL statement at point of execution. 
  441. * @param string $from_sql Current FROM MySQL statement at point of execution. 
  442. * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. 
  443. */ 
  444. $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql ); 
  445.  
  446. // Join the where conditions together. 
  447. $where_sql = 'WHERE ' . join( ' AND ', $where_conditions ); 
  448.  
  449. /** 
  450. * Filter the MySQL JOIN clause for the main activity query. 
  451. * @since 2.5.0 
  452. * @param string $join_sql JOIN clause. 
  453. * @param array $r Method parameters. 
  454. * @param string $select_sql Current SELECT MySQL statement. 
  455. * @param string $from_sql Current FROM MySQL statement. 
  456. * @param string $where_sql Current WHERE MySQL statement. 
  457. */ 
  458. $join_sql = apply_filters( 'bp_activity_get_join_sql', $join_sql, $r, $select_sql, $from_sql, $where_sql ); 
  459.  
  460. /** 
  461. * Filters the preferred order of indexes for activity item. 
  462. * @since 1.6.0 
  463. * @param array $value Array of indexes in preferred order. 
  464. */ 
  465. $indexes = apply_filters( 'bp_activity_preferred_index_order', array( 'user_id', 'item_id', 'secondary_item_id', 'date_recorded', 'component', 'type', 'hide_sitewide', 'is_spam' ) ); 
  466.  
  467. foreach( $indexes as $key => $index ) { 
  468. if ( false !== strpos( $where_sql, $index ) ) { 
  469. $the_index = $index; 
  470. break; // Take the first one we find. 
  471.  
  472. if ( !empty( $the_index ) ) { 
  473. $index_hint_sql = "USE INDEX ({$the_index})"; 
  474. } else { 
  475. $index_hint_sql = ''; 
  476.  
  477. // Sanitize page and per_page parameters. 
  478. $page = absint( $r['page'] ); 
  479. $per_page = absint( $r['per_page'] ); 
  480.  
  481. $retval = array( 
  482. 'activities' => null,  
  483. 'total' => null,  
  484. 'has_more_items' => null,  
  485. ); 
  486.  
  487. /** 
  488. * Filters if BuddyPress should use legacy query structure over current structure for version 2.0+. 
  489. * It is not recommended to use the legacy structure, but allowed to if needed. 
  490. * @since 2.0.0 
  491. * @param bool $value Whether to use legacy structure or not. 
  492. * @param BP_Activity_Activity $value Current method being called. 
  493. * @param array $r Parsed arguments passed into method. 
  494. */ 
  495. if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) { 
  496.  
  497. // Legacy queries joined against the user table. 
  498. $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name"; 
  499. $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID"; 
  500.  
  501. if ( ! empty( $page ) && ! empty( $per_page ) ) { 
  502. $pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page ); 
  503.  
  504. /** This filter is documented in bp-activity/bp-activity-classes.php */ 
  505. $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) ); 
  506. } else { 
  507. $pag_sql = ''; 
  508.  
  509. /** 
  510. * Filters the legacy MySQL query statement so plugins can alter before results are fetched. 
  511. * @since 1.5.0 
  512. * @param string $value Concatenated MySQL statement pieces to be query results with for legacy query. 
  513. * @param string $select_sql Final SELECT MySQL statement portion for legacy query. 
  514. * @param string $from_sql Final FROM MySQL statement portion for legacy query. 
  515. * @param string $where_sql Final WHERE MySQL statement portion for legacy query. 
  516. * @param string $sort Final sort direction for legacy query. 
  517. */ 
  518. $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) ); 
  519.  
  520. // Integer casting for legacy activity query. 
  521. foreach ( (array) $activities as $i => $ac ) { 
  522. $activities[ $i ]->id = (int) $ac->id; 
  523. $activities[ $i ]->item_id = (int) $ac->item_id; 
  524. $activities[ $i ]->secondary_item_id = (int) $ac->secondary_item_id; 
  525. $activities[ $i ]->user_id = (int) $ac->user_id; 
  526. $activities[ $i ]->hide_sitewide = (int) $ac->hide_sitewide; 
  527. $activities[ $i ]->mptt_left = (int) $ac->mptt_left; 
  528. $activities[ $i ]->mptt_right = (int) $ac->mptt_right; 
  529. $activities[ $i ]->is_spam = (int) $ac->is_spam; 
  530.  
  531. } else { 
  532. // Query first for activity IDs. 
  533. $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, a.id {$sort}"; 
  534.  
  535. if ( ! empty( $per_page ) && ! empty( $page ) ) { 
  536. // We query for $per_page + 1 items in order to 
  537. // populate the has_more_items flag. 
  538. $activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 ); 
  539.  
  540. /** 
  541. * Filters the paged activities MySQL statement. 
  542. * @since 2.0.0 
  543. * @param string $activity_ids_sql MySQL statement used to query for Activity IDs. 
  544. * @param array $r Array of arguments passed into method. 
  545. */ 
  546. $activity_ids_sql = apply_filters( 'bp_activity_paged_activities_sql', $activity_ids_sql, $r ); 
  547.  
  548. /** 
  549. * Queries that include 'last_activity' are cached separately,  
  550. * since they are generally much less long-lived. 
  551. */ 
  552. if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_ids_sql ) ) { 
  553. $cache_group = 'bp_activity'; 
  554. } else { 
  555. $cache_group = 'bp_activity_with_last_activity'; 
  556.  
  557. $cached = bp_core_get_incremented_cache( $activity_ids_sql, $cache_group ); 
  558. if ( false === $cached ) { 
  559. $activity_ids = $wpdb->get_col( $activity_ids_sql ); 
  560. bp_core_set_incremented_cache( $activity_ids_sql, $cache_group, $activity_ids ); 
  561. } else { 
  562. $activity_ids = $cached; 
  563.  
  564. $retval['has_more_items'] = ! empty( $per_page ) && count( $activity_ids ) > $per_page; 
  565.  
  566. // If we've fetched more than the $per_page value, we 
  567. // can discard the extra now. 
  568. if ( ! empty( $per_page ) && count( $activity_ids ) === $per_page + 1 ) { 
  569. array_pop( $activity_ids ); 
  570.  
  571. if ( 'ids' === $r['fields'] ) { 
  572. $activities = array_map( 'intval', $activity_ids ); 
  573. } else { 
  574. $activities = self::get_activity_data( $activity_ids ); 
  575.  
  576. if ( 'ids' !== $r['fields'] ) { 
  577. // Get the fullnames of users so we don't have to query in the loop. 
  578. $activities = self::append_user_fullnames( $activities ); 
  579.  
  580. // Get activity meta. 
  581. $activity_ids = array(); 
  582. foreach ( (array) $activities as $activity ) { 
  583. $activity_ids[] = $activity->id; 
  584.  
  585. if ( ! empty( $activity_ids ) && $r['update_meta_cache'] ) { 
  586. bp_activity_update_meta_cache( $activity_ids ); 
  587.  
  588. if ( $activities && $r['display_comments'] ) { 
  589. $activities = BP_Activity_Activity::append_comments( $activities, $r['spam'] ); 
  590.  
  591. // Pre-fetch data associated with activity users and other objects. 
  592. BP_Activity_Activity::prefetch_object_data( $activities ); 
  593.  
  594. // Generate action strings. 
  595. $activities = BP_Activity_Activity::generate_action_strings( $activities ); 
  596.  
  597. $retval['activities'] = $activities; 
  598.  
  599. // If $max is set, only return up to the max results. 
  600. if ( ! empty( $r['count_total'] ) ) { 
  601.  
  602. /** 
  603. * Filters the total activities MySQL statement. 
  604. * @since 1.5.0 
  605. * @param string $value MySQL statement used to query for total activities. 
  606. * @param string $where_sql MySQL WHERE statement portion. 
  607. * @param string $sort Sort direction for query. 
  608. */ 
  609. $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$join_sql} {$where_sql}", $where_sql, $sort ); 
  610. $cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group ); 
  611. if ( false === $cached ) { 
  612. $total_activities = $wpdb->get_var( $total_activities_sql ); 
  613. bp_core_set_incremented_cache( $total_activities_sql, $cache_group, $total_activities ); 
  614. } else { 
  615. $total_activities = $cached; 
  616.  
  617. if ( !empty( $r['max'] ) ) { 
  618. if ( (int) $total_activities > (int) $r['max'] ) { 
  619. $total_activities = $r['max']; 
  620.  
  621. $retval['total'] = $total_activities; 
  622.  
  623. return $retval; 
  624.  
  625. /** 
  626. * Convert activity IDs to activity objects, as expected in template loop. 
  627. * @since 2.0.0 
  628. * @param array $activity_ids Array of activity IDs. 
  629. * @return array 
  630. */ 
  631. protected static function get_activity_data( $activity_ids = array() ) { 
  632. global $wpdb; 
  633.  
  634. // Bail if no activity ID's passed. 
  635. if ( empty( $activity_ids ) ) { 
  636. return array(); 
  637.  
  638. // Get BuddyPress. 
  639. $bp = buddypress(); 
  640.  
  641. $activities = array(); 
  642. $uncached_ids = bp_get_non_cached_ids( $activity_ids, 'bp_activity' ); 
  643.  
  644. // Prime caches as necessary. 
  645. if ( ! empty( $uncached_ids ) ) { 
  646. // Format the activity ID's for use in the query below. 
  647. $uncached_ids_sql = implode( ', ', wp_parse_id_list( $uncached_ids ) ); 
  648.  
  649. // Fetch data from activity table, preserving order. 
  650. $queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$uncached_ids_sql})"); 
  651.  
  652. // Put that data into the placeholders created earlier,  
  653. // and add it to the cache. 
  654. foreach ( (array) $queried_adata as $adata ) { 
  655. wp_cache_set( $adata->id, $adata, 'bp_activity' ); 
  656.  
  657. // Now fetch data from the cache. 
  658. foreach ( $activity_ids as $activity_id ) { 
  659. // Integer casting. 
  660. $activity = wp_cache_get( $activity_id, 'bp_activity' ); 
  661. if ( ! empty( $activity ) ) { 
  662. $activity->id = (int) $activity->id; 
  663. $activity->user_id = (int) $activity->user_id; 
  664. $activity->item_id = (int) $activity->item_id; 
  665. $activity->secondary_item_id = (int) $activity->secondary_item_id; 
  666. $activity->hide_sitewide = (int) $activity->hide_sitewide; 
  667. $activity->mptt_left = (int) $activity->mptt_left; 
  668. $activity->mptt_right = (int) $activity->mptt_right; 
  669. $activity->is_spam = (int) $activity->is_spam; 
  670.  
  671. $activities[] = $activity; 
  672.  
  673. // Then fetch user data. 
  674. $user_query = new BP_User_Query( array( 
  675. 'user_ids' => wp_list_pluck( $activities, 'user_id' ),  
  676. 'populate_extras' => false,  
  677. ) ); 
  678.  
  679. // Associated located user data with activity items. 
  680. foreach ( $activities as $a_index => $a_item ) { 
  681. $a_user_id = intval( $a_item->user_id ); 
  682. $a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : ''; 
  683.  
  684. if ( !empty( $a_user ) ) { 
  685. $activities[ $a_index ]->user_email = $a_user->user_email; 
  686. $activities[ $a_index ]->user_nicename = $a_user->user_nicename; 
  687. $activities[ $a_index ]->user_login = $a_user->user_login; 
  688. $activities[ $a_index ]->display_name = $a_user->display_name; 
  689.  
  690. return $activities; 
  691.  
  692. /** 
  693. * Append xProfile fullnames to an activity array. 
  694. * @since 2.0.0 
  695. * @param array $activities Activities array. 
  696. * @return array 
  697. */ 
  698. protected static function append_user_fullnames( $activities ) { 
  699.  
  700. if ( bp_is_active( 'xprofile' ) && ! empty( $activities ) ) { 
  701. $activity_user_ids = wp_list_pluck( $activities, 'user_id' ); 
  702.  
  703. if ( ! empty( $activity_user_ids ) ) { 
  704. $fullnames = bp_core_get_user_displaynames( $activity_user_ids ); 
  705. if ( ! empty( $fullnames ) ) { 
  706. foreach ( (array) $activities as $i => $activity ) { 
  707. if ( ! empty( $fullnames[ $activity->user_id ] ) ) { 
  708. $activities[ $i ]->user_fullname = $fullnames[ $activity->user_id ]; 
  709.  
  710. return $activities; 
  711.  
  712. /** 
  713. * Pre-fetch data for objects associated with activity items. 
  714. * Activity items are associated with users, and often with other 
  715. * BuddyPress data objects. Here, we pre-fetch data about these 
  716. * associated objects, so that inline lookups - done primarily when 
  717. * building action strings - do not result in excess database queries. 
  718. * The only object data required for activity component activity types 
  719. * (activity_update and activity_comment) is related to users, and that 
  720. * info is fetched separately in BP_Activity_Activity::get_activity_data(). 
  721. * So this method contains nothing but a filter that allows other 
  722. * components, such as bp-friends and bp-groups, to hook in and prime 
  723. * their own caches at the beginning of an activity loop. 
  724. * @since 2.0.0 
  725. * @param array $activities Array of activities. 
  726. * @return array $activities Array of activities. 
  727. */ 
  728. protected static function prefetch_object_data( $activities ) { 
  729.  
  730. /** 
  731. * Filters inside prefetch_object_data method to aid in pre-fetching object data associated with activity item. 
  732. * @since 2.0.0 
  733. * @param array $activities Array of activities. 
  734. */ 
  735. return apply_filters( 'bp_activity_prefetch_object_data', $activities ); 
  736.  
  737. /** 
  738. * Generate action strings for the activities located in BP_Activity_Activity::get(). 
  739. * If no string can be dynamically generated for a given item 
  740. * (typically because the activity type has not been properly 
  741. * registered), the static 'action' value pulled from the database will 
  742. * be left in place. 
  743. * @since 2.0.0 
  744. * @param array $activities Array of activities. 
  745. * @return array 
  746. */ 
  747. protected static function generate_action_strings( $activities ) { 
  748. foreach ( $activities as $key => $activity ) { 
  749. $generated_action = bp_activity_generate_action_string( $activity ); 
  750. if ( false !== $generated_action ) { 
  751. $activity->action = $generated_action; 
  752.  
  753. $activities[ $key ] = $activity; 
  754.  
  755. return $activities; 
  756.  
  757. /** 
  758. * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get(). 
  759. * We use WP_Meta_Query to do the heavy lifting of parsing the 
  760. * meta_query array and creating the necessary SQL clauses. However,  
  761. * since BP_Activity_Activity::get() builds its SQL differently than 
  762. * WP_Query, we have to alter the return value (stripping the leading 
  763. * AND keyword from the 'where' clause). 
  764. * @since 1.8.0 
  765. * @param array $meta_query An array of meta_query filters. See the 
  766. * documentation for WP_Meta_Query for details. 
  767. * @return array $sql_array 'join' and 'where' clauses. 
  768. */ 
  769. public static function get_meta_query_sql( $meta_query = array() ) { 
  770. global $wpdb; 
  771.  
  772. $sql_array = array( 
  773. 'join' => '',  
  774. 'where' => '',  
  775. ); 
  776.  
  777. if ( ! empty( $meta_query ) ) { 
  778. $activity_meta_query = new WP_Meta_Query( $meta_query ); 
  779.  
  780. // WP_Meta_Query expects the table name at 
  781. // $wpdb->activitymeta. 
  782. $wpdb->activitymeta = buddypress()->activity->table_name_meta; 
  783.  
  784. $meta_sql = $activity_meta_query->get_sql( 'activity', 'a', 'id' ); 
  785.  
  786. // Strip the leading AND - BP handles it in get(). 
  787. $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] ); 
  788. $sql_array['join'] = $meta_sql['join']; 
  789.  
  790. return $sql_array; 
  791.  
  792. /** 
  793. * Get the SQL for the 'date_query' param in BP_Activity_Activity::get(). 
  794. * We use BP_Date_Query, which extends WP_Date_Query, to do the heavy lifting 
  795. * of parsing the date_query array and creating the necessary SQL clauses. 
  796. * However, since BP_Activity_Activity::get() builds its SQL differently than 
  797. * WP_Query, we have to alter the return value (stripping the leading AND 
  798. * keyword from the query). 
  799. * @since 2.1.0 
  800. * @param array $date_query An array of date_query parameters. See the 
  801. * documentation for the first parameter of WP_Date_Query. 
  802. * @return string 
  803. */ 
  804. public static function get_date_query_sql( $date_query = array() ) { 
  805. $sql = ''; 
  806.  
  807. // Date query. 
  808. if ( ! empty( $date_query ) && is_array( $date_query ) ) { 
  809. $date_query = new BP_Date_Query( $date_query, 'date_recorded' ); 
  810. $sql = preg_replace( '/^\sAND/', '', $date_query->get_sql() ); 
  811.  
  812. return $sql; 
  813.  
  814. /** 
  815. * Get the SQL for the 'scope' param in BP_Activity_Activity::get(). 
  816. * A scope is a predetermined set of activity arguments. This method is used 
  817. * to grab these activity arguments and override any existing args if needed. 
  818. * Can handle multiple scopes. 
  819. * @since 2.2.0 
  820. * @param mixed $scope The activity scope. Accepts string or array of scopes. 
  821. * @param array $r Current activity arguments. Same as those of BP_Activity_Activity::get(),  
  822. * but merged with defaults. 
  823. * @return array 'sql' WHERE SQL string and 'override' activity args. 
  824. */ 
  825. public static function get_scope_query_sql( $scope = false, $r = array() ) { 
  826.  
  827. // Define arrays for future use. 
  828. $query_args = array(); 
  829. $override = array(); 
  830. $retval = array(); 
  831.  
  832. // Check for array of scopes. 
  833. if ( is_array( $scope ) ) { 
  834. $scopes = $scope; 
  835.  
  836. // Explode a comma separated string of scopes. 
  837. } elseif ( is_string( $scope ) ) { 
  838. $scopes = explode( ', ', $scope ); 
  839.  
  840. // Bail if no scope passed. 
  841. if ( empty( $scopes ) ) { 
  842. return false; 
  843.  
  844. // Helper to easily grab the 'user_id'. 
  845. if ( ! empty( $r['filter']['user_id'] ) ) { 
  846. $r['user_id'] = $r['filter']['user_id']; 
  847.  
  848. // Parse each scope; yes! we handle multiples! 
  849. foreach ( $scopes as $scope ) { 
  850. $scope_args = array(); 
  851.  
  852. /** 
  853. * Plugins can hook here to set their activity arguments for custom scopes. 
  854. * This is a dynamic filter based on the activity scope. eg: 
  855. * - 'bp_activity_set_groups_scope_args' 
  856. * - 'bp_activity_set_friends_scope_args' 
  857. * To see how this filter is used, plugin devs should check out: 
  858. * - bp_groups_filter_activity_scope() - used for 'groups' scope 
  859. * - bp_friends_filter_activity_scope() - used for 'friends' scope 
  860. * @since 2.2.0 
  861. * @param array { 
  862. * Activity query clauses. 
  863. * @type array { 
  864. * Activity arguments for your custom scope. 
  865. * See {@link BP_Activity_Query::_construct()} for more details. 
  866. * } 
  867. * @type array $override Optional. Override existing activity arguments passed by $r. 
  868. * } 
  869. * } 
  870. * @param array $r Current activity arguments passed in BP_Activity_Activity::get(). 
  871. */ 
  872. $scope_args = apply_filters( "bp_activity_set_{$scope}_scope_args", array(), $r ); 
  873.  
  874. if ( ! empty( $scope_args ) ) { 
  875. // Merge override properties from other scopes 
  876. // this might be a problem... 
  877. if ( ! empty( $scope_args['override'] ) ) { 
  878. $override = array_merge( $override, $scope_args['override'] ); 
  879. unset( $scope_args['override'] ); 
  880.  
  881. // Save scope args. 
  882. if ( ! empty( $scope_args ) ) { 
  883. $query_args[] = $scope_args; 
  884.  
  885. if ( ! empty( $query_args ) ) { 
  886. // Set relation to OR. 
  887. $query_args['relation'] = 'OR'; 
  888.  
  889. $query = new BP_Activity_Query( $query_args ); 
  890. $sql = $query->get_sql(); 
  891. if ( ! empty( $sql ) ) { 
  892. $retval['sql'] = $sql; 
  893.  
  894. if ( ! empty( $override ) ) { 
  895. $retval['override'] = $override; 
  896.  
  897. return $retval; 
  898.  
  899. /** 
  900. * In BuddyPress 1.2.x, this was used to retrieve specific activity stream items (for example, on an activity's permalink page). 
  901. * As of 1.5.x, use BP_Activity_Activity::get() with an 'in' parameter instead. 
  902. * @since 1.2.0 
  903. * @deprecated 1.5 
  904. * @deprecated Use BP_Activity_Activity::get() with an 'in' parameter instead. 
  905. * @param mixed $activity_ids Array or comma-separated string of activity IDs to retrieve. 
  906. * @param int|bool $max Maximum number of results to return. (Optional; default is no maximum). 
  907. * @param int $page The set of results that the user is viewing. Used in pagination. (Optional; default is 1). 
  908. * @param int $per_page Specifies how many results per page. Used in pagination. (Optional; default is 25). 
  909. * @param string $sort MySQL column sort; ASC or DESC. (Optional; default is DESC). 
  910. * @param bool $display_comments Retrieve an activity item's associated comments or not. (Optional; default is false). 
  911. * @return array 
  912. */ 
  913. public static function get_specific( $activity_ids, $max = false, $page = 1, $per_page = 25, $sort = 'DESC', $display_comments = false ) { 
  914. _deprecated_function( __FUNCTION__, '1.5', 'Use BP_Activity_Activity::get() with the "in" parameter instead.' ); 
  915. return BP_Activity_Activity::get( $max, $page, $per_page, $sort, false, false, $display_comments, false, false, $activity_ids ); 
  916.  
  917. /** 
  918. * Get the first activity ID that matches a set of criteria. 
  919. * @since 1.2.0 
  920. * @todo Should parameters be optional? 
  921. * @param int $user_id User ID to filter by. 
  922. * @param string $component Component to filter by. 
  923. * @param string $type Activity type to filter by. 
  924. * @param int $item_id Associated item to filter by. 
  925. * @param int $secondary_item_id Secondary associated item to filter by. 
  926. * @param string $action Action to filter by. 
  927. * @param string $content Content to filter by. 
  928. * @param string $date_recorded Date to filter by. 
  929. * @return int|bool Activity ID on success, false if none is found. 
  930. */ 
  931. public static function get_id( $user_id, $component, $type, $item_id, $secondary_item_id, $action, $content, $date_recorded ) { 
  932. global $wpdb; 
  933.  
  934. $bp = buddypress(); 
  935.  
  936. $where_args = false; 
  937.  
  938. if ( ! empty( $user_id ) ) { 
  939. $where_args[] = $wpdb->prepare( "user_id = %d", $user_id ); 
  940.  
  941. if ( ! empty( $component ) ) { 
  942. $where_args[] = $wpdb->prepare( "component = %s", $component ); 
  943.  
  944. if ( ! empty( $type ) ) { 
  945. $where_args[] = $wpdb->prepare( "type = %s", $type ); 
  946.  
  947. if ( ! empty( $item_id ) ) { 
  948. $where_args[] = $wpdb->prepare( "item_id = %d", $item_id ); 
  949.  
  950. if ( ! empty( $secondary_item_id ) ) { 
  951. $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id ); 
  952.  
  953. if ( ! empty( $action ) ) { 
  954. $where_args[] = $wpdb->prepare( "action = %s", $action ); 
  955.  
  956. if ( ! empty( $content ) ) { 
  957. $where_args[] = $wpdb->prepare( "content = %s", $content ); 
  958.  
  959. if ( ! empty( $date_recorded ) ) { 
  960. $where_args[] = $wpdb->prepare( "date_recorded = %s", $date_recorded ); 
  961.  
  962. if ( ! empty( $where_args ) ) { 
  963. $where_sql = 'WHERE ' . join( ' AND ', $where_args ); 
  964. $result = $wpdb->get_var( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" ); 
  965.  
  966. return is_numeric( $result ) ? (int) $result : false; 
  967.  
  968. return false; 
  969.  
  970. /** 
  971. * Delete activity items from the database. 
  972. * To delete a specific activity item, pass an 'id' parameter. 
  973. * Otherwise use the filters. 
  974. * @since 1.2.0 
  975. * @param array $args { 
  976. * @int $id Optional. The ID of a specific item to delete. 
  977. * @string $action Optional. The action to filter by. 
  978. * @string $content Optional. The content to filter by. 
  979. * @string $component Optional. The component name to filter by. 
  980. * @string $type Optional. The activity type to filter by. 
  981. * @string $primary_link Optional. The primary URL to filter by. 
  982. * @int $user_id Optional. The user ID to filter by. 
  983. * @int $item_id Optional. The associated item ID to filter by. 
  984. * @int $secondary_item_id Optional. The secondary associated item ID to filter by. 
  985. * @string $date_recorded Optional. The date to filter by. 
  986. * @int $hide_sitewide Optional. Default: false. 
  987. * } 
  988. * @return array|bool An array of deleted activity IDs on success, false on failure. 
  989. */ 
  990. public static function delete( $args = array() ) { 
  991. global $wpdb; 
  992.  
  993. $bp = buddypress(); 
  994. $r = wp_parse_args( $args, array( 
  995. 'id' => false,  
  996. 'action' => false,  
  997. 'content' => false,  
  998. 'component' => false,  
  999. 'type' => false,  
  1000. 'primary_link' => false,  
  1001. 'user_id' => false,  
  1002. 'item_id' => false,  
  1003. 'secondary_item_id' => false,  
  1004. 'date_recorded' => false,  
  1005. 'hide_sitewide' => false 
  1006. ) ); 
  1007.  
  1008. // Setup empty array from where query arguments. 
  1009. $where_args = array(); 
  1010.  
  1011. // ID. 
  1012. if ( ! empty( $r['id'] ) ) { 
  1013. $where_args[] = $wpdb->prepare( "id = %d", $r['id'] ); 
  1014.  
  1015. // User ID. 
  1016. if ( ! empty( $r['user_id'] ) ) { 
  1017. $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] ); 
  1018.  
  1019. // Action. 
  1020. if ( ! empty( $r['action'] ) ) { 
  1021. $where_args[] = $wpdb->prepare( "action = %s", $r['action'] ); 
  1022.  
  1023. // Content. 
  1024. if ( ! empty( $r['content'] ) ) { 
  1025. $where_args[] = $wpdb->prepare( "content = %s", $r['content'] ); 
  1026.  
  1027. // Component. 
  1028. if ( ! empty( $r['component'] ) ) { 
  1029. $where_args[] = $wpdb->prepare( "component = %s", $r['component'] ); 
  1030.  
  1031. // Type. 
  1032. if ( ! empty( $r['type'] ) ) { 
  1033. $where_args[] = $wpdb->prepare( "type = %s", $r['type'] ); 
  1034.  
  1035. // Primary Link. 
  1036. if ( ! empty( $r['primary_link'] ) ) { 
  1037. $where_args[] = $wpdb->prepare( "primary_link = %s", $r['primary_link'] ); 
  1038.  
  1039. // Item ID. 
  1040. if ( ! empty( $r['item_id'] ) ) { 
  1041. $where_args[] = $wpdb->prepare( "item_id = %d", $r['item_id'] ); 
  1042.  
  1043. // Secondary item ID. 
  1044. if ( ! empty( $r['secondary_item_id'] ) ) { 
  1045. $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $r['secondary_item_id'] ); 
  1046.  
  1047. // Date Recorded. 
  1048. if ( ! empty( $r['date_recorded'] ) ) { 
  1049. $where_args[] = $wpdb->prepare( "date_recorded = %s", $r['date_recorded'] ); 
  1050.  
  1051. // Hidden sitewide. 
  1052. if ( ! empty( $r['hide_sitewide'] ) ) { 
  1053. $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $r['hide_sitewide'] ); 
  1054.  
  1055. // Bail if no where arguments. 
  1056. if ( empty( $where_args ) ) { 
  1057. return false; 
  1058.  
  1059. // Join the where arguments for querying. 
  1060. $where_sql = 'WHERE ' . join( ' AND ', $where_args ); 
  1061.  
  1062. // Fetch all activities being deleted so we can perform more actions. 
  1063. $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} {$where_sql}" ); 
  1064.  
  1065. /** 
  1066. * Action to allow intercepting activity items to be deleted. 
  1067. * @since 2.3.0 
  1068. * @param array $activities Array of activities. 
  1069. * @param array $r Array of parsed arguments. 
  1070. */ 
  1071. do_action_ref_array( 'bp_activity_before_delete', array( $activities, $r ) ); 
  1072.  
  1073. // Attempt to delete activities from the database. 
  1074. $deleted = $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ); 
  1075.  
  1076. // Bail if nothing was deleted. 
  1077. if ( empty( $deleted ) ) { 
  1078. return false; 
  1079.  
  1080. /** 
  1081. * Action to allow intercepting activity items just deleted. 
  1082. * @since 2.3.0 
  1083. * @param array $activities Array of activities. 
  1084. * @param array $r Array of parsed arguments. 
  1085. */ 
  1086. do_action_ref_array( 'bp_activity_after_delete', array( $activities, $r ) ); 
  1087.  
  1088. // Pluck the activity IDs out of the $activities array. 
  1089. $activity_ids = wp_parse_id_list( wp_list_pluck( $activities, 'id' ) ); 
  1090.  
  1091. // Handle accompanying activity comments and meta deletion. 
  1092. if ( ! empty( $activity_ids ) ) { 
  1093.  
  1094. // Delete all activity meta entries for activity items. 
  1095. BP_Activity_Activity::delete_activity_meta_entries( $activity_ids ); 
  1096.  
  1097. // Setup empty array for comments. 
  1098. $comment_ids = array(); 
  1099.  
  1100. // Loop through activity ids and attempt to delete comments. 
  1101. foreach ( $activity_ids as $activity_id ) { 
  1102.  
  1103. // Attempt to delete comments. 
  1104. $comments = BP_Activity_Activity::delete( array( 
  1105. 'type' => 'activity_comment',  
  1106. 'item_id' => $activity_id 
  1107. ) ); 
  1108.  
  1109. // Merge IDs together. 
  1110. if ( ! empty( $comments ) ) { 
  1111. $comment_ids = array_merge( $comment_ids, $comments ); 
  1112.  
  1113. // Merge activity IDs with any deleted comment IDs. 
  1114. if ( ! empty( $comment_ids ) ) { 
  1115. $activity_ids = array_unique( array_merge( $activity_ids, $comment_ids ) ); 
  1116.  
  1117. return $activity_ids; 
  1118.  
  1119. /** 
  1120. * Delete the comments associated with a set of activity items. 
  1121. * This method is no longer used by BuddyPress, and it is recommended not to 
  1122. * use it going forward, and use BP_Activity_Activity::delete() instead. 
  1123. * @since 1.2.0 
  1124. * @deprecated 2.3.0 
  1125. * @param array $activity_ids Activity IDs whose comments should be deleted. 
  1126. * @param bool $delete_meta Should we delete the activity meta items for these comments. 
  1127. * @return bool True on success. 
  1128. */ 
  1129. public static function delete_activity_item_comments( $activity_ids = array(), $delete_meta = true ) { 
  1130. global $wpdb; 
  1131.  
  1132. $bp = buddypress(); 
  1133.  
  1134. $delete_meta = (bool) $delete_meta; 
  1135. $activity_ids = implode( ', ', wp_parse_id_list( $activity_ids ) ); 
  1136.  
  1137. if ( $delete_meta ) { 
  1138. // Fetch the activity comment IDs for our deleted activity items. 
  1139. $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" ); 
  1140.  
  1141. if ( ! empty( $activity_comment_ids ) ) { 
  1142. self::delete_activity_meta_entries( $activity_comment_ids ); 
  1143.  
  1144. return $wpdb->query( "DELETE FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" ); 
  1145.  
  1146. /** 
  1147. * Delete the meta entries associated with a set of activity items. 
  1148. * @since 1.2.0 
  1149. * @param array $activity_ids Activity IDs whose meta should be deleted. 
  1150. * @return bool True on success. 
  1151. */ 
  1152. public static function delete_activity_meta_entries( $activity_ids = array() ) { 
  1153. $activity_ids = wp_parse_id_list( $activity_ids ); 
  1154.  
  1155. foreach ( $activity_ids as $activity_id ) { 
  1156. bp_activity_delete_meta( $activity_id ); 
  1157.  
  1158. return true; 
  1159.  
  1160. /** 
  1161. * Append activity comments to their associated activity items. 
  1162. * @since 1.2.0 
  1163. * @global wpdb $wpdb WordPress database object. 
  1164. * @param array $activities Activities to fetch comments for. 
  1165. * @param string $spam Optional. 'ham_only' (default), 'spam_only' or 'all'. 
  1166. * @return array The updated activities with nested comments. 
  1167. */ 
  1168. public static function append_comments( $activities, $spam = 'ham_only' ) { 
  1169. $activity_comments = array(); 
  1170.  
  1171. // Now fetch the activity comments and parse them into the correct position in the activities array. 
  1172. foreach ( (array) $activities as $activity ) { 
  1173. $top_level_parent_id = 'activity_comment' == $activity->type ? $activity->item_id : 0; 
  1174. $activity_comments[$activity->id] = BP_Activity_Activity::get_activity_comments( $activity->id, $activity->mptt_left, $activity->mptt_right, $spam, $top_level_parent_id ); 
  1175.  
  1176. // Merge the comments with the activity items. 
  1177. foreach ( (array) $activities as $key => $activity ) { 
  1178. if ( isset( $activity_comments[$activity->id] ) ) { 
  1179. $activities[$key]->children = $activity_comments[$activity->id]; 
  1180.  
  1181. return $activities; 
  1182.  
  1183. /** 
  1184. * Get activity comments that are associated with a specific activity ID. 
  1185. * @since 1.2.0 
  1186. * @global wpdb $wpdb WordPress database object. 
  1187. * @param int $activity_id Activity ID to fetch comments for. 
  1188. * @param int $left Left-most node boundary. 
  1189. * @param int $right Right-most node boundary. 
  1190. * @param string $spam Optional. 'ham_only' (default), 'spam_only' or 'all'. 
  1191. * @param int $top_level_parent_id Optional. The id of the root-level parent activity item. 
  1192. * @return array The updated activities with nested comments. 
  1193. */ 
  1194. public static function get_activity_comments( $activity_id, $left, $right, $spam = 'ham_only', $top_level_parent_id = 0 ) { 
  1195. global $wpdb; 
  1196.  
  1197. if ( empty( $top_level_parent_id ) ) { 
  1198. $top_level_parent_id = $activity_id; 
  1199.  
  1200. $comments = wp_cache_get( $activity_id, 'bp_activity_comments' ); 
  1201.  
  1202. // We store the string 'none' to cache the fact that the 
  1203. // activity item has no comments. 
  1204. if ( 'none' === $comments ) { 
  1205. $comments = false; 
  1206.  
  1207. // A true cache miss. 
  1208. } elseif ( empty( $comments ) ) { 
  1209.  
  1210. $bp = buddypress(); 
  1211.  
  1212. // Select the user's fullname with the query. 
  1213. if ( bp_is_active( 'xprofile' ) ) { 
  1214. $fullname_select = ", pd.value as user_fullname"; 
  1215. $fullname_from = ", {$bp->profile->table_name_data} pd "; 
  1216. $fullname_where = "AND pd.user_id = a.user_id AND pd.field_id = 1"; 
  1217.  
  1218. // Prevent debug errors. 
  1219. } else { 
  1220. $fullname_select = $fullname_from = $fullname_where = ''; 
  1221.  
  1222. // Don't retrieve activity comments marked as spam. 
  1223. if ( 'ham_only' == $spam ) { 
  1224. $spam_sql = 'AND a.is_spam = 0'; 
  1225. } elseif ( 'spam_only' == $spam ) { 
  1226. $spam_sql = 'AND a.is_spam = 1'; 
  1227. } else { 
  1228. $spam_sql = ''; 
  1229.  
  1230. // Legacy query - not recommended. 
  1231.  
  1232. /** 
  1233. * Filters if BuddyPress should use the legacy activity query. 
  1234. * @since 2.0.0 
  1235. * @param bool $value Whether or not to use the legacy query. 
  1236. * @param BP_Activity_Activity $value Magic method referring to currently called method. 
  1237. * @param array $func_args Array of the method's argument list. 
  1238. */ 
  1239. if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, func_get_args() ) ) { 
  1240.  
  1241. /** 
  1242. * Filters the MySQL prepared statement for the legacy activity query. 
  1243. * @since 1.5.0 
  1244. * @param string $value Prepared statement for the activity query. 
  1245. * @param int $activity_id Activity ID to fetch comments for. 
  1246. * @param int $left Left-most node boundary. 
  1247. * @param int $right Right-most node boundary. 
  1248. * @param string $spam_sql SQL Statement portion to differentiate between ham or spam. 
  1249. */ 
  1250. $sql = apply_filters( 'bp_activity_comments_user_join_filter', $wpdb->prepare( "SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name{$fullname_select} FROM {$bp->activity->table_name} a, {$wpdb->users} u{$fullname_from} WHERE u.ID = a.user_id {$fullname_where} AND a.type = 'activity_comment' {$spam_sql} AND a.item_id = %d AND a.mptt_left > %d AND a.mptt_left < %d ORDER BY a.date_recorded ASC", $top_level_parent_id, $left, $right ), $activity_id, $left, $right, $spam_sql ); 
  1251.  
  1252. $descendants = $wpdb->get_results( $sql ); 
  1253.  
  1254. // We use the mptt BETWEEN clause to limit returned 
  1255. // descendants to the correct part of the tree. 
  1256. } else { 
  1257. $sql = $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} a WHERE a.type = 'activity_comment' {$spam_sql} AND a.item_id = %d and a.mptt_left > %d AND a.mptt_left < %d ORDER BY a.date_recorded ASC", $top_level_parent_id, $left, $right ); 
  1258.  
  1259. $descendant_ids = $wpdb->get_col( $sql ); 
  1260. $descendants = self::get_activity_data( $descendant_ids ); 
  1261. $descendants = self::append_user_fullnames( $descendants ); 
  1262.  
  1263. $ref = array(); 
  1264.  
  1265. // Loop descendants and build an assoc array. 
  1266. foreach ( (array) $descendants as $d ) { 
  1267. $d->children = array(); 
  1268.  
  1269. // If we have a reference on the parent. 
  1270. if ( isset( $ref[ $d->secondary_item_id ] ) ) { 
  1271. $ref[ $d->secondary_item_id ]->children[ $d->id ] = $d; 
  1272. $ref[ $d->id ] =& $ref[ $d->secondary_item_id ]->children[ $d->id ]; 
  1273.  
  1274. // If we don't have a reference on the parent, put in the root level. 
  1275. } else { 
  1276. $comments[ $d->id ] = $d; 
  1277. $ref[ $d->id ] =& $comments[ $d->id ]; 
  1278.  
  1279. // Calculate depth for each item. 
  1280. foreach ( $ref as &$r ) { 
  1281. $depth = 1; 
  1282. $parent_id = $r->secondary_item_id; 
  1283.  
  1284. while ( $parent_id !== $r->item_id ) { 
  1285. $depth++; 
  1286.  
  1287. // When display_comments=stream, the parent comment may not be part of the 
  1288. // returned results, so we manually fetch it. 
  1289. if ( empty( $ref[ $parent_id ] ) ) { 
  1290. $direct_parent = new BP_Activity_Activity( $parent_id ); 
  1291. if ( isset( $direct_parent->secondary_item_id ) ) { 
  1292. // If the direct parent is not an activity update, that means we've reached 
  1293. // the parent activity item (eg. new_blog_post). 
  1294. if ( 'activity_update' !== $direct_parent->type ) { 
  1295. $parent_id = $r->item_id; 
  1296.  
  1297. } else { 
  1298. $parent_id = $direct_parent->secondary_item_id; 
  1299.  
  1300. } else { 
  1301. // Something went wrong. Short-circuit the depth calculation. 
  1302. $parent_id = $r->item_id; 
  1303. } else { 
  1304. $parent_id = $ref[ $parent_id ]->secondary_item_id; 
  1305. $r->depth = $depth; 
  1306.  
  1307. // If we cache a value of false, it'll count as a cache 
  1308. // miss the next time the activity comments are fetched. 
  1309. // Storing the string 'none' is a hack workaround to 
  1310. // avoid unnecessary queries. 
  1311. if ( false === $comments ) { 
  1312. $cache_value = 'none'; 
  1313. } else { 
  1314. $cache_value = $comments; 
  1315.  
  1316. wp_cache_set( $activity_id, $cache_value, 'bp_activity_comments' ); 
  1317.  
  1318. return $comments; 
  1319.  
  1320. /** 
  1321. * Rebuild nested comment tree under an activity or activity comment. 
  1322. * @since 1.2.0 
  1323. * @global wpdb $wpdb WordPress database object. 
  1324. * @param int $parent_id ID of an activity or activity comment. 
  1325. * @param int $left Node boundary start for activity or activity comment. 
  1326. * @return int Right Node boundary of activity or activity comment. 
  1327. */ 
  1328. public static function rebuild_activity_comment_tree( $parent_id, $left = 1 ) { 
  1329. global $wpdb; 
  1330.  
  1331. $bp = buddypress(); 
  1332.  
  1333. // The right value of this node is the left value + 1. 
  1334. $right = intval( $left + 1 ); 
  1335.  
  1336. // Get all descendants of this node. 
  1337. $comments = BP_Activity_Activity::get_child_comments( $parent_id ); 
  1338. $descendants = wp_list_pluck( $comments, 'id' ); 
  1339.  
  1340. // Loop the descendants and recalculate the left and right values. 
  1341. foreach ( (array) $descendants as $descendant_id ) { 
  1342. $right = BP_Activity_Activity::rebuild_activity_comment_tree( $descendant_id, $right ); 
  1343.  
  1344. // We've got the left value, and now that we've processed the children 
  1345. // of this node we also know the right value. 
  1346. if ( 1 === $left ) { 
  1347. $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE id = %d", $left, $right, $parent_id ) ); 
  1348. } else { 
  1349. $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE type = 'activity_comment' AND id = %d", $left, $right, $parent_id ) ); 
  1350.  
  1351. // Return the right value of this node + 1. 
  1352. return intval( $right + 1 ); 
  1353.  
  1354. /** 
  1355. * Get child comments of an activity or activity comment. 
  1356. * @since 1.2.0 
  1357. * @global wpdb $wpdb WordPress database object. 
  1358. * @param int $parent_id ID of an activity or activity comment. 
  1359. * @return object Numerically indexed array of child comments. 
  1360. */ 
  1361. public static function get_child_comments( $parent_id ) { 
  1362. global $wpdb; 
  1363.  
  1364. $bp = buddypress(); 
  1365.  
  1366. return $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND secondary_item_id = %d", $parent_id ) ); 
  1367.  
  1368. /** 
  1369. * Get a list of components that have recorded activity associated with them. 
  1370. * @since 1.2.0 
  1371. * @param bool $skip_last_activity If true, components will not be 
  1372. * included if the only activity type associated with them is 
  1373. * 'last_activity'. (Since 2.0.0, 'last_activity' is stored in 
  1374. * the activity table, but these items are not full-fledged 
  1375. * activity items.) Default: true. 
  1376. * @return array List of component names. 
  1377. */ 
  1378. public static function get_recorded_components( $skip_last_activity = true ) { 
  1379. global $wpdb; 
  1380.  
  1381. $bp = buddypress(); 
  1382.  
  1383. if ( true === $skip_last_activity ) { 
  1384. $components = $wpdb->get_col( "SELECT DISTINCT component FROM {$bp->activity->table_name} WHERE action != '' AND action != 'last_activity' ORDER BY component ASC" ); 
  1385. } else { 
  1386. $components = $wpdb->get_col( "SELECT DISTINCT component FROM {$bp->activity->table_name} ORDER BY component ASC" ); 
  1387.  
  1388. return $components; 
  1389.  
  1390. /** 
  1391. * Get sitewide activity items for use in an RSS feed. 
  1392. * @since 1.0.0 
  1393. * @param int $limit Optional. Number of items to fetch. Default: 35. 
  1394. * @return array $activity_feed List of activity items, with RSS data added. 
  1395. */ 
  1396. public static function get_sitewide_items_for_feed( $limit = 35 ) { 
  1397. $activities = bp_activity_get_sitewide( array( 'max' => $limit ) ); 
  1398. $activity_feed = array(); 
  1399.  
  1400. for ( $i = 0, $count = count( $activities ); $i < $count; ++$i ) { 
  1401. $title = explode( '<span', $activities[$i]['content'] ); 
  1402. $activity_feed[$i]['title'] = trim( strip_tags( $title[0] ) ); 
  1403. $activity_feed[$i]['link'] = $activities[$i]['primary_link']; 
  1404. $activity_feed[$i]['description'] = @sprintf( $activities[$i]['content'], '' ); 
  1405. $activity_feed[$i]['pubdate'] = $activities[$i]['date_recorded']; 
  1406.  
  1407. return $activity_feed; 
  1408.  
  1409. /** 
  1410. * Create SQL IN clause for filter queries. 
  1411. * @since 1.5.0 
  1412. * @see BP_Activity_Activity::get_filter_sql() 
  1413. * @param string $field The database field. 
  1414. * @param array|bool $items The values for the IN clause, or false when none are found. 
  1415. * @return string|bool 
  1416. */ 
  1417. public static function get_in_operator_sql( $field, $items ) { 
  1418. global $wpdb; 
  1419.  
  1420. // Split items at the comma. 
  1421. if ( ! is_array( $items ) ) { 
  1422. $items = explode( ', ', $items ); 
  1423.  
  1424. // Array of prepared integers or quoted strings. 
  1425. $items_prepared = array(); 
  1426.  
  1427. // Clean up and format each item. 
  1428. foreach ( $items as $item ) { 
  1429. // Clean up the string. 
  1430. $item = trim( $item ); 
  1431. // Pass everything through prepare for security and to safely quote strings. 
  1432. $items_prepared[] = ( is_numeric( $item ) ) ? $wpdb->prepare( '%d', $item ) : $wpdb->prepare( '%s', $item ); 
  1433.  
  1434. // Build IN operator sql syntax. 
  1435. if ( count( $items_prepared ) ) 
  1436. return sprintf( '%s IN ( %s )', trim( $field ), implode( ', ', $items_prepared ) ); 
  1437. else 
  1438. return false; 
  1439.  
  1440. /** 
  1441. * Create filter SQL clauses. 
  1442. * @since 1.5.0 
  1443. * @param array $filter_array { 
  1444. * Fields and values to filter by. 
  1445. * @type array|string|int $user_id User ID(s). 
  1446. * @type array|string $object Corresponds to the 'component' 
  1447. * column in the database. 
  1448. * @type array|string $action Corresponds to the 'type' column 
  1449. * in the database. 
  1450. * @type array|string|int $primary_id Corresponds to the 'item_id' 
  1451. * column in the database. 
  1452. * @type array|string|int $secondary_id Corresponds to the 
  1453. * 'secondary_item_id' column in the database. 
  1454. * @type int $offset Return only those items with an ID greater 
  1455. * than the offset value. 
  1456. * @type string $since Return only those items that have a 
  1457. * date_recorded value greater than a 
  1458. * given MySQL-formatted date. 
  1459. * } 
  1460. * @return string The filter clause, for use in a SQL query. 
  1461. */ 
  1462. public static function get_filter_sql( $filter_array ) { 
  1463.  
  1464. $filter_sql = array(); 
  1465.  
  1466. if ( !empty( $filter_array['user_id'] ) ) { 
  1467. $user_sql = BP_Activity_Activity::get_in_operator_sql( 'a.user_id', $filter_array['user_id'] ); 
  1468. if ( !empty( $user_sql ) ) 
  1469. $filter_sql[] = $user_sql; 
  1470.  
  1471. if ( !empty( $filter_array['object'] ) ) { 
  1472. $object_sql = BP_Activity_Activity::get_in_operator_sql( 'a.component', $filter_array['object'] ); 
  1473. if ( !empty( $object_sql ) ) 
  1474. $filter_sql[] = $object_sql; 
  1475.  
  1476. if ( !empty( $filter_array['action'] ) ) { 
  1477. $action_sql = BP_Activity_Activity::get_in_operator_sql( 'a.type', $filter_array['action'] ); 
  1478. if ( ! empty( $action_sql ) ) 
  1479. $filter_sql[] = $action_sql; 
  1480.  
  1481. if ( !empty( $filter_array['primary_id'] ) ) { 
  1482. $pid_sql = BP_Activity_Activity::get_in_operator_sql( 'a.item_id', $filter_array['primary_id'] ); 
  1483. if ( !empty( $pid_sql ) ) 
  1484. $filter_sql[] = $pid_sql; 
  1485.  
  1486. if ( !empty( $filter_array['secondary_id'] ) ) { 
  1487. $sid_sql = BP_Activity_Activity::get_in_operator_sql( 'a.secondary_item_id', $filter_array['secondary_id'] ); 
  1488. if ( !empty( $sid_sql ) ) 
  1489. $filter_sql[] = $sid_sql; 
  1490.  
  1491. if ( ! empty( $filter_array['offset'] ) ) { 
  1492. $sid_sql = absint( $filter_array['offset'] ); 
  1493. $filter_sql[] = "a.id >= {$sid_sql}"; 
  1494.  
  1495. if ( ! empty( $filter_array['since'] ) ) { 
  1496. // Validate that this is a proper Y-m-d H:i:s date. 
  1497. // Trick: parse to UNIX date then translate back. 
  1498. $translated_date = date( 'Y-m-d H:i:s', strtotime( $filter_array['since'] ) ); 
  1499. if ( $translated_date === $filter_array['since'] ) { 
  1500. $filter_sql[] = "a.date_recorded > '{$translated_date}'"; 
  1501.  
  1502. if ( empty( $filter_sql ) ) 
  1503. return false; 
  1504.  
  1505. return join( ' AND ', $filter_sql ); 
  1506.  
  1507. /** 
  1508. * Get the date/time of last recorded activity. 
  1509. * @since 1.2.0 
  1510. * @return string ISO timestamp. 
  1511. */ 
  1512. public static function get_last_updated() { 
  1513. global $wpdb; 
  1514.  
  1515. $bp = buddypress(); 
  1516.  
  1517. return $wpdb->get_var( "SELECT date_recorded FROM {$bp->activity->table_name} ORDER BY date_recorded DESC LIMIT 1" ); 
  1518.  
  1519. /** 
  1520. * Get favorite count for a given user. 
  1521. * @since 1.2.0 
  1522. * @param int $user_id The ID of the user whose favorites you're counting. 
  1523. * @return int $value A count of the user's favorites. 
  1524. */ 
  1525. public static function total_favorite_count( $user_id ) { 
  1526.  
  1527. // Get activities from user meta. 
  1528. $favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 
  1529. if ( ! empty( $favorite_activity_entries ) ) { 
  1530. return count( maybe_unserialize( $favorite_activity_entries ) ); 
  1531.  
  1532. // No favorites. 
  1533. return 0; 
  1534.  
  1535. /** 
  1536. * Check whether an activity item exists with a given string content. 
  1537. * @since 1.1.0 
  1538. * @param string $content The content to filter by. 
  1539. * @return int|bool The ID of the first matching item if found, otherwise false. 
  1540. */ 
  1541. public static function check_exists_by_content( $content ) { 
  1542. global $wpdb; 
  1543.  
  1544. $bp = buddypress(); 
  1545.  
  1546. $result = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE content = %s", $content ) ); 
  1547.  
  1548. return is_numeric( $result ) ? (int) $result : false; 
  1549.  
  1550. /** 
  1551. * Hide all activity for a given user. 
  1552. * @since 1.2.0 
  1553. * @param int $user_id The ID of the user whose activity you want to mark hidden. 
  1554. * @return mixed 
  1555. */ 
  1556. public static function hide_all_for_user( $user_id ) { 
  1557. global $wpdb; 
  1558.  
  1559. $bp = buddypress(); 
  1560.  
  1561. return $wpdb->get_var( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET hide_sitewide = 1 WHERE user_id = %d", $user_id ) );