PluginProbe
Advanced Custom Fields (ACF®) / 3.0.4
Advanced Custom Fields (ACF®) v3.0.4
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / core / fields / select.php

select.php in Advanced Custom Fields (ACF®) 3.0.4, at core/fields/select.php

291 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class acf_Select extends acf_Field
4 {
5
6 /*--------------------------------------------------------------------------------------
7 *
8 * Constructor
9 *
10 * @author Elliot Condon
11 * @since 1.0.0
12 * @updated 2.2.0
13 *
14 *-------------------------------------------------------------------------------------*/
15
16 function __construct($parent)
17 {
18 parent::__construct($parent);
19
20 $this->name = 'select';
21 $this->title = __("Select",'acf');
22
23 }
24
25
26
27 /*--------------------------------------------------------------------------------------
28 *
29 * create_field
30 *
31 * @author Elliot Condon
32 * @since 2.0.5
33 * @updated 2.2.0
34 *
35 *-------------------------------------------------------------------------------------*/
36
37 function create_field($field)
38 {
39 // defaults
40 $field['value'] = isset($field['value']) ? $field['value'] : array();
41 $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false;
42 $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : false;
43 $field['choices'] = isset($field['choices']) ? $field['choices'] : array();
44 $field['optgroup'] = isset($field['optgroup']) ? $field['optgroup'] : false;
45
46
47 // no choices
48 if(empty($field['choices']))
49 {
50 echo '<p>' . __("No choices to choose from",'acf') . '</p>';
51 return false;
52 }
53
54 // multiple select
55 $multiple = '';
56 if($field['multiple'] == '1')
57 {
58 $multiple = ' multiple="multiple" size="5" ';
59 $field['name'] .= '[]';
60 }
61
62 // html
63 echo '<select id="' . $field['name'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >';
64
65 // null
66 if($field['allow_null'] == '1')
67 {
68 echo '<option value="null"> - Select - </option>';
69 }
70
71 // loop through values and add them as options
72 foreach($field['choices'] as $key => $value)
73 {
74 if($field['optgroup'])
75 {
76 // this select is grouped with optgroup
77 if($key != '') echo '<optgroup label="'.$key.'">';
78
79 if($value)
80 {
81 foreach($value as $id => $label)
82 {
83 $selected = '';
84 if(is_array($field['value']) && in_array($id, $field['value']))
85 {
86 // 2. If the value is an array (multiple select), loop through values and check if it is selected
87 $selected = 'selected="selected"';
88 }
89 else
90 {
91 // 3. this is not a multiple select, just check normaly
92 if($id == $field['value'])
93 {
94 $selected = 'selected="selected"';
95 }
96 }
97 echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>';
98 }
99 }
100
101 if($key != '') echo '</optgroup>';
102 }
103 else
104 {
105 $selected = '';
106 if(is_array($field['value']) && in_array($key, $field['value']))
107 {
108 // 2. If the value is an array (multiple select), loop through values and check if it is selected
109 $selected = 'selected="selected"';
110 }
111 else
112 {
113 // 3. this is not a multiple select, just check normaly
114 if($key == $field['value'])
115 {
116 $selected = 'selected="selected"';
117 }
118 }
119 echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
120 }
121
122
123
124 }
125
126 echo '</select>';
127 }
128
129
130 /*--------------------------------------------------------------------------------------
131 *
132 * create_options
133 *
134 * @author Elliot Condon
135 * @since 2.0.6
136 * @updated 2.2.0
137 *
138 *-------------------------------------------------------------------------------------*/
139
140 function create_options($key, $field)
141 {
142 // defaults
143 $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : '0';
144 $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : '0';
145 $field['default_value'] = isset($field['default_value']) ? $field['default_value'] : '';
146
147 // implode selects so they work in a textarea
148 if(isset($field['choices']) && is_array($field['choices']))
149 {
150 foreach($field['choices'] as $choice_key => $choice_val)
151 {
152 $field['choices'][$choice_key] = $choice_key.' : '.$choice_val;
153 }
154 $field['choices'] = implode("\n", $field['choices']);
155 }
156 else
157 {
158 $field['choices'] = "";
159 }
160
161 ?>
162 <tr class="field_option field_option_<?php echo $this->name; ?>">
163 <td class="label">
164 <label for=""><?php _e("Choices",'acf'); ?></label>
165 <p class="description"><?php _e("Enter your choices one per line<br />
166 <br />
167 Red<br />
168 Blue<br />
169 <br />
170 or<br />
171 <br />
172 red : Red<br />
173 blue : Blue",'acf'); ?></p>
174 </td>
175 <td>
176 <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea>
177 </td>
178 </tr>
179 <tr class="field_option field_option_<?php echo $this->name; ?>">
180 <td class="label">
181 <label><?php _e("Default Value",'acf'); ?></label>
182 </td>
183 <td>
184 <?php
185 $this->parent->create_field(array(
186 'type' => 'text',
187 'name' => 'fields['.$key.'][default_value]',
188 'value' => $field['default_value'],
189 ));
190 ?>
191 </td>
192 </tr>
193 <tr class="field_option field_option_<?php echo $this->name; ?>">
194 <td class="label">
195 <label><?php _e("Allow Null?",'acf'); ?></label>
196 </td>
197 <td>
198 <?php
199 $this->parent->create_field(array(
200 'type' => 'radio',
201 'name' => 'fields['.$key.'][allow_null]',
202 'value' => $field['allow_null'],
203 'choices' => array(
204 '1' => 'Yes',
205 '0' => 'No',
206 ),
207 'layout' => 'horizontal',
208 ));
209 ?>
210 </td>
211 </tr>
212 <tr class="field_option field_option_<?php echo $this->name; ?>">
213 <td class="label">
214 <label><?php _e("Select multiple values?",'acf'); ?></label>
215 </td>
216 <td>
217 <?php
218 $this->parent->create_field(array(
219 'type' => 'radio',
220 'name' => 'fields['.$key.'][multiple]',
221 'value' => $field['multiple'],
222 'choices' => array(
223 '1' => 'Yes',
224 '0' => 'No',
225 ),
226 'layout' => 'horizontal',
227 ));
228 ?>
229 </td>
230 </tr>
231
232 <?php
233 }
234
235
236 /*--------------------------------------------------------------------------------------
237 *
238 * pre_save_field
239 * - called just before saving the field to the database.
240 *
241 * @author Elliot Condon
242 * @since 2.2.0
243 *
244 *-------------------------------------------------------------------------------------*/
245
246 function pre_save_field($field)
247 {
248 // defaults
249 $field['choices'] = isset($field['choices']) ? $field['choices'] : '';
250
251 // vars
252 $new_choices = array();
253
254 // explode choices from each line
255 if(strpos($field['choices'], "\n") !== false)
256 {
257 // found multiple lines, explode it
258 $field['choices'] = explode("\n", $field['choices']);
259 }
260 else
261 {
262 // no multiple lines!
263 $field['choices'] = array($field['choices']);
264 }
265
266 // key => value
267 foreach($field['choices'] as $choice)
268 {
269 if(strpos($choice, ' : ') !== false)
270 {
271 $choice = explode(' : ', $choice);
272 $new_choices[trim($choice[0])] = trim($choice[1]);
273 }
274 else
275 {
276 $new_choices[trim($choice)] = trim($choice);
277 }
278 }
279
280 // update choices
281 $field['choices'] = $new_choices;
282
283 // return updated field
284 return $field;
285
286 }
287
288
289 }
290
291 ?>