PluginProbe
ElasticPress / 5.0.1
ElasticPress v5.0.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / REST / Comments.php

Comments.php in ElasticPress 5.0.1, at includes/classes/REST/Comments.php

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