Source: includes/classes/REST/Comments.php

  1. <?php
  2. /**
  3. * Comments REST API Controller
  4. *
  5. * @since 5.0.0
  6. * @package elasticpress
  7. */
  8. namespace ElasticPress\REST;
  9. use ElasticPress\Features;
  10. use ElasticPress\Indexables;
  11. /**
  12. * Comments API controller class.
  13. *
  14. * @since 5.0.0
  15. * @package elasticpress
  16. */
  17. class Comments {
  18. /**
  19. * Register routes.
  20. *
  21. * @return void
  22. */
  23. public function register_routes() {
  24. register_rest_route(
  25. 'elasticpress/v1',
  26. 'comments',
  27. [
  28. 'args' => $this->get_args(),
  29. 'callback' => [ $this, 'get_comments' ],
  30. 'methods' => 'GET',
  31. 'permission_callback' => '__return_true',
  32. ]
  33. );
  34. }
  35. /**
  36. * Get args schema.
  37. *
  38. * @return array
  39. */
  40. public function get_args() {
  41. $post_types = $this->get_searchable_post_types();
  42. return [
  43. 'post_type' => [
  44. 'default' => '',
  45. 'description' => __( 'Post type of the posts whose comments to search.', 'elasticpress' ),
  46. 'enum' => $post_types,
  47. 'required' => false,
  48. ],
  49. 's' => [
  50. 'description' => __( 'Search query.', 'elasticpress' ),
  51. 'required' => true,
  52. 'type' => 'string',
  53. 'validate_callback' => fn( $param ) => ! empty( $param ),
  54. ],
  55. ];
  56. }
  57. /**
  58. * Get comments.
  59. *
  60. * @param \WP_REST_Request $request Full details about the request.
  61. * @return array
  62. */
  63. public function get_comments( \WP_REST_Request $request ) {
  64. $search = $request->get_param( 's' );
  65. $post_type_filter = explode( ',', $request->get_param( 'post_type' ) );
  66. $searchable_post_types = $this->get_searchable_post_types();
  67. if ( ! empty( $post_type_filter ) && is_array( $searchable_post_types ) ) {
  68. $post_type_filter = array_intersect( $post_type_filter, $searchable_post_types );
  69. }
  70. $default_args = [
  71. 'status' => 'approve',
  72. 'search' => $search,
  73. 'type' => Indexables::factory()->get( 'comment' )->get_indexable_comment_types(),
  74. 'post_type' => empty( $post_type_filter ) ? $searchable_post_types : $post_type_filter,
  75. 'post_status' => 'publish',
  76. 'number' => 5,
  77. ];
  78. /**
  79. * Filter to args used in WP_Comment_Query in Widget Search Comment
  80. *
  81. * @hook ep_comment_search_widget_args
  82. * @since 3.6.0
  83. * @param {array} $default_args Defaults args
  84. * @return {array} New value
  85. */
  86. $args = apply_filters( 'ep_comment_search_widget_args', $default_args );
  87. /**
  88. * Fires before the comment query is executed.
  89. *
  90. * @hook ep_comment_pre_search_widget
  91. * @since 3.6.0
  92. * @param {array} $args Args passed to `WP_Comment_Query`.
  93. * @param {WP_REST_Request} $request Rest request.
  94. */
  95. do_action( 'ep_comment_pre_search_widget', $args, $request );
  96. $comment_query = new \WP_Comment_Query( $args );
  97. /**
  98. * Fires after the comment query is executed.
  99. *
  100. * @hook ep_comment_after_search_widget
  101. * @since 3.6.0
  102. * @param {WP_Comment_Query} $comment_query WP_Comment_Query object.
  103. * @param {WP_REST_Request} $request Rest request.
  104. */
  105. do_action( 'ep_comment_after_search_widget', $comment_query, $request );
  106. $return = [];
  107. foreach ( $comment_query->comments as $comment ) {
  108. $return[ $comment->comment_ID ] = [
  109. 'id' => $comment->comment_ID,
  110. 'content' => $comment->comment_content,
  111. 'link' => get_comment_link( $comment ),
  112. ];
  113. }
  114. /**
  115. * Filters the comments response
  116. *
  117. * @hook ep_comment_search_widget_response
  118. * @since 3.6.0
  119. * @param {array} $return The result of fetched comments.
  120. * @return {array} New value
  121. */
  122. return apply_filters( 'ep_comment_search_widget_response', $return );
  123. }
  124. /**
  125. * Get searchable post types.
  126. *
  127. * @return array
  128. */
  129. protected function get_searchable_post_types() {
  130. $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
  131. $post_types = array_filter( $post_types, fn( $post_type ) => post_type_supports( $post_type, 'comments' ) );
  132. return $post_types;
  133. }
  134. }