PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
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.3.5, at includes/classes/REST/Comments.php

181 lines 4.6 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 if ( ! $this->can_read_comment( $comment ) ) {
124 continue;
125 }
126
127 $return[ $comment->comment_ID ] = [
128 'id' => $comment->comment_ID,
129 'content' => $comment->comment_content,
130 'link' => get_comment_link( $comment ),
131 ];
132 }
133
134 /**
135 * Filters the comments response
136 *
137 * @hook ep_comment_search_widget_response
138 * @since 3.6.0
139 * @param {array} $return The result of fetched comments.
140 * @return {array} New value
141 */
142 return apply_filters( 'ep_comment_search_widget_response', $return );
143 }
144
145 /**
146 * Whether the current user can read a comment's parent post.
147 *
148 * Mirrors WP_REST_Comments_Controller::check_read_post_permission() for a
149 * collection search: password-protected parents stay hidden unless the
150 * visitor has the password cookie or can edit the post.
151 *
152 * @since 5.3.5
153 * @param \WP_Comment $comment Comment object.
154 * @return bool
155 */
156 protected function can_read_comment( $comment ) {
157 $post = get_post( (int) $comment->comment_post_ID );
158 if ( ! $post ) {
159 return false;
160 }
161
162 if ( post_password_required( $post ) ) {
163 return current_user_can( 'edit_post', $post->ID );
164 }
165
166 return true;
167 }
168
169 /**
170 * Get searchable post types.
171 *
172 * @return array
173 */
174 protected function get_searchable_post_types() {
175 $post_types = Features::factory()->get_registered_feature( 'search' )->get_searchable_post_types();
176 $post_types = array_filter( $post_types, fn( $post_type ) => post_type_supports( $post_type, 'comments' ) );
177
178 return $post_types;
179 }
180 }
181