PluginProbe
Advanced Custom Fields (ACF®) / 3.3.1
Advanced Custom Fields (ACF®) v3.3.1
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 / checkbox.php
checkbox.php
184 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class acf_Checkbox 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 = 'checkbox';
21 $this->title = __("Checkbox",'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 if(empty($field['value']))
40 {
41 $field['value'] = array();
42 }
43
44
45 // single value to array conversion
46 if( !is_array($field['value']) )
47 {
48 $field['value'] = array( $field['value'] );
49 }
50
51
52 // no choices
53 if(empty($field['choices']))
54 {
55 echo '<p>' . __("No choices to choose from",'acf') . '</p>';
56 return false;
57 }
58
59
60 // html
61 echo '<ul class="checkbox_list '.$field['class'].'">';
62 echo '<input type="hidden" name="'.$field['name'].'" value="" />';
63 // checkbox saves an array
64 $field['name'] .= '[]';
65
66 // foreach choices
67 foreach($field['choices'] as $key => $value)
68 {
69 $selected = '';
70 if(in_array($key, $field['value']))
71 {
72 $selected = 'checked="yes"';
73 }
74 echo '<li><label><input type="checkbox" class="' . $field['class'] . '" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>';
75 }
76
77 echo '</ul>';
78
79 }
80
81
82 /*--------------------------------------------------------------------------------------
83 *
84 * create_options
85 *
86 * @author Elliot Condon
87 * @since 2.0.6
88 * @updated 2.2.0
89 *
90 *-------------------------------------------------------------------------------------*/
91
92 function create_options($key, $field)
93 {
94 // defaults
95
96
97 // implode checkboxes so they work in a textarea
98 if(isset($field['choices']) && is_array($field['choices']))
99 {
100 foreach($field['choices'] as $choice_key => $choice_val)
101 {
102 $field['choices'][$choice_key] = $choice_key.' : '.$choice_val;
103 }
104 $field['choices'] = implode("\n", $field['choices']);
105 }
106 else
107 {
108 $field['choices'] = "";
109 }
110 ?>
111 <tr class="field_option field_option_<?php echo $this->name; ?>">
112 <td class="label">
113 <label for=""><?php _e("Choices",'acf'); ?></label>
114 <p class="description"><?php _e("Enter your choices one per line",'acf'); ?><br />
115 <br />
116 <?php _e("Red",'acf'); ?><br />
117 <?php _e("Blue",'acf'); ?><br />
118 <br />
119 <?php _e("red : Red",'acf'); ?><br />
120 <?php _e("blue : Blue",'acf'); ?><br />
121 </p>
122 </td>
123 <td>
124 <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea>
125 </td>
126 </tr>
127 <?php
128 }
129
130
131 /*--------------------------------------------------------------------------------------
132 *
133 * pre_save_field
134 * - called just before saving the field to the database.
135 *
136 * @author Elliot Condon
137 * @since 2.2.0
138 *
139 *-------------------------------------------------------------------------------------*/
140
141 function pre_save_field($field)
142 {
143 // defaults
144 $field['choices'] = isset($field['choices']) ? $field['choices'] : '';
145
146 // vars
147 $new_choices = array();
148
149 // explode choices from each line
150 if(strpos($field['choices'], "\n") !== false)
151 {
152 // found multiple lines, explode it
153 $field['choices'] = explode("\n", $field['choices']);
154 }
155 else
156 {
157 // no multiple lines!
158 $field['choices'] = array($field['choices']);
159 }
160
161 // key => value
162 foreach($field['choices'] as $choice)
163 {
164 if(strpos($choice, ' : ') !== false)
165 {
166 $choice = explode(' : ', $choice);
167 $new_choices[trim($choice[0])] = trim($choice[1]);
168 }
169 else
170 {
171 $new_choices[trim($choice)] = trim($choice);
172 }
173 }
174
175 // update choices
176 $field['choices'] = $new_choices;
177
178 // return updated field
179 return $field;
180
181 }
182
183 }
184 ?>