service.php
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class NF_FU_External_Abstracts_Service
*/
abstract class NF_FU_External_Abstracts_Service {
/**
* @var array
*/
private static $instances = array();
/**
* @var string
*/
public $file;
/**
* @var array
*/
public $settings;
/**
* @var string
*/
public $slug;
/**
* @var string
*/
public $name;
/**
* @var string
*/
protected $library_alias;
/**
* @var string
*/
protected $library_file;
/**
* @var string Path to file being uploaded
*/
protected $upload_file;
/**
* @var string Path prefix to file on service
*/
protected $external_path;
/**
* @var string Filename of file on service
*/
protected $external_filename;
/**
* @var array Ninja Form settings
*/
protected $nf_settings;
/**
* @var int Maximum file size in bytes to send to service in a single request
*/
protected $max_single_upload_file_size;
/**
* @var NF_FU_External_Abstracts_Backgroundupload
*/
protected $upload_process;
/**
* Main Plugin Instance
*
* Insures that only one instance of a service class exists in memory at any one
* time.
*
* @return NF_FU_External_Abstracts_Service
*/
final public static function instance() {
$called_class = get_called_class();
if ( ! isset( self::$instances[ $called_class ] ) ) {
self::$instances[ $called_class ] = new $called_class();
$parts = explode( '_', str_replace( '_Service', '', $called_class ) );
$slug = strtolower( array_pop( $parts ) );
$reflector = new ReflectionClass( $called_class );
self::$instances[ $called_class ]->file = $reflector->getFileName();
self::$instances[ $called_class ]->slug = $slug;
self::$instances[ $called_class ]->init();
}
return self::$instances[ $called_class ];
}
/**
* Get instance internally
*
* @return mixed
*/
protected static function get_instance() {
$called_class = get_called_class();
return self::$instances[ $called_class ];
}
/**
* Do things on initialization
*/
protected function init() {
$class = get_called_class();
$process_class = substr( $class, 0, -8 ) . '_Backgroundupload';
if ( class_exists( $process_class ) ) {
$this->upload_process = new $process_class( $this->slug );
}
}
/**
* @param string $upload_file
*/
public function set_upload_file( $upload_file ) {
$this->upload_file = $upload_file;
}
/**
* @param string $external_path
*/
public function set_external_path( $external_path ) {
$this->external_path = $external_path;
}
/**
* @param string $external_filename
*/
public function set_external_filename( $external_filename ) {
$this->external_filename = $external_filename;
}
/**
* Get settings
*
* @return array
*/
public function get_settings() {
$config = dirname( self::get_instance()->file ) . '/config.php';
if ( file_exists( dirname( self::get_instance()->file ) . '/config.php' ) ) {
return include $config;
}
return array();
}
/**
* Load the service specific settings
*
* @return array
*/
public function load_settings() {
if ( is_null( $this->settings ) ) {
$plugin_settings = NF_File_Uploads()->controllers->settings->get_settings();
$settings = $this->get_settings();
$this->settings = array();
foreach ( $settings as $key => $setting ) {
$default = isset( $setting['default'] ) ? $setting['default'] : '';
if ( NF_File_Uploads()->controllers->settings->is_defined( $key ) ) {
$constant_name = NF_File_Uploads()->controllers->settings->get_defined_name( $key );
$value = constant( $constant_name );
} else {
$value = isset( $plugin_settings[ $key ] ) ? $plugin_settings[ $key ] : $default;
}
$this->settings[ $key ] = $value;
}
}
return $this->settings;
}
/**
* Get service name
*
* @return string
*/
public function get_name() {
$name = self::get_instance()->slug;
if ( ! is_null( self::get_instance()->name ) ) {
$name = self::get_instance()->name;
}
return $name;
}
/**
* Get the external filename for a file
*
* @param null $timestamp
*
* @return string
*/
protected function get_filename_external( $timestamp = null ) {
$original_filename = NF_FU_Helper::remove_directory_from_file( $this->upload_file );
if ( empty( $timestamp ) ) {
$timestamp = time();
}
$filename = $timestamp . '-' . $original_filename;
return apply_filters( 'ninja_forms_uploads_' . self::get_instance()->slug . '_filename', $filename, $original_filename );
}
/**
* Get path on service
*
* @return string
*/
protected abstract function get_path_setting();
/**
* Get the external file path
*
* @param string $custom_path
*
* @return string
*/
protected function get_external_path( $custom_path ) {
$path = $this->settings[ $this->get_path_setting() ];
$path = untrailingslashit( $path ) . '/' . untrailingslashit( $custom_path );
return apply_filters( 'ninja_forms_uploads_' . $this->slug . '_path', $this->prepare_path( $path ) );
}
/**
* Prepare path
*
* @param string $path
* @param string $suffix
*
* @return string
*/
protected function prepare_path( $path, $suffix = '/' ) {
$path = ltrim( $path, '/' );
$path = rtrim( $path, '/' );
if ( ! empty( $path ) ) {
$path .= $suffix;
}
return $path;
}
/**
* Upload the attachment to the service
*
* @param array $data
* @param bool $remove_from_server
* @param null $upload_timestamp
* @param array $field
* @param null|int $form_id
* @param bool $background_upload
*
* @return array
*/
public function process_upload( $data, $remove_from_server = false, $upload_timestamp = null, $field = array(), $form_id = null, $background_upload = false ) {
$this->load_settings();
$this->upload_file = $data['file_path'];
$custom_path = isset( $data['custom_path'] ) ? $data['custom_path'] : '';
$this->external_path = $this->get_external_path( $custom_path );
$this->external_filename = $this->get_filename_external( $upload_timestamp );
if ( $this->should_background_upload( $background_upload, $data['file_path'], $field, $form_id ) ) {
$chunked = $this->is_file_larger_than_max_chunk( $data['file_path'] );
$this->init_background_upload( $data, $chunked );
if ( $remove_from_server ) {
$data['defer_remove_from_server'] = $remove_from_server;
}
$data['external_locations'][ $this->slug ] = 0;
NF_File_Uploads()->model->update( $data['upload_id'], $data );
return $data;
}
$data = $this->upload_file( $data );
if ( false === $data ) {
return $data;
}
$data['upload_location'] = $this->slug;
$data['external_path'] = $this->external_path;
$data['external_filename'] = $this->external_filename;
$data['external_locations'][ $this->slug ] = 1;
// Update uploads table
NF_File_Uploads()->model->update( $data['upload_id'], $data );
return $data;
}
/**
* Is the service compatible with the site?
*
* @return bool
*/
public function is_compatible() {
$missing_requirements = $this->get_missing_requirements();
return empty( $missing_requirements );
}
/**
* Get the missing service requirements
*
* @return array|bool
*/
public function get_missing_requirements() {
$missing_requirements = array();
return $missing_requirements;
}
/**
* Get notices for the service.
*
* @return array|bool
*/
public function get_notices() {
return array();
}
/**
* Check if the file should be uploaded in a single process or multipart upload to the service.
*
* @param bool $should_bg_upload
* @param string $file
* @param array $field
* @param int $form_id
*
* @return bool
*/
protected function should_background_upload( $should_bg_upload, $file, $field, $form_id ) {
$pre = apply_filters( 'ninja_forms_uploads_should_background_upload', $should_bg_upload, $file, $this->slug, $field, $form_id );
if ( false !== $pre ) {
return $pre;
}
return $this->is_file_larger_than_max_chunk( $file );
}
/**
* Is the file larger than allowed in a single upload by the service.
*
* @param string $file_path
*
* @return bool
*/
protected function is_file_larger_than_max_chunk( $file_path ) {
$file_size = NF_FU_Helper::get_file_size( $file_path );
if ( $file_size < apply_filters( 'ninja_forms_uploads_max_chunk_size_' . $this->slug, $this->max_single_upload_file_size ) ) {
return false;
}
return true;
}
/**
* Prepare the file upload data to be passed to the background process.
*
* @return array
*/
protected function prepare_data_for_background_upload() {
return array(
'upload_file' => $this->upload_file,
'external_path' => $this->external_path,
'external_filename' => $this->external_filename,
);
}
/**
* Pass the file data to a background process to upload to the service.
*
* @param array $data
* @param bool $chunked Should we upload in chunks
*/
protected function init_background_upload( $data, $chunked = true ) {
$item = $this->prepare_data_for_background_upload();
$data['chunked'] = $chunked;
if ( ! $chunked ) {
$data['external_path'] = $this->external_path;
$data['external_filename'] = $this->external_filename;
}
$item['data'] = $data;
$this->upload_process->push_to_queue( $item )->save()->data( array() )->dispatch();
}
/**
* Upload the file to the service
*
* @param array $data
*
* @return array|bool
*/
public abstract function upload_file( $data );
/**
* Get the service URL to the file
*
* @param string $filename
* @param string $path
* @param array $data
*
* @return string
*/
public abstract function get_url( $filename, $path = '', $data = array() );
/**
* @param null|array $settings
*
* @return bool
*/
public abstract function is_connected( $settings = null );
/**
* Protected constructor to prevent creating a new instance of the
* class via the `new` operator from outside of this class.
*/
protected function __construct() {
}
/**
* As this class is a singleton it should not be clone-able
*/
protected function __clone() {
}
/**
* As this class is a singleton it should not be able to be unserialized
*/
public function __wakeup() {
}
}