[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-members-suggestions.php (source)

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   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 to the Suggestions API.
  15   *
  16   * This class is in the Core component because it's required by a class in the Groups component,
  17   * and Groups is loaded before Members (alphabetical order).
  18   *
  19   * @since 2.1.0
  20   */
  21  class BP_Members_Suggestions extends BP_Suggestions {
  22  
  23      /**
  24       * Default arguments for this suggestions service.
  25       *
  26       * @since 2.1.0
  27       * @var array $args {
  28       *     @type int    $limit        Maximum number of results to display. Default: 16.
  29       *     @type bool   $only_friends If true, only match the current user's friends. Default: false.
  30       *     @type string $term         The suggestion service will try to find results that contain this string.
  31       *                                Mandatory.
  32       * }
  33       */
  34      protected $default_args = array(
  35          'limit'        => 10,
  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['only_friends'] = (bool) $this->args['only_friends'];
  51  
  52          /**
  53           * Filters the members suggestions args for the current user.
  54           *
  55           * @since 2.1.0
  56           *
  57           * @param array                  $args Array of arguments for the member suggestions.
  58           * @param BP_Members_Suggestions $this Current BP_Members_Suggestions instance.
  59           */
  60          $this->args                 = apply_filters( 'bp_members_suggestions_args', $this->args, $this );
  61  
  62          // Check for invalid or missing mandatory parameters.
  63          if ( $this->args['only_friends'] && ( ! bp_is_active( 'friends' ) || ! is_user_logged_in() ) ) {
  64              return new WP_Error( 'missing_requirement' );
  65          }
  66  
  67          /**
  68           * Filters the validation status for the suggestion service query.
  69           *
  70           * @since 2.1.0
  71           *
  72           * @param bool|WP_Error          $value Results of validation check.
  73           * @param BP_Members_Suggestions $this  Current BP_Members_Suggestions instance.
  74           */
  75          return apply_filters( 'bp_members_suggestions_validate_args', parent::validate(), $this );
  76      }
  77  
  78      /**
  79       * Find and return a list of username suggestions that match the query.
  80       *
  81       * @since 2.1.0
  82       *
  83       * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object.
  84       */
  85  	public function get_suggestions() {
  86          $user_query = array(
  87              'count_total'     => '',  // Prevents total count.
  88              'populate_extras' => false,
  89              'type'            => 'alphabetical',
  90  
  91              'page'            => 1,
  92              'per_page'        => $this->args['limit'],
  93              'search_terms'    => $this->args['term'],
  94              'search_wildcard' => 'right',
  95          );
  96  
  97          // Only return matches of friends of this user.
  98          if ( $this->args['only_friends'] && is_user_logged_in() ) {
  99              $user_query['user_id'] = get_current_user_id();
 100          }
 101  
 102          /**
 103           * Filters the members suggestions query args.
 104           *
 105           * @since 2.1.0
 106           *
 107           * @param array                  $user_query Array of query arguments.
 108           * @param BP_Members_Suggestions $this       Current BP_Members_Suggestions instance.
 109           */
 110          $user_query = apply_filters( 'bp_members_suggestions_query_args', $user_query, $this );
 111          if ( is_wp_error( $user_query ) ) {
 112              return $user_query;
 113          }
 114  
 115  
 116          $user_query = new BP_User_Query( $user_query );
 117          $results    = array();
 118  
 119          foreach ( $user_query->results as $user ) {
 120              $result          = new stdClass();
 121              $result->ID      = $user->user_nicename;
 122              $result->image   = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) );
 123              $result->name    = bp_core_get_user_displayname( $user->ID );
 124              $result->user_id = $user->ID;
 125  
 126              $results[] = $result;
 127          }
 128  
 129          /**
 130           * Filters the members suggestions results.
 131           *
 132           * @since 2.1.0
 133           *
 134           * @param array                  $results Array of users to suggest.
 135           * @param BP_Members_Suggestions $this    Current BP_Members_Suggestions instance.
 136           */
 137          return apply_filters( 'bp_members_suggestions_get_suggestions', $results, $this );
 138      }
 139  }


Generated: Thu Apr 25 01:01:12 2024 Cross-referenced by PHPXref 0.7.1