BP_Activity_Activity: get (数组args美元=数组())
获取由参数指定的活动项。
描述
另请参阅
- BP_Activity_Activity: get_filter_sql ():查询“filter”参数的说明。
- WP_Meta_Query:查询:查询参数格式的描述。
参数
- args美元
-
(数组)(可选)一个参数数组。所有项目都是可选的。
- “页面”
(int)获取结果的哪一页。使用page=1而不使用per_page将不会导致分页。默认值:1。 - “per_page”
(int | bool)每页的结果数。默认值:25。 - “马克斯”
(int | bool)返回的最大结果数。默认值:false(无限)。 - “字段”
(字符串)返回的活动字段。通过'ids'只获取活动id。'all'返回完整的活动对象。 - “排序”
(字符串)ASC或DESC,默认为'DESC'。 - “order_by”
(字符串)按列对结果排序。 - “排除”
(数组)要排除的活动id数组。默认值:false。 - “在”
(数组)通过(IN)限制查询的id数组。默认值:false。 - “meta_query”
(数组)meta_query条件数组。看到WP_Meta_Query:查询。 - “date_query”
(数组)date_query条件数组。参见WP_Date_Query::__construct()的第一个参数。 - “filter_query”
(数组)高级查询条件阵列。看到BP_Activity_Query: __construct (). - “范围”
(字符串|数组)预先确定的活动参数集。 - “过滤”
(数组)看到BP_Activity_Activity: get_filter_sql (). - “search_term”
(字符串)通过搜索词限制结果。默认值:false。 - “隐私”
(字符串)用隐私来限制结果。默认值:公众。 - “display_comments”
(保龄球)是否包含活动注释。默认值:false。 - “show_hidden”
(保龄球)是否显示标记为hide_sitewide的项目。默认值:false。 - “垃圾邮件”
(字符串)垃圾邮件的状态。默认值:“ham_only”。 - “update_meta_cache”
(保龄球)是否预取查询到的活动项的元数据。默认值:真的。 - “count_total”
(字符串| bool)如果为true,则运行一个额外的DB查询来计算查询的活动项总数。默认值:false。
默认值:数组()
- “页面”
返回
(数组)返回的数组有两个键:- 'total'是已定位活动的计数- 'activities'是已定位活动的数组
源
文件:bp-activity /类/ class-bp-activity-activity.php
公共静态函数get($ args = array()){global $ wpdb;//与传递参数的旧方法向后兼容。if(!artaray($ args)|| func_num_args()> 1){_deprecated_argument(__method__,'1.6',Sprintf(__(传递给%1 $ s的参数应该在关联数组中。查看百分比的内联文档2美元有更多细节。','buddyboss'),__method__,__file__));$ old_args_keys =数组(0 =>'max',1 =>'页',2 =>'per_page',3 =>'sort',4 =>'search_terms',5 =>'filter',6 =>'display_comments',7 =>'show_hidden',8 =>'排除',9 =>'',10 =>'垃圾邮件');$ args = bp_core_parse_args_array($ old_args_keys,func_get_args());$ bp = buddypress();$ r = wp_parse_args($ args,array('页面'=> 1,//当前页面。'per_page'=> 25,//每页的活动项目。'max'=> false,// max的项目数要返回。“字段”=>“所有”,//字段包含。'sort' =>'desc',// ASC或DESC。'ORDER_BY'=>'date_recorded',//列订购。'排除'=> false,// ids的数组排除。'in'=> false,// ids数组以限制查询(in)。'meta_query'=> false,// 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. 'privacy' => false, // public, loggedin, onlyme, friends, media. '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 BuddyPress 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})"; } // The filter activities by their privacy if ( ! empty( $r['privacy'] ) ) { $privacy = "'" . implode ( "', '", $r['privacy'] ) . "'"; $where_conditions['privacy'] = "a.privacy IN ({$privacy})"; } // 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 BuddyPress 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 BuddyPress 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 BuddyPress 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 BuddyPress 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 BuddyPress 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 BuddyPress 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; }
更新日志
版本 | 描述 |
---|---|
BuddyPress 1.2.0 | 介绍了。 |