1 <?php
2
3 4 5 6 7 8
9 abstract class Fieldmanager_Options extends Fieldmanager_Field {
10
11 12 13 14
15 public $data = array();
16
17 18 19 20
21 public $options = array();
22
23 24 25 26 27
28 public $grouped = false;
29
30 31 32 33
34 public $first_element = array();
35
36 37 38 39
40 public $multiple = False;
41
42 43 44 45
46 public $options_template = '';
47
48 49 50 51
52 private $has_built_data = False;
53
54 55 56 57 58
59 public function __construct( $label = '', $options = array() ) {
60 $this->sanitize = array( $this, 'sanitize' );
61 parent::__construct( $label, $options );
62
63 if ( !empty( $this->options ) ) {
64 $this->add_options( $this->options );
65 }
66
67
68 fm_add_style( 'fm_options_css', 'css/fieldmanager-options.css' );
69 }
70
71 72 73 74 75
76 public function sanitize( $value ) {
77 if ( isset( $value ) && is_array( $value ) && ! empty( $value ) ) {
78 return array_map( 'sanitize_text_field', $value );
79 } else {
80 return sanitize_text_field( $value );
81 }
82 }
83
84 85 86 87 88
89 public function add_options( $options ) {
90 $values = array_values( $options );
91 if ( isset( $values[0] ) && is_array( $values[0] ) ) {
92 foreach ( $options as $group => $data ) {
93 foreach ( $data as $value => $label ) {
94 $this->add_option_data( $value, $label, $group, $group );
95 }
96 }
97 } else {
98 $keys = array_keys( $options );
99 $use_name_as_value = ( array_keys( $keys ) === $keys );
100 foreach ( $options as $k => $v ) {
101 $this->add_option_data( $v, ( $use_name_as_value ? $v : $k ) );
102 }
103 }
104 }
105
106 107 108 109 110
111 public function form_data_elements( $value ) {
112
113 if ( !$this->has_built_data ) {
114 if ( $this->datasource ) {
115 $this->add_options( $this->datasource->get_items() );
116 }
117
118
119 if ( !empty( $this->first_element ) ) array_unshift( $this->data, $this->first_element );
120 $this->has_built_data = True;
121 }
122
123
124 if ( !is_array( $value ) && isset( $value ) ) {
125 $value = array( $value );
126 }
127
128
129 $form_data_elements_html = '';
130
131 if ( !empty( $this->data ) ) {
132
133 $current_group = '';
134
135 foreach( $this->data as $data_element ) {
136
137
138
139 if( $this->grouped && ( $current_group != $data_element['group'] ) ) {
140
141
142 if ( $current_group != '' ) $form_data_elements_html .= $this->form_data_end_group();
143
144
145 $form_data_elements_html .= $this->form_data_start_group( $data_element['group'] );
146
147
148 $current_group = $data_element['group'];
149 }
150
151
152 $form_data_elements_html .= $this->form_data_element( $data_element, $value );
153 }
154
155
156 if( $this->grouped || ( isset( $this->datasource ) && $this->datasource->grouped ) ) $form_data_elements_html .= $this->form_data_end_group();
157 }
158
159 return $form_data_elements_html;
160
161 }
162
163 164 165 166
167 public function form_data_element( $data_row, $value ) {
168 if ( !$this->options_template ) {
169 $tpl_slug = 'options-' . strtolower( str_replace( 'Fieldmanager_', '', get_class( $this ) ));
170 $this->options_template = fieldmanager_get_template( $tpl_slug );
171 }
172 ob_start();
173 include $this->options_template;
174 return ob_get_clean();
175 }
176
177 178 179 180 181 182 183
184 public function option_selected( $current_option, $options, $attribute ) {
185 if ( ( $options != null && !empty( $options ) ) && in_array( $current_option, $options ) ) return $attribute;
186 else return '';
187 }
188
189 190 191 192 193
194 public function presave_all( $values, $current_values ) {
195
196
197 if ( 1 !== $this->limit && '' === $values ) {
198 $values = null;
199 }
200
201 return parent::presave_all( $values, $current_values );
202 }
203
204 205 206 207 208
209 public function presave( $value, $current_value = array() ) {
210 if ( !empty( $this->datasource ) ) {
211 return $this->datasource->presave( $this, $value, $current_value );
212 }
213 foreach ( $this->validate as $func ) {
214 if ( !call_user_func( $func, $value ) ) {
215 $this->_failed_validation( sprintf(
216 __( 'Input "%1$s" is not valid for field "%2$s" ', 'fieldmanager' ),
217 (string) $value,
218 $this->label
219 ) );
220 }
221 }
222 return call_user_func( $this->sanitize, $value );
223 }
224
225 226 227 228
229 public function preload_alter_values( $values ) {
230 if ( $this->datasource ) return $this->datasource->preload_alter_values( $this, $values );
231 return $values;
232 }
233
234 235 236 237 238 239
240 public function presave_alter_values( $values, $current_values = array() ) {
241 if ( !empty( $this->datasource ) ) {
242 if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
243 $this->skip_save = true;
244 } elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
245 $this->skip_save = true;
246 }
247 return $this->datasource->presave_alter_values( $this, $values, $current_values );
248 }
249 return $values;
250 }
251
252
253 254 255 256 257 258 259 260
261 protected function add_option_data( $name, $value, $group=null, $group_id=null ) {
262 $data = array(
263 'name' => $name,
264 'value' => $value
265 );
266 if( isset( $group ) ) $data['group'] = $group;
267 if( isset( $group_id ) ) $data['group_id'] = $group_id;
268
269 $this->data[] = $data;
270 }
271
272 273 274 275 276 277 278 279 280
281 protected function add_meta_boxes_to_remove( &$meta_boxes_to_remove ) {
282 if ( $this->remove_default_meta_boxes && get_class( $this->datasource ) == 'Fieldmanager_Datasource_Term' ) {
283
284 $meta_boxes = array();
285 foreach( $this->datasource->get_taxonomies() as $taxonomy ) {
286
287 $taxonomy_obj = get_taxonomy( $taxonomy );
288 if ( false !== $taxonomy_obj ) {
289 if ( $taxonomy_obj->hierarchical )
290 $id = $taxonomy . "div";
291 else
292 $id = 'tagsdiv-' . $taxonomy;
293
294 $meta_boxes[$id] = array(
295 'id' => $id,
296 'context' => 'side'
297 );
298 }
299 }
300
301
302 $meta_boxes_to_remove = array_merge( $meta_boxes_to_remove, $meta_boxes );
303 }
304 }
305 }
306
307 require_once( dirname( __FILE__ ) . '/class-fieldmanager-select.php' );
308
309 require_once( dirname( __FILE__ ) . '/class-fieldmanager-radios.php' );
310
311 require_once( dirname( __FILE__ ) . '/class-fieldmanager-checkboxes.php' );
312