|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Helper\Helper; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* File upload field class. |
|
10
|
|
|
* |
|
11
|
|
|
* Allows selecting and saving a media attachment file, |
|
12
|
|
|
* where the file ID is saved in the database. |
|
13
|
|
|
*/ |
|
14
|
|
|
class File_Field extends Field { |
|
15
|
|
|
|
|
16
|
|
|
// empty for all types. available types: audio, video, image and all WordPress-recognized mime types |
|
17
|
|
|
public $field_type = ''; |
|
18
|
|
|
|
|
19
|
|
|
// alt, author, caption, dateFormatted, description, editLink, filename, height, icon, id, link, menuOrder, mime, name, status, subtype, title, type, uploadedTo, url, width |
|
20
|
|
|
public $value_type = 'id'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Change the type of the field |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $type |
|
26
|
|
|
*/ |
|
27
|
|
|
public function set_type( $type ) { |
|
28
|
|
|
$this->field_type = $type; |
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Change the value type of the field. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $value_type |
|
36
|
|
|
*/ |
|
37
|
|
|
public function set_value_type( $value_type ) { |
|
38
|
|
|
$this->value_type = $value_type; |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
|
44
|
|
|
* |
|
45
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function to_json( $load ) { |
|
49
|
|
|
$field_data = parent::to_json( $load ); |
|
50
|
|
|
|
|
51
|
|
|
$value = $this->get_value(); |
|
52
|
|
|
|
|
53
|
|
|
$attachment_metadata = array(); |
|
54
|
|
|
if ( $value ) { |
|
55
|
|
|
$attachment_metadata = Helper::get_attachment_metadata( $value, $this->value_type ); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$field_data = array_merge( $field_data, $attachment_metadata, array( |
|
59
|
|
|
'type_filter' => $this->field_type, |
|
60
|
|
|
'value_type' => $this->value_type, |
|
61
|
|
|
) ); |
|
62
|
|
|
|
|
63
|
|
|
return $field_data; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.