PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.20
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.20
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / api / class-weforms-uploads-controller.php

class-weforms-uploads-controller.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.20, at includes/api/class-weforms-uploads-controller.php

381 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Settings manager class
5 *
6 * @since 1.4.2
7 */
8 class Weforms_Upload_Controller extends Weforms_REST_Controller {
9
10 /**
11 * Endpoint namespace
12 *
13 * @var string
14 */
15 protected $namespace = 'weforms/v1';
16
17 /**
18 * Route name
19 *
20 * @var string
21 */
22 protected $rest_base = 'uploads';
23
24 /**
25 * Register all routes releated with forms
26 *
27 * @return void
28 */
29 public function register_routes() {
30 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/files', [
31 'args' => [
32 'form_id' => [
33 'required' => true,
34 'description' => __( 'Unique identifier for the object.', 'weforms' ),
35 'sanitize_callback' => 'absint',
36 'type' => 'integer',
37 'validate_callback' => [ $this, 'is_form_exists' ],
38 ],
39 'field_id' => [
40 'required' => true,
41 'description' => __( 'Unique identifier for the object.', 'weforms' ),
42 'sanitize_callback' => 'absint',
43 'type' => 'integer',
44 ],
45 ],
46 [
47 'methods' => WP_REST_Server::CREATABLE,
48 'callback' => [ $this, 'upload_file' ],
49 'permission_callback' => [ $this, 'upload_permissions_check' ],
50 ],
51 ] );
52
53 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/files/(?P<id>[\d]+)/', [
54 'args' => [
55 'form_id' => [
56 'description' => __( 'Unique identifier for the object.', 'weforms' ),
57 'validate_callback' => [ $this, 'is_form_exists' ],
58 'required' => true,
59 'sanitize_callback' => 'absint',
60 'type' => 'integer',
61 ],
62 'id' => [
63 'description' => __( 'Unique identifier for the object.', 'weforms' ),
64 'validate_callback' => [ $this, 'is_form_attach_exist' ],
65 'required' => true,
66 'sanitize_callback' => 'absint',
67 'type' => 'integer',
68 ],
69 'force' => [
70 'required' => true,
71 'type' => 'boolean',
72 'description' => __( '', 'weforms' ),
73 'default' => true,
74 ],
75 ],
76
77 [
78 'methods' => WP_REST_Server::DELETABLE,
79 'callback' => [ $this, 'delete_file' ],
80 'permission_callback' => [ $this, 'upload_permissions_check' ],
81 ],
82 ] );
83 }
84
85 /**
86 * Check form Attach exists
87 *
88 * @since 1.4.2
89 *
90 * @param string $param
91 * @param WP_REST_Request $request
92 * @param string $key
93 *
94 * @return bool
95 */
96 public function is_form_attach_exist( $param, $request, $key ) {
97 $form_id = $request->get_param( 'form_id' );
98 $attach_id = (int) $param;
99 $attach_form_id = get_post_meta( $attach_id, 'attachment_form_id', true );
100
101 if ( $attach_form_id == $form_id ) {
102 return true;
103 } else {
104 return false;
105 }
106 }
107
108 /**
109 * Check Form Field Exist
110 *
111 * @since 1.4.2
112 *
113 * @param string $param
114 * @param WP_REST_Request $request
115 * @param string $key
116 *
117 * @return bool
118 */
119 public function is_file_validate( $request ) {
120 $file_error = new WP_Error();
121 $form = weforms()->form->get( (int) $request['form_id'] );
122 $form_settings = $form->get_settings();
123 $form_fields = $form->get_fields();
124 $files = $request->get_file_params();
125 $headers = $request->get_headers();
126 $field_id = $request->get_param( 'field_id' );
127
128 if ( !empty( $files ) ) {
129 foreach ( $form_fields as $field ) {
130 if ( 'image_upload' === $field['template'] && $field['id'] == $field_id ) {
131 $allowed_extension = weforms_allowed_extensions();
132 $allowed_file_type = explode( ',', $allowed_extension['images']['ext'] );
133 $file_type = wp_check_filetype( $files['file']['name'], $mimes = null );
134
135 if ( in_array( $file_type['ext'], $allowed_file_type ) ) {
136 if ( $files['file']['size'] <= ( $field['max_size'] * 1024 ) ) {
137 return true;
138 } else {
139 $file_error->add( 'rest_weforms_invalid_file_size', __( 'File Size exceeds limit!', 'weforms' ), [ 'status' => 404 ] );
140 }
141 }
142 }
143
144 if ( 'file_upload' === $field['template'] && $field['id'] == $field_id ) {
145 $file_type = wp_check_filetype( $files['file']['name'], $mimes = null );
146
147 if ( $this->is_allow_file_type( $field['extension'], $file_type['ext'] ) ) {
148 if ( $files['file']['size'] <= ( $field['max_size'] * 1024 ) ) {
149 return true;
150 } else {
151 $file_error->add( 'rest_weforms_invalid_file_size', __( 'File Size exceeds limit!', 'weforms' ), [ 'status' => 404 ] );
152 }
153 }
154 }
155 }
156 }
157
158 if ( count( $file_error->get_error_messages() ) > 0 ) {
159 return $file_error;
160 } else {
161 return true;
162 }
163 }
164
165 /**
166 * Upload File
167 *
168 * @since 1.4.2
169 *
170 * @param WP_REST_Request $request
171 *
172 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
173 */
174 public function upload_file( $request ) {
175 global $wp_rest_server;
176
177 $endpoints = $wp_rest_server->get_routes();
178 $attachment_controller = $endpoints['/wp/v2/media'][0]['callback'][0];
179 $files = $request->get_file_params();
180 $headers = $request->get_headers();
181 $attachment_response = $attachment_controller->create_item( $request );
182
183 if ( !empty( $attachment_response->data['id'] ) ) {
184 $form_id = $request->get_param( 'form_id' );
185
186 update_post_meta( $attachment_response->data['id'], 'attachment_form_id', $form_id );
187
188 if ( wp_attachment_is_image( $attachment_response->data['id'] ) ) {
189 $image = wp_get_attachment_image_src( $attachment_response->data['id'], 'thumbnail' );
190 $image = $image[0];
191 } else {
192 $image = wp_mime_type_icon( $attachment_response->data['id'] );
193 }
194
195 $data = [
196 'form_id' => $form_id,
197 'attach_id' => $attachment_response->data['id'],
198 'link' => $image,
199 ];
200
201 $response = $this->prepare_response_for_collection( $data, $request );
202 $response['html '] = $this->attach_html( $attachment_response->data['id'] );
203 $response = rest_ensure_response( $response );
204
205 return $response;
206 } else {
207 return new WP_Error( 'rest_weforms', __( 'could not upload file', 'weforms' ), [ 'status' => 404 ] );
208 }
209 }
210
211 /**
212 * Delete File
213 *
214 * @since 1.4.2
215 *
216 * @param WP_REST_Request $request
217 *
218 * @return WP_REST_Response|WP_Error response object on success, or WP_Error object on failure
219 */
220 public function delete_file( $request ) {
221 global $wp_rest_server;
222
223 $endpoints = $wp_rest_server->get_routes();
224 $attachment_controller = $endpoints['/wp/v2/media'][0]['callback'][0];
225 $attachment_response = $attachment_controller->delete_item( $request );
226 $data = [];
227 $data['id'] = $attachment_response->data['previous']['id'];
228 $data['message'] = 'File deleted successfully';
229 $data['data']['status'] = 200;
230 $response = $this->prepare_response_for_collection( $data );
231 $response = rest_ensure_response( $response );
232
233 return $response;
234 }
235
236 /**
237 * Image attachment response
238 *
239 * @since 1.4.2
240 *
241 * @param int $attach_id
242 * @param string $type
243 *
244 * @return string
245 */
246 public static function attach_html( $attach_id, $type = NULL ) {
247 if ( ! $type ) {
248 $type = isset( $_GET['type'] ) ? sanitize_text_field( wp_unslash( $_GET['type'] ) ) : 'image';
249 }
250
251 $attachment = get_post( $attach_id );
252
253 if ( !$attachment ) {
254 return;
255 }
256
257 if ( wp_attachment_is_image( $attach_id ) ) {
258 $image = wp_get_attachment_image_src( $attach_id, 'thumbnail' );
259 $image = $image[0];
260 } else {
261 $image = wp_mime_type_icon( $attach_id );
262 }
263
264 $html = '<li class="ui-state-default wpuf-image-wrap thumbnail">';
265 $html .= sprintf( '<div class="attachment-name"><img src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) );
266 $html .= sprintf( '<input type="hidden" name="wpuf_files[%s][]" value="%d">', $type, $attach_id );
267 $html .= '<div class="caption">';
268 $html .= sprintf( '<a href="#" class="attachment-delete" data-attach_id="%d"> <img src="%s" /></a>', $attach_id, WEFORMS_ASSET_URI . '/images/del-img.png' );
269 $html .= sprintf( '<span class="wpuf-drag-file"> <img src="%s" /></span>', WEFORMS_ASSET_URI . '/images/move-img.png' );
270 $html .= '</div>';
271 $html .= '</li>';
272
273 return $html;
274 }
275
276 /**
277 * Compare Two Array Return One Array
278 *
279 * @since 1.4.2
280 *
281 * @return array
282 **/
283 public function compare_array( $field_extensions, $allowd_extensions ) {
284 $array_new = [];
285
286 foreach ( $field_extensions as $key => $value ) {
287 if ( array_key_exists( $value, $allowd_extensions ) ) {
288 $array_new[$key] = $allowd_extensions[$value];
289 }
290 }
291
292 return $array_new;
293 }
294
295 /**
296 * Get Allowed Extension
297 *
298 * @since 1.4.2
299 *
300 * @param string $field_extension
301 * @param string $file_extension
302 *
303 * @return bool
304 */
305 public function is_allow_file_type( $field_extension, $file_extension ) {
306 $allowed_extensions = weforms_allowed_extensions();
307 $field_allowed_extensions = $this->get_allowed_extension( $allowed_extensions, $field_extension );
308
309 if ( in_array( $file_extension, $field_allowed_extensions ) ) {
310 return true;
311 }
312
313 return false;
314 }
315
316 /**
317 * Get Allowed Extension Type
318 *
319 * @since 1.4.2
320 *
321 * @param array $allowd_extensions
322 * @param array $field_extensions
323 *
324 * @return array
325 */
326 public function get_allowed_extension( $allowd_extensions, $field_extensions ) {
327 $allowd_ext_array = [];
328
329 if ( is_array( $field_extensions ) ) {
330 $allowd_extensions = $this->compare_array( $field_extensions, $allowd_extensions );
331
332 foreach ( $allowd_extensions as $key => $allowd_extension ) {
333 $extensions = explode( ',', $allowd_extension['ext'] );
334
335 foreach ( $extensions as $key => $extension ) {
336 array_push( $allowd_ext_array, $extension );
337 }
338 }
339 } else {
340 if ( array_key_exists( $field_extensions, $allowd_extensions ) ) {
341 foreach ( $allowd_extensions[ $field_extensions ][ 'ext' ] as $key => $allowd_extension ) {
342 $extensions = explode( ',', $allowd_extension['ext'] );
343
344 foreach ( $extensions as $key => $extension ) {
345 array_push( $allowd_ext_array, $extension );
346 }
347 }
348 }
349 }
350
351 return $allowd_ext_array;
352 }
353
354 public function upload_permissions_check( $request ) {
355 if ( !is_weforms_api_allowed_guest_submission() ) {
356 return new WP_Error( 'rest_weforms_cannot_upload', __( 'Sorry, you have no permission to upload File.', 'weforms' ), [ 'status' => rest_authorization_required_code() ] );
357 }
358
359 $form = weforms()->form->get( (int) $request['form_id'] );
360 $form_is_open = $form->is_submission_open();
361
362 if ( is_wp_error( $form_is_open ) ) {
363 return new WP_Error( 'rest_weforms_form_permission', $form_is_open->get_error_message(), [ 'status' => 404 ] );
364 }
365
366 $file_validations = $this->is_file_validate( $request );
367
368 if ( is_wp_error( $file_validations ) ) {
369 $file_message = [];
370
371 foreach ( $file_validations->get_error_messages() as $error ) {
372 $file_message[] = $error;
373 }
374
375 return new WP_Error( 'error', $file_message, [ 'status' => 404 ] );
376 }
377
378 return true;
379 }
380 }
381