PluginProbe
Advanced Custom Fields (ACF®) / 3.1.5
Advanced Custom Fields (ACF®) v3.1.5
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
173 lines 3.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_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'])) $field['value'] = array();
40
41 // no choices
42 if(empty($field['choices']))
43 {
44 echo '<p>' . __("No choices to choose from",'acf') . '</p>';
45 return false;
46 }
47
48 // html
49 echo '<ul class="checkbox_list '.$field['class'].'">';
50 echo '<input type="hidden" name="'.$field['name'].'" value="" />';
51 // checkbox saves an array
52 $field['name'] .= '[]';
53
54 // foreach choices
55 foreach($field['choices'] as $key => $value)
56 {
57 $selected = '';
58 if(in_array($key, $field['value']))
59 {
60 $selected = 'checked="yes"';
61 }
62 echo '<li><label><input type="checkbox" class="' . $field['class'] . '" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>';
63 }
64
65 echo '</ul>';
66
67 }
68
69
70 /*--------------------------------------------------------------------------------------
71 *
72 * create_options
73 *
74 * @author Elliot Condon
75 * @since 2.0.6
76 * @updated 2.2.0
77 *
78 *-------------------------------------------------------------------------------------*/
79
80 function create_options($key, $field)
81 {
82 // defaults
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 <tr class="field_option field_option_<?php echo $this->name; ?>">
100 <td class="label">
101 <label for=""><?php _e("Choices",'acf'); ?></label>
102 <p class="description"><?php _e("Enter your choices one per line<br />
103 <br />
104 Red<br />
105 Blue<br />
106 <br />
107 or<br />
108 <br />
109 red : Red<br />
110 blue : Blue",'acf'); ?></p>
111 </td>
112 <td>
113 <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea>
114 </td>
115 </tr>
116 <?php
117 }
118
119
120 /*--------------------------------------------------------------------------------------
121 *
122 * pre_save_field
123 * - called just before saving the field to the database.
124 *
125 * @author Elliot Condon
126 * @since 2.2.0
127 *
128 *-------------------------------------------------------------------------------------*/
129
130 function pre_save_field($field)
131 {
132 // defaults
133 $field['choices'] = isset($field['choices']) ? $field['choices'] : '';
134
135 // vars
136 $new_choices = array();
137
138 // explode choices from each line
139 if(strpos($field['choices'], "\n") !== false)
140 {
141 // found multiple lines, explode it
142 $field['choices'] = explode("\n", $field['choices']);
143 }
144 else
145 {
146 // no multiple lines!
147 $field['choices'] = array($field['choices']);
148 }
149
150 // key => value
151 foreach($field['choices'] as $choice)
152 {
153 if(strpos($choice, ' : ') !== false)
154 {
155 $choice = explode(' : ', $choice);
156 $new_choices[trim($choice[0])] = trim($choice[1]);
157 }
158 else
159 {
160 $new_choices[trim($choice)] = trim($choice);
161 }
162 }
163
164 // update choices
165 $field['choices'] = $new_choices;
166
167 // return updated field
168 return $field;
169
170 }
171
172 }
173 ?>