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