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

Media_Gallery_Field::admin_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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