PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
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.2.6, at includes/api/class-announcements-controller.php

338 lines 11.1 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::DELETABLE,
68 'callback' => [ $this, 'delete_announcement' ],
69 'permission_callback' => function ( $request ) {
70 return current_user_can( 'erp_manage_announcement' );
71 },
72 ],
73 'schema' => [ $this, 'get_public_item_schema' ],
74 ] );
75 }
76
77 /**
78 * Get a collection of announcements
79 *
80 * @param WP_REST_Request $request
81 *
82 * @return WP_Error|WP_REST_Response
83 */
84 public function get_announcements( $request ) {
85 $args = [
86 'posts_per_page' => $request['per_page'],
87 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
88 ];
89
90 $args['post_type'] = 'erp_hr_announcement';
91
92 $items = get_posts( $args );
93
94 $count_items = wp_count_posts( $args['post_type'] );
95 $total_items = (int) $count_items->publish;
96
97 $formated_items = [];
98 foreach ( $items as $item ) {
99 $item->id = $item->ID;
100 $data = $this->prepare_item_for_response( $item, $request );
101 $formated_items[] = $this->prepare_response_for_collection( $data );
102 }
103
104 $response = rest_ensure_response( $formated_items );
105 $response = $this->format_collection_response( $response, $request, $total_items );
106
107 return $response;
108 }
109
110 /**
111 * Get a specific announcement
112 *
113 * @param WP_REST_Request $request
114 *
115 * @return WP_Error|WP_REST_Response
116 */
117 public function get_announcement( $request ) {
118 $id = (int) $request['id'];
119 $item = get_post( $id );
120 $item->id = $item->ID;
121
122 if ( empty( $id ) || empty( $item->id ) ) {
123 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
124 }
125
126 $item = $this->prepare_item_for_response( $item, $request );
127 $response = rest_ensure_response( $item );
128
129 return $response;
130 }
131
132 /**
133 * Create an announcement
134 *
135 * @param WP_REST_Request $request
136 *
137 * @return WP_Error|WP_REST_Request
138 */
139 public function create_announcement( $request ) {
140 $item = $this->prepare_item_for_database( $request );
141
142 $id = wp_insert_post( $item );
143
144 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
145
146 $employees = [];
147 if ( $type == 'selected_employee' ) {
148 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
149 }
150
151 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
152
153 $announcement = get_post( $id );
154 $announcement->id = $announcement->ID;
155
156 $request->set_param( 'context', 'edit' );
157 $response = $this->prepare_item_for_response( $announcement, $request );
158 $response = rest_ensure_response( $response );
159 $response->set_status( 201 );
160 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
161
162 return $response;
163 }
164
165 /**
166 * Update an announcement
167 *
168 * @param WP_REST_Request $request
169 *
170 * @return WP_Error|WP_REST_Request
171 */
172 public function update_announcement( $request ) {
173 $id = (int) $request['id'];
174
175 $announcement = get_post( $id );
176 if ( empty( $id ) || empty( $announcement->ID ) ) {
177 return new WP_Error( 'rest_announcement_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 400 ] );
178 }
179
180 $item = $this->prepare_item_for_database( $request );
181
182 $id = wp_insert_post( $item );
183 $announcement = get_post( $id );
184
185 $type = ( $request['recipient_type'] == 'all_employees' ) ? 'all_employee' : 'selected_employee';
186
187 $employees = [];
188 if ( $type == 'selected_employee' ) {
189 $employees = explode( ',', str_replace( ' ', '', $request['employees'] ) );
190 }
191
192 erp_hr_assign_announcements_to_employees( $id, $type, $employees );
193
194 $announcement->id = $announcement->ID;
195
196 $request->set_param( 'context', 'edit' );
197 $response = $this->prepare_item_for_response( $announcement, $request );
198 $response = rest_ensure_response( $response );
199 $response->set_status( 201 );
200 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
201
202 return $response;
203 }
204
205 /**
206 * Delete an announcement
207 *
208 * @param WP_REST_Request $request
209 *
210 * @return WP_Error|WP_REST_Request
211 */
212 public function delete_announcement( $request ) {
213 $id = (int) $request['id'];
214
215 $force_delete = true;
216 wp_delete_post( $id );
217
218 return new WP_REST_Response( true, 204 );
219 }
220
221 /**
222 * Prepare a single item for create or update
223 *
224 * @param WP_REST_Request $request Request object.
225 *
226 * @return array $prepared_item
227 */
228 protected function prepare_item_for_database( $request ) {
229 $prepared_item = [];
230
231 // required arguments.
232 if ( isset( $request['title'] ) ) {
233 $prepared_item['post_title'] = $request['title'];
234 }
235
236 if ( isset( $request['body'] ) ) {
237 $prepared_item['post_content'] = $request['body'];
238 }
239
240 // optional arguments.
241 if ( isset( $request['id'] ) ) {
242 $prepared_item['ID'] = absint( $request['id'] );
243 }
244
245 if ( isset( $request['status'] ) ) {
246 $prepared_item['post_status'] = $request['status'];
247 }
248
249 $prepared_item['post_type'] = 'erp_hr_announcement';
250
251 return $prepared_item;
252 }
253
254 /**
255 * Prepare a single user output for response
256 *
257 * @param object $item
258 * @param WP_REST_Request $request Request object.
259 * @param array $additional_fields (optional)
260 *
261 * @return WP_REST_Response $response Response data.
262 */
263 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
264 $data = [
265 'id' => (int) $item->id,
266 'title' => $item->post_title,
267 'body' => $item->post_content,
268 'status' => $item->post_status,
269 ];
270
271 $data = array_merge( $data, $additional_fields );
272
273 // Wrap the data in a response object
274 $response = rest_ensure_response( $data );
275
276 $response = $this->add_links( $response, $item );
277
278 return $response;
279 }
280
281 /**
282 * Get the User's schema, conforming to JSON Schema
283 *
284 * @return array
285 */
286 public function get_item_schema() {
287 $schema = [
288 '$schema' => 'http://json-schema.org/draft-04/schema#',
289 'title' => 'announcement',
290 'type' => 'object',
291 'properties' => [
292 'id' => [
293 'description' => __( 'Unique identifier for the resource.' ),
294 'type' => 'integer',
295 'context' => [ 'embed', 'view', 'edit' ],
296 'readonly' => true,
297 ],
298 'title' => [
299 'description' => __( 'Title for the resource.' ),
300 'type' => 'string',
301 'context' => [ 'edit' ],
302 'arg_options' => [
303 'sanitize_callback' => 'sanitize_text_field',
304 ],
305 'required' => true,
306 ],
307 'body' => [
308 'description' => __( 'Body for the resource.' ),
309 'type' => 'string',
310 'context' => [ 'edit' ],
311 'arg_options' => [
312 'sanitize_callback' => 'sanitize_text_field',
313 ],
314 'required' => true,
315 ],
316 'status' => [
317 'description' => __( 'Status for the resource.' ),
318 'type' => 'string',
319 'context' => [ 'edit' ],
320 'arg_options' => [
321 'sanitize_callback' => 'sanitize_text_field',
322 ],
323 ],
324 'recipient_type' => [
325 'description' => __( 'Recipient type for the resource.' ),
326 'type' => 'string',
327 'context' => [ 'edit' ],
328 'arg_options' => [
329 'sanitize_callback' => 'sanitize_text_field',
330 ],
331 'required' => true,
332 ],
333 ],
334 ];
335
336 return $schema;
337 }
338 }