PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / modules / block-parsers / file-uploader.php

file-uploader.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at modules/block-parsers/file-uploader.php

325 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Modules\Block_Parsers;
5
6 use Jet_Form_Builder\Classes\Resources\File;
7 use Jet_Form_Builder\Classes\Resources\File_Collection;
8 use Jet_Form_Builder\Classes\Resources\File_Tools;
9 use Jet_Form_Builder\Classes\Resources\Media_Block_Value;
10 use Jet_Form_Builder\Classes\Resources\Uploaded_Collection;
11 use Jet_Form_Builder\Classes\Resources\Uploaded_File;
12 use Jet_Form_Builder\Classes\Resources\Upload_Exception;
13 use Jet_Form_Builder\Classes\Resources\Upload_Permission_Exception;
14 use Jet_Form_Builder\Classes\Tools;
15 use JFB_Modules\Block_Parsers\Fields\Media_Field_Parser;
16
17 // If this file is called directly, abort.
18 if ( ! defined( 'WPINC' ) ) {
19 die;
20 }
21
22 class File_Uploader {
23
24 protected $settings = array();
25
26 /** @var Uploaded_File|Uploaded_File[] */
27 protected $preset = array();
28
29 /** @var File|File_Collection */
30 protected $file;
31
32 protected $default_value;
33
34 protected $max_files;
35 protected $max_size;
36 protected $allowed_user_cap;
37 protected $allowed_mimes;
38 protected $insert_attachment;
39
40 /** @var Field_Data_Parser|Media_Field_Parser */
41 protected $parser;
42
43 /**
44 * @return Media_Block_Value
45 * @throws Upload_Exception
46 * @throws Upload_Permission_Exception
47 */
48 public function upload() {
49 $this->sanitize();
50
51 return $this->upload_files();
52 }
53
54 /**
55 * @throws Upload_Permission_Exception|Upload_Exception
56 */
57 protected function sanitize() {
58 $this->sanitize_permissions();
59 $this->sanitize_max_files();
60 $this->sanitize_files();
61 }
62
63 /**
64 * @return Media_Block_Value
65 * @throws Upload_Exception
66 */
67 protected function upload_files() {
68 if ( $this->file instanceof File ) {
69 return $this->upload_file( $this->file );
70 }
71
72 $uploaded = array();
73
74 /** @var File $file */
75 foreach ( $this->file as $file ) {
76 try {
77 $uploaded[] = $this->upload_file( $file );
78 } catch ( Upload_Exception $exception ) {
79 continue;
80 }
81 }
82
83 return new Uploaded_Collection( $uploaded );
84 }
85
86 /**
87 * @param File $file
88 *
89 * @return Uploaded_File
90 * @throws Upload_Exception
91 */
92 protected function upload_file( File $file ): Uploaded_File {
93 $preset_uploaded = $this->get_uploaded( $file );
94
95 if ( $preset_uploaded instanceof Uploaded_File ) {
96 return $preset_uploaded;
97 }
98
99 $uploaded = new Uploaded_File();
100 $uploaded->upload( $file );
101
102 if ( ! $this->insert_attachment ) {
103 return $uploaded;
104 }
105
106 $uploaded->add_attachment();
107
108 do_action( 'jet-form-builder/inserted-attachment', $uploaded, $this );
109
110 return $uploaded;
111 }
112
113 protected function get_uploaded( File $file ) {
114 if ( empty( $this->preset ) ) {
115 return false;
116 }
117
118 return File_Tools::get_uploaded( $file, $this->preset );
119 }
120
121 /**
122 * @throws Upload_Permission_Exception
123 */
124 protected function sanitize_permissions() {
125 $cap = ! empty( $this->settings['allowed_user_cap'] ) ? $this->settings['allowed_user_cap'] : 'upload_files';
126
127 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped
128 if ( 'any_user' !== $cap && ! is_user_logged_in() ) {
129 throw new Upload_Permission_Exception(
130 __( 'You are not allowed to upload files', 'jet-form-builder' )
131 );
132 }
133
134 if ( ! in_array( $cap, array( 'all', 'any_user' ), true ) && ! current_user_can( $cap ) ) {
135 throw new Upload_Permission_Exception(
136 __( 'You are not allowed to upload files', 'jet-form-builder' )
137 );
138 }
139 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
140 }
141
142 /**
143 * @throws Upload_Exception
144 */
145 protected function sanitize_max_files() {
146 if ( $this->count_files() > $this->max_files ) {
147 throw new Upload_Exception( 'upload_max_files' );
148 }
149 }
150
151 /**
152 * @throws Upload_Exception
153 */
154 protected function sanitize_files() {
155 if ( $this->file instanceof File ) {
156 $this->sanitize_file( $this->file );
157
158 return;
159 }
160
161 if ( ! ( $this->file instanceof File_Collection ) ) {
162 return;
163 }
164
165 /** @var File $file */
166 foreach ( $this->file as $file ) {
167 $this->sanitize_file( $file );
168 }
169 }
170
171 /**
172 * @param File $file
173 *
174 * @throws Upload_Exception
175 */
176 protected function sanitize_file( File $file ) {
177 if ( $file->get_size() > $this->max_size ) {
178 throw new Upload_Exception( 'upload_max_size' );
179 }
180
181 if ( empty( $this->allowed_mimes ) ) {
182 return;
183 }
184
185 if ( ! in_array( $file->get_type(), $this->allowed_mimes, true ) ) {
186 throw new Upload_Exception( 'upload_mime_types' );
187 }
188 }
189
190 public function get_max_size(): int {
191 $max_size = wp_max_upload_size();
192
193 if ( empty( $this->settings['max_size'] ) ) {
194 return $max_size;
195 }
196
197 $field_max_size = floatval(
198 \JFB_Modules\Rich_Content\Module::rich( $this->settings['max_size'] ?? '' )
199 );
200 $field_max_size = intval( $field_max_size * MB_IN_BYTES );
201
202 if ( $field_max_size > $max_size ) {
203 return $max_size;
204 }
205
206 return $field_max_size;
207 }
208
209 public function count_files(): int {
210 $counter = 0;
211 if ( $this->file instanceof File ) {
212 ++$counter;
213 } elseif ( $this->file instanceof File_Collection ) {
214 $counter += count( $this->file );
215 }
216
217 return $counter;
218 }
219
220 public function get_max_files(): int {
221 $files = absint(
222 \JFB_Modules\Rich_Content\Module::rich( $this->settings['max_files'] ?? '' )
223 );
224
225 return empty( $files ) ? 1 : $files;
226 }
227
228 public function get_mime_types(): array {
229 return $this->settings['allowed_mimes'] ?? array();
230 }
231
232 /**
233 * @return bool
234 */
235 public function is_insert_attachment(): bool {
236 // Prevent non logged-in users insert attachment
237 if ( ! is_user_logged_in() ) {
238 return $this->parser->get_context()->is_allowed_for_guest();
239 }
240
241 return ! empty( $this->settings['insert_attachment'] );
242 }
243
244 public function get_allowed_user_cap(): string {
245 $capability = $this->settings['allowed_user_cap'] ?? 'upload_files';
246
247 return empty( $capability ) ? 'upload_files' : $capability;
248 }
249
250 public function is_must_be_single(): bool {
251 return 1 === $this->max_files;
252 }
253
254 /**
255 * @param File|File_Collection $file
256 *
257 * @return File_Uploader
258 */
259 public function set_file( $file ): File_Uploader {
260 $this->file = $file;
261
262 return $this;
263 }
264
265 public function set_preset( $preset ): File_Uploader {
266 if ( empty( $preset ) || ! is_array( $preset ) ) {
267 return $this;
268 }
269
270 if ( $this->is_must_be_single() ) {
271 $uploaded = new Uploaded_File();
272 $this->preset = $uploaded->set_from_array( $preset[0] ?? $preset );
273
274 return $this;
275 }
276
277 $files = isset( $preset[0] ) ? $preset : array( $preset );
278
279 foreach ( $files as $index => $file ) {
280 if ( ! is_array( $file ) ) {
281 unset( $files[ $index ] );
282 continue;
283 }
284 $files[ $index ] = ( new Uploaded_File() )->set_from_array( $file );
285 }
286
287 $this->preset = $files;
288
289 return $this;
290 }
291
292 /**
293 * @param array $settings
294 *
295 * @return File_Uploader
296 */
297 public function set_settings( array $settings ): File_Uploader {
298 $this->settings = $settings;
299
300 $this->max_files = $this->get_max_files();
301 $this->max_size = $this->get_max_size();
302 $this->allowed_user_cap = $this->get_allowed_user_cap();
303 $this->insert_attachment = $this->is_insert_attachment();
304 $this->allowed_mimes = $this->get_mime_types();
305
306 return $this;
307 }
308
309 public function set_context( Field_Data_Parser $parser ): File_Uploader {
310 $this->parser = $parser;
311
312 $this->set_settings( $parser->get_settings() );
313 $this->set_preset( Tools::decode_json( $parser->get_value() ) );
314 $this->set_file( $parser->get_file() );
315
316 return $this;
317 }
318
319 public function get_settings(): array {
320 return $this->settings;
321 }
322
323
324 }
325