/
class-bp-activity-activity.php
1902 lines (1615 loc) · 63.7 KB
/
class-bp-activity-activity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* BuddyPress Activity Classes
*
* @package BuddyPress
* @subpackage Activity
* @since 1.0.0
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Database interaction class for the BuddyPress activity component.
* Instance methods are available for creating/editing an activity,
* static methods for querying activities.
*
* @since 1.0.0
*/
class BP_Activity_Activity {
/** Properties ************************************************************/
/**
* ID of the activity item.
*
* @since 1.0.0
* @var int
*/
var $id;
/**
* ID of the associated item.
*
* @since 1.0.0
* @var int
*/
var $item_id;
/**
* ID of the associated secondary item.
*
* @since 1.0.0
* @var int
*/
var $secondary_item_id;
/**
* ID of user associated with the activity item.
*
* @since 1.0.0
* @var int
*/
var $user_id;
/**
* The primary URL for the activity in RSS feeds.
*
* @since 1.0.0
* @var string
*/
var $primary_link;
/**
* BuddyPress component the activity item relates to.
*
* @since 1.2.0
* @var string
*/
var $component;
/**
* Activity type, eg 'new_blog_post'.
*
* @since 1.2.0
* @var string
*/
var $type;
/**
* Description of the activity, eg 'Alex updated his profile.'.
*
* @since 1.2.0
* @var string
*/
var $action;
/**
* The content of the activity item.
*
* @since 1.2.0
* @var string
*/
var $content;
/**
* The date the activity item was recorded, in 'Y-m-d h:i:s' format.
*
* @since 1.0.0
* @var string
*/
var $date_recorded;
/**
* Whether the item should be hidden in sitewide streams.
*
* @since 1.1.0
* @var int
*/
var $hide_sitewide = 0;
/**
* Node boundary start for activity or activity comment.
*
* @since 1.5.0
* @var int
*/
var $mptt_left;
/**
* Node boundary end for activity or activity comment.
*
* @since 1.5.0
* @var int
*/
var $mptt_right;
/**
* Whether this item is marked as spam.
*
* @since 1.6.0
* @var int
*/
var $is_spam;
/**
* Error holder.
*
* @since 2.6.0
*
* @var WP_Error
*/
public $errors;
/**
* Error type to return. Either 'bool' or 'wp_error'.
*
* @since 2.6.0
*
* @var string
*/
public $error_type = 'bool';
/**
* Constructor method.
*
* @since 1.5.0
*
* @param int|bool $id Optional. The ID of a specific activity item.
*/
public function __construct( $id = false ) {
// Instantiate errors object.
$this->errors = new WP_Error;
if ( !empty( $id ) ) {
$this->id = (int) $id;
$this->populate();
}
}
/**
* Populate the object with data about the specific activity item.
*
* @since 1.0.0
*/
public function populate() {
global $wpdb;
$row = wp_cache_get( $this->id, 'bp_activity' );
if ( false === $row ) {
$bp = buddypress();
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE id = %d", $this->id ) );
wp_cache_set( $this->id, $row, 'bp_activity' );
}
if ( empty( $row ) ) {
$this->id = 0;
return;
}
$this->id = (int) $row->id;
$this->item_id = (int) $row->item_id;
$this->secondary_item_id = (int) $row->secondary_item_id;
$this->user_id = (int) $row->user_id;
$this->primary_link = $row->primary_link;
$this->component = $row->component;
$this->type = $row->type;
$this->action = $row->action;
$this->content = $row->content;
$this->date_recorded = $row->date_recorded;
$this->hide_sitewide = (int) $row->hide_sitewide;
$this->mptt_left = (int) $row->mptt_left;
$this->mptt_right = (int) $row->mptt_right;
$this->is_spam = (int) $row->is_spam;
// Generate dynamic 'action' when possible.
$action = bp_activity_generate_action_string( $this );
if ( false !== $action ) {
$this->action = $action;
// If no callback is available, use the literal string from
// the database row.
} elseif ( ! empty( $row->action ) ) {
$this->action = $row->action;
// Provide a fallback to avoid PHP notices.
} else {
$this->action = '';
}
}
/**
* Save the activity item to the database.
*
* @since 1.0.0
*
* @return WP_Error|bool True on success.
*/
public function save() {
global $wpdb;
$bp = buddypress();
$this->id = apply_filters_ref_array( 'bp_activity_id_before_save', array( $this->id, &$this ) );
$this->item_id = apply_filters_ref_array( 'bp_activity_item_id_before_save', array( $this->item_id, &$this ) );
$this->secondary_item_id = apply_filters_ref_array( 'bp_activity_secondary_item_id_before_save', array( $this->secondary_item_id, &$this ) );
$this->user_id = apply_filters_ref_array( 'bp_activity_user_id_before_save', array( $this->user_id, &$this ) );
$this->primary_link = apply_filters_ref_array( 'bp_activity_primary_link_before_save', array( $this->primary_link, &$this ) );
$this->component = apply_filters_ref_array( 'bp_activity_component_before_save', array( $this->component, &$this ) );
$this->type = apply_filters_ref_array( 'bp_activity_type_before_save', array( $this->type, &$this ) );
$this->action = apply_filters_ref_array( 'bp_activity_action_before_save', array( $this->action, &$this ) );
$this->content = apply_filters_ref_array( 'bp_activity_content_before_save', array( $this->content, &$this ) );
$this->date_recorded = apply_filters_ref_array( 'bp_activity_date_recorded_before_save', array( $this->date_recorded, &$this ) );
$this->hide_sitewide = apply_filters_ref_array( 'bp_activity_hide_sitewide_before_save', array( $this->hide_sitewide, &$this ) );
$this->mptt_left = apply_filters_ref_array( 'bp_activity_mptt_left_before_save', array( $this->mptt_left, &$this ) );
$this->mptt_right = apply_filters_ref_array( 'bp_activity_mptt_right_before_save', array( $this->mptt_right, &$this ) );
$this->is_spam = apply_filters_ref_array( 'bp_activity_is_spam_before_save', array( $this->is_spam, &$this ) );
/**
* Fires before the current activity item gets saved.
*
* Please use this hook to filter the properties above. Each part will be passed in.
*
* @since 1.0.0
*
* @param BP_Activity_Activity $this Current instance of the activity item being saved. Passed by reference.
*/
do_action_ref_array( 'bp_activity_before_save', array( &$this ) );
if ( 'wp_error' === $this->error_type && $this->errors->get_error_code() ) {
return $this->errors;
}
if ( empty( $this->component ) || empty( $this->type ) ) {
if ( 'bool' === $this->error_type ) {
return false;
} else {
if ( empty( $this->component ) ) {
$this->errors->add( 'bp_activity_missing_component' );
} else {
$this->errors->add( 'bp_activity_missing_type' );
}
return $this->errors;
}
}
if ( empty( $this->primary_link ) ) {
$this->primary_link = bp_loggedin_user_domain();
}
// If we have an existing ID, update the activity item, otherwise insert it.
if ( ! empty( $this->id ) ) {
$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 );
} else {
$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 );
}
if ( false === $wpdb->query( $q ) ) {
return false;
}
// If this is a new activity item, set the $id property.
if ( empty( $this->id ) ) {
$this->id = $wpdb->insert_id;
// If an existing activity item, prevent any changes to the content generating new @mention notifications.
} else {
add_filter( 'bp_activity_at_name_do_notifications', '__return_false' );
}
/**
* Fires after an activity item has been saved to the database.
*
* @since 1.0.0
*
* @param BP_Activity_Activity $this Current instance of activity item being saved. Passed by reference.
*/
do_action_ref_array( 'bp_activity_after_save', array( &$this ) );
return true;
}
/** Static Methods ***************************************************/
/**
* Get activity items, as specified by parameters.
*
* @since 1.2.0
* @since 2.4.0 Introduced the `$fields` parameter.
* @since 2.9.0 Introduced the `$order_by` parameter.
*
* @see BP_Activity_Activity::get_filter_sql() for a description of the
* 'filter' parameter.
* @see WP_Meta_Query::queries for a description of the 'meta_query'
* parameter format.
*
* @param array $args {
* An array of arguments. All items are optional.
* @type int $page Which page of results to fetch. Using page=1 without per_page will result
* in no pagination. Default: 1.
* @type int|bool $per_page Number of results per page. Default: 25.
* @type int|bool $max Maximum number of results to return. Default: false (unlimited).
* @type string $fields Activity fields to return. Pass 'ids' to get only the activity IDs.
* 'all' returns full activity objects.
* @type string $sort ASC or DESC. Default: 'DESC'.
* @type string $order_by Column to order results by.
* @type array $exclude Array of activity IDs to exclude. Default: false.
* @type array $in Array of ids to limit query by (IN). Default: false.
* @type array $meta_query Array of meta_query conditions. See WP_Meta_Query::queries.
* @type array $date_query Array of date_query conditions. See first parameter of
* WP_Date_Query::__construct().
* @type array $filter_query Array of advanced query conditions. See BP_Activity_Query::__construct().
* @type string|array $scope Pre-determined set of activity arguments.
* @type array $filter See BP_Activity_Activity::get_filter_sql().
* @type string $search_terms Limit results by a search term. Default: false.
* @type bool $display_comments Whether to include activity comments. Default: false.
* @type bool $show_hidden Whether to show items marked hide_sitewide. Default: false.
* @type string $spam Spam status. Default: 'ham_only'.
* @type bool $update_meta_cache Whether to pre-fetch metadata for queried activity items. Default: true.
* @type string|bool $count_total If true, an additional DB query is run to count the total activity items
* for the query. Default: false.
* }
* @return array The array returned has two keys:
* - 'total' is the count of located activities
* - 'activities' is an array of the located activities
*/
public static function get( $args = array() ) {
global $wpdb;
// Backward compatibility with old method of passing arguments.
if ( !is_array( $args ) || func_num_args() > 1 ) {
_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__ ) );
$old_args_keys = array(
0 => 'max',
1 => 'page',
2 => 'per_page',
3 => 'sort',
4 => 'search_terms',
5 => 'filter',
6 => 'display_comments',
7 => 'show_hidden',
8 => 'exclude',
9 => 'in',
10 => 'spam'
);
$args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
}
$bp = buddypress();
$r = wp_parse_args( $args, array(
'page' => 1, // The current page.
'per_page' => 25, // Activity items per page.
'max' => false, // Max number of items to return.
'fields' => 'all', // Fields to include.
'sort' => 'DESC', // ASC or DESC.
'order_by' => 'date_recorded', // Column to order by.
'exclude' => false, // Array of ids to exclude.
'in' => false, // Array of ids to limit query by (IN).
'meta_query' => false, // Filter by activitymeta.
'date_query' => false, // Filter by date.
'filter_query' => false, // Advanced filtering - see BP_Activity_Query.
'filter' => false, // See self::get_filter_sql().
'scope' => false, // Preset activity arguments.
'search_terms' => false, // Terms to search by.
'display_comments' => false, // Whether to include activity comments.
'show_hidden' => false, // Show items marked hide_sitewide.
'spam' => 'ham_only', // Spam status.
'update_meta_cache' => true, // Whether or not to update meta cache.
'count_total' => false, // Whether or not to use count_total.
) );
// Select conditions.
$select_sql = "SELECT DISTINCT a.id";
$from_sql = " FROM {$bp->activity->table_name} a";
$join_sql = '';
// Where conditions.
$where_conditions = array();
// Excluded types.
$excluded_types = array();
// Scope takes precedence.
if ( ! empty( $r['scope'] ) ) {
$scope_query = self::get_scope_query_sql( $r['scope'], $r );
// Add our SQL conditions if matches were found.
if ( ! empty( $scope_query['sql'] ) ) {
$where_conditions['scope_query_sql'] = $scope_query['sql'];
}
// Override some arguments if needed.
if ( ! empty( $scope_query['override'] ) ) {
$r = array_replace_recursive( $r, $scope_query['override'] );
}
// Advanced filtering.
} elseif ( ! empty( $r['filter_query'] ) ) {
$filter_query = new BP_Activity_Query( $r['filter_query'] );
$sql = $filter_query->get_sql();
if ( ! empty( $sql ) ) {
$where_conditions['filter_query_sql'] = $sql;
}
}
// Regular filtering.
if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) {
$where_conditions['filter_sql'] = $filter_sql;
}
// Spam.
if ( 'ham_only' == $r['spam'] ) {
$where_conditions['spam_sql'] = 'a.is_spam = 0';
} elseif ( 'spam_only' == $r['spam'] ) {
$where_conditions['spam_sql'] = 'a.is_spam = 1';
}
// Searching.
if ( $r['search_terms'] ) {
$search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
$where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like );
/**
* Filters whether or not to include users for search parameters.
*
* @since 3.0.0
*
* @param bool $value Whether or not to include user search. Default false.
*/
if ( apply_filters( 'bp_activity_get_include_user_search', false ) ) {
$user_search = get_user_by( 'slug', $r['search_terms'] );
if ( false !== $user_search ) {
$user_id = $user_search->ID;
$where_conditions['search_sql'] .= $wpdb->prepare( ' OR a.user_id = %d', $user_id );
}
}
}
// Sorting.
$sort = $r['sort'];
if ( $sort != 'ASC' && $sort != 'DESC' ) {
$sort = 'DESC';
}
switch( $r['order_by'] ) {
case 'id' :
case 'user_id' :
case 'component' :
case 'type' :
case 'action' :
case 'content' :
case 'primary_link' :
case 'item_id' :
case 'secondary_item_id' :
case 'date_recorded' :
case 'hide_sitewide' :
case 'mptt_left' :
case 'mptt_right' :
case 'is_spam' :
break;
default :
$r['order_by'] = 'date_recorded';
break;
}
$order_by = 'a.' . $r['order_by'];
// Hide Hidden Items?
if ( ! $r['show_hidden'] ) {
$where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
}
// Exclude specified items.
if ( ! empty( $r['exclude'] ) ) {
$exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) );
$where_conditions['exclude'] = "a.id NOT IN ({$exclude})";
}
// The specific ids to which you want to limit the query.
if ( ! empty( $r['in'] ) ) {
$in = implode( ',', wp_parse_id_list( $r['in'] ) );
$where_conditions['in'] = "a.id IN ({$in})";
}
// Process meta_query into SQL.
$meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
if ( ! empty( $meta_query_sql['join'] ) ) {
$join_sql .= $meta_query_sql['join'];
}
if ( ! empty( $meta_query_sql['where'] ) ) {
$where_conditions[] = $meta_query_sql['where'];
}
// Process date_query into SQL.
$date_query_sql = self::get_date_query_sql( $r['date_query'] );
if ( ! empty( $date_query_sql ) ) {
$where_conditions['date'] = $date_query_sql;
}
// Alter the query based on whether we want to show activity item
// comments in the stream like normal comments or threaded below
// the activity.
if ( false === $r['display_comments'] || 'threaded' === $r['display_comments'] ) {
$excluded_types[] = 'activity_comment';
}
// Exclude 'last_activity' items unless the 'action' filter has
// been explicitly set.
if ( empty( $r['filter']['object'] ) ) {
$excluded_types[] = 'last_activity';
}
// Build the excluded type sql part.
if ( ! empty( $excluded_types ) ) {
$not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'";
$where_conditions['excluded_types'] = "a.type NOT IN ({$not_in})";
}
/**
* Filters the MySQL WHERE conditions for the Activity items get method.
*
* @since 1.9.0
*
* @param array $where_conditions Current conditions for MySQL WHERE statement.
* @param array $r Parsed arguments passed into method.
* @param string $select_sql Current SELECT MySQL statement at point of execution.
* @param string $from_sql Current FROM MySQL statement at point of execution.
* @param string $join_sql Current INNER JOIN MySQL statement at point of execution.
*/
$where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql );
// Join the where conditions together.
$where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
/**
* Filter the MySQL JOIN clause for the main activity query.
*
* @since 2.5.0
*
* @param string $join_sql JOIN clause.
* @param array $r Method parameters.
* @param string $select_sql Current SELECT MySQL statement.
* @param string $from_sql Current FROM MySQL statement.
* @param string $where_sql Current WHERE MySQL statement.
*/
$join_sql = apply_filters( 'bp_activity_get_join_sql', $join_sql, $r, $select_sql, $from_sql, $where_sql );
// Sanitize page and per_page parameters.
$page = absint( $r['page'] );
$per_page = absint( $r['per_page'] );
$retval = array(
'activities' => null,
'total' => null,
'has_more_items' => null,
);
/**
* Filters if BuddyPress should use legacy query structure over current structure for version 2.0+.
*
* It is not recommended to use the legacy structure, but allowed to if needed.
*
* @since 2.0.0
*
* @param bool $value Whether to use legacy structure or not.
* @param BP_Activity_Activity $value Current method being called.
* @param array $r Parsed arguments passed into method.
*/
if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) {
// Legacy queries joined against the user table.
$select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
$from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
if ( ! empty( $page ) && ! empty( $per_page ) ) {
$pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
/** This filter is documented in bp-activity/bp-activity-classes.php */
$activity_sql = 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 );
} else {
$pag_sql = '';
/**
* Filters the legacy MySQL query statement so plugins can alter before results are fetched.
*
* @since 1.5.0
*
* @param string $value Concatenated MySQL statement pieces to be query results with for legacy query.
* @param string $select_sql Final SELECT MySQL statement portion for legacy query.
* @param string $from_sql Final FROM MySQL statement portion for legacy query.
* @param string $where_sql Final WHERE MySQL statement portion for legacy query.
* @param string $sort Final sort direction for legacy query.
*/
$activity_sql = 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 );
}
/*
* Queries that include 'last_activity' are cached separately,
* since they are generally much less long-lived.
*/
if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_sql ) ) {
$cache_group = 'bp_activity';
} else {
$cache_group = 'bp_activity_with_last_activity';
}
$activities = $wpdb->get_results( $activity_sql );
// Integer casting for legacy activity query.
foreach ( (array) $activities as $i => $ac ) {
$activities[ $i ]->id = (int) $ac->id;
$activities[ $i ]->item_id = (int) $ac->item_id;
$activities[ $i ]->secondary_item_id = (int) $ac->secondary_item_id;
$activities[ $i ]->user_id = (int) $ac->user_id;
$activities[ $i ]->hide_sitewide = (int) $ac->hide_sitewide;
$activities[ $i ]->mptt_left = (int) $ac->mptt_left;
$activities[ $i ]->mptt_right = (int) $ac->mptt_right;
$activities[ $i ]->is_spam = (int) $ac->is_spam;
}
} else {
// Query first for activity IDs.
$activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, a.id {$sort}";
if ( ! empty( $per_page ) && ! empty( $page ) ) {
// We query for $per_page + 1 items in order to
// populate the has_more_items flag.
$activity_ids_sql .= $wpdb->prepare( " LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page + 1 );
}
/**
* Filters the paged activities MySQL statement.
*
* @since 2.0.0
*
* @param string $activity_ids_sql MySQL statement used to query for Activity IDs.
* @param array $r Array of arguments passed into method.
*/
$activity_ids_sql = apply_filters( 'bp_activity_paged_activities_sql', $activity_ids_sql, $r );
/*
* Queries that include 'last_activity' are cached separately,
* since they are generally much less long-lived.
*/
if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $activity_ids_sql ) ) {
$cache_group = 'bp_activity';
} else {
$cache_group = 'bp_activity_with_last_activity';
}
$cached = bp_core_get_incremented_cache( $activity_ids_sql, $cache_group );
if ( false === $cached ) {
$activity_ids = $wpdb->get_col( $activity_ids_sql );
bp_core_set_incremented_cache( $activity_ids_sql, $cache_group, $activity_ids );
} else {
$activity_ids = $cached;
}
$retval['has_more_items'] = ! empty( $per_page ) && count( $activity_ids ) > $per_page;
// If we've fetched more than the $per_page value, we
// can discard the extra now.
if ( ! empty( $per_page ) && count( $activity_ids ) === $per_page + 1 ) {
array_pop( $activity_ids );
}
if ( 'ids' === $r['fields'] ) {
$activities = array_map( 'intval', $activity_ids );
} else {
$activities = self::get_activity_data( $activity_ids );
}
}
if ( 'ids' !== $r['fields'] ) {
// Get the fullnames of users so we don't have to query in the loop.
$activities = self::append_user_fullnames( $activities );
// Get activity meta.
$activity_ids = array();
foreach ( (array) $activities as $activity ) {
$activity_ids[] = $activity->id;
}
if ( ! empty( $activity_ids ) && $r['update_meta_cache'] ) {
bp_activity_update_meta_cache( $activity_ids );
}
if ( $activities && $r['display_comments'] ) {
$activities = BP_Activity_Activity::append_comments( $activities, $r['spam'] );
}
// Pre-fetch data associated with activity users and other objects.
BP_Activity_Activity::prefetch_object_data( $activities );
// Generate action strings.
$activities = BP_Activity_Activity::generate_action_strings( $activities );
}
$retval['activities'] = $activities;
// If $max is set, only return up to the max results.
if ( ! empty( $r['count_total'] ) ) {
/**
* Filters the total activities MySQL statement.
*
* @since 1.5.0
*
* @param string $value MySQL statement used to query for total activities.
* @param string $where_sql MySQL WHERE statement portion.
* @param string $sort Sort direction for query.
*/
$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 );
$cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group );
if ( false === $cached ) {
$total_activities = $wpdb->get_var( $total_activities_sql );
bp_core_set_incremented_cache( $total_activities_sql, $cache_group, $total_activities );
} else {
$total_activities = $cached;
}
if ( !empty( $r['max'] ) ) {
if ( (int) $total_activities > (int) $r['max'] ) {
$total_activities = $r['max'];
}
}
$retval['total'] = $total_activities;
}
return $retval;
}
/**
* Convert activity IDs to activity objects, as expected in template loop.
*
* @since 2.0.0
*
* @param array $activity_ids Array of activity IDs.
* @return array
*/
protected static function get_activity_data( $activity_ids = array() ) {
global $wpdb;
// Bail if no activity ID's passed.
if ( empty( $activity_ids ) ) {
return array();
}
// Get BuddyPress.
$bp = buddypress();
$activities = array();
$uncached_ids = bp_get_non_cached_ids( $activity_ids, 'bp_activity' );
// Prime caches as necessary.
if ( ! empty( $uncached_ids ) ) {
// Format the activity ID's for use in the query below.
$uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
// Fetch data from activity table, preserving order.
$queried_adata = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} WHERE id IN ({$uncached_ids_sql})");
// Put that data into the placeholders created earlier,
// and add it to the cache.
foreach ( (array) $queried_adata as $adata ) {
wp_cache_set( $adata->id, $adata, 'bp_activity' );
}
}
// Now fetch data from the cache.
foreach ( $activity_ids as $activity_id ) {
// Integer casting.
$activity = wp_cache_get( $activity_id, 'bp_activity' );
if ( ! empty( $activity ) ) {
$activity->id = (int) $activity->id;
$activity->user_id = (int) $activity->user_id;
$activity->item_id = (int) $activity->item_id;
$activity->secondary_item_id = (int) $activity->secondary_item_id;
$activity->hide_sitewide = (int) $activity->hide_sitewide;
$activity->mptt_left = (int) $activity->mptt_left;
$activity->mptt_right = (int) $activity->mptt_right;
$activity->is_spam = (int) $activity->is_spam;
}
$activities[] = $activity;
}
// Then fetch user data.
$user_query = new BP_User_Query( array(
'user_ids' => wp_list_pluck( $activities, 'user_id' ),
'populate_extras' => false,
) );
// Associated located user data with activity items.
foreach ( $activities as $a_index => $a_item ) {
$a_user_id = intval( $a_item->user_id );
$a_user = isset( $user_query->results[ $a_user_id ] ) ? $user_query->results[ $a_user_id ] : '';
if ( !empty( $a_user ) ) {
$activities[ $a_index ]->user_email = $a_user->user_email;
$activities[ $a_index ]->user_nicename = $a_user->user_nicename;
$activities[ $a_index ]->user_login = $a_user->user_login;
$activities[ $a_index ]->display_name = $a_user->display_name;
}
}
return $activities;
}
/**
* Append xProfile fullnames to an activity array.
*
* @since 2.0.0
*
* @param array $activities Activities array.
* @return array
*/
protected static function append_user_fullnames( $activities ) {
if ( bp_is_active( 'xprofile' ) && ! empty( $activities ) ) {
$activity_user_ids = wp_list_pluck( $activities, 'user_id' );
if ( ! empty( $activity_user_ids ) ) {
$fullnames = bp_core_get_user_displaynames( $activity_user_ids );
if ( ! empty( $fullnames ) ) {
foreach ( (array) $activities as $i => $activity ) {
if ( ! empty( $fullnames[ $activity->user_id ] ) ) {
$activities[ $i ]->user_fullname = $fullnames[ $activity->user_id ];
}
}
}
}
}
return $activities;
}
/**
* Pre-fetch data for objects associated with activity items.
*
* Activity items are associated with users, and often with other
* BuddyPress data objects. Here, we pre-fetch data about these
* associated objects, so that inline lookups - done primarily when
* building action strings - do not result in excess database queries.
*
* The only object data required for activity component activity types
* (activity_update and activity_comment) is related to users, and that
* info is fetched separately in BP_Activity_Activity::get_activity_data().
* So this method contains nothing but a filter that allows other
* components, such as bp-friends and bp-groups, to hook in and prime
* their own caches at the beginning of an activity loop.
*
* @since 2.0.0
*
* @param array $activities Array of activities.
* @return array $activities Array of activities.
*/
protected static function prefetch_object_data( $activities ) {
/**
* Filters inside prefetch_object_data method to aid in pre-fetching object data associated with activity item.
*
* @since 2.0.0
*
* @param array $activities Array of activities.
*/
return apply_filters( 'bp_activity_prefetch_object_data', $activities );
}
/**
* Generate action strings for the activities located in BP_Activity_Activity::get().
*
* If no string can be dynamically generated for a given item
* (typically because the activity type has not been properly
* registered), the static 'action' value pulled from the database will
* be left in place.
*
* @since 2.0.0
*
* @param array $activities Array of activities.
* @return array
*/
protected static function generate_action_strings( $activities ) {
foreach ( $activities as $key => $activity ) {
$generated_action = bp_activity_generate_action_string( $activity );
if ( false !== $generated_action ) {
$activity->action = $generated_action;
}
$activities[ $key ] = $activity;
}
return $activities;
}
/**
* Get the SQL for the 'meta_query' param in BP_Activity_Activity::get().
*
* We use WP_Meta_Query to do the heavy lifting of parsing the
* meta_query array and creating the necessary SQL clauses. However,
* since BP_Activity_Activity::get() builds its SQL differently than
* WP_Query, we have to alter the return value (stripping the leading
* AND keyword from the 'where' clause).
*
* @since 1.8.0
*
* @param array $meta_query An array of meta_query filters. See the
* documentation for WP_Meta_Query for details.
* @return array $sql_array 'join' and 'where' clauses.
*/
public static function get_meta_query_sql( $meta_query = array() ) {
global $wpdb;
$sql_array = array(
'join' => '',
'where' => '',
);
if ( ! empty( $meta_query ) ) {
$activity_meta_query = new WP_Meta_Query( $meta_query );
// WP_Meta_Query expects the table name at
// $wpdb->activitymeta.
$wpdb->activitymeta = buddypress()->activity->table_name_meta;
$meta_sql = $activity_meta_query->get_sql( 'activity', 'a', 'id' );
// Strip the leading AND - BP handles it in get().
$sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] );
$sql_array['join'] = $meta_sql['join'];
}
return $sql_array;
}
/**
* Get the SQL for the 'date_query' param in BP_Activity_Activity::get().
*
* We use BP_Date_Query, which extends WP_Date_Query, to do the heavy lifting
* of parsing the date_query array and creating the necessary SQL clauses.
* However, since BP_Activity_Activity::get() builds its SQL differently than
* WP_Query, we have to alter the return value (stripping the leading AND
* keyword from the query).
*
* @since 2.1.0
*
* @param array $date_query An array of date_query parameters. See the
* documentation for the first parameter of WP_Date_Query.
* @return string
*/
public static function get_date_query_sql( $date_query = array() ) {
$sql = '';
// Date query.
if ( ! empty( $date_query ) && is_array( $date_query ) ) {
$date_query = new BP_Date_Query( $date_query, 'date_recorded' );
$sql = preg_replace( '/^\sAND/', '', $date_query->get_sql() );
}