[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-activity/classes/ -> class-bp-activity-activity.php (source)

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


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1