Changeset 8557 for trunk/src/bp-groups/bp-groups-classes.php
- Timestamp:
- 07/03/2014 08:02:18 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-groups/bp-groups-classes.php
r8541 r8557 4077 4077 ' ), 11 ); 4078 4078 } 4079 4080 /** 4081 * Adds support for user at-mentions (for users in a specific Group) to the Suggestions API. 4082 * 4083 * @since BuddyPress (2.1.0) 4084 */ 4085 class BP_Groups_Member_Suggestions extends BP_Members_Suggestions { 4086 4087 /** 4088 * Default arguments for this suggestions service. 4089 * 4090 * @since BuddyPress (2.1.0) 4091 * @var array $args { 4092 * @type int $group_id Positive integers will restrict the search to members in that group. 4093 * Negative integers will restrict the search to members in every other group. 4094 * @type int $limit Maximum number of results to display. Default: 16. 4095 * @type bool $only_friends If true, only match the current user's friends. Default: false. 4096 * @type string $term The suggestion service will try to find results that contain this string. 4097 * Mandatory. 4098 * } 4099 */ 4100 protected $default_args = array( 4101 'group_id' => 0, 4102 'limit' => 16, 4103 'only_friends' => false, 4104 'term' => '', 4105 'type' => '', 4106 ); 4107 4108 4109 /** 4110 * Validate and sanitise the parameters for the suggestion service query. 4111 * 4112 * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool). 4113 * @since BuddyPress (2.1.0) 4114 */ 4115 public function validate() { 4116 $this->args['group_id'] = (int) $this->args['group_id']; 4117 $this->args = apply_filters( 'bp_groups_member_suggestions_args', $this->args, $this ); 4118 4119 // Check for invalid or missing mandatory parameters. 4120 if ( ! $this->args['group_id'] || ! bp_is_active( 'groups' ) ) { 4121 return new WP_Error( 'missing_requirement' ); 4122 } 4123 4124 // Check that the specified group_id exists, and that the current user can access it. 4125 $the_group = groups_get_group( array( 4126 'group_id' => absint( $this->args['group_id'] ), 4127 'populate_extras' => true, 4128 ) ); 4129 4130 if ( $the_group->id === 0 || ! $the_group->user_has_access ) { 4131 return new WP_Error( 'access_denied' ); 4132 } 4133 4134 return apply_filters( 'bp_groups_member_suggestions_validate_args', parent::validate(), $this ); 4135 } 4136 4137 /** 4138 * Find and return a list of username suggestions that match the query. 4139 * 4140 * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object. 4141 * @since BuddyPress (2.1.0) 4142 */ 4143 public function get_suggestions() { 4144 $user_query = array( 4145 'count_total' => '', // Prevents total count 4146 'populate_extras' => false, 4147 'type' => 'alphabetical', 4148 4149 'group_role' => array( 'admin', 'member', 'mod' ), 4150 'page' => 1, 4151 'per_page' => $this->args['limit'], 4152 'search_terms' => $this->args['term'], 4153 ); 4154 4155 // Only return matches of friends of this user. 4156 if ( $this->args['only_friends'] && is_user_logged_in() ) { 4157 $user_query['user_id'] = get_current_user_id(); 4158 } 4159 4160 // Positive Group IDs will restrict the search to members in that group. 4161 if ( $this->args['group_id'] > 0 ) { 4162 $user_query['group_id'] = $this->args['group_id']; 4163 4164 // Negative Group IDs will restrict the search to members in every other group. 4165 } else { 4166 $group_query = array( 4167 'count_total' => '', // Prevents total count 4168 'populate_extras' => false, 4169 'type' => 'alphabetical', 4170 4171 'group_id' => absint( $this->args['group_id'] ), 4172 'group_role' => array( 'admin', 'member', 'mod' ), 4173 'page' => 1, 4174 ); 4175 $group_users = new BP_Group_Member_Query( $group_query ); 4176 4177 if ( $group_users->results ) { 4178 $user_query['exclude'] = wp_list_pluck( $group_users->results, 'ID' ); 4179 } else { 4180 $user_query['include'] = array( 0 ); 4181 } 4182 } 4183 4184 $user_query = apply_filters( 'bp_groups_member_suggestions_query_args', $user_query, $this ); 4185 if ( is_wp_error( $user_query ) ) { 4186 return $user_query; 4187 } 4188 4189 4190 if ( isset( $user_query['group_id'] ) ) { 4191 $user_query = new BP_Group_Member_Query( $user_query ); 4192 } else { 4193 $user_query = new BP_User_Query( $user_query ); 4194 } 4195 4196 $results = array(); 4197 foreach ( $user_query->results as $user ) { 4198 $result = new stdClass(); 4199 $result->ID = $user->user_nicename; 4200 $result->image = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) ); 4201 $result->name = bp_core_get_user_displayname( $user->ID ); 4202 4203 $results[] = $result; 4204 } 4205 4206 return apply_filters( 'bp_groups_member_suggestions_get_suggestions', $results, $this ); 4207 } 4208 }
Note: See TracChangeset
for help on using the changeset viewer.