[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/classes/ -> class-bp-groups-member-suggestions.php (source)

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


Generated: Tue Oct 22 01:01:36 2019 Cross-referenced by PHPXref 0.7.1