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