Completed
Push — development ( 86ecdb...a89d07 )
by
unknown
02:11
created

Media_Gallery_Field::get_duplicates_allowed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
 * Allows to create a set of checkboxes where multiple can be selected.
11
 */
12
class Media_Gallery_Field extends Predefined_Options_Field {
13
14
	/**
15
	 * Add file button label
16
	 *
17
	 * @var string
18
	 */
19
	protected $button_label = '';
20
21
	/**
22
	 * Select file button label
23
	 *
24
	 * @var string
25
	 */
26
	protected $window_button_label = '';
27
28
	/**
29
	 * Window title label
30
	 *
31
	 * @var string
32
	 */
33
	protected $window_label = '';
34
35
	/**
36
	 * File type filter. Leave a blank string for any file type.
37
	 * Available types: audio, video, image and all WordPress-recognized mime types
38
	 *
39
	 * @var string|array
40
	 */
41
	protected $file_type = '';
42
43
	/**
44
	 * What value to store
45
	 *
46
	 * @var string
47
	 */
48
	protected $value_type = 'id';
49
50
	/**
51
	 * Default field value
52
	 *
53
	 * @var array
54
	 */
55
	protected $default_value = array();
56
57
	/**
58
	 * Allow items to be added multiple times
59
	 *
60
	 * @var boolean
61
	 */
62
	protected $duplicates_allowed = true;
63
64
	/**
65
	 * Create a field from a certain type with the specified label.
66
	 *
67
	 * @param string $type  Field type
68
	 * @param string $name  Field name
69
	 * @param string $label Field label
70
	 */
71
	public function __construct( $type, $name, $label ) {
72
		$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) );
73
		parent::__construct( $type, $name, $label );
74
	}
75
76
	/**
77
	 * Admin initialization actions
78
	 */
79
	public function admin_init() {
80
		$this->button_label = __( 'Add File', 'carbon-fields' );
81
		$this->window_button_label = __( 'Select File', 'carbon-fields' );
82
		$this->window_label = __( 'Files', 'carbon-fields' );
83
	}
84
85
	/**
86
	 * Change the type of the field
87
	 *
88
	 * @param string $type
89
	 */
90
	public function set_type( $type ) {
91
		$this->file_type = $type;
92
		return $this;
93
	}
94
95
	/**
96
	 * Get whether entry duplicates are allowed.
97
	 *
98
	 * @return boolean
99
	 */
100
	public function get_duplicates_allowed() {
101
		return $this->duplicates_allowed;
102
	}
103
104
	/**
105
	 * Set whether entry duplicates are allowed.
106
	 *
107
	 * @param  boolean $allowed
108
	 * @return Field   $this
109
	 */
110
	public function set_duplicates_allowed( $allowed ) {
111
		$this->duplicates_allowed = $allowed;
112
		return $this;
113
	}
114
115
	/**
116
	 * Load the field value from an input array based on it's name
117
	 *
118
	 * @param  array $input Array of field names and values.
119
	 * @return Field $this
120
	 */
121 View Code Duplication
	public function set_value_from_input( $input ) {
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...
122
		if ( ! isset( $input[ $this->name ] ) ) {
123
			$this->set_value( array() );
124
		} else {
125
			$value = stripslashes_deep( $input[ $this->name ] );
126
			if ( is_array( $value ) ) {
127
				$value = array_values( $value );
128
			}
129
			$this->set_value( $value );
130
		}
131
		return $this;
132
	}
133
134
	/**
135
	 * Converts the field values into a usable associative array.
136
	 *
137
	 * @access protected
138
	 * @return array
139
	 */
140
	protected function value_to_json() {
141
		$value_set  = $this->get_value();
142
		$value      = array();
143
		$value_meta = array();
144
145
		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...
146
			$attachment_id     = absint( $attachment_id );
147
			$attachment_metata = Helper::get_attachment_metadata( $attachment_id, $this->value_type );
148
149
			$value[] = $attachment_id;
150
			$value_meta[ $attachment_id ] = $attachment_metata;
151
		}
152
153
		return array(
154
			'value'      => $value,
155
			'value_meta' => $value_meta,
156
		);
157
	}
158
159
	/**
160
	 * Returns an array that holds the field data, suitable for JSON representation.
161
	 *
162
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
163
	 * @return array
164
	 */
165
	public function to_json( $load ) {
166
		$field_data = parent::to_json( $load );
167
168
		$field_data = array_merge( $field_data, $this->value_to_json(), array(
169
			'options'             => $this->parse_options( $this->get_options() ),
170
			'value_type'          => $this->value_type,
171
			'button_label'        => $this->button_label,
172
			'window_label'        => $this->window_label,
173
			'window_button_label' => $this->window_button_label,
174
			'type_filter'         => $this->file_type,
175
			'duplicates_allowed'  => $this->get_duplicates_allowed(),
176
		) );
177
178
		return $field_data;
179
	}
180
}
181