post_object.php
| 1 | <?php |
| 2 | |
| 3 | class acf_field_post_object extends acf_field |
| 4 | { |
| 5 | // vars |
| 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 = 'post_object'; |
| 22 | $this->label = __("Post Object",'acf'); |
| 23 | $this->category = __("Relational",'acf'); |
| 24 | $this->defaults = array( |
| 25 | 'post_type' => array('all'), |
| 26 | 'taxonomy' => array('all'), |
| 27 | 'multiple' => 0, |
| 28 | 'allow_null' => 0, |
| 29 | ); |
| 30 | |
| 31 | |
| 32 | // do not delete! |
| 33 | parent::__construct(); |
| 34 | |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * create_field() |
| 40 | * |
| 41 | * Create the HTML interface for your field |
| 42 | * |
| 43 | * @param $field - an array holding all the field's data |
| 44 | * |
| 45 | * @type action |
| 46 | * @since 3.6 |
| 47 | * @date 23/01/13 |
| 48 | */ |
| 49 | |
| 50 | function create_field( $field ) |
| 51 | { |
| 52 | // temp store the_post |
| 53 | global $post; |
| 54 | $the_post = $post; |
| 55 | |
| 56 | |
| 57 | // vars |
| 58 | $args = array( |
| 59 | 'numberposts' => -1, |
| 60 | 'post_type' => null, |
| 61 | 'orderby' => 'title', |
| 62 | 'order' => 'ASC', |
| 63 | 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'), |
| 64 | 'suppress_filters' => false, |
| 65 | ); |
| 66 | |
| 67 | |
| 68 | $field = array_merge($this->defaults, $field); |
| 69 | |
| 70 | |
| 71 | // validate post_type |
| 72 | if( !$field['post_type'] || !is_array($field['post_type']) || in_array('', $field['post_type']) ) |
| 73 | { |
| 74 | $field['post_type'] = array( 'all' ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | // validate taxonomy |
| 79 | if( !$field['taxonomy'] || !is_array($field['taxonomy']) || in_array('', $field['taxonomy']) ) |
| 80 | { |
| 81 | $field['taxonomy'] = array( 'all' ); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | // load all post types by default |
| 86 | if( in_array('all', $field['post_type']) ) |
| 87 | { |
| 88 | $field['post_type'] = apply_filters('acf/get_post_types', array()); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // create tax queries |
| 93 | if( ! in_array('all', $field['taxonomy']) ) |
| 94 | { |
| 95 | // vars |
| 96 | $taxonomies = array(); |
| 97 | $args['tax_query'] = array(); |
| 98 | |
| 99 | foreach( $field['taxonomy'] as $v ) |
| 100 | { |
| 101 | |
| 102 | // find term (find taxonomy!) |
| 103 | // $term = array( 0 => $taxonomy, 1 => $term_id ) |
| 104 | $term = explode(':', $v); |
| 105 | |
| 106 | |
| 107 | // validate |
| 108 | if( !is_array($term) || !isset($term[1]) ) |
| 109 | { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | // add to tax array |
| 115 | $taxonomies[ $term[0] ][] = $term[1]; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // now create the tax queries |
| 121 | foreach( $taxonomies as $k => $v ) |
| 122 | { |
| 123 | $args['tax_query'][] = array( |
| 124 | 'taxonomy' => $k, |
| 125 | 'field' => 'id', |
| 126 | 'terms' => $v, |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // Change Field into a select |
| 133 | $field['type'] = 'select'; |
| 134 | $field['choices'] = array(); |
| 135 | |
| 136 | |
| 137 | foreach( $field['post_type'] as $post_type ) |
| 138 | { |
| 139 | // set post_type |
| 140 | $args['post_type'] = $post_type; |
| 141 | |
| 142 | |
| 143 | // set order |
| 144 | $get_pages = false; |
| 145 | if( is_post_type_hierarchical($post_type) && !isset($args['tax_query']) ) |
| 146 | { |
| 147 | $args['sort_column'] = 'menu_order, post_title'; |
| 148 | $args['sort_order'] = 'ASC'; |
| 149 | |
| 150 | $get_pages = true; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | // filters |
| 155 | $args = apply_filters('acf/fields/post_object/query', $args, $field, $the_post); |
| 156 | $args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $the_post ); |
| 157 | $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $the_post ); |
| 158 | |
| 159 | |
| 160 | if( $get_pages ) |
| 161 | { |
| 162 | $posts = get_pages( $args ); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | $posts = get_posts( $args ); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | if($posts) |
| 171 | { |
| 172 | foreach( $posts as $post ) |
| 173 | { |
| 174 | // find title. Could use get_the_title, but that uses get_post(), so I think this uses less Memory |
| 175 | $title = ''; |
| 176 | $ancestors = get_ancestors( $post->ID, $post->post_type ); |
| 177 | if($ancestors) |
| 178 | { |
| 179 | foreach($ancestors as $a) |
| 180 | { |
| 181 | $title .= '–'; |
| 182 | } |
| 183 | } |
| 184 | $title .= ' ' . apply_filters( 'the_title', $post->post_title, $post->ID ); |
| 185 | |
| 186 | |
| 187 | // status |
| 188 | if($post->post_status != "publish") |
| 189 | { |
| 190 | $title .= " ($post->post_status)"; |
| 191 | } |
| 192 | |
| 193 | // WPML |
| 194 | if( defined('ICL_LANGUAGE_CODE') ) |
| 195 | { |
| 196 | $title .= ' (' . ICL_LANGUAGE_CODE . ')'; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | // filters |
| 201 | $title = apply_filters('acf/fields/post_object/result', $title, $post, $field, $the_post); |
| 202 | $title = apply_filters('acf/fields/post_object/result/name=' . $field['name'] , $title, $post, $field, $the_post); |
| 203 | $title = apply_filters('acf/fields/post_object/result/key=' . $field['key'], $title, $post, $field, $the_post); |
| 204 | |
| 205 | |
| 206 | // add to choices |
| 207 | if( count($field['post_type']) == 1 ) |
| 208 | { |
| 209 | $field['choices'][ $post->ID ] = $title; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | // group by post type |
| 214 | $post_type_object = get_post_type_object( $post->post_type ); |
| 215 | $post_type_name = $post_type_object->labels->name; |
| 216 | |
| 217 | $field['choices'][ $post_type_name ][ $post->ID ] = $title; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | } |
| 222 | // foreach( $posts as $post ) |
| 223 | } |
| 224 | // if($posts) |
| 225 | } |
| 226 | // foreach( $field['post_type'] as $post_type ) |
| 227 | |
| 228 | |
| 229 | // create field |
| 230 | do_action('acf/create_field', $field ); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * create_options() |
| 236 | * |
| 237 | * Create extra options for your field. This is rendered when editing a field. |
| 238 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 239 | * |
| 240 | * @type action |
| 241 | * @since 3.6 |
| 242 | * @date 23/01/13 |
| 243 | * |
| 244 | * @param $field - an array holding all the field's data |
| 245 | */ |
| 246 | |
| 247 | function create_options( $field ) |
| 248 | { |
| 249 | // vars |
| 250 | $defaults = array( |
| 251 | 'post_type' => '', |
| 252 | 'multiple' => 0, |
| 253 | 'allow_null' => 0, |
| 254 | 'taxonomy' => array('all'), |
| 255 | ); |
| 256 | |
| 257 | $field = array_merge($defaults, $field); |
| 258 | $key = $field['name']; |
| 259 | |
| 260 | ?> |
| 261 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 262 | <td class="label"> |
| 263 | <label for=""><?php _e("Post Type",'acf'); ?></label> |
| 264 | </td> |
| 265 | <td> |
| 266 | <?php |
| 267 | |
| 268 | $choices = array( |
| 269 | '' => __("All",'acf') |
| 270 | ); |
| 271 | $choices = apply_filters('acf/get_post_types', $choices); |
| 272 | |
| 273 | |
| 274 | do_action('acf/create_field', array( |
| 275 | 'type' => 'select', |
| 276 | 'name' => 'fields['.$key.'][post_type]', |
| 277 | 'value' => $field['post_type'], |
| 278 | 'choices' => $choices, |
| 279 | 'multiple' => 1, |
| 280 | )); |
| 281 | |
| 282 | ?> |
| 283 | </td> |
| 284 | </tr> |
| 285 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 286 | <td class="label"> |
| 287 | <label><?php _e("Filter from Taxonomy",'acf'); ?></label> |
| 288 | </td> |
| 289 | <td> |
| 290 | <?php |
| 291 | $choices = array( |
| 292 | '' => array( |
| 293 | 'all' => __("All",'acf') |
| 294 | ) |
| 295 | ); |
| 296 | $simple_value = false; |
| 297 | $choices = apply_filters('acf/get_taxonomies_for_select', $choices, $simple_value); |
| 298 | |
| 299 | do_action('acf/create_field', array( |
| 300 | 'type' => 'select', |
| 301 | 'name' => 'fields['.$key.'][taxonomy]', |
| 302 | 'value' => $field['taxonomy'], |
| 303 | 'choices' => $choices, |
| 304 | 'multiple' => 1, |
| 305 | )); |
| 306 | |
| 307 | ?> |
| 308 | </td> |
| 309 | </tr> |
| 310 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 311 | <td class="label"> |
| 312 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 313 | </td> |
| 314 | <td> |
| 315 | <?php |
| 316 | |
| 317 | do_action('acf/create_field', array( |
| 318 | 'type' => 'radio', |
| 319 | 'name' => 'fields['.$key.'][allow_null]', |
| 320 | 'value' => $field['allow_null'], |
| 321 | 'choices' => array( |
| 322 | 1 => __("Yes",'acf'), |
| 323 | 0 => __("No",'acf'), |
| 324 | ), |
| 325 | 'layout' => 'horizontal', |
| 326 | )); |
| 327 | |
| 328 | ?> |
| 329 | </td> |
| 330 | </tr> |
| 331 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 332 | <td class="label"> |
| 333 | <label><?php _e("Select multiple values?",'acf'); ?></label> |
| 334 | </td> |
| 335 | <td> |
| 336 | <?php |
| 337 | |
| 338 | do_action('acf/create_field', array( |
| 339 | 'type' => 'radio', |
| 340 | 'name' => 'fields['.$key.'][multiple]', |
| 341 | 'value' => $field['multiple'], |
| 342 | 'choices' => array( |
| 343 | 1 => __("Yes",'acf'), |
| 344 | 0 => __("No",'acf'), |
| 345 | ), |
| 346 | 'layout' => 'horizontal', |
| 347 | )); |
| 348 | |
| 349 | ?> |
| 350 | </td> |
| 351 | </tr> |
| 352 | <?php |
| 353 | |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* |
| 358 | * format_value_for_api() |
| 359 | * |
| 360 | * 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 |
| 361 | * |
| 362 | * @type filter |
| 363 | * @since 3.6 |
| 364 | * @date 23/01/13 |
| 365 | * |
| 366 | * @param $value - the value which was loaded from the database |
| 367 | * @param $post_id - the $post_id from which the value was loaded |
| 368 | * @param $field - the field array holding all the field options |
| 369 | * |
| 370 | * @return $value - the modified value |
| 371 | */ |
| 372 | |
| 373 | function format_value_for_api( $value, $post_id, $field ) |
| 374 | { |
| 375 | // no value? |
| 376 | if( !$value ) |
| 377 | { |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | // null? |
| 383 | if( $value == 'null' ) |
| 384 | { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | // multiple / single |
| 390 | if( is_array($value) ) |
| 391 | { |
| 392 | // find posts (DISTINCT POSTS) |
| 393 | $posts = get_posts(array( |
| 394 | 'numberposts' => -1, |
| 395 | 'post__in' => $value, |
| 396 | 'post_type' => apply_filters('acf/get_post_types', array()), |
| 397 | 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'), |
| 398 | )); |
| 399 | |
| 400 | |
| 401 | $ordered_posts = array(); |
| 402 | foreach( $posts as $post ) |
| 403 | { |
| 404 | // create array to hold value data |
| 405 | $ordered_posts[ $post->ID ] = $post; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | // override value array with attachments |
| 410 | foreach( $value as $k => $v) |
| 411 | { |
| 412 | // check that post exists (my have been trashed) |
| 413 | if( !isset($ordered_posts[ $v ]) ) |
| 414 | { |
| 415 | unset( $value[ $k ] ); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | $value[ $k ] = $ordered_posts[ $v ]; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | $value = get_post($value); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | // return the value |
| 431 | return $value; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* |
| 436 | * update_value() |
| 437 | * |
| 438 | * This filter is appied to the $value before it is updated in the db |
| 439 | * |
| 440 | * @type filter |
| 441 | * @since 3.6 |
| 442 | * @date 23/01/13 |
| 443 | * |
| 444 | * @param $value - the value which will be saved in the database |
| 445 | * @param $post_id - the $post_id of which the value will be saved |
| 446 | * @param $field - the field array holding all the field options |
| 447 | * |
| 448 | * @return $value - the modified value |
| 449 | */ |
| 450 | |
| 451 | function update_value( $value, $post_id, $field ) |
| 452 | { |
| 453 | // object / array? |
| 454 | if( is_object($value) && isset($value->ID) ) |
| 455 | { |
| 456 | $value = $value->ID; |
| 457 | } |
| 458 | elseif( is_array($value) ){ foreach( $value as $k => $v ){ |
| 459 | |
| 460 | // object? |
| 461 | if( is_object($v) && isset($v->ID) ) |
| 462 | { |
| 463 | $value[ $k ] = $v->ID; |
| 464 | } |
| 465 | |
| 466 | }} |
| 467 | |
| 468 | |
| 469 | return $value; |
| 470 | } |
| 471 | |
| 472 | } |
| 473 | |
| 474 | new acf_field_post_object(); |
| 475 | |
| 476 | ?> |