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 / radio.php
radio.php
211 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class acf_Radio 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 = 'radio';
21 $this->title = __('Radio Button','acf');
22
23 }
24
25
26 /*--------------------------------------------------------------------------------------
27 *
28 * create_field
29 *
30 * @author Elliot Condon
31 * @since 2.0.5
32 * @updated 2.2.0
33 *
34 *-------------------------------------------------------------------------------------*/
35
36 function create_field($field)
37 {
38 // defaults
39 $field['layout'] = isset($field['layout']) ? $field['layout'] : 'vertical';
40 $field['choices'] = isset($field['choices']) ? $field['choices'] : array();
41
42 // no choices
43 if(empty($field['choices']))
44 {
45 echo '<p>' . __("No choices to choose from",'acf') . '</p>';
46 return false;
47 }
48
49 echo '<ul class="radio_list ' . $field['class'] . ' ' . $field['layout'] . '">';
50
51 foreach($field['choices'] as $key => $value)
52 {
53 $selected = '';
54
55 if($key == $field['value'])
56 {
57 $selected = 'checked="checked" data-checked="checked"';
58 }
59
60 echo '<li><label><input type="radio" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>';
61 }
62
63 echo '</ul>';
64
65 }
66
67
68 /*--------------------------------------------------------------------------------------
69 *
70 * create_options
71 *
72 * @author Elliot Condon
73 * @since 2.0.6
74 * @updated 2.2.0
75 *
76 *-------------------------------------------------------------------------------------*/
77
78 function create_options($key, $field)
79 {
80 // defaults
81 $field['layout'] = isset($field['layout']) ? $field['layout'] : 'vertical';
82 $field['default_value'] = isset($field['default_value']) ? $field['default_value'] : '';
83
84
85 // implode checkboxes so they work in a textarea
86 if(isset($field['choices']) && is_array($field['choices']))
87 {
88 foreach($field['choices'] as $choice_key => $choice_val)
89 {
90 $field['choices'][$choice_key] = $choice_key.' : '.$choice_val;
91 }
92 $field['choices'] = implode("\n", $field['choices']);
93 }
94 else
95 {
96 $field['choices'] = "";
97 }
98
99 ?>
100
101
102 <tr class="field_option field_option_<?php echo $this->name; ?>">
103 <td class="label">
104 <label for=""><?php _e("Choices",'acf'); ?></label>
105 <p class="description"><?php _e("Enter your choices one per line<br />
106 <br />
107 Red<br />
108 Blue<br />
109 <br />
110 or<br />
111 <br />
112 red : Red<br />
113 blue : Blue",'acf'); ?></p>
114 </td>
115 <td>
116 <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea>
117 </td>
118 </tr>
119 <tr class="field_option field_option_<?php echo $this->name; ?>">
120 <td class="label">
121 <label><?php _e("Default Value",'acf'); ?></label>
122 </td>
123 <td>
124 <?php
125 $this->parent->create_field(array(
126 'type' => 'text',
127 'name' => 'fields['.$key.'][default_value]',
128 'value' => $field['default_value'],
129 ));
130 ?>
131 </td>
132 </tr>
133 <tr class="field_option field_option_<?php echo $this->name; ?>">
134 <td class="label">
135 <label for=""><?php _e("Layout",'acf'); ?></label>
136 </td>
137 <td>
138 <?php
139 $this->parent->create_field(array(
140 'type' => 'radio',
141 'name' => 'fields['.$key.'][layout]',
142 'value' => $field['layout'],
143 'layout' => 'horizontal',
144 'choices' => array(
145 'vertical' => 'Vertical',
146 'horizontal' => 'Horizontal'
147 )
148 ));
149 ?>
150 </td>
151 </tr>
152
153
154 <?php
155 }
156
157
158 /*--------------------------------------------------------------------------------------
159 *
160 * pre_save_field
161 * - called just before saving the field to the database.
162 *
163 * @author Elliot Condon
164 * @since 2.2.0
165 *
166 *-------------------------------------------------------------------------------------*/
167
168 function pre_save_field($field)
169 {
170 // defaults
171 $field['choices'] = isset($field['choices']) ? $field['choices'] : '';
172
173 // vars
174 $new_choices = array();
175
176 // explode choices from each line
177 if(strpos($field['choices'], "\n") !== false)
178 {
179 // found multiple lines, explode it
180 $field['choices'] = explode("\n", $field['choices']);
181 }
182 else
183 {
184 // no multiple lines!
185 $field['choices'] = array($field['choices']);
186 }
187
188 // key => value
189 foreach($field['choices'] as $choice)
190 {
191 if(strpos($choice, ' : ') !== false)
192 {
193 $choice = explode(' : ', $choice);
194 $new_choices[trim($choice[0])] = trim($choice[1]);
195 }
196 else
197 {
198 $new_choices[trim($choice)] = trim($choice);
199 }
200 }
201
202 // update choices
203 $field['choices'] = $new_choices;
204
205 // return updated field
206 return $field;
207
208 }
209 }
210
211 ?>