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
|
|
|
public $button_label = ''; |
17
|
|
|
|
18
|
|
|
public $window_button_label = ''; |
19
|
|
|
|
20
|
|
|
public $window_label = ''; |
21
|
|
|
|
22
|
|
|
// empty for all types. available types: audio, video, image and all WordPress-recognized mime types |
23
|
|
|
public $field_type = ''; |
24
|
|
|
|
25
|
|
|
// alt, author, caption, dateFormatted, description, editLink, filename, height, icon, id, link, menuOrder, mime, name, status, subtype, title, type, uploadedTo, url, width |
26
|
|
|
public $value_type = 'id'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Admin initialization actions |
30
|
|
|
*/ |
31
|
|
|
public function admin_init() { |
32
|
|
|
$this->button_label = __( 'Select File', 'carbon-fields' ); |
33
|
|
|
$this->window_button_label = __( 'Select File', 'carbon-fields' ); |
34
|
|
|
$this->window_label = __( 'Files', 'carbon-fields' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Change the type of the field |
39
|
|
|
* |
40
|
|
|
* @param string $type |
41
|
|
|
*/ |
42
|
|
|
public function set_type( $type ) { |
43
|
|
|
$this->field_type = $type; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Change the value type of the field. |
49
|
|
|
* |
50
|
|
|
* @param string $value_type |
51
|
|
|
*/ |
52
|
|
|
public function set_value_type( $value_type ) { |
53
|
|
|
$this->value_type = $value_type; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
59
|
|
|
* |
60
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function to_json( $load ) { |
64
|
|
|
$field_data = parent::to_json( $load ); |
65
|
|
|
|
66
|
|
|
$value = $this->get_value(); |
67
|
|
|
|
68
|
|
|
$attachment_metadata = array(); |
69
|
|
|
if ( $value ) { |
70
|
|
|
$attachment_metadata = Helper::get_attachment_metadata( $value, $this->value_type ); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$field_data = array_merge( $field_data, $attachment_metadata, array( |
74
|
|
|
'button_label' => $this->button_label, |
75
|
|
|
'window_button_label' => $this->window_button_label, |
76
|
|
|
'window_label' => $this->window_label, |
77
|
|
|
'type_filter' => $this->field_type, |
78
|
|
|
'value_type' => $this->value_type, |
79
|
|
|
) ); |
80
|
|
|
|
81
|
|
|
return $field_data; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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.