Completed
Pull Request — development (#536)
by
unknown
02:12
created

Media_Gallery_Field::set_value_from_input()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Value_Set\Value_Set;
6
use Carbon_Fields\Helper\Helper;
7
8
/**
9
 * Set field class.
10
 *
11
 * Allows selecting multiple attachments and stores
12
 * their IDs in the Database.
13
 */
14
class Media_Gallery_Field extends Field {
15
16
	/**
17
	 * File type filter. Leave a blank string for any file type.
18
	 * Available types: audio, video, image and all WordPress-recognized mime types
19
	 *
20
	 * @var string|array
21
	 */
22
	protected $file_type = '';
23
24
	/**
25
	 * What value to store
26
	 *
27
	 * @var string
28
	 */
29
	protected $value_type = 'id';
30
31
	/**
32
	 * Default field value
33
	 *
34
	 * @var array
35
	 */
36
	protected $default_value = array();
37
38
	/**
39
	 * Allow items to be added multiple times
40
	 *
41
	 * @var boolean
42
	 */
43
	protected $duplicates_allowed = true;
44
45
	/**
46
	 * Toggle the inline edit functionality
47
	 *
48
	 * @var boolean
49
	 */
50
	protected $can_edit_inline = true;
51
52
	/**
53
	 * Create a field from a certain type with the specified label.
54
	 *
55
	 * @param string $type  Field type
56
	 * @param string $name  Field name
57
	 * @param string $label Field label
58
	 */
59
	public function __construct( $type, $name, $label ) {
60
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
61
		parent::__construct( $type, $name, $label );
62
	}
63
64
	/**
65
	 * Change the type of the field
66
	 *
67
	 * @param string $type
68
	 */
69
	public function set_type( $type ) {
70
		$this->file_type = $type;
71
		return $this;
72
	}
73
74
	/**
75
	 * Get whether entry duplicates are allowed.
76
	 *
77
	 * @return boolean
78
	 */
79
	public function get_duplicates_allowed() {
80
		return $this->duplicates_allowed;
81
	}
82
83
	/**
84
	 * Set whether entry duplicates are allowed.
85
	 *
86
	 * @param  boolean $allowed
87
	 * @return self    $this
88
	 */
89
	public function set_duplicates_allowed( $allowed ) {
90
		$this->duplicates_allowed = $allowed;
91
		return $this;
92
	}
93
94
	/**
95
	 * Set wether the edit functionality will open inline or in the media popup
96
	 *
97
	 * @param boolean $can_edit_inline
98
	 * @param  self   $this
0 ignored issues
show
Bug introduced by
There is no parameter named $this. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99
	 */
100
	public function set_edit_inline( $can_edit_inline )
101
	{
102
		$this->can_edit_inline = $can_edit_inline;
103
		return $this;
104
	}
105
106
	/**
107
	 * Load the field value from an input array based on its name
108
	 *
109
	 * @param  array $input Array of field names and values.
110
	 * @return self  $this
111
	 */
112
	public function set_value_from_input( $input ) {
113
		if ( ! isset( $input[ $this->name ] ) ) {
114
			$this->set_value( array() );
115
		} else {
116
			$value = stripslashes_deep( $input[ $this->name ] );
117
			if ( is_array( $value ) ) {
118
				$value = array_values( $value );
119
			}
120
			$this->set_value( $value );
121
		}
122
		return $this;
123
	}
124
125
	/**
126
	 * Converts the field values into a usable associative array.
127
	 *
128
	 * @access protected
129
	 * @return array
130
	 */
131
	protected function value_to_json() {
132
		$value_set  = $this->get_value();
133
		$value_meta = array();
134
135
		foreach ( $value_set as $attachment_id ) {
0 ignored issues
show
Bug introduced by
The expression $value_set of type string|array|null 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...
136
			$attachment_id     = absint( $attachment_id );
137
			$attachment_metata = Helper::get_attachment_metadata( $attachment_id, $this->value_type );
138
139
			$value_meta[ $attachment_id ] = $attachment_metata;
140
		}
141
142
		return array(
143
			'value'      => array_map( 'absint', $value_set ),
144
			'value_meta' => $value_meta,
145
		);
146
	}
147
148
	/**
149
	 * Returns an array that holds the field data, suitable for JSON representation.
150
	 *
151
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
152
	 * @return array
153
	 */
154
	public function to_json( $load ) {
155
		$field_data = parent::to_json( $load );
156
157
		$field_data = array_merge( $field_data, $this->value_to_json(), array(
158
			'value_type'          => $this->value_type,
159
			'type_filter'         => $this->file_type,
160
			'can_edit_inline'         => $this->can_edit_inline,
161
			'duplicates_allowed'  => $this->get_duplicates_allowed(),
162
		) );
163
164
		return $field_data;
165
	}
166
}
167