/wp-content/plugins/buddypress/bp-core/classes/class-bp-suggestions.php

https://github.com/livinglab/openlab · PHP · 145 lines · 32 code · 13 blank · 100 comment · 6 complexity · fda207ea58a56e4fbac3c395e1907390 MD5 · raw file

  1. <?php
  2. /**
  3. * Core component classes.
  4. *
  5. * @package BuddyPress
  6. * @subpackage Core
  7. * @since 2.1.0
  8. */
  9. // Exit if accessed directly.
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Base class for the BuddyPress Suggestions API.
  13. *
  14. * Originally built to power BuddyPress' at-mentions suggestions, it's flexible enough to be used
  15. * for similar kinds of future core requirements, or those desired by third-party developers.
  16. *
  17. * To implement a new suggestions service, create a new class that extends this one, and update
  18. * the list of default services in {@link bp_core_get_suggestions()}. If you're building a plugin,
  19. * it's recommend that you use the `bp_suggestions_services` filter to do this. :)
  20. *
  21. * While the implementation of the query logic is left to you, it should be as quick and efficient
  22. * as possible. When implementing the abstract methods in this class, pay close attention to the
  23. * recommendations provided in the phpDoc blocks, particularly the expected return types.
  24. *
  25. * @since 2.1.0
  26. */
  27. abstract class BP_Suggestions {
  28. /**
  29. * Default arguments common to all suggestions services.
  30. *
  31. * If your custom service requires further defaults, add them here.
  32. *
  33. * @since 2.1.0
  34. * @var array
  35. */
  36. protected $default_args = array(
  37. 'limit' => 16,
  38. 'term' => '',
  39. 'type' => '',
  40. );
  41. /**
  42. * Holds the arguments for the query (about to made to the suggestions service).
  43. *
  44. * This includes `$default_args`, as well as the user-supplied values.
  45. *
  46. * @since 2.1.0
  47. * @var array
  48. */
  49. protected $args = array(
  50. );
  51. /**
  52. * Constructor.
  53. *
  54. * @since 2.1.0
  55. *
  56. * @param array $args Optional. If set, used as the parameters for the suggestions service query.
  57. */
  58. public function __construct( array $args = array() ) {
  59. if ( ! empty( $args ) ) {
  60. $this->set_query( $args );
  61. }
  62. }
  63. /**
  64. * Set the parameters for the suggestions service query.
  65. *
  66. * @since 2.1.0
  67. *
  68. * @param array $args {
  69. * @type int $limit Maximum number of results to display. Optional, default: 16.
  70. * @type string $type The name of the suggestion service to use for the request. Mandatory.
  71. * @type string $term The suggestion service will try to find results that contain this string.
  72. * Mandatory.
  73. * }
  74. */
  75. public function set_query( array $args = array() ) {
  76. $this->args = wp_parse_args( $args, $this->default_args );
  77. }
  78. /**
  79. * Validate and sanitise the parameters for the suggestion service query.
  80. *
  81. * Be sure to call this class' version of this method when implementing it in your own service.
  82. * If validation fails, you must return a WP_Error object.
  83. *
  84. * @since 2.1.0
  85. *
  86. * @return true|WP_Error If validation fails, return a WP_Error object. On success, return true (bool).
  87. */
  88. public function validate() {
  89. $this->args['limit'] = absint( $this->args['limit'] );
  90. $this->args['term'] = trim( sanitize_text_field( $this->args['term'] ) );
  91. /**
  92. * Filters the arguments to be validated for the BP_Suggestions query.
  93. *
  94. * @since 2.1.0
  95. *
  96. * @param BP_Suggestions $value Arguments to be validated.
  97. * @param BP_Suggestions $this Current BP_Suggestions instance.
  98. */
  99. $this->args = apply_filters( 'bp_suggestions_args', $this->args, $this );
  100. // Check for invalid or missing mandatory parameters.
  101. if ( ! $this->args['limit'] || ! $this->args['term'] ) {
  102. return new WP_Error( 'missing_parameter' );
  103. }
  104. // Check for blocked users (e.g. deleted accounts, or spammers).
  105. if ( is_user_logged_in() && ! bp_is_user_active( get_current_user_id() ) ) {
  106. return new WP_Error( 'invalid_user' );
  107. }
  108. /**
  109. * Filters the status of validation for the BP_Suggestions query.
  110. *
  111. * @since 2.1.0
  112. *
  113. * @param bool $value Whether or not the values are valid.
  114. * @param BP_Suggestions $this Current BP_Suggestions instance.
  115. */
  116. return apply_filters( 'bp_suggestions_validate_args', true, $this );
  117. }
  118. /**
  119. * Find and return a list of suggestions that match the query.
  120. *
  121. * The return type is important. If no matches are found, an empty array must be returned.
  122. * Matches must be returned as objects in an array.
  123. *
  124. * The object format for each match must be: { 'ID': string, 'image': string, 'name': string }
  125. * For example: { 'ID': 'admin', 'image': 'http://example.com/logo.png', 'name': 'Name Surname' }
  126. *
  127. * @since 2.1.0
  128. *
  129. * @return array|WP_Error Array of results. If there were problems, returns a WP_Error object.
  130. */
  131. abstract public function get_suggestions();
  132. }