relationship.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Relationship 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 = 'relationship'; |
| 21 | $this->title = __("Relationship",'acf'); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /*-------------------------------------------------------------------------------------- |
| 27 | * |
| 28 | * admin_print_scripts / admin_print_styles |
| 29 | * |
| 30 | * @author Elliot Condon |
| 31 | * @since 3.0.0 |
| 32 | * |
| 33 | *-------------------------------------------------------------------------------------*/ |
| 34 | |
| 35 | function admin_print_scripts() |
| 36 | { |
| 37 | wp_enqueue_script(array( |
| 38 | 'jquery-ui-sortable', |
| 39 | )); |
| 40 | } |
| 41 | |
| 42 | function admin_print_styles() |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /*-------------------------------------------------------------------------------------- |
| 49 | * |
| 50 | * create_field |
| 51 | * |
| 52 | * @author Elliot Condon |
| 53 | * @since 2.0.5 |
| 54 | * @updated 2.2.0 |
| 55 | * |
| 56 | *-------------------------------------------------------------------------------------*/ |
| 57 | |
| 58 | function create_field($field) |
| 59 | { |
| 60 | |
| 61 | $field['max'] = isset($field['max']) ? $field['max'] : '-1'; |
| 62 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : false; |
| 63 | $field['taxonomy'] = isset($field['taxonomy']) ? $field['taxonomy'] : array('all'); |
| 64 | //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : false; |
| 65 | //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : false; |
| 66 | |
| 67 | if(!$field['post_type'] || !is_array($field['post_type']) || $field['post_type'][0] == "") |
| 68 | { |
| 69 | $field['post_type'] = get_post_types(array('public' => true)); |
| 70 | } |
| 71 | |
| 72 | // attachment doesn't work if it is the only item in an array??? |
| 73 | if(is_array($field['post_type']) && count($field['post_type']) == 1) |
| 74 | { |
| 75 | $field['post_type'] = $field['post_type'][0]; |
| 76 | } |
| 77 | |
| 78 | $posts = get_posts(array( |
| 79 | 'numberposts' => -1, |
| 80 | 'post_type' => $field['post_type'], |
| 81 | 'orderby' => 'title', |
| 82 | 'order' => 'ASC', |
| 83 | 'post_status' => array('publish', 'private', 'draft'), |
| 84 | //'meta_key' => $field['meta_key'], |
| 85 | //'meta_value' => $field['meta_value'], |
| 86 | )); |
| 87 | |
| 88 | // filter by taxonomy |
| 89 | if(in_array('all', $field['taxonomy'])) |
| 90 | { |
| 91 | // leave all posts |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | if($posts) |
| 96 | { |
| 97 | foreach($posts as $k => $post) |
| 98 | { |
| 99 | if(!$this->parent->in_taxonomy($post, $field['taxonomy'])) |
| 100 | { |
| 101 | unset($posts[$k]); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | $values_array = array(); |
| 108 | if($field['value'] != "") |
| 109 | { |
| 110 | $temp_array = explode(',', $field['value']); |
| 111 | foreach($temp_array as $p) |
| 112 | { |
| 113 | // if the post doesn't exist, continue |
| 114 | if(!get_the_title($p)) continue; |
| 115 | |
| 116 | $values_array[] = $p; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | ?> |
| 125 | <div class="acf_relationship" data-max="<?php echo $field['max']; ?>"> |
| 126 | |
| 127 | <input type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo implode(',', $values_array); ?>" /> |
| 128 | |
| 129 | <div class="relationship_left"> |
| 130 | <table class="widefat"> |
| 131 | <thead> |
| 132 | <tr> |
| 133 | <th> |
| 134 | <label class="relationship_label" for="relationship_<?php echo $field['name']; ?>">Search...</label> |
| 135 | <input class="relationship_search" type="text" id="relationship_<?php echo $field['name']; ?>" /> |
| 136 | <div class="clear_relationship_search"></div> |
| 137 | </th> |
| 138 | </tr> |
| 139 | </thead> |
| 140 | </table> |
| 141 | <div class="relationship_list"> |
| 142 | <?php |
| 143 | if($posts) |
| 144 | { |
| 145 | foreach($posts as $post) |
| 146 | { |
| 147 | if(!get_the_title($post->ID)) continue; |
| 148 | |
| 149 | $class = in_array($post->ID, $values_array) ? 'hide' : ''; |
| 150 | |
| 151 | $title = get_the_title($post->ID); |
| 152 | // status |
| 153 | if($post->post_status == "private" || $post->post_status == "draft") |
| 154 | { |
| 155 | $title .= " ($post->post_status)"; |
| 156 | } |
| 157 | |
| 158 | echo '<a href="javascript:;" class="' . $class . '" data-post_id="' . $post->ID . '">' . $title . '<span class="add"></span></a>'; |
| 159 | } |
| 160 | } |
| 161 | ?> |
| 162 | </div> |
| 163 | </div> |
| 164 | |
| 165 | <div class="relationship_right"> |
| 166 | <div class="relationship_list"> |
| 167 | <?php |
| 168 | $temp_posts = array(); |
| 169 | |
| 170 | if($posts) |
| 171 | { |
| 172 | foreach($posts as $post) |
| 173 | { |
| 174 | $temp_posts[$post->ID] = $post; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if($temp_posts) |
| 179 | { |
| 180 | foreach($values_array as $value) |
| 181 | { |
| 182 | $post = $temp_posts[$value]; |
| 183 | |
| 184 | $title = get_the_title($post->ID); |
| 185 | // status |
| 186 | if($post->post_status == "private" || $post->post_status == "draft") |
| 187 | { |
| 188 | $title .= " ($post->post_status)"; |
| 189 | } |
| 190 | |
| 191 | echo '<a href="javascript:;" class="" data-post_id="' . $temp_posts[$value]->ID . '">' . $title . '<span class="remove"></span></a>'; |
| 192 | unset($temp_posts[$value]); |
| 193 | } |
| 194 | |
| 195 | foreach($temp_posts as $id => $post) |
| 196 | { |
| 197 | $title = get_the_title($post->ID); |
| 198 | // status |
| 199 | if($post->post_status == "private" || $post->post_status == "draft") |
| 200 | { |
| 201 | $title .= " ($post->post_status)"; |
| 202 | } |
| 203 | |
| 204 | echo '<a href="javascript:;" class="hide" data-post_id="' . $post->ID . '">' . $title . '<span class="remove"></span></a>'; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | ?> |
| 209 | </div> |
| 210 | </div> |
| 211 | |
| 212 | |
| 213 | </div> |
| 214 | <?php |
| 215 | |
| 216 | |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /*-------------------------------------------------------------------------------------- |
| 221 | * |
| 222 | * create_options |
| 223 | * |
| 224 | * @author Elliot Condon |
| 225 | * @since 2.0.6 |
| 226 | * @updated 2.2.0 |
| 227 | * |
| 228 | *-------------------------------------------------------------------------------------*/ |
| 229 | |
| 230 | function create_options($key, $field) |
| 231 | { |
| 232 | // defaults |
| 233 | $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : ''; |
| 234 | $field['max'] = isset($field['max']) ? $field['max'] : '-1'; |
| 235 | $field['taxonomy'] = isset($field['taxonomy']) ? $field['taxonomy'] : array('all'); |
| 236 | //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : ''; |
| 237 | //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : ''; |
| 238 | ?> |
| 239 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 240 | <td class="label"> |
| 241 | <label for=""><?php _e("Post Type",'acf'); ?></label> |
| 242 | </td> |
| 243 | <td> |
| 244 | <?php |
| 245 | $post_types = array('' => '- All -'); |
| 246 | |
| 247 | foreach (get_post_types(array('public' => true)) as $post_type ) { |
| 248 | $post_types[$post_type] = $post_type; |
| 249 | } |
| 250 | |
| 251 | $this->parent->create_field(array( |
| 252 | 'type' => 'select', |
| 253 | 'name' => 'fields['.$key.'][post_type]', |
| 254 | 'value' => $field['post_type'], |
| 255 | 'choices' => $post_types, |
| 256 | 'multiple' => '1', |
| 257 | )); |
| 258 | ?> |
| 259 | </td> |
| 260 | </tr> |
| 261 | <?php /*<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 262 | <td class="label"> |
| 263 | <label><?php _e("Filter Posts",'acf'); ?></label> |
| 264 | <p class="description"><?php _e("Where meta_key == meta_value",'acf'); ?></p> |
| 265 | </td> |
| 266 | <td> |
| 267 | <div style="width:45%; float:left"> |
| 268 | <?php |
| 269 | $this->parent->create_field(array( |
| 270 | 'type' => 'text', |
| 271 | 'name' => 'fields['.$key.'][meta_key]', |
| 272 | 'value' => $field['meta_key'], |
| 273 | )); |
| 274 | ?> |
| 275 | </div> |
| 276 | <div style="width:10%; float:left; text-align:center; padding:5px 0 0;">is equal to</div> |
| 277 | <div style="width:45%; float:left"> |
| 278 | <?php |
| 279 | $this->parent->create_field(array( |
| 280 | 'type' => 'text', |
| 281 | 'name' => 'fields['.$key.'][meta_value]', |
| 282 | 'value' => $field['meta_value'], |
| 283 | )); |
| 284 | ?> |
| 285 | </div> |
| 286 | </td> |
| 287 | </tr>*/ ?> |
| 288 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 289 | <td class="label"> |
| 290 | <label><?php _e("Filter from Taxonomy",'acf'); ?></label> |
| 291 | </td> |
| 292 | <td> |
| 293 | <?php |
| 294 | $choices = array( |
| 295 | '' => array( |
| 296 | 'all' => '- All -' |
| 297 | ) |
| 298 | ); |
| 299 | $choices = array_merge($choices, $this->parent->get_taxonomies_for_select()); |
| 300 | $this->parent->create_field(array( |
| 301 | 'type' => 'select', |
| 302 | 'name' => 'fields['.$key.'][taxonomy]', |
| 303 | 'value' => $field['taxonomy'], |
| 304 | 'choices' => $choices, |
| 305 | 'optgroup' => true, |
| 306 | 'multiple' => '1', |
| 307 | )); |
| 308 | ?> |
| 309 | </td> |
| 310 | </tr> |
| 311 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 312 | <td class="label"> |
| 313 | <label><?php _e("Maximum posts",'acf'); ?></label> |
| 314 | <p class="description"><?php _e("Set to -1 for inifinit",'acf'); ?></p> |
| 315 | </td> |
| 316 | <td> |
| 317 | <?php |
| 318 | $this->parent->create_field(array( |
| 319 | 'type' => 'text', |
| 320 | 'name' => 'fields['.$key.'][max]', |
| 321 | 'value' => $field['max'], |
| 322 | )); |
| 323 | ?> |
| 324 | </td> |
| 325 | </tr> |
| 326 | |
| 327 | |
| 328 | |
| 329 | <?php |
| 330 | } |
| 331 | |
| 332 | |
| 333 | /*-------------------------------------------------------------------------------------- |
| 334 | * |
| 335 | * get_value_for_api |
| 336 | * |
| 337 | * @author Elliot Condon |
| 338 | * @since 3.0.0 |
| 339 | * |
| 340 | *-------------------------------------------------------------------------------------*/ |
| 341 | |
| 342 | function get_value_for_api($post_id, $field) |
| 343 | { |
| 344 | // vars |
| 345 | $value = parent::get_value($post_id, $field); |
| 346 | $return = false; |
| 347 | |
| 348 | if(!$value || $value == "") |
| 349 | { |
| 350 | return $return; |
| 351 | } |
| 352 | |
| 353 | $value = explode(',', $value); |
| 354 | |
| 355 | if(is_array($value)) |
| 356 | { |
| 357 | $return = array(); |
| 358 | foreach($value as $v) |
| 359 | { |
| 360 | $return[] = get_post($v); |
| 361 | } |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | $return = array(get_post($value)); |
| 366 | } |
| 367 | |
| 368 | return $return; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | } |
| 374 | |
| 375 | ?> |