Completed
Push — 2.x ( 971a6c...befcfa )
by Scott Kingsley
08:24 queued 03:02
created

PodsField_File::build_dfv_field_options()   F

Complexity

Conditions 28
Paths 3240

Size

Total Lines 175
Code Lines 108

Duplication

Lines 35
Ratio 20 %

Importance

Changes 0
Metric Value
cc 28
eloc 108
nc 3240
nop 2
dl 35
loc 175
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package Pods\Fields
5
 */
6
class PodsField_File extends PodsField {
7
8
	/**
9
	 * Field Type Group
10
	 *
11
	 * @var string
12
	 * @since 2.0
13
	 */
14
	public static $group = 'Relationships / Media';
15
16
	/**
17
	 * Field Type Identifier
18
	 *
19
	 * @var string
20
	 * @since 2.0
21
	 */
22
	public static $type = 'file';
23
24
	/**
25
	 * Field Type Label
26
	 *
27
	 * @var string
28
	 * @since 2.0
29
	 */
30
	public static $label = 'File / Image / Video';
31
32
	/**
33
	 * API caching for fields that need it during validate/save
34
	 *
35
	 * @var \PodsAPI
36
	 * @since 2.3
37
	 */
38
	protected static $api = false;
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	public function __construct() {
44
45
		self::$label = __( 'File / Image / Video', 'pods' );
46
47
	}
48
49
	/**
50
	 * Add admin_init actions.
51
	 *
52
	 * @since 2.3
53
	 */
54
	public function admin_init() {
55
56
		// Hook into AJAX for Uploads.
57
		add_action( 'wp_ajax_pods_upload', array( $this, 'admin_ajax_upload' ) );
58
		add_action( 'wp_ajax_nopriv_pods_upload', array( $this, 'admin_ajax_upload' ) );
59
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65
	public function options() {
66
67
		$sizes = get_intermediate_image_sizes();
68
69
		$image_sizes = array();
70
71
		foreach ( $sizes as $size ) {
72
			$image_sizes[ $size ] = ucwords( str_replace( '-', ' ', $size ) );
73
		}
74
75
		$options = array(
76
			self::$type . '_format_type'            => array(
77
				'label'      => __( 'Upload Limit', 'pods' ),
78
				'default'    => 'single',
79
				'type'       => 'pick',
80
				'data'       => array(
81
					'single' => __( 'Single File', 'pods' ),
82
					'multi'  => __( 'Multiple Files', 'pods' ),
83
				),
84
				'dependency' => true,
85
			),
86
			self::$type . '_uploader'               => array(
87
				'label'      => __( 'File Uploader', 'pods' ),
88
				'default'    => 'attachment',
89
				'type'       => 'pick',
90
				'data'       => apply_filters( 'pods_form_ui_field_' . self::$type . '_uploader_options', array(
91
						'attachment' => __( 'Upload and/or Select (Media Library)', 'pods' ),
92
						'plupload'   => __( 'Upload only (Plupload)', 'pods' ),
93
					)
94
				),
95
				'dependency' => true,
96
			),
97
			self::$type . '_attachment_tab'         => array(
98
				'label'      => __( 'Attachments Default Tab', 'pods' ),
99
				'depends-on' => array( self::$type . '_uploader' => 'attachment' ),
100
				'default'    => 'upload',
101
				'type'       => 'pick',
102
				'data'       => array(
103
					// These keys must match WP media modal router names.
104
					'upload' => __( 'Upload File', 'pods' ),
105
					'browse' => __( 'Media Library', 'pods' ),
106
				),
107
			),
108
			self::$type . '_edit_title'             => array(
109
				'label'   => __( 'Editable Title', 'pods' ),
110
				'default' => 1,
111
				'type'    => 'boolean',
112
			),
113
			self::$type . '_show_edit_link'         => array(
114
				'label'   => __( 'Show Edit Link', 'pods' ),
115
				'default' => 0,
116
				'type'    => 'boolean',
117
			),
118
			self::$type . '_linked'                 => array(
119
				'label'   => __( 'Show Download Link', 'pods' ),
120
				'default' => 0,
121
				'type'    => 'boolean',
122
			),
123
			self::$type . '_limit'                  => array(
124
				'label'      => __( 'Max Number of Files', 'pods' ),
125
				'depends-on' => array( self::$type . '_format_type' => 'multi' ),
126
				'default'    => 0,
127
				'type'       => 'number',
128
			),
129
			self::$type . '_restrict_filesize'      => array(
130
				'label'      => __( 'Restrict File Size', 'pods' ),
131
				'depends-on' => array( self::$type . '_uploader' => 'plupload' ),
132
				'default'    => '10MB',
133
				'type'       => 'text',
134
			),
135
			self::$type . '_type'                   => array(
136
				'label'      => __( 'Restrict File Types', 'pods' ),
137
				'default'    => apply_filters( 'pods_form_ui_field_' . self::$type . '_type_default', 'images' ),
138
				'type'       => 'pick',
139
				'data'       => apply_filters( 'pods_form_ui_field_' . self::$type . '_type_options', array(
140
						'images' => __( 'Images (jpg, jpeg, png, gif)', 'pods' ),
141
						'video'  => __( 'Video (mpg, mov, flv, mp4, etc..)', 'pods' ),
142
						'audio'  => __( 'Audio (mp3, m4a, wav, wma, etc..)', 'pods' ),
143
						'text'   => __( 'Text (txt, csv, tsv, rtx, etc..)', 'pods' ),
144
						'any'    => __( 'Any Type (no restriction)', 'pods' ),
145
						'other'  => __( 'Other (customize allowed extensions)', 'pods' ),
146
					)
147
				),
148
				'dependency' => true,
149
			),
150
			self::$type . '_allowed_extensions'     => array(
151
				'label'       => __( 'Allowed File Extensions', 'pods' ),
152
				'description' => __( 'Separate file extensions with a comma (ex. jpg,png,mp4,mov)', 'pods' ),
153
				'depends-on'  => array( self::$type . '_type' => 'other' ),
154
				'default'     => apply_filters( 'pods_form_ui_field_' . self::$type . '_extensions_default', '' ),
155
				'type'        => 'text',
156
			),
157
			self::$type . '_field_template'         => array(
158
				'label'      => __( 'List Style', 'pods' ),
159
				'help'       => __( 'You can choose which style you would like the files to appear within the form.', 'pods' ),
160
				'depends-on' => array( self::$type . '_type' => 'images' ),
161
				'default'    => apply_filters( 'pods_form_ui_field_' . self::$type . '_template_default', 'rows' ),
162
				'type'       => 'pick',
163
				'data'       => apply_filters( 'pods_form_ui_field_' . self::$type . '_type_templates', array(
164
						'rows'  => __( 'Rows', 'pods' ),
165
						'tiles' => __( 'Tiles', 'pods' ),
166
					)
167
				),
168
			),
169
			/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
170
            self::$type . '_image_size' => array(
171
                'label' => __( 'Excluded Image Sizes', 'pods' ),
172
                'description' => __( 'Image sizes not to generate when processing the image', 'pods' ),
173
                'depends-on' => array( self::$type . '_type' => 'images' ),
174
                'default' => 'images',
175
                'type' => 'pick',
176
                'pick_format_type' => 'multi',
177
                'pick_format_multi' => 'checkbox',
178
                'data' => apply_filters(
179
                    'pods_form_ui_field_' . self::$type . '_image_size_options',
180
                    $image_sizes
181
                )
182
            ),
183
			*/
184
			self::$type . '_add_button'             => array(
185
				'label'   => __( 'Add Button Text', 'pods' ),
186
				'default' => __( 'Add File', 'pods' ),
187
				'type'    => 'text',
188
			),
189
			self::$type . '_modal_title'            => array(
190
				'label'      => __( 'Modal Title', 'pods' ),
191
				'depends-on' => array( self::$type . '_uploader' => 'attachment' ),
192
				'default'    => __( 'Attach a file', 'pods' ),
193
				'type'       => 'text',
194
			),
195
			self::$type . '_modal_add_button'       => array(
196
				'label'      => __( 'Modal Add Button Text', 'pods' ),
197
				'depends-on' => array( self::$type . '_uploader' => 'attachment' ),
198
				'default'    => __( 'Add File', 'pods' ),
199
				'type'       => 'text',
200
			),
201
202
			/* WP GALLERY OUTPUT */
203
			self::$type . '_wp_gallery_output'      => array(
204
				'label'      => __( 'Output as a WP Gallery', 'pods' ),
205
				'help'       => sprintf( __( '<a href="%s" target="_blank">Click here for more info</a>', 'pods' ), 'https://codex.wordpress.org/The_WordPress_Gallery' ),
206
				'depends-on' => array( self::$type . '_type' => 'images' ),
207
				'dependency' => true,
208
				'type'       => 'boolean',
209
			),
210
			self::$type . '_wp_gallery_link'        => array(
211
				'label'      => __( 'Gallery image links', 'pods' ),
212
				'depends-on' => array( self::$type . '_wp_gallery_output' => 1 ),
213
				'type'       => 'pick',
214
				'data'       => array(
215
					'post' => __( 'Attachment Page', 'pods' ),
216
					'file' => __( 'Media File', 'pods' ),
217
					'none' => __( 'None', 'pods' ),
218
				),
219
			),
220
			self::$type . '_wp_gallery_columns'     => array(
221
				'label'      => __( 'Gallery image columns', 'pods' ),
222
				'depends-on' => array( self::$type . '_wp_gallery_output' => 1 ),
223
				'type'       => 'pick',
224
				'data'       => array(
225
					'1' => 1,
226
					'2' => 2,
227
					'3' => 3,
228
					'4' => 4,
229
					'5' => 5,
230
					'6' => 6,
231
					'7' => 7,
232
					'8' => 8,
233
					'9' => 9,
234
				),
235
			),
236
			self::$type . '_wp_gallery_random_sort' => array(
237
				'label'      => __( 'Gallery randomized order', 'pods' ),
238
				'depends-on' => array( self::$type . '_wp_gallery_output' => 1 ),
239
				'type'       => 'boolean',
240
			),
241
			self::$type . '_wp_gallery_size'        => array(
242
				'label'      => __( 'Gallery image size', 'pods' ),
243
				'depends-on' => array( self::$type . '_wp_gallery_output' => 1 ),
244
				'type'       => 'pick',
245
				'data'       => $this->data_image_sizes(),
246
			),
247
		);
248
249
		return $options;
250
251
	}
252
253
	/**
254
	 * {@inheritdoc}
255
	 */
256
	public function schema( $options = null ) {
257
258
		$schema = false;
259
260
		return $schema;
261
262
	}
263
264
	/**
265
	 * {@inheritdoc}
266
	 */
267
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
268
269
		if ( ! empty( $options[ self::$type . '_wp_gallery_output' ] ) ) {
270
			return $this->do_wp_gallery( $value, $options );
271
		}
272
273
		if ( is_array( $value ) && ! empty( $value ) ) {
274
			if ( isset( $value['ID'] ) ) {
275
				$value = wp_get_attachment_url( $value['ID'] );
276
			} else {
277
				$attachments = $value;
278
				$value       = array();
279
280 View Code Duplication
				foreach ( $attachments as $v ) {
281
					if ( ! is_array( $v ) ) {
282
						$value[] = $v;
283
					} elseif ( isset( $v['ID'] ) ) {
284
						$value[] = wp_get_attachment_url( $v['ID'] );
285
					}
286
				}
287
288
				$value = implode( ' ', $value );
289
			}
290
		}
291
292
		return $value;
293
294
	}
295
296
	/**
297
	 * {@inheritdoc}
298
	 */
299
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
300
301
		$options = (array) $options;
302
303
		$type = pods_v( 'type', $options, static::$type );
304
305
		$args = compact( array_keys( get_defined_vars() ) );
306
		$args = (object) $args;
307
308
		/**
309
		 * Access Checking
310
		 */
311
		$is_user_logged_in = is_user_logged_in();
312
313
		$file_upload_requirements = array(
314
			'disabled'          => ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ),
315
			'require_login'     => ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && true === PODS_UPLOAD_REQUIRE_LOGIN && ! $is_user_logged_in ),
316
			'require_login_cap' => ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_string( PODS_UPLOAD_REQUIRE_LOGIN ) && ( ! $is_user_logged_in || ! current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ),
317
		);
318
319
		$file_browser_requirements = array(
320
			'disabled'          => ( defined( 'PODS_DISABLE_FILE_BROWSER' ) && true === PODS_DISABLE_FILE_BROWSER ),
321
			'require_login'     => ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && true === PODS_FILES_REQUIRE_LOGIN && ! $is_user_logged_in ),
322
			'require_login_cap' => ( defined( 'PODS_FILES_REQUIRE_LOGIN' ) && is_string( PODS_FILES_REQUIRE_LOGIN ) && ( ! $is_user_logged_in || ! current_user_can( PODS_FILES_REQUIRE_LOGIN ) ) ),
323
		);
324
325
		$file_upload_requirements  = array_filter( $file_upload_requirements );
326
		$file_browser_requirements = array_filter( $file_browser_requirements );
327
328
		if ( ! empty( $file_upload_requirements ) && ! empty( $file_browser_requirements ) ) {
329
			?>
330
			<p><?php esc_html_e( 'You do not have access to upload / browse files. Contact your website admin to resolve.', 'pods' ); ?></p>
331
			<?php
332
333
			return;
334
		}
335
336
		wp_enqueue_style( 'pods-dfv-list' );
337
		wp_enqueue_script( 'pods-dfv' );
338
339
		$this->render_input_script( $args );
340
341
		return;
342
343
		// @todo: we're short-circuiting for prototyping above. The actions below will need to be woven in somehow.
344
		if ( ! in_array( pods_v( $form_field_type . '_uploader', $options ), array( 'attachment', 'plupload', 'media' ) ) ) {
0 ignored issues
show
Unused Code introduced by
// @todo: we're short-ci...od, $id); return; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
345
			// Support custom File Uploader integration
346
			do_action( 'pods_form_ui_field_' . self::$type . '_uploader_' . pods_v( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id );
347
			do_action( 'pods_form_ui_field_' . self::$type . '_uploader', pods_v( self::$type . '_uploader', $options ), $name, $value, $options, $pod, $id );
348
349
			return;
350
		}
351
352
	}
353
354
	/**
355
	 * {@inheritdoc}
356
	 */
357
	public function build_dfv_field_options( $options, $args ) {
358
359 View Code Duplication
		if ( ! is_admin() ) {
360
			include_once( ABSPATH . '/wp-admin/includes/template.php' );
361
362
			if ( is_multisite() ) {
363
				include_once( ABSPATH . '/wp-admin/includes/ms.php' );
364
			}
365
		}
366
367
		// Handle default template setting.
368
		$file_field_template = pods_v( $args->type . '_field_template', $options, 'rows', true );
369
370
		// Get which file types the field is limited to.
371
		$limit_file_type = pods_v( $args->type . '_type', $options, 'images' );
372
373
		// Non-image file types are forced to rows template right now.
374
		if ( 'images' !== $limit_file_type ) {
375
			$file_field_template = 'rows';
376
		}
377
378
		$options[ $args->type . '_field_template' ] = $file_field_template;
379
380
		// Enforce limit.
381
		$file_limit = 1;
382
383
		if ( 'multi' === pods_v( $args->type . '_format_type', $options, 'single' ) ) {
384
			$file_limit = (int) pods_v( $args->type . '_limit', $options, 0 );
385
386
			if ( $file_limit < 0 ) {
387
				$file_limit = 0;
388
			}
389
		}
390
391
		$options[ $args->type . '_limit' ] = $file_limit;
392
393
		// Build types and extensions to limit by.
394
		if ( 'images' === $limit_file_type ) {
395
			$limit_types      = 'image';
396
			$limit_extensions = 'jpg,jpeg,png,gif';
397
		} elseif ( 'video' === $limit_file_type ) {
398
			$limit_types      = 'video';
399
			$limit_extensions = 'mpg,mov,flv,mp4';
400
		} elseif ( 'audio' === $limit_file_type ) {
401
			$limit_types      = 'audio';
402
			$limit_extensions = 'mp3,m4a,wav,wma';
403
		} elseif ( 'text' === $limit_file_type ) {
404
			$limit_types      = 'text';
405
			$limit_extensions = 'txt,rtx,csv,tsv';
406
		} elseif ( 'any' === $limit_file_type ) {
407
			$limit_types      = '';
408
			$limit_extensions = '*';
409
		} else {
410
			$limit_types = $limit_extensions = pods_v( $args->type . '_allowed_extensions', $options, '', true );
411
		}
412
413
		// Find and replace certain characters to properly split by commas.
414
		$find    = array(
415
			' ',
416
			'.',
417
			"\n",
418
			"\t",
419
			';',
420
		);
421
		$replace = array(
422
			'',
423
			',',
424
			',',
425
			',',
426
		);
427
428
		$limit_types      = trim( str_replace( $find, $replace, $limit_types ), ',' );
429
		$limit_extensions = trim( str_replace( $find, $replace, $limit_extensions ), ',' );
430
		$mime_types       = wp_get_mime_types();
431
432
		if ( ! in_array( $limit_file_type, array( 'images', 'video', 'audio', 'text', 'any' ), true ) ) {
433
			$new_limit_types = array();
434
435
			$limit_types = explode( ',', $limit_types );
436
437 View Code Duplication
			foreach ( $limit_types as $k => $limit_type ) {
438
				if ( isset( $mime_types[ $limit_type ] ) ) {
439
					$mime = explode( '/', $mime_types[ $limit_type ] );
440
					$mime = $mime[0];
441
442
					if ( ! in_array( $mime, $new_limit_types, true ) ) {
443
						$new_limit_types[] = $mime;
444
					}
445
				} else {
446
					$found = false;
447
448
					foreach ( $mime_types as $type => $mime ) {
449
						if ( false !== strpos( $type, $limit_type ) ) {
450
							$mime = explode( '/', $mime );
451
							$mime = $mime[0];
452
453
							if ( ! in_array( $mime, $new_limit_types, true ) ) {
454
								$new_limit_types[] = $mime;
455
							}
456
457
							$found = true;
458
						}
459
					}
460
461
					if ( ! $found ) {
462
						$new_limit_types[] = $limit_type;
463
					}
464
				}
465
			}
466
467
			if ( ! empty( $new_limit_types ) ) {
468
				$limit_types = implode( ',', $new_limit_types );
469
			}
470
		}
471
472
		$options['limit_types']      = $limit_types;
473
		$options['limit_extensions'] = $limit_extensions;
474
475
		$is_user_logged_in = is_user_logged_in();
476
477
		// @todo test frontend media modal
478
		if ( empty( $options[ self::$type . '_uploader' ] ) || ! is_admin() || ! $is_user_logged_in
479
			 || ( ! current_user_can( 'upload_files' ) && ! current_user_can( 'edit_files' ) ) ) {
480
			$options[ self::$type . '_uploader' ] = 'plupload';
481
		}
482
483
		// @todo: plupload specific options need accommodation
484
		if ( 'plupload' === $options[ self::$type . '_uploader' ] ) {
485
			wp_enqueue_script( 'plupload-all' );
486
487
			if ( $is_user_logged_in ) {
488
				$uid = 'user_' . get_current_user_id();
489
			} else {
490
				$uid = @session_id();
491
			}
492
493
			$pod_id = '0';
494
495
			if ( is_object( $args->pod ) ) {
496
				$pod_id = $args->pod->pod_id;
497
			}
498
499
			$uri_hash    = wp_create_nonce( 'pods_uri_' . $_SERVER['REQUEST_URI'] );
500
			$field_nonce = wp_create_nonce( 'pods_upload_' . $pod_id . '_' . $uid . '_' . $uri_hash . '_' . $options['id'] );
501
502
			$options['plupload_init'] = array(
503
				'runtimes'            => 'html5,silverlight,flash,html4',
504
				'url'                 => admin_url( 'admin-ajax.php?pods_ajax=1', 'relative' ),
505
				'file_data_name'      => 'Filedata',
506
				'multiple_queues'     => false,
507
				'max_file_size'       => wp_max_upload_size() . 'b',
508
				'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
509
				'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
510
				'filters'             => array(
511
					array(
512
						'title'      => __( 'Allowed Files', 'pods' ),
513
						'extensions' => '*',
514
					),
515
				),
516
				'multipart'           => true,
517
				'urlstream_upload'    => true,
518
				'multipart_params'    => array(
519
					'_wpnonce' => $field_nonce,
520
					'action'   => 'pods_upload',
521
					'method'   => 'upload',
522
					'pod'      => $pod_id,
523
					'field'    => $options['id'],
524
					'uri'      => $uri_hash,
525
				),
526
			);
527
		}
528
529
		return $options;
530
531
	}
532
533
	/**
534
	 * {@inheritdoc}
535
	 */
536
	public function build_dfv_field_attributes( $attributes, $args ) {
537
538
		// Add template class.
539
		$attributes['class'] .= ' pods-field-template-' . $args->options[ $args->type . '_field_template' ];
540
541
		return $attributes;
542
543
	}
544
545
	/**
546
	 * {@inheritdoc}
547
	 */
548
	public function build_dfv_field_item_data( $args ) {
549
550
		$data = array();
551
552
		$title_editable = (int) pods_v( $args->type . '_edit_title', $args->options, 0 );
553
554
		$value = $args->value;
555
556
		if ( empty( $value ) ) {
557
			$value = array();
558
		} else {
559
			$value = (array) $value;
560
		}
561
562
		foreach ( $value as $id ) {
563
			$attachment = get_post( $id );
564
565
			if ( empty( $attachment ) ) {
566
				continue;
567
			}
568
569
			$icon = '';
570
			
571
			// @todo Add access check
572
			$edit_link = get_edit_post_link( $attachment->ID, 'raw' );
573
			
574
			$link = get_permalink( $attachment->ID );
575
			$download = wp_get_attachment_url( $attachment->ID );
576
577
			$thumb = wp_get_attachment_image_src( $id, 'thumbnail', true );
578
579
			if ( ! empty( $thumb[0] ) ) {
580
				$icon = $thumb[0];
581
			}
582
583
			$title = $attachment->post_title;
584
585
			if ( 0 === $title_editable ) {
586
				$title = basename( $attachment->guid );
587
			}
588
589
			$data[] = array(
590
				'id'        => $id,
591
				'icon'      => $icon,
592
				'name'      => $title,
593
				'edit_link' => $edit_link,
594
				'link'      => $link,
595
				'download'  => $download,
596
			);
597
		}
598
599
		return $data;
600
601
	}
602
603
	/**
604
	 * {@inheritdoc}
605
	 */
606
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
607
608
		return false;
609
610
	}
611
612
	/**
613
	 * {@inheritdoc}
614
	 */
615
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
616
617
		// @todo Check file size
618
		// @todo Check file extensions
619
620
		return true;
621
622
	}
623
624
	/**
625
	 * {@inheritdoc}
626
	 */
627
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
628
629
		return $value;
630
631
	}
632
633
	/**
634
	 * {@inheritdoc}
635
	 */
636
	public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
637
638
		if ( empty( self::$api ) ) {
639
			self::$api = pods_api();
640
		}
641
642
		// Handle File title saving.
643
		foreach ( $value as $id ) {
644
			$title = false;
645
646
			if ( is_array( $id ) ) {
647 View Code Duplication
				if ( isset( $id['title'] ) && 0 < strlen( trim( $id['title'] ) ) ) {
648
					$title = trim( $id['title'] );
649
				}
650
651
				if ( isset( $id['id'] ) ) {
652
					$id = (int) $id['id'];
653
				} else {
654
					$id = 0;
655
				}
656
			}
657
658
			if ( empty( $id ) ) {
659
				continue;
660
			}
661
662
			$attachment_data = array();
663
664
			// Update the title if set.
665
			if ( false !== $title && 1 === (int) pods_v( self::$type . '_edit_title', $options, 0 ) ) {
666
				$attachment_data['post_title'] = $title;
667
			}
668
669
			// Update attachment parent if it's not set yet and we're updating a post.
670
			if ( ! empty( $params->id ) && ! empty( $pod['type'] ) && 'post_type' === $pod['type'] ) {
671
				$attachment = get_post( $id );
672
673
				if ( isset( $attachment->post_parent ) && 0 === (int) $attachment->post_parent ) {
674
					$attachment_data['post_parent'] = (int) $params->id;
675
				}
676
			}
677
678
			// Update the attachment if it the data array is not still empty.
679
			if ( ! empty( $attachment_data ) ) {
680
				$attachment_data['ID'] = $id;
681
682
				self::$api->save_wp_object( 'media', $attachment_data );
683
			}
684
		}
685
686
	}
687
688
	/**
689
	 * {@inheritdoc}
690
	 */
691
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
692
693
		if ( empty( $value ) ) {
694
			return;
695
		}
696
697
		if ( ! empty( $value ) && isset( $value['ID'] ) ) {
698
			$value = array( $value );
699
		}
700
701
		$image_size = apply_filters( 'pods_form_ui_field_' . self::$type . '_ui_image_size', 'thumbnail', $id, $value, $name, $options, $pod );
702
703
		return $this->images( $id, $value, $name, $options, $pod, $image_size );
704
705
	}
706
707
	/**
708
	 * Return image(s) markup
709
	 *
710
	 * @param int    $id
711
	 * @param mixed  $value
712
	 * @param string $name
713
	 * @param array  $options
714
	 * @param array  $pod
715
	 * @param string $image_size
716
	 *
717
	 * @return string
718
	 * @since 2.3
719
	 */
720
	public function images( $id, $value, $name = null, $options = null, $pod = null, $image_size = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
721
722
		$images = '';
723
724
		if ( empty( $value ) || ! is_array( $value ) ) {
725
			return $images;
726
		}
727
728
		foreach ( $value as $v ) {
729
			$images .= pods_image( $v, $image_size );
730
		}
731
732
		return $images;
733
734
	}
735
736
	/**
737
	 * Data callback for Image Sizes
738
	 *
739
	 * @param string       $name    The name of the field
740
	 * @param string|array $value   The value of the field
741
	 * @param array        $options Field options
742
	 * @param array        $pod     Pod data
743
	 * @param int          $id      Item ID
744
	 *
745
	 * @return array
746
	 *
747
	 * @since 2.3
748
	 */
749 View Code Duplication
	public function data_image_sizes( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
750
751
		$data = array();
752
753
		$image_sizes = get_intermediate_image_sizes();
754
755
		foreach ( $image_sizes as $image_size ) {
756
			$data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
757
		}
758
759
		return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
760
761
	}
762
763
	/**
764
	 * Create a WP Gallery from the passed values (need to be attachments)
765
	 *
766
	 * @since  2.7
767
	 *
768
	 * @param  string|array $value   The value(s)
769
	 * @param  array        $options The field options
770
	 *
771
	 * @return string
772
	 */
773
	public function do_wp_gallery( $value, $options ) {
774
775
		$shortcode_args = array();
776
777
		if ( ! empty( $options[ self::$type . '_wp_gallery_columns' ] ) ) {
778
			$shortcode_args['columns'] = absint( $options[ self::$type . '_wp_gallery_columns' ] );
779
		}
780
781
		if ( ! empty( $options[ self::$type . '_wp_gallery_random_sort' ] ) ) {
782
			$shortcode_args['orderby'] = 'rand';
783
		}
784
785
		if ( ! empty( $options[ self::$type . '_wp_gallery_link' ] ) ) {
786
			$shortcode_args['link'] = $options[ self::$type . '_wp_gallery_link' ];
787
		}
788
789
		if ( ! empty( $options[ self::$type . '_wp_gallery_size' ] ) ) {
790
			$shortcode_args['size'] = $options[ self::$type . '_wp_gallery_size' ];
791
		}
792
793
		if ( isset( $value['ID'] ) ) {
794
			$shortcode_args['ids'] = $value['ID'];
795
		} else {
796
			$images = array();
797
798 View Code Duplication
			foreach ( $value as $v ) {
0 ignored issues
show
Bug introduced by
The expression $value of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
799
				if ( ! is_array( $v ) ) {
800
					$images[] = (int) $v;
801
				} elseif ( isset( $v['ID'] ) ) {
802
					$images[] = (int) $v['ID'];
803
				}
804
			}
805
806
			$shortcode_args['ids'] = implode( ',', $images );
807
		}
808
809
		if ( is_callable( 'gallery_shortcode' ) ) {
810
			return gallery_shortcode( $shortcode_args );
811
		} else {
812
			$shortcode = '[gallery';
813
814
			foreach ( $shortcode_args as $key => $shortcode_arg ) {
815
				$shortcode .= ' ' . esc_attr( $key ) . '="' . esc_attr( $shortcode_arg ) . '"';
816
			}
817
818
			$shortcode .= ']';
819
820
			return do_shortcode( $shortcode );
821
		}
822
823
	}
824
825
	/**
826
	 * Handle file row output for uploaders
827
	 *
828
	 * @param array  $attributes
829
	 * @param int    $limit
830
	 * @param bool   $editable
831
	 * @param int    $id
832
	 * @param string $icon
833
	 * @param string $name
834
	 *
835
	 * @return string
836
	 * @since 2.0
837
	 *
838
	 * @deprecated 2.7
839
	 */
840
	public function markup( $attributes, $limit = 1, $editable = true, $id = null, $icon = null, $name = null, $linked = false, $link = null ) {
841
842
		_doing_it_wrong( 'PodsField_File::markup', esc_html__( 'This method has been deprecated and will be removed from Pods 3.0', 'pods' ), '2.7' );
843
844
		// Preserve current file type.
845
		$field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
846
847
		ob_start();
848
849
		if ( empty( $id ) ) {
850
			$id = '{{id}}';
851
		}
852
853
		if ( empty( $icon ) ) {
854
			$icon = '{{icon}}';
855
		} else {
856
			$icon = esc_url( $icon );
857
		}
858
859
		if ( empty( $name ) ) {
860
			$name = '{{name}}';
861
		}
862
863
		if ( empty( $link ) ) {
864
			$link = '{{link}}';
865
		}
866
867
		$editable = (boolean) $editable;
868
		$linked   = (boolean) $linked;
869
		?>
870
		<li class="pods-file hidden" id="pods-file-<?php echo esc_attr( $id ); ?>">
871
			<?php echo PodsForm::field( $attributes['name'] . '[' . $id . '][id]', $id, 'hidden' ); ?>
872
873
			<ul class="pods-file-meta media-item">
874
				<?php if ( 1 != $limit ) { ?>
875
					<li class="pods-file-col pods-file-handle">Handle</li>
876
				<?php } ?>
877
878
				<li class="pods-file-col pods-file-icon">
879
					<img class="pinkynail" src="<?php echo $icon; ?>" alt="Icon" />
880
				</li>
881
882
				<li class="pods-file-col pods-file-name">
883
					<?php
884
					if ( $editable ) {
885
						echo PodsForm::field( $attributes['name'] . '[' . $id . '][title]', $name, 'text' );
886
					} else {
887
						echo( empty( $name ) ? '{{name}}' : $name );
888
					}
889
					?>
890
				</li>
891
892
				<li class="pods-file-col pods-file-actions">
893
					<ul>
894
						<li class="pods-file-col pods-file-delete"><a href="#delete">Delete</a></li>
895
						<?php
896
						if ( $linked ) {
897
							?>
898
							<li class="pods-file-col pods-file-download">
899
								<a href="<?php echo esc_url( $link ); ?>" target="_blank">Download</a></li>
900
							<?php
901
						}
902
						?>
903
					</ul>
904
				</li>
905
			</ul>
906
		</li>
907
		<?php
908
		PodsForm::$field_type = $field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
909
910
		return ob_get_clean();
911
912
	}
913
914
	/**
915
	 * Handle AJAX plupload calls.
916
	 *
917
	 * @since 2.3
918
	 */
919
	public function admin_ajax_upload() {
920
921
		pods_session_start();
922
923
		// Sanitize input
924
		$params = pods_unslash( (array) $_POST );
925
926 View Code Duplication
		foreach ( $params as $key => $value ) {
927
			if ( 'action' === $key ) {
928
				continue;
929
			}
930
931
			unset( $params[ $key ] );
932
933
			$params[ str_replace( '_podsfix_', '', $key ) ] = $value;
934
		}
935
936
		$params = (object) $params;
937
938
		$methods = array(
939
			'upload',
940
		);
941
942
		if ( ! isset( $params->method ) || ! in_array( $params->method, $methods, true ) || ! isset( $params->pod ) || ! isset( $params->field ) || ! isset( $params->uri ) || empty( $params->uri ) ) {
943
			pods_error( 'Invalid AJAX request', PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
944
		} elseif ( ! empty( $params->pod ) && empty( $params->field ) ) {
945
			pods_error( 'Invalid AJAX request', PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
946
		} elseif ( empty( $params->pod ) && ! current_user_can( 'upload_files' ) ) {
947
			pods_error( 'Invalid AJAX request', PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
948
		}
949
950
		// Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead
951
		if ( is_ssl() && empty( $_COOKIE[ SECURE_AUTH_COOKIE ] ) && ! empty( $_REQUEST['auth_cookie'] ) ) {
952
			$_COOKIE[ SECURE_AUTH_COOKIE ] = $_REQUEST['auth_cookie'];
953
		} elseif ( empty( $_COOKIE[ AUTH_COOKIE ] ) && ! empty( $_REQUEST['auth_cookie'] ) ) {
954
			$_COOKIE[ AUTH_COOKIE ] = $_REQUEST['auth_cookie'];
955
		}
956
957
		if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) && ! empty( $_REQUEST['logged_in_cookie'] ) ) {
958
			$_COOKIE[ LOGGED_IN_COOKIE ] = $_REQUEST['logged_in_cookie'];
959
		}
960
961
		global $current_user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
962
		unset( $current_user );
963
964
		/**
965
		 * Access Checking
966
		 */
967
		$upload_disabled   = false;
968
		$is_user_logged_in = is_user_logged_in();
969
970
		if ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ) {
971
			$upload_disabled = true;
972
		} elseif ( ! $is_user_logged_in ) {
973
			if ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && true === PODS_UPLOAD_REQUIRE_LOGIN ) {
974
				$upload_disabled = true;
975
			} elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_string( PODS_UPLOAD_REQUIRE_LOGIN ) && ! current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) {
976
				$upload_disabled = true;
977
			}
978
		}
979
980
		$uid = @session_id();
981
982
		if ( $is_user_logged_in ) {
983
			$uid = 'user_' . get_current_user_id();
984
		}
985
986
		$nonce_check = 'pods_upload_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
987
988 View Code Duplication
		if ( true === $upload_disabled || ! isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) ) {
989
			pods_error( __( 'Unauthorized request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
990
		}
991
992
		$pod   = array();
993
		$field = array(
994
			'type'    => 'file',
995
			'options' => array()
996
		);
997
998
		if ( empty( self::$api ) ) {
999
			self::$api = pods_api();
1000
		}
1001
1002
		self::$api->display_errors = false;
1003
1004
		if ( ! empty( $params->pod ) ) {
1005
			$pod   = self::$api->load_pod( array( 'id' => (int) $params->pod ) );
1006
			$field = self::$api->load_field( array( 'id' => (int) $params->field ) );
1007
1008
			if ( empty( $pod ) || empty( $field ) || $pod['id'] != $field['pod_id'] || ! isset( $pod['fields'][ $field['name'] ] ) ) {
1009
				pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1010
			}
1011
1012
			if ( ! in_array( $field['type'], PodsForm::file_field_types(), true ) ) {
1013
				pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1014
			}
1015
		}
1016
1017
		$method = $params->method;
1018
1019
		// Cleaning up $params
1020
		unset( $params->action );
1021
		unset( $params->method );
1022
		unset( $params->_wpnonce );
1023
1024
		$params->post_id = (int) pods_v( 'post_id', $params, 0 );
1025
1026
		/**
1027
		 * Upload a new file (advanced - returns URL and ID)
1028
		 */
1029
		if ( 'upload' === $method ) {
1030
			$file = $_FILES['Filedata'];
1031
1032
			$limit_size = pods_v( $field['type'] . '_restrict_filesize', $field['options'] );
1033
1034
			if ( ! empty( $limit_size ) ) {
1035
				if ( false !== stripos( $limit_size, 'MB' ) ) {
1036
					$limit_size = (float) trim( str_ireplace( 'MB', '', $limit_size ) );
1037
					$limit_size = $limit_size * 1025 * 1025; // convert to KB to B
1038
				} elseif ( false !== stripos( $limit_size, 'KB' ) ) {
1039
					$limit_size = (float) trim( str_ireplace( 'KB', '', $limit_size ) );
1040
					$limit_size = $limit_size * 1025 * 1025; // convert to B
1041
				} elseif ( false !== stripos( $limit_size, 'GB' ) ) {
1042
					$limit_size = (float) trim( str_ireplace( 'GB', '', $limit_size ) );
1043
					$limit_size = $limit_size * 1025 * 1025 * 1025; // convert to MB to KB to B
1044
				} elseif ( false !== stripos( $limit_size, 'B' ) ) {
1045
					$limit_size = (float) trim( str_ireplace( 'B', '', $limit_size ) );
1046
				} else {
1047
					$limit_size = wp_max_upload_size();
1048
				}
1049
1050
				if ( 0 < $limit_size && $limit_size < $file['size'] ) {
1051
					$error = __( 'File size too large, max size is %s', 'pods' );
1052
					$error = sprintf( $error, pods_v( $field['type'] . '_restrict_filesize', $field['options'] ) );
1053
1054
					pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' );
1055
				}
1056
			}
1057
1058
			$limit_file_type = pods_v( $field['type'] . '_type', $field['options'], 'images' );
1059
1060
			if ( 'images' === $limit_file_type ) {
1061
				$limit_types = 'jpg,jpeg,png,gif';
1062
			} elseif ( 'video' === $limit_file_type ) {
1063
				$limit_types = 'mpg,mov,flv,mp4';
1064
			} elseif ( 'audio' === $limit_file_type ) {
1065
				$limit_types = 'mp3,m4a,wav,wma';
1066
			} elseif ( 'text' === $limit_file_type ) {
1067
				$limit_types = 'txt,rtx,csv,tsv';
1068
			} elseif ( 'any' === $limit_file_type ) {
1069
				$limit_types = '';
1070
			} else {
1071
				$limit_types = pods_v( $field['type'] . '_allowed_extensions', $field['options'], '', true );
1072
			}
1073
1074
			$limit_types = trim( str_replace( array( ' ', '.', "\n", "\t", ';' ), array(
1075
				'',
1076
				',',
1077
				',',
1078
				','
1079
			), $limit_types ), ',' );
1080
1081 View Code Duplication
			if ( pods_version_check( 'wp', '3.5' ) ) {
1082
				$mime_types = wp_get_mime_types();
1083
1084
				if ( in_array( $limit_file_type, array( 'images', 'audio', 'video' ), true ) ) {
1085
					$new_limit_types = array();
1086
1087
					foreach ( $mime_types as $type => $mime ) {
1088
						if ( 0 === strpos( $mime, $limit_file_type ) ) {
1089
							$type = explode( '|', $type );
1090
1091
							$new_limit_types = array_merge( $new_limit_types, $type );
1092
						}
1093
					}
1094
1095
					if ( ! empty( $new_limit_types ) ) {
1096
						$limit_types = implode( ',', $new_limit_types );
1097
					}
1098
				} elseif ( 'any' != $limit_file_type ) {
1099
					$new_limit_types = array();
1100
1101
					$limit_types = explode( ',', $limit_types );
1102
1103
					foreach ( $limit_types as $k => $limit_type ) {
1104
						$found = false;
1105
1106
						foreach ( $mime_types as $type => $mime ) {
1107
							if ( 0 === strpos( $mime, $limit_type ) ) {
1108
								$type = explode( '|', $type );
1109
1110
								foreach ( $type as $t ) {
1111
									if ( ! in_array( $t, $new_limit_types, true ) ) {
1112
										$new_limit_types[] = $t;
1113
									}
1114
								}
1115
1116
								$found = true;
1117
							}
1118
						}
1119
1120
						if ( ! $found ) {
1121
							$new_limit_types[] = $limit_type;
1122
						}
1123
					}
1124
1125
					if ( ! empty( $new_limit_types ) ) {
1126
						$limit_types = implode( ',', $new_limit_types );
1127
					}
1128
				}
1129
			}
1130
1131
			$limit_types = explode( ',', $limit_types );
1132
1133
			$limit_types = array_filter( array_unique( $limit_types ) );
1134
1135
			if ( ! empty( $limit_types ) ) {
1136
				$ok = false;
1137
1138
				foreach ( $limit_types as $limit_type ) {
1139
					$limit_type = '.' . trim( $limit_type, ' .' );
1140
1141
					$pos = ( strlen( $file['name'] ) - strlen( $limit_type ) );
1142
1143
					if ( $pos === stripos( $file['name'], $limit_type ) ) {
1144
						$ok = true;
1145
1146
						break;
1147
					}
1148
				}
1149
1150
				if ( false === $ok ) {
1151
					$error = __( 'File type not allowed, please use one of the following: %s', 'pods' );
1152
					$error = sprintf( $error, '.' . implode( ', .', $limit_types ) );
1153
1154
					pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' );
1155
				}
1156
			}
1157
1158
			$custom_handler = apply_filters( 'pods_upload_handle', null, 'Filedata', $params->post_id, $params, $field );
1159
1160
			if ( null === $custom_handler ) {
1161
				$attachment_id = media_handle_upload( 'Filedata', $params->post_id );
1162
1163
				if ( is_object( $attachment_id ) ) {
1164
					$errors = array();
1165
1166
					foreach ( $attachment_id->errors['upload_error'] as $error_code => $error_message ) {
1167
						$errors[] = '[' . $error_code . '] ' . $error_message;
1168
					}
1169
1170
					pods_error( '<div style="color:#FF0000">Error: ' . implode( '</div><div>', $errors ) . '</div>' );
1171
				} else {
1172
					$attachment = get_post( $attachment_id, ARRAY_A );
1173
1174
					$attachment['filename'] = basename( $attachment['guid'] );
1175
1176
					$thumb = wp_get_attachment_image_src( $attachment['ID'], 'thumbnail', true );
1177
1178
					$attachment['thumbnail'] = '';
1179
1180
					if ( ! empty( $thumb[0] ) ) {
1181
						$attachment['thumbnail'] = $thumb[0];
1182
					}
1183
1184
					$attachment['link']      = get_permalink( $attachment['ID'] );
1185
					$attachment['edit_link'] = get_edit_post_link( $attachment['ID'] );
1186
					$attachment['download']  = wp_get_attachment_url( $attachment['ID'] );
1187
1188
					$attachment = apply_filters( 'pods_upload_attachment', $attachment, $params->post_id );
1189
1190
					wp_send_json( $attachment );
1191
				}
1192
			}
1193
		}
1194
1195
		die(); // KBAI!
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_ajax_upload() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1196
1197
	}
1198
1199
}
1200