PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / api / class-announcements-controller.php

class-announcements-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/api/class-announcements-controller.php

351 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WeDevs\ERP\API;
3
4 use WP_REST_Server;
5 use WP_REST_Response;
6 use WP_Error;
7
8 class Announcements_Controller extends REST_Controller {
9 /**
10 * Endpoint namespace.
11 *
12 * @var string
13 */
14 protected $namespace = 'erp/v1';
15
16 /**
17 * Route base.
18 *
19 * @var string
20 */
21 protected $rest_base = 'hrm/announcements';
22
23 /**
24 * Register the routes for the objects of the controller.
25 */
26 public function register_routes() {
27 register_rest_route( $this->namespace, '/' . $this->rest_base, [
28 [
29 'methods' => WP_REST_Server::READABLE,
30 'callback' => [ $this, 'get_announcements' ],
31 'args' => $this->get_collection_params(),
32 'permission_callback' => function ( $request ) {
33 return current_user_can( 'erp_manage_announcement' );
34 },
35 ],
36 [
37 'methods' => WP_REST_Server::CREATABLE,
38 'callback' => [ $this, 'create_announcement' ],
39 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
40 'permission_callback' => function ( $request ) {
41 return current_user_can( 'erp_manage_announcement' );
42 },
43 ],
44 'schema' => [ $this, 'get_public_item_schema' ],
45 ] );
46
47 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
48 [
49 'methods' => WP_REST_Server::READABLE,
50 'callback' => [ $this, 'get_announcement' ],
51 'args' => [
52 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
53 ],
54 'permission_callback' => function ( $request ) {
55 return current_user_can( 'erp_manage_announcement' );
56 },
57 ],
58 [
59 'methods' => WP_REST_Server::EDITABLE,
60 'callback' => [ $this, 'update_announcement' ],
61 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
62 'permission_callback' => function ( $request ) {
63 return current_user_can( 'erp_manage_announcement' );
64 },
65 ],
66 [
67 'methods' => WP_REST_Server::EDITABLE,
68 'callback' => [ $this, 'update_announcement' ],
69 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
70 'permission_callback' => function ( $request ) {
71 return current_user_can( 'erp_manage_announcement' );
72 },
73 ],
74 [
75 'methods' => WP_REST_Server::DELETABLE,
76 'callback' => [ $this, 'delete_announcement' ],
77 'permission_callback' => function ( $request ) {
78 return current_user_can( 'erp_manage_announcement' );
79 },
80 ],
81 'schema' => [ $this, 'get_public_item_schema' ],
82 ] );
83 }
84
85 /**
86 * Get a collection of announcements
87 *
88 * @param WP_REST_Request $request
89 *
90 * @return WP_Error|WP_REST_Response
91 */
92 public function get_announcements( $request ) {
93 $args = [
94 'posts_per_page' => $request['per_page'],
95 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
96 ];
97
98 $args['post_type'] = 'erp_hr_announcement';
99
100 $items = get_posts( $args );
101
102 $count_items = wp_count_posts( $args['post_type'] );
103 $total_items = (int) $count_items->publish;
104
105 $formated_items = [];
106 foreach ( $items as $item ) {
107 $item->id = $item->ID;
108 $data = $this->prepare_item_for_response( $item, $request );
109 $formated_items[] = $this->prepare_response_for_collection( $data );
110 }
111
112 $response = rest_ensure_response( $formated_items );
113 $response = $this->format_collection_response( $response, $request, $total_items );
114
115 return $response;
116 }
117
118 /**
119 * Get a specific announcement
120 *
121 * @param WP_REST_Request $request
122 *
123 * @return WP_Error|WP_REST_Response
124 */
125 public function get_announcement( $request ) {
126 $id = (int) $request['id'];
127 $item = get_post( $id );
128 $item->id = $item->ID;
129
130 if ( empty( $id ) || empty( $item->id ) ) {
131 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
132 }
133
134 $item = $this->prepare_item_for_response( $item, $request );
135 $response = rest_ensure_response( $item );
136
137 return $response;
138 }
139
140 /**
141 * Create an announcement
142 *
143 * @param WP_REST_Request $request
144 *
145 * @return WP_Error|WP_REST_Request
146 */
147 public function create_announcement( $request ) {
148 $item = $this->prepare_item_for_database( $request );
149
150 $id = wp_insert_post( $item );
151
152 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
153
154 $employees = [];
155 if ( $type == 'selected_employee' ) {
156 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
157 }
158
159 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
160
161 $announcement = get_post( $id );
162 $announcement->id = $announcement->ID;
163
164 $request->set_param( 'context', 'edit' );
165 $response = $this->prepare_item_for_response( $announcement, $request );
166 $response = rest_ensure_response( $response );
167 $response->set_status( 201 );
168 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
169
170 return $response;
171 }
172
173 /**
174 * Update an announcement
175 *
176 * @param WP_REST_Request $request
177 *
178 * @return WP_Error|WP_REST_Request
179 */
180 public function update_announcement( $request ) {
181 $id = (int) $request['id'];
182
183 $announcement = get_post( $id );
184 if ( empty( $id ) || empty( $announcement->ID ) ) {
185 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
186 }
187
188 $item = $this->prepare_item_for_database( $request );
189
190 $id = wp_insert_post( $item );
191 $announcement = get_post( $id );
192
193 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
194
195 $employees = [];
196 if ( $type == 'selected_employee' ) {
197 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
198 }
199
200 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
201
202 $announcement->id = $announcement->ID;
203
204 $request->set_param( 'context', 'edit' );
205 $response = $this->prepare_item_for_response( $announcement, $request );
206 $response = rest_ensure_response( $response );
207 $response->set_status( 201 );
208 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
209
210 return $response;
211 }
212
213 /**
214 * Delete an announcement
215 *
216 * @param WP_REST_Request $request
217 *
218 * @return WP_Error|WP_REST_Request
219 */
220 public function delete_announcement( $request ) {
221 $id = (int) $request['id'];
222
223 $force_delete = true;
224 wp_delete_post( $id );
225
226 return new WP_REST_Response( true, 204 );
227 }
228
229 /**
230 * Prepare a single item for create or update
231 *
232 * @param WP_REST_Request $request Request object.
233 *
234 * @return array $prepared_item
235 */
236 protected function prepare_item_for_database( $request ) {
237 $prepared_item = [];
238
239 // required arguments.
240 if ( isset( $request['title'] ) ) {
241 $prepared_item['post_title'] = $request['title'];
242 }
243
244 if ( isset( $request['body'] ) ) {
245 $prepared_item['post_content'] = $request['body'];
246 }
247
248 // optional arguments.
249 if ( isset( $request['id'] ) ) {
250 $prepared_item['ID'] = absint( $request['id'] );
251 }
252
253 if ( isset( $request['status'] ) ) {
254 $prepared_item['post_status'] = $request['status'];
255 }
256
257 $prepared_item['post_type'] = 'erp_hr_announcement';
258
259 return $prepared_item;
260 }
261
262 /**
263 * Prepare a single user output for response
264 *
265 * @param object $item
266 * @param WP_REST_Request $request Request object.
267 * @param array $additional_fields (optional)
268 *
269 * @return WP_REST_Response $response Response data.
270 */
271 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
272 $post_date = new \DateTime($item->post_date);
273 $post_author = get_user_by('id', $item->post_author);
274
275 $data = [
276 'id' => (int) $item->id,
277 'title' => $item->post_title,
278 'body' => $item->post_content,
279 'status' => $item->post_status,
280 'date' => $post_date->format('Y-m-d'),
281 'author' => $post_author->user_login
282 ];
283
284 $data = array_merge( $data, $additional_fields );
285
286 // Wrap the data in a response object
287 $response = rest_ensure_response( $data );
288
289 $response = $this->add_links( $response, $item );
290
291 return $response;
292 }
293
294 /**
295 * Get the User's schema, conforming to JSON Schema
296 *
297 * @return array
298 */
299 public function get_item_schema() {
300 $schema = [
301 '$schema' => 'http://json-schema.org/draft-04/schema#',
302 'title' => 'announcement',
303 'type' => 'object',
304 'properties' => [
305 'id' => [
306 'description' => __( 'Unique identifier for the resource.' ),
307 'type' => 'integer',
308 'context' => [ 'embed', 'view', 'edit' ],
309 'readonly' => true,
310 ],
311 'title' => [
312 'description' => __( 'Title for the resource.' ),
313 'type' => 'string',
314 'context' => [ 'edit' ],
315 'arg_options' => [
316 'sanitize_callback' => 'sanitize_text_field',
317 ],
318 'required' => true,
319 ],
320 'body' => [
321 'description' => __( 'Body for the resource.' ),
322 'type' => 'string',
323 'context' => [ 'edit' ],
324 'arg_options' => [
325 'sanitize_callback' => 'sanitize_text_field',
326 ],
327 'required' => true,
328 ],
329 'status' => [
330 'description' => __( 'Status for the resource.' ),
331 'type' => 'string',
332 'context' => [ 'edit' ],
333 'arg_options' => [
334 'sanitize_callback' => 'sanitize_text_field',
335 ],
336 ],
337 'recipient_type' => [
338 'description' => __( 'Recipient type for the resource.' ),
339 'type' => 'string',
340 'context' => [ 'edit' ],
341 'arg_options' => [
342 'sanitize_callback' => 'sanitize_text_field',
343 ],
344 'required' => true,
345 ],
346 ],
347 ];
348
349 return $schema;
350 }
351 }