BP_Members_Suggestions

Adds support for user at-mentions to the Suggestions API.

Defined (1)

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

/bp-core/classes/class-bp-members-suggestions.php  
  1. class BP_Members_Suggestions extends BP_Suggestions { 
  2.  
  3. /** 
  4. * Default arguments for this suggestions service. 
  5. * @since 2.1.0 
  6. * @var array $args { 
  7. * @type int $limit Maximum number of results to display. Default: 16. 
  8. * @type bool $only_friends If true, only match the current user's friends. Default: false. 
  9. * @type string $term The suggestion service will try to find results that contain this string. 
  10. * Mandatory. 
  11. * } 
  12. */ 
  13. protected $default_args = array( 
  14. 'limit' => 10,  
  15. 'only_friends' => false,  
  16. 'term' => '',  
  17. 'type' => '',  
  18. ); 
  19.  
  20.  
  21. /** 
  22. * Validate and sanitise the parameters for the suggestion service query. 
  23. * @since 2.1.0 
  24. * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool). 
  25. */ 
  26. public function validate() { 
  27. $this->args['only_friends'] = (bool) $this->args['only_friends']; 
  28.  
  29. /** 
  30. * Filters the members suggestions args for the current user. 
  31. * @since 2.1.0 
  32. * @param array $args Array of arguments for the member suggestions. 
  33. * @param BP_Members_Suggestions $this Current BP_Members_Suggestions instance. 
  34. */ 
  35. $this->args = apply_filters( 'bp_members_suggestions_args', $this->args, $this ); 
  36.  
  37. // Check for invalid or missing mandatory parameters. 
  38. if ( $this->args['only_friends'] && ( ! bp_is_active( 'friends' ) || ! is_user_logged_in() ) ) { 
  39. return new WP_Error( 'missing_requirement' ); 
  40.  
  41. /** 
  42. * Filters the validation status for the suggestion service query. 
  43. * @since 2.1.0 
  44. * @param bool|WP_Error $value Results of validation check. 
  45. * @param BP_Members_Suggestions $this Current BP_Members_Suggestions instance. 
  46. */ 
  47. return apply_filters( 'bp_members_suggestions_validate_args', parent::validate(), $this ); 
  48.  
  49. /** 
  50. * Find and return a list of username suggestions that match the query. 
  51. * @since 2.1.0 
  52. * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object. 
  53. */ 
  54. public function get_suggestions() { 
  55. $user_query = array( 
  56. 'count_total' => '', // Prevents total count. 
  57. 'populate_extras' => false,  
  58. 'type' => 'alphabetical',  
  59.  
  60. 'page' => 1,  
  61. 'per_page' => $this->args['limit'],  
  62. 'search_terms' => $this->args['term'],  
  63. 'search_wildcard' => 'right',  
  64. ); 
  65.  
  66. // Only return matches of friends of this user. 
  67. if ( $this->args['only_friends'] && is_user_logged_in() ) { 
  68. $user_query['user_id'] = get_current_user_id(); 
  69.  
  70. /** 
  71. * Filters the members suggestions query args. 
  72. * @since 2.1.0 
  73. * @param array $user_query Array of query arguments. 
  74. * @param BP_Members_Suggestions $this Current BP_Members_Suggestions instance. 
  75. */ 
  76. $user_query = apply_filters( 'bp_members_suggestions_query_args', $user_query, $this ); 
  77. if ( is_wp_error( $user_query ) ) { 
  78. return $user_query; 
  79.  
  80.  
  81. $user_query = new BP_User_Query( $user_query ); 
  82. $results = array(); 
  83.  
  84. foreach ( $user_query->results as $user ) { 
  85. $result = new stdClass(); 
  86. $result->ID = $user->user_nicename; 
  87. $result->image = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) ); 
  88. $result->name = bp_core_get_user_displayname( $user->ID ); 
  89.  
  90. $results[] = $result; 
  91.  
  92. /** 
  93. * Filters the members suggestions results. 
  94. * @since 2.1.0 
  95. * @param array $results Array of users to suggest. 
  96. * @param BP_Members_Suggestions $this Current BP_Members_Suggestions instance. 
  97. */ 
  98. return apply_filters( 'bp_members_suggestions_get_suggestions', $results, $this );