1 | <?php |
||
6 | class WP_REST_React_Controller { |
||
7 | /** |
||
8 | * The namespace of this controller's route. |
||
9 | * |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $namespace; |
||
13 | |||
14 | /** |
||
15 | * The base of this controller's route. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $rest_base; |
||
20 | |||
21 | /** |
||
22 | * Constructor. |
||
23 | */ |
||
24 | public function __construct() { |
||
28 | |||
29 | /** |
||
30 | * Register the routes for the objects of the controller. |
||
31 | */ |
||
32 | public function register_routes() { |
||
49 | |||
50 | /** |
||
51 | * Check if a given request has access to read reactions. |
||
52 | * |
||
53 | * @param WP_REST_Request $request Full details about the request. |
||
54 | * @return WP_Error|boolean |
||
55 | */ |
||
56 | public function get_items_permissions_check( $request ) { |
||
70 | |||
71 | /** |
||
72 | * Get a list of reactions. |
||
73 | * |
||
74 | * @param WP_REST_Request $request Full details about the request. |
||
75 | * @return WP_Error|WP_REST_Response |
||
76 | */ |
||
77 | public function get_items( $request ) { |
||
78 | $prepared_args = array( |
||
79 | 'post__in' => $request['post'], |
||
80 | 'type' => 'reaction', |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * Filter arguments, before passing to WP_Comment_Query, when querying reactions via the REST API. |
||
85 | * |
||
86 | * @see https://developer.wordpress.org/reference/classes/wp_comment_query/ |
||
87 | * |
||
88 | * @param array $prepared_args Array of arguments for WP_Comment_Query. |
||
89 | * @param WP_REST_Request $request The current request. |
||
90 | */ |
||
91 | $prepared_args = apply_filters( 'rest_reaction_query', $prepared_args, $request ); |
||
92 | |||
93 | $query = new WP_Comment_Query; |
||
94 | $query_result = $query->query( $prepared_args ); |
||
95 | |||
96 | $reactions_count = array(); |
||
97 | foreach ( $query_result as $reaction ) { |
||
98 | if ( empty( $reactions_count[ $reaction->comment_content ] ) ) { |
||
99 | $reactions_count[ $reaction->comment_content ] = array( |
||
100 | 'count' => 0, |
||
101 | 'post_id' => $reaction->comment_post_ID, |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | $reactions_count[ $reaction->comment_content ]++; |
||
106 | } |
||
107 | |||
108 | $reactions = array(); |
||
109 | foreach ( $reactions_count as $emoji => $data ) { |
||
110 | $reaction = array( |
||
111 | 'emoji' => $emoji, |
||
112 | 'count' => $data['count'], |
||
113 | 'post_id' => $data['post_id'], |
||
114 | ); |
||
115 | |||
116 | $data = $this->prepare_item_for_response( $reaction, $request ); |
||
117 | $reactions[] = $this->prepare_response_for_collection( $data ); |
||
118 | } |
||
119 | |||
120 | $total_reactions = (int) $query->found_comments; |
||
121 | $reaction_groups = count( $reactions ); |
||
122 | |||
123 | $response = rest_ensure_response( $reactions ); |
||
124 | $response->header( 'X-WP-Total', $total_reactions ); |
||
125 | $response->header( 'X-WP-TotalGroups', $reaction_groups ); |
||
126 | |||
127 | return $response; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Check if a given request has access to create a reaction |
||
132 | * |
||
133 | * @param WP_REST_Request $request Full details about the request. |
||
134 | * @return WP_Error|boolean |
||
135 | */ |
||
136 | public function create_item_permissions_check( $request ) { |
||
139 | |||
140 | /** |
||
141 | * Create a reaction. |
||
142 | * |
||
143 | * @param WP_REST_Request $request Full details about the request. |
||
144 | * @return WP_Error|WP_REST_Response |
||
145 | */ |
||
146 | public function create_item( $request ) { |
||
148 | |||
149 | /** |
||
150 | * Check if we can read a post. |
||
151 | * |
||
152 | * Correctly handles posts with the inherit status. |
||
153 | * |
||
154 | * @param object $post Post object. |
||
155 | * @return boolean Can we read it? |
||
156 | */ |
||
157 | public function check_read_post_permission( $post ) { |
||
162 | |||
163 | /** |
||
164 | * Prepare a reaction group output for response. |
||
165 | * |
||
166 | * @param array $reaction Reaction data. |
||
167 | * @param WP_REST_Request $request Request object. |
||
168 | * @return WP_REST_Response $response |
||
169 | */ |
||
170 | public function prepare_item_for_response( $reaction, $request ) { |
||
193 | |||
194 | /** |
||
195 | * Prepare a response for inserting into a collection. |
||
196 | * |
||
197 | * @param WP_REST_Response $response Response object. |
||
198 | * @return array Response data, ready for insertion into collection data. |
||
199 | */ |
||
200 | public function prepare_response_for_collection( $response ) { |
||
213 | |||
214 | /** |
||
215 | * Prepare links for the request. |
||
216 | * |
||
217 | * @param array $reaction Reaction. |
||
218 | * @return array Links for the given reaction. |
||
219 | */ |
||
220 | protected function prepare_links( $reaction ) { |
||
245 | |||
246 | /** |
||
247 | * Get the query params for collections |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | public function get_collection_params() { |
||
264 | /** |
||
265 | * Get the query params for collections |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function get_creation_params() { |
||
289 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.