post_object.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Post_object extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'post_object'; |
| 21 | $this->title = __("Post Object",'acf'); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /*-------------------------------------------------------------------------------------- |
| 27 | * |
| 28 | * create_field |
| 29 | * |
| 30 | * @author Elliot Condon |
| 31 | * @since 2.0.5 |
| 32 | * @updated 2.2.0 |
| 33 | * |
| 34 | *-------------------------------------------------------------------------------------*/ |
| 35 | |
| 36 | function create_field($field) |
| 37 | { |
| 38 | // vars |
| 39 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false; |
| 40 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : false; |
| 41 | //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : false; |
| 42 | //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : false; |
| 43 | |
| 44 | |
| 45 | if(!$field['post_type'] || !is_array($field['post_type']) || $field['post_type'][0] == "") |
| 46 | { |
| 47 | $field['post_type'] = get_post_types(array('public' => true)); |
| 48 | } |
| 49 | |
| 50 | // multiple select |
| 51 | $multiple = ''; |
| 52 | if($field['multiple'] == '1') |
| 53 | { |
| 54 | $multiple = ' multiple="multiple" size="5" '; |
| 55 | $field['name'] .= '[]'; |
| 56 | } |
| 57 | |
| 58 | // html |
| 59 | echo '<select id="' . $field['name'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >'; |
| 60 | |
| 61 | // null |
| 62 | if($field['allow_null'] == '1') |
| 63 | { |
| 64 | echo '<option value="null"> - ' . __("Select",'acf') . ' - </option>'; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | foreach($field['post_type'] as $post_type) |
| 69 | { |
| 70 | // get posts |
| 71 | $posts = false; |
| 72 | |
| 73 | if(is_post_type_hierarchical($post_type)) |
| 74 | { |
| 75 | // get pages |
| 76 | $posts = get_pages(array( |
| 77 | 'numberposts' => -1, |
| 78 | 'post_type' => $post_type, |
| 79 | 'sort_column' => 'menu_order', |
| 80 | 'order' => 'ASC', |
| 81 | 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'), |
| 82 | 'suppress_filters' => false, |
| 83 | //'meta_key' => $field['meta_key'], |
| 84 | //'meta_value' => $field['meta_value'], |
| 85 | )); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | // get posts |
| 90 | $posts = get_posts(array( |
| 91 | 'numberposts' => -1, |
| 92 | 'post_type' => $post_type, |
| 93 | 'orderby' => 'title', |
| 94 | 'order' => 'ASC', |
| 95 | 'post_status' => array('publish', 'private', 'draft', 'inherit', 'future'), |
| 96 | 'suppress_filters' => false, |
| 97 | //'meta_key' => $field['meta_key'], |
| 98 | //'meta_value' => $field['meta_value'], |
| 99 | )); |
| 100 | } |
| 101 | |
| 102 | // filter by taxonomy |
| 103 | if(in_array('all', $field['taxonomy'])) |
| 104 | { |
| 105 | // leave all posts |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | if($posts) |
| 110 | { |
| 111 | foreach($posts as $k => $post) |
| 112 | { |
| 113 | if(!$this->parent->in_taxonomy($post, $field['taxonomy'])) |
| 114 | { |
| 115 | unset($posts[$k]); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // if posts, make a group for them |
| 123 | if($posts) |
| 124 | { |
| 125 | $post_type_object = get_post_type_object($post_type); |
| 126 | $post_type_name = $post_type_object->labels->name; |
| 127 | |
| 128 | echo '<optgroup label="'.$post_type_name.'">'; |
| 129 | |
| 130 | foreach($posts as $post) |
| 131 | { |
| 132 | $key = $post->ID; |
| 133 | |
| 134 | $value = ''; |
| 135 | $ancestors = get_ancestors($post->ID, $post_type); |
| 136 | if($ancestors) |
| 137 | { |
| 138 | foreach($ancestors as $a) |
| 139 | { |
| 140 | $value .= '– '; |
| 141 | } |
| 142 | } |
| 143 | $value .= get_the_title($post->ID); |
| 144 | |
| 145 | // status |
| 146 | if($post->post_status == "private" || $post->post_status == "draft") |
| 147 | { |
| 148 | $value .= " ($post->post_status)"; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | $selected = ''; |
| 153 | |
| 154 | |
| 155 | if(is_array($field['value'])) |
| 156 | { |
| 157 | // 2. If the value is an array (multiple select), loop through values and check if it is selected |
| 158 | if(in_array($key, $field['value'])) |
| 159 | { |
| 160 | $selected = 'selected="selected"'; |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | // 3. this is not a multiple select, just check normaly |
| 166 | if($key == $field['value']) |
| 167 | { |
| 168 | $selected = 'selected="selected"'; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; |
| 174 | |
| 175 | |
| 176 | } |
| 177 | |
| 178 | echo '</optgroup>'; |
| 179 | |
| 180 | }// endif |
| 181 | |
| 182 | }// endforeach |
| 183 | |
| 184 | |
| 185 | echo '</select>'; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /*-------------------------------------------------------------------------------------- |
| 190 | * |
| 191 | * create_options |
| 192 | * |
| 193 | * @author Elliot Condon |
| 194 | * @since 2.0.6 |
| 195 | * @updated 2.2.0 |
| 196 | * |
| 197 | *-------------------------------------------------------------------------------------*/ |
| 198 | |
| 199 | function create_options($key, $field) |
| 200 | { |
| 201 | // defaults |
| 202 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : ''; |
| 203 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : '0'; |
| 204 | $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : '0'; |
| 205 | $field['taxonomy'] = isset($field['taxonomy']) ? $field['taxonomy'] : array('all'); |
| 206 | //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : ''; |
| 207 | //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : ''; |
| 208 | ?> |
| 209 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 210 | <td class="label"> |
| 211 | <label for=""><?php _e("Post Type",'acf'); ?></label> |
| 212 | </td> |
| 213 | <td> |
| 214 | <?php |
| 215 | $post_types = array('' => __("All",'acf')); |
| 216 | |
| 217 | foreach (get_post_types(array('public' => true)) as $post_type ) { |
| 218 | $post_types[$post_type] = $post_type; |
| 219 | } |
| 220 | |
| 221 | $this->parent->create_field(array( |
| 222 | 'type' => 'select', |
| 223 | 'name' => 'fields['.$key.'][post_type]', |
| 224 | 'value' => $field['post_type'], |
| 225 | 'choices' => $post_types, |
| 226 | 'multiple' => '1', |
| 227 | )); |
| 228 | ?> |
| 229 | </td> |
| 230 | </tr> |
| 231 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 232 | <td class="label"> |
| 233 | <label><?php _e("Filter from Taxonomy",'acf'); ?></label> |
| 234 | </td> |
| 235 | <td> |
| 236 | <?php |
| 237 | $choices = array( |
| 238 | '' => array( |
| 239 | 'all' => __("All",'acf') |
| 240 | ) |
| 241 | ); |
| 242 | $choices = array_merge($choices, $this->parent->get_taxonomies_for_select()); |
| 243 | $this->parent->create_field(array( |
| 244 | 'type' => 'select', |
| 245 | 'name' => 'fields['.$key.'][taxonomy]', |
| 246 | 'value' => $field['taxonomy'], |
| 247 | 'choices' => $choices, |
| 248 | 'optgroup' => true, |
| 249 | 'multiple' => '1', |
| 250 | )); |
| 251 | ?> |
| 252 | </td> |
| 253 | </tr> |
| 254 | <?php /*<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 255 | <td class="label"> |
| 256 | <label><?php _e("Filter Posts",'acf'); ?></label> |
| 257 | <p class="description"><?php _e("Where meta_key == meta_value",'acf'); ?></p> |
| 258 | </td> |
| 259 | <td> |
| 260 | <div style="width:45%; float:left"> |
| 261 | <?php |
| 262 | $this->parent->create_field(array( |
| 263 | 'type' => 'text', |
| 264 | 'name' => 'fields['.$key.'][meta_key]', |
| 265 | 'value' => $field['meta_key'], |
| 266 | )); |
| 267 | ?> |
| 268 | </div> |
| 269 | <div style="width:10%; float:left; text-align:center; padding:5px 0 0;">is equal to</div> |
| 270 | <div style="width:45%; float:left"> |
| 271 | <?php |
| 272 | $this->parent->create_field(array( |
| 273 | 'type' => 'text', |
| 274 | 'name' => 'fields['.$key.'][meta_value]', |
| 275 | 'value' => $field['meta_value'], |
| 276 | )); |
| 277 | ?> |
| 278 | </div> |
| 279 | </td> |
| 280 | </tr>*/ ?> |
| 281 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 282 | <td class="label"> |
| 283 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 284 | </td> |
| 285 | <td> |
| 286 | <?php |
| 287 | $this->parent->create_field(array( |
| 288 | 'type' => 'radio', |
| 289 | 'name' => 'fields['.$key.'][allow_null]', |
| 290 | 'value' => $field['allow_null'], |
| 291 | 'choices' => array( |
| 292 | '1' => __("Yes",'acf'), |
| 293 | '0' => __("No",'acf'), |
| 294 | ), |
| 295 | 'layout' => 'horizontal', |
| 296 | )); |
| 297 | ?> |
| 298 | </td> |
| 299 | </tr> |
| 300 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 301 | <td class="label"> |
| 302 | <label><?php _e("Select multiple values?",'acf'); ?></label> |
| 303 | </td> |
| 304 | <td> |
| 305 | <?php |
| 306 | $this->parent->create_field(array( |
| 307 | 'type' => 'radio', |
| 308 | 'name' => 'fields['.$key.'][multiple]', |
| 309 | 'value' => $field['multiple'], |
| 310 | 'choices' => array( |
| 311 | '1' => __("Yes",'acf'), |
| 312 | '0' => __("No",'acf'), |
| 313 | ), |
| 314 | 'layout' => 'horizontal', |
| 315 | )); |
| 316 | ?> |
| 317 | </td> |
| 318 | </tr> |
| 319 | <?php |
| 320 | } |
| 321 | |
| 322 | |
| 323 | /*-------------------------------------------------------------------------------------- |
| 324 | * |
| 325 | * get_value_for_api |
| 326 | * |
| 327 | * @author Elliot Condon |
| 328 | * @since 3.0.0 |
| 329 | * |
| 330 | *-------------------------------------------------------------------------------------*/ |
| 331 | |
| 332 | function get_value_for_api($post_id, $field) |
| 333 | { |
| 334 | // get value |
| 335 | $value = parent::get_value($post_id, $field); |
| 336 | |
| 337 | if(!$value) |
| 338 | { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if($value == 'null') |
| 343 | { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if(is_array($value)) |
| 348 | { |
| 349 | foreach($value as $k => $v) |
| 350 | { |
| 351 | $value[$k] = get_post($v); |
| 352 | } |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | $value = get_post($value); |
| 357 | } |
| 358 | |
| 359 | return $value; |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
| 364 | ?> |