Completed
Push — development ( d05c14...79d660 )
by
unknown
03:42
created

File_Field::to_json()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 20
ccs 0
cts 15
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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 );
0 ignored issues
show
Bug introduced by
It seems like $value defined by $this->get_value() on line 66 can also be of type array; however, Carbon_Fields\Helper\Hel...t_attachment_metadata() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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