PluginProbe
Advanced Custom Fields (ACF®) / 4.1.2
Advanced Custom Fields (ACF®) v4.1.2
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
190 lines 3.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_field_checkbox extends acf_field
4 {
5
6 /*
7 * __construct
8 *
9 * Set name / label needed for actions / filters
10 *
11 * @since 3.6
12 * @date 23/01/13
13 */
14
15 function __construct()
16 {
17 // vars
18 $this->name = 'checkbox';
19 $this->label = __("Checkbox",'acf');
20 $this->category = __("Choice",'acf');
21
22
23 // do not delete!
24 parent::__construct();
25 }
26
27
28 /*
29 * create_field()
30 *
31 * Create the HTML interface for your field
32 *
33 * @param $field - an array holding all the field's data
34 *
35 * @type action
36 * @since 3.6
37 * @date 23/01/13
38 */
39
40 function create_field( $field )
41 {
42 // vars
43 $defaults = array(
44 'layout' => 'vertical',
45 'choices' => array(),
46 );
47
48 $field = array_merge($defaults, $field);
49
50
51 // value must be array
52 if( !is_array($field['value']) )
53 {
54 // perhaps this is a default value with new lines in it?
55 if( strpos($field['value'], "\n") !== false )
56 {
57 // found multiple lines, explode it
58 $field['value'] = explode("\n", $field['value']);
59 }
60 else
61 {
62 $field['value'] = array( $field['value'] );
63 }
64 }
65
66
67 // trim value
68 $field['value'] = array_map('trim', $field['value']);
69
70
71 echo '<input type="hidden" name="' . $field['name'] . '" value="" />';
72 echo '<ul class="checkbox_list ' . $field['class'] . '">';
73
74
75 // checkbox saves an array
76 $field['name'] .= '[]';
77
78
79 // foreach choices
80 foreach($field['choices'] as $key => $value)
81 {
82 $selected = '';
83 if( in_array($key, $field['value']) )
84 {
85 $selected = 'checked="yes"';
86 }
87 if( isset($field['disabled']) && in_array($key, $field['disabled']) )
88 {
89 $selected .= ' disabled="true"';
90 }
91
92
93 // ID
94 // each checkbox ID is generated with the $key, however, the first checkbox must not use $key so that it matches the field's label for attribute
95 $id = $field['id'];
96
97 if( $key > 1 )
98 {
99 $id .= '-' . $key;
100 }
101
102 echo '<li><label><input id="' . $id . '" type="checkbox" class="' . $field['class'] . '" name="' . $field['name'] . '" value="' . $key . '" ' . $selected . ' />' . $value . '</label></li>';
103 }
104
105 echo '</ul>';
106 }
107
108
109 /*
110 * create_options()
111 *
112 * Create extra options for your field. This is rendered when editing a field.
113 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
114 *
115 * @type action
116 * @since 3.6
117 * @date 23/01/13
118 *
119 * @param $field - an array holding all the field's data
120 */
121
122 function create_options( $field )
123 {
124 // vars
125 $defaults = array(
126 'default_value' => '',
127 'choices' => '',
128 );
129
130 $field = array_merge($defaults, $field);
131 $key = $field['name'];
132
133
134 // implode checkboxes so they work in a textarea
135 if( is_array($field['choices']) )
136 {
137 foreach( $field['choices'] as $k => $v )
138 {
139 $field['choices'][ $k ] = $k . ' : ' . $v;
140 }
141 $field['choices'] = implode("\n", $field['choices']);
142 }
143
144 ?>
145 <tr class="field_option field_option_<?php echo $this->name; ?>">
146 <td class="label">
147 <label for=""><?php _e("Choices",'acf'); ?></label>
148 <p><?php _e("Enter each choice on a new line.",'acf'); ?></p>
149 <p><?php _e("For more control, you may specify both a value and label like this:",'acf'); ?></p>
150 <p><?php _e("red : Red",'acf'); ?><br /><?php _e("blue : Blue",'acf'); ?></p>
151 </td>
152 <td>
153 <?php
154
155 do_action('acf/create_field', array(
156 'type' => 'textarea',
157 'class' => 'textarea field_option-choices',
158 'name' => 'fields['.$key.'][choices]',
159 'value' => $field['choices'],
160 ));
161
162 ?>
163 </td>
164 </tr>
165 <tr class="field_option field_option_<?php echo $this->name; ?>">
166 <td class="label">
167 <label><?php _e("Default Value",'acf'); ?></label>
168 <p class="description"><?php _e("Enter each default value on a new line",'acf'); ?></p>
169 </td>
170 <td>
171 <?php
172
173 do_action('acf/create_field', array(
174 'type' => 'textarea',
175 'name' => 'fields['.$key.'][default_value]',
176 'value' => $field['default_value'],
177 ));
178
179 ?>
180 </td>
181 </tr>
182 <?php
183
184 }
185
186 }
187
188 new acf_field_checkbox();
189
190 ?>