Changeset 8557 for trunk/src/bp-core/bp-core-classes.php
- Timestamp:
- 07/03/2014 08:02:18 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-classes.php
r8541 r8557 2408 2408 } 2409 2409 } 2410 2411 /** 2412 * Base class for the BuddyPress Suggestions API. 2413 * 2414 * Originally built to power BuddyPress' at-mentions suggestions, it's flexible enough to be used 2415 * for similar kinds of future core requirements, or those desired by third-party developers. 2416 * 2417 * To implement a new suggestions service, create a new class that extends this one, and update 2418 * the list of default services in {@link bp_core_get_suggestions()}. If you're building a plugin, 2419 * it's recommend that you use the `bp_suggestions_services` filter to do this. :) 2420 * 2421 * While the implementation of the query logic is left to you, it should be as quick and efficient 2422 * as possible. When implementing the abstract methods in this class, pay close attention to the 2423 * recommendations provided in the phpDoc blocks, particularly the expected return types. 2424 * 2425 * @since BuddyPress (2.1.0) 2426 */ 2427 abstract class BP_Suggestions { 2428 2429 /** 2430 * Default arguments common to all suggestions services. 2431 * 2432 * If your custom service requires further defaults, add them here. 2433 * 2434 * @since BuddyPress (2.1.0) 2435 * @var array 2436 */ 2437 protected $default_args = array( 2438 'limit' => 16, 2439 'term' => '', 2440 'type' => '', 2441 ); 2442 2443 /** 2444 * Holds the arguments for the query (about to made to the suggestions service). 2445 * 2446 * This includes `$default_args`, as well as the user-supplied values. 2447 * 2448 * @since BuddyPress (2.1.0) 2449 * @var array 2450 */ 2451 protected $args = array( 2452 ); 2453 2454 2455 /** 2456 * Constructor. 2457 * 2458 * @param array $args Optional. If set, used as the parameters for the suggestions service query. 2459 * @since BuddyPress (2.1.0) 2460 */ 2461 public function __construct( array $args = array() ) { 2462 if ( ! empty( $args ) ) { 2463 $this->set_query( $args ); 2464 } 2465 } 2466 2467 /** 2468 * Set the parameters for the suggestions service query. 2469 * 2470 * @param array $args { 2471 * @type int $limit Maximum number of results to display. Optional, default: 16. 2472 * @type string $type The name of the suggestion service to use for the request. Mandatory. 2473 * @type string $term The suggestion service will try to find results that contain this string. 2474 * Mandatory. 2475 * } 2476 * @since BuddyPress (2.1.0) 2477 */ 2478 public function set_query( array $args = array() ) { 2479 $this->args = wp_parse_args( $args, $this->default_args ); 2480 } 2481 2482 /** 2483 * Validate and sanitise the parameters for the suggestion service query. 2484 * 2485 * Be sure to call this class' version of this method when implementing it in your own service. 2486 * If validation fails, you must return a WP_Error object. 2487 * 2488 * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool). 2489 * @since BuddyPress (2.1.0) 2490 */ 2491 public function validate() { 2492 $this->args['limit'] = absint( $this->args['limit'] ); 2493 $this->args['term'] = trim( sanitize_text_field( $this->args['term'] ) ); 2494 $this->args = apply_filters( 'bp_suggestions_args', $this->args, $this ); 2495 2496 2497 // Check for invalid or missing mandatory parameters. 2498 if ( ! $this->args['limit'] || ! $this->args['term'] ) { 2499 return new WP_Error( 'missing_parameter' ); 2500 } 2501 2502 // Check for blocked users (e.g. deleted accounts, or spammers). 2503 if ( is_user_logged_in() && ! bp_is_user_active( get_current_user_id() ) ) { 2504 return new WP_Error( 'invalid_user' ); 2505 } 2506 2507 return apply_filters( 'bp_suggestions_validate_args', true, $this ); 2508 } 2509 2510 /** 2511 * Find and return a list of suggestions that match the query. 2512 * 2513 * The return type is important. If no matches are found, an empty array must be returned. 2514 * Matches must be returned as objects in an array. 2515 * 2516 * The object format for each match must be: { 'ID': string, 'image': string, 'name': string } 2517 * For example: { 'ID': 'admin', 'image': 'http://example.com/logo.png', 'name': 'Name Surname' } 2518 * 2519 * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object. 2520 * @since BuddyPress (2.1.0) 2521 */ 2522 abstract public function get_suggestions(); 2523 } 2524 2525 /** 2526 * Adds support for user at-mentions to the Suggestions API. 2527 * 2528 * This class is in the Core component because it's required by a class in the Groups component, 2529 * and Groups is loaded before Members (alphabetical order). 2530 * 2531 * @since BuddyPress (2.1.0) 2532 */ 2533 class BP_Members_Suggestions extends BP_Suggestions { 2534 2535 /** 2536 * Default arguments for this suggestions service. 2537 * 2538 * @since BuddyPress (2.1.0) 2539 * @var array $args { 2540 * @type int $limit Maximum number of results to display. Default: 16. 2541 * @type bool $only_friends If true, only match the current user's friends. Default: false. 2542 * @type string $term The suggestion service will try to find results that contain this string. 2543 * Mandatory. 2544 * } 2545 */ 2546 protected $default_args = array( 2547 'limit' => 16, 2548 'only_friends' => false, 2549 'term' => '', 2550 'type' => '', 2551 ); 2552 2553 2554 /** 2555 * Validate and sanitise the parameters for the suggestion service query. 2556 * 2557 * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool). 2558 * @since BuddyPress (2.1.0) 2559 */ 2560 public function validate() { 2561 $this->args['only_friends'] = (bool) $this->args['only_friends']; 2562 $this->args = apply_filters( 'bp_members_suggestions_args', $this->args, $this ); 2563 2564 // Check for invalid or missing mandatory parameters. 2565 if ( $this->args['only_friends'] && ( ! bp_is_active( 'friends' ) || ! is_user_logged_in() ) ) { 2566 return new WP_Error( 'missing_requirement' ); 2567 } 2568 2569 return apply_filters( 'bp_members_suggestions_validate_args', parent::validate(), $this ); 2570 } 2571 2572 /** 2573 * Find and return a list of username suggestions that match the query. 2574 * 2575 * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object. 2576 * @since BuddyPress (2.1.0) 2577 */ 2578 public function get_suggestions() { 2579 $user_query = array( 2580 'count_total' => '', // Prevents total count 2581 'populate_extras' => false, 2582 'type' => 'alphabetical', 2583 2584 'page' => 1, 2585 'per_page' => $this->args['limit'], 2586 'search_terms' => $this->args['term'], 2587 ); 2588 2589 // Only return matches of friends of this user. 2590 if ( $this->args['only_friends'] && is_user_logged_in() ) { 2591 $user_query['user_id'] = get_current_user_id(); 2592 } 2593 2594 $user_query = apply_filters( 'bp_members_suggestions_query_args', $user_query, $this ); 2595 if ( is_wp_error( $user_query ) ) { 2596 return $user_query; 2597 } 2598 2599 2600 $user_query = new BP_User_Query( $user_query ); 2601 $results = array(); 2602 2603 foreach ( $user_query->results as $user ) { 2604 $result = new stdClass(); 2605 $result->ID = $user->user_nicename; 2606 $result->image = bp_core_fetch_avatar( array( 'html' => false, 'item_id' => $user->ID ) ); 2607 $result->name = bp_core_get_user_displayname( $user->ID ); 2608 2609 $results[] = $result; 2610 } 2611 2612 return apply_filters( 'bp_members_suggestions_get_suggestions', $results, $this ); 2613 } 2614 }
Note: See TracChangeset
for help on using the changeset viewer.