taxonomy.php
| 1 | <?php |
| 2 | |
| 3 | class acf_field_taxonomy extends acf_field |
| 4 | { |
| 5 | |
| 6 | var $defaults; |
| 7 | |
| 8 | |
| 9 | /* |
| 10 | * __construct |
| 11 | * |
| 12 | * Set name / label needed for actions / filters |
| 13 | * |
| 14 | * @since 3.6 |
| 15 | * @date 23/01/13 |
| 16 | */ |
| 17 | |
| 18 | function __construct() |
| 19 | { |
| 20 | // vars |
| 21 | $this->name = 'taxonomy'; |
| 22 | $this->label = __("Taxonomy",'acf'); |
| 23 | $this->category = __("Relational",'acf'); |
| 24 | |
| 25 | |
| 26 | // settings |
| 27 | $this->defaults = array( |
| 28 | 'taxonomy' => 'category', |
| 29 | 'field_type' => 'checkbox', |
| 30 | 'allow_null' => 0, |
| 31 | 'load_save_terms' => 0, |
| 32 | 'multiple' => 0, |
| 33 | 'return_format' => 'id' |
| 34 | ); |
| 35 | |
| 36 | |
| 37 | // do not delete! |
| 38 | parent::__construct(); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * load_value() |
| 45 | * |
| 46 | * This filter is appied to the $value after it is loaded from the db |
| 47 | * |
| 48 | * @type filter |
| 49 | * @since 3.6 |
| 50 | * @date 23/01/13 |
| 51 | * |
| 52 | * @param $value - the value found in the database |
| 53 | * @param $post_id - the $post_id from which the value was loaded from |
| 54 | * @param $field - the field array holding all the field options |
| 55 | * |
| 56 | * @return $value - the value to be saved in te database |
| 57 | */ |
| 58 | |
| 59 | function load_value( $value, $post_id, $field ) |
| 60 | { |
| 61 | // vars |
| 62 | $field = array_merge($this->defaults, $field); |
| 63 | |
| 64 | |
| 65 | if( $field['load_save_terms'] ) |
| 66 | { |
| 67 | $value = array(); |
| 68 | |
| 69 | $terms = get_the_terms( $post_id, $field['taxonomy'] ); |
| 70 | |
| 71 | if( is_array($terms) ){ foreach( $terms as $term ){ |
| 72 | |
| 73 | $value[] = $term->term_id; |
| 74 | |
| 75 | }} |
| 76 | |
| 77 | } |
| 78 | |
| 79 | |
| 80 | return $value; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | * update_value() |
| 86 | * |
| 87 | * This filter is appied to the $value before it is updated in the db |
| 88 | * |
| 89 | * @type filter |
| 90 | * @since 3.6 |
| 91 | * @date 23/01/13 |
| 92 | * |
| 93 | * @param $value - the value which will be saved in the database |
| 94 | * @param $field - the field array holding all the field options |
| 95 | * @param $post_id - the $post_id of which the value will be saved |
| 96 | * |
| 97 | * @return $value - the modified value |
| 98 | */ |
| 99 | |
| 100 | function update_value( $value, $post_id, $field ) |
| 101 | { |
| 102 | // vars |
| 103 | $field = array_merge($this->defaults, $field); |
| 104 | if( is_array($value) ) |
| 105 | { |
| 106 | $value = array_filter($value); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | if( $field['load_save_terms'] ) |
| 111 | { |
| 112 | // Parse values |
| 113 | $value = apply_filters( 'acf/parse_types', $value ); |
| 114 | |
| 115 | wp_set_object_terms( $post_id, $value, $field['taxonomy'], false ); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | return $value; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * format_value_for_api() |
| 125 | * |
| 126 | * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field |
| 127 | * |
| 128 | * @type filter |
| 129 | * @since 3.6 |
| 130 | * @date 23/01/13 |
| 131 | * |
| 132 | * @param $value - the value which was loaded from the database |
| 133 | * @param $post_id - the $post_id from which the value was loaded |
| 134 | * @param $field - the field array holding all the field options |
| 135 | * |
| 136 | * @return $value - the modified value |
| 137 | */ |
| 138 | |
| 139 | function format_value_for_api( $value, $post_id, $field ) |
| 140 | { |
| 141 | // defaults |
| 142 | $field = array_merge($this->defaults, $field); |
| 143 | |
| 144 | |
| 145 | // temp convert to array |
| 146 | $is_array = true; |
| 147 | |
| 148 | if( !is_array($value) ) |
| 149 | { |
| 150 | $is_array = false; |
| 151 | $value = array( $value ); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | // format |
| 156 | if( $field['return_format'] == 'object' ) |
| 157 | { |
| 158 | foreach( $value as $k => $v ) |
| 159 | { |
| 160 | $value[ $k ] = get_term( $v, $field['taxonomy'] ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | // de-convert from array |
| 166 | if( !$is_array && isset($value[0]) ) |
| 167 | { |
| 168 | $value = $value[0]; |
| 169 | } |
| 170 | |
| 171 | // Note: This function can be removed if not used |
| 172 | return $value; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * create_field() |
| 178 | * |
| 179 | * Create the HTML interface for your field |
| 180 | * |
| 181 | * @type action |
| 182 | * @since 3.6 |
| 183 | * @date 23/01/13 |
| 184 | * |
| 185 | * @param $field - an array holding all the field's data |
| 186 | */ |
| 187 | |
| 188 | function create_field( $field ) |
| 189 | { |
| 190 | // vars |
| 191 | $field = array_merge($this->defaults, $field); |
| 192 | $single_name = $field['name']; |
| 193 | |
| 194 | |
| 195 | // multi select? |
| 196 | if( $field['field_type'] == 'multi_select' ) |
| 197 | { |
| 198 | $field['multiple'] = 1; |
| 199 | $field['field_type'] = 'select'; |
| 200 | $field['name'] .= '[]'; |
| 201 | } |
| 202 | elseif( $field['field_type'] == 'checkbox' ) |
| 203 | { |
| 204 | $field['name'] .= '[]'; |
| 205 | } |
| 206 | |
| 207 | // value must be array! |
| 208 | if( !is_array($field['value']) ) |
| 209 | { |
| 210 | $field['value'] = array( $field['value'] ); |
| 211 | } |
| 212 | |
| 213 | ?> |
| 214 | <div class="acf-taxonomy-field"> |
| 215 | <input type="hidden" name="<?php echo $single_name; ?>" value="" /> |
| 216 | |
| 217 | <?php if( $field['field_type'] == 'select' ): ?> |
| 218 | |
| 219 | <select name="<?php echo $field['name']; ?>" <?php if( $field['multiple'] ): ?>multiple="multiple" size="5"<?php endif; ?>> |
| 220 | <?php if( $field['allow_null'] ): ?> |
| 221 | <option value=""><?php _e("None", 'acf'); ?></option> |
| 222 | <?php endif; ?> |
| 223 | |
| 224 | <?php else: ?> |
| 225 | <div class="categorychecklist-holder"> |
| 226 | <ul class="categorychecklist"> |
| 227 | <?php if( $field['allow_null'] ): ?> |
| 228 | <li> |
| 229 | <label class="selectit"> |
| 230 | <input type="<?php echo $field['field_type']; ?>" name="<?php echo $field['name']; ?>" value="" /> <?php _e("None", 'acf'); ?> |
| 231 | </label> |
| 232 | </li> |
| 233 | <?php endif; ?> |
| 234 | |
| 235 | <?php endif; ?> |
| 236 | |
| 237 | <?php |
| 238 | |
| 239 | wp_list_categories( array( |
| 240 | 'taxonomy' => $field['taxonomy'], |
| 241 | 'hide_empty' => false, |
| 242 | 'style' => 'none', |
| 243 | 'walker' => new acf_taxonomy_field_walker( $field ), |
| 244 | )); |
| 245 | |
| 246 | ?> |
| 247 | |
| 248 | <?php if( $field['field_type'] == 'select' ): ?> |
| 249 | |
| 250 | </select> |
| 251 | |
| 252 | <?php else: ?> |
| 253 | |
| 254 | </ul> |
| 255 | </div> |
| 256 | |
| 257 | <?php endif; ?> |
| 258 | |
| 259 | </div> |
| 260 | <?php |
| 261 | |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | * create_options() |
| 267 | * |
| 268 | * Create extra options for your field. This is rendered when editing a field. |
| 269 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 270 | * |
| 271 | * @type action |
| 272 | * @since 3.6 |
| 273 | * @date 23/01/13 |
| 274 | * |
| 275 | * @param $field - an array holding all the field's data |
| 276 | */ |
| 277 | |
| 278 | function create_options( $field ) |
| 279 | { |
| 280 | // vars |
| 281 | $field = array_merge($this->defaults, $field); |
| 282 | $key = $field['name']; |
| 283 | |
| 284 | ?> |
| 285 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 286 | <td class="label"> |
| 287 | <label><?php _e("Taxonomy",'acf'); ?></label> |
| 288 | </td> |
| 289 | <td> |
| 290 | <?php |
| 291 | |
| 292 | $choices = array(); |
| 293 | $taxonomies = get_taxonomies( array('public' => true), 'objects' ); |
| 294 | |
| 295 | foreach($taxonomies as $taxonomy) |
| 296 | { |
| 297 | $choices[ $taxonomy->name ] = $taxonomy->labels->name; |
| 298 | } |
| 299 | |
| 300 | // unset post_format (why is this a public taxonomy?) |
| 301 | if( isset($choices['post_format']) ) |
| 302 | { |
| 303 | unset( $choices['post_format']) ; |
| 304 | } |
| 305 | |
| 306 | do_action('acf/create_field', array( |
| 307 | 'type' => 'select', |
| 308 | 'name' => 'fields['.$key.'][taxonomy]', |
| 309 | 'value' => $field['taxonomy'], |
| 310 | 'choices' => $choices, |
| 311 | )); |
| 312 | ?> |
| 313 | </td> |
| 314 | </tr> |
| 315 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 316 | <td class="label"> |
| 317 | <label><?php _e("Field Type",'acf'); ?></label> |
| 318 | </td> |
| 319 | <td> |
| 320 | <?php |
| 321 | do_action('acf/create_field', array( |
| 322 | 'type' => 'select', |
| 323 | 'name' => 'fields['.$key.'][field_type]', |
| 324 | 'value' => $field['field_type'], |
| 325 | 'optgroup' => true, |
| 326 | 'choices' => array( |
| 327 | __("Multiple Values",'acf') => array( |
| 328 | 'checkbox' => __('Checkbox', 'acf'), |
| 329 | 'multi_select' => __('Multi Select', 'acf') |
| 330 | ), |
| 331 | __("Single Value",'acf') => array( |
| 332 | 'radio' => __('Radio Buttons', 'acf'), |
| 333 | 'select' => __('Select', 'acf') |
| 334 | ) |
| 335 | ) |
| 336 | )); |
| 337 | ?> |
| 338 | </td> |
| 339 | </tr> |
| 340 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 341 | <td class="label"> |
| 342 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 343 | </td> |
| 344 | <td> |
| 345 | <?php |
| 346 | do_action('acf/create_field', array( |
| 347 | 'type' => 'radio', |
| 348 | 'name' => 'fields['.$key.'][allow_null]', |
| 349 | 'value' => $field['allow_null'], |
| 350 | 'choices' => array( |
| 351 | 1 => __("Yes",'acf'), |
| 352 | 0 => __("No",'acf'), |
| 353 | ), |
| 354 | 'layout' => 'horizontal', |
| 355 | )); |
| 356 | ?> |
| 357 | </td> |
| 358 | </tr> |
| 359 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 360 | <td class="label"> |
| 361 | <label><?php _e("Load & Save Terms to Post",'acf'); ?></label> |
| 362 | </td> |
| 363 | <td> |
| 364 | <?php |
| 365 | do_action('acf/create_field', array( |
| 366 | 'type' => 'true_false', |
| 367 | 'name' => 'fields['.$key.'][load_save_terms]', |
| 368 | 'value' => $field['load_save_terms'], |
| 369 | 'message' => __("Load value based on the post's terms and update the post's terms on save",'acf') |
| 370 | )); |
| 371 | ?> |
| 372 | </td> |
| 373 | </tr> |
| 374 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 375 | <td class="label"> |
| 376 | <label><?php _e("Return Value",'acf'); ?></label> |
| 377 | </td> |
| 378 | <td> |
| 379 | <?php |
| 380 | do_action('acf/create_field', array( |
| 381 | 'type' => 'radio', |
| 382 | 'name' => 'fields['.$key.'][return_format]', |
| 383 | 'value' => $field['return_format'], |
| 384 | 'layout' => 'horizontal', |
| 385 | 'choices' => array( |
| 386 | 'object' => __("Term Object",'acf'), |
| 387 | 'id' => __("Term ID",'acf') |
| 388 | ) |
| 389 | )); |
| 390 | ?> |
| 391 | </td> |
| 392 | </tr> |
| 393 | <?php |
| 394 | |
| 395 | } |
| 396 | |
| 397 | |
| 398 | } |
| 399 | |
| 400 | new acf_field_taxonomy(); |
| 401 | |
| 402 | |
| 403 | class acf_taxonomy_field_walker extends Walker |
| 404 | { |
| 405 | // vars |
| 406 | var $field = null, |
| 407 | $tree_type = 'category', |
| 408 | $db_fields = array ( 'parent' => 'parent', 'id' => 'term_id' ); |
| 409 | |
| 410 | |
| 411 | // construct |
| 412 | function __construct( $field ) |
| 413 | { |
| 414 | $this->field = $field; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | // start_el |
| 419 | function start_el( &$output, $term, $depth, $args = array(), $current_object_id = 0) |
| 420 | { |
| 421 | // vars |
| 422 | $selected = in_array( $term->term_id, $this->field['value'] ); |
| 423 | |
| 424 | if( $this->field['field_type'] == 'checkbox' ) |
| 425 | { |
| 426 | $output .= '<li><label class="selectit"><input type="checkbox" name="' . $this->field['name'] . '" value="' . $term->term_id . '" ' . ($selected ? 'checked="checked"' : '') . ' /> ' . $term->name . '</label>'; |
| 427 | } |
| 428 | elseif( $this->field['field_type'] == 'radio' ) |
| 429 | { |
| 430 | $output .= '<li><label class="selectit"><input type="radio" name="' . $this->field['name'] . '" value="' . $term->term_id . '" ' . ($selected ? 'checked="checkbox"' : '') . ' /> ' . $term->name . '</label>'; |
| 431 | } |
| 432 | elseif( $this->field['field_type'] == 'select' ) |
| 433 | { |
| 434 | $indent = str_repeat("—", $depth); |
| 435 | $output .= '<option value="' . $term->term_id . '" ' . ($selected ? 'selected="selected"' : '') . '>' . $indent . ' ' . $term->name . '</option>'; |
| 436 | } |
| 437 | |
| 438 | } |
| 439 | |
| 440 | |
| 441 | //end_el |
| 442 | function end_el( &$output, $term, $depth = 0, $args = array() ) |
| 443 | { |
| 444 | if( in_array($this->field['field_type'], array('checkbox', 'radio')) ) |
| 445 | { |
| 446 | $output .= '</li>'; |
| 447 | } |
| 448 | |
| 449 | $output .= "\n"; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | // start_lvl |
| 454 | function start_lvl( &$output, $depth = 0, $args = array() ) |
| 455 | { |
| 456 | // indent |
| 457 | //$output .= str_repeat( "\t", $depth); |
| 458 | |
| 459 | |
| 460 | // wrap element |
| 461 | if( in_array($this->field['field_type'], array('checkbox', 'radio')) ) |
| 462 | { |
| 463 | $output .= '<ul class="children">' . "\n"; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | |
| 468 | // end_lvl |
| 469 | function end_lvl( &$output, $depth = 0, $args = array() ) |
| 470 | { |
| 471 | // indent |
| 472 | //$output .= str_repeat( "\t", $depth); |
| 473 | |
| 474 | |
| 475 | // wrap element |
| 476 | if( in_array($this->field['field_type'], array('checkbox', 'radio')) ) |
| 477 | { |
| 478 | $output .= '</ul>' . "\n"; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | } |
| 483 | |
| 484 | ?> |