BP_Groups_Member_Suggestions

Adds support for user at-mentions (for users in a specific Group) to the Suggestions API.

Defined (1)

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

/bp-groups/classes/class-bp-groups-member-suggestions.php  
  1. class BP_Groups_Member_Suggestions extends BP_Members_Suggestions { 
  2.  
  3. /** 
  4. * Default arguments for this suggestions service. 
  5. * @since 2.1.0 
  6. * @var array $args { 
  7. * @type int $group_id Positive integers will restrict the search to members in that group. 
  8. * Negative integers will restrict the search to members in every other group. 
  9. * @type int $limit Maximum number of results to display. Default: 16. 
  10. * @type bool $only_friends If true, only match the current user's friends. Default: false. 
  11. * @type string $term The suggestion service will try to find results that contain this string. 
  12. * Mandatory. 
  13. * } 
  14. */ 
  15. protected $default_args = array( 
  16. 'group_id' => 0,  
  17. 'limit' => 16,  
  18. 'only_friends' => false,  
  19. 'term' => '',  
  20. 'type' => '',  
  21. ); 
  22.  
  23.  
  24. /** 
  25. * Validate and sanitise the parameters for the suggestion service query. 
  26. * @since 2.1.0 
  27. * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool). 
  28. */ 
  29. public function validate() { 
  30. $this->args['group_id'] = (int) $this->args['group_id']; 
  31.  
  32. /** 
  33. * Filters the arguments used to validate and sanitize suggestion service query. 
  34. * @since 2.1.0 
  35. * @param array $args Array of arguments for the suggestion service query. 
  36. * @param BP_Groups_Member_Suggestions $this Instance of the current suggestion class. 
  37. */ 
  38. $this->args = apply_filters( 'bp_groups_member_suggestions_args', $this->args, $this ); 
  39.  
  40. // Check for invalid or missing mandatory parameters. 
  41. if ( ! $this->args['group_id'] || ! bp_is_active( 'groups' ) ) { 
  42. return new WP_Error( 'missing_requirement' ); 
  43.  
  44. // Check that the specified group_id exists, and that the current user can access it. 
  45. $the_group = groups_get_group( absint( $this->args['group_id'] ) ); 
  46.  
  47. if ( $the_group->id === 0 || ! $the_group->user_has_access ) { 
  48. return new WP_Error( 'access_denied' ); 
  49.  
  50. /** 
  51. * Filters the validation results for the suggestion service query. 
  52. * @since 2.1.0 
  53. * @param bool|WP_Error $value True if valid, WP_Error if not. 
  54. * @param BP_Groups_Member_Suggestions $this Instance of the current suggestion class. 
  55. */ 
  56. return apply_filters( 'bp_groups_member_suggestions_validate_args', parent::validate(), $this ); 
  57.  
  58. /** 
  59. * Find and return a list of username suggestions that match the query. 
  60. * @since 2.1.0 
  61. * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object. 
  62. */ 
  63. public function get_suggestions() { 
  64. $user_query = array( 
  65. 'count_total' => '', // Prevents total count. 
  66. 'type' => 'alphabetical',  
  67.  
  68. 'group_role' => array( 'admin', 'member', 'mod' ),  
  69. 'page' => 1,  
  70. 'per_page' => $this->args['limit'],  
  71. 'search_terms' => $this->args['term'],  
  72. 'search_wildcard' => 'right',  
  73. ); 
  74.  
  75. // Only return matches of friends of this user. 
  76. if ( $this->args['only_friends'] && is_user_logged_in() ) { 
  77. $user_query['user_id'] = get_current_user_id(); 
  78.  
  79. // Positive Group IDs will restrict the search to members in that group. 
  80. if ( $this->args['group_id'] > 0 ) { 
  81. $user_query['group_id'] = $this->args['group_id']; 
  82.  
  83. // Negative Group IDs will restrict the search to members in every other group. 
  84. } else { 
  85. $group_query = array( 
  86. 'count_total' => '', // Prevents total count. 
  87. 'type' => 'alphabetical',  
  88.  
  89. 'group_id' => absint( $this->args['group_id'] ),  
  90. 'group_role' => array( 'admin', 'member', 'mod' ),  
  91. 'page' => 1,  
  92. ); 
  93. $group_users = new BP_Group_Member_Query( $group_query ); 
  94.  
  95. if ( $group_users->results ) { 
  96. $user_query['exclude'] = wp_list_pluck( $group_users->results, 'ID' ); 
  97. } else { 
  98. $user_query['include'] = array( 0 ); 
  99.  
  100. /** 
  101. * Filters the arguments for the user query for the Suggestion API. 
  102. * @since 2.1.0 
  103. * @param array $user_query Array of arguments for the query. 
  104. * @param BP_Groups_Member_Suggestions $this Instance of the current suggestion class. 
  105. */ 
  106. $user_query = apply_filters( 'bp_groups_member_suggestions_query_args', $user_query, $this ); 
  107. if ( is_wp_error( $user_query ) ) { 
  108. return $user_query; 
  109.  
  110.  
  111. if ( isset( $user_query['group_id'] ) ) { 
  112. $user_query = new BP_Group_Member_Query( $user_query ); 
  113. } else { 
  114. $user_query = new BP_User_Query( $user_query ); 
  115.  
  116. $results = array(); 
  117. foreach ( $user_query->results as $user ) { 
  118. $result = new stdClass(); 
  119. $result->ID = $user->user_nicename; 
  120. $result->image = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) ); 
  121. $result->name = bp_core_get_user_displayname( $user->ID ); 
  122.  
  123. $results[] = $result; 
  124.  
  125. /** 
  126. * Filters the results of the member suggestions user query. 
  127. * @since 2.1.0 
  128. * @param array $results Array of member suggestions. 
  129. * @param BP_Groups_Member_Suggestions $this Instance of the current suggestion class. 
  130. */ 
  131. return apply_filters( 'bp_groups_member_suggestions_get_suggestions', $results, $this );