PluginProbe
Advanced Custom Fields (ACF®) / 3.3.1
Advanced Custom Fields (ACF®) v3.3.1
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / core / fields / relationship.php
relationship.php
386 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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', 'inherit', 'future'),
84 'suppress_filters' => false,
85 //'meta_key' => $field['meta_key'],
86 //'meta_value' => $field['meta_value'],
87 ));
88
89 // filter by taxonomy
90 if(in_array('all', $field['taxonomy']))
91 {
92 // leave all posts
93 }
94 else
95 {
96 if($posts)
97 {
98 foreach($posts as $k => $post)
99 {
100 if(!$this->parent->in_taxonomy($post, $field['taxonomy']))
101 {
102 unset($posts[$k]);
103 }
104 }
105 }
106 }
107
108 $values_array = array();
109 if($field['value'] != "")
110 {
111 $temp_array = explode(',', $field['value']);
112 foreach($temp_array as $p)
113 {
114 // if the post doesn't exist, continue
115 if(!get_the_title($p)) continue;
116
117 $values_array[] = $p;
118 }
119 }
120
121
122
123
124
125 ?>
126 <div class="acf_relationship" data-max="<?php echo $field['max']; ?>">
127
128 <input type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo implode(',', $values_array); ?>" />
129
130 <div class="relationship_left">
131 <table class="widefat">
132 <thead>
133 <tr>
134 <th>
135 <label class="relationship_label" for="relationship_<?php echo $field['name']; ?>"><?php _e("Search",'acf'); ?>...</label>
136 <input class="relationship_search" type="text" id="relationship_<?php echo $field['name']; ?>" />
137 <div class="clear_relationship_search"></div>
138 </th>
139 </tr>
140 </thead>
141 </table>
142 <div class="relationship_list">
143 <?php
144 if($posts)
145 {
146 foreach($posts as $post)
147 {
148 if(!get_the_title($post->ID)) continue;
149
150 $class = in_array($post->ID, $values_array) ? 'hide' : '';
151
152 $title = get_the_title($post->ID);
153 // status
154 if($post->post_status == "private" || $post->post_status == "draft")
155 {
156 $title .= " ($post->post_status)";
157 }
158
159 echo '<a href="javascript:;" class="' . $class . '" data-post_id="' . $post->ID . '">' . $title . '<span class="add"></span></a>';
160 }
161 }
162 ?>
163 </div>
164 </div>
165
166 <div class="relationship_right">
167 <div class="relationship_list">
168 <?php
169 $temp_posts = array();
170
171 if($posts)
172 {
173 foreach($posts as $post)
174 {
175 $temp_posts[$post->ID] = $post;
176 }
177 }
178
179 if($temp_posts)
180 {
181 foreach($values_array as $value)
182 {
183 if(!isset($temp_posts[$value]))
184 {
185 continue;
186 }
187
188 $post = $temp_posts[$value];
189
190 $title = get_the_title($post->ID);
191 // status
192 if($post->post_status == "private" || $post->post_status == "draft")
193 {
194 $title .= " ($post->post_status)";
195 }
196
197 echo '<a href="javascript:;" class="" data-post_id="' . $temp_posts[$value]->ID . '">' . $title . '<span class="remove"></span></a>';
198 unset($temp_posts[$value]);
199 }
200
201 foreach($temp_posts as $id => $post)
202 {
203 $title = get_the_title($post->ID);
204 // status
205 if($post->post_status == "private" || $post->post_status == "draft")
206 {
207 $title .= " ($post->post_status)";
208 }
209
210 echo '<a href="javascript:;" class="hide" data-post_id="' . $post->ID . '">' . $title . '<span class="remove"></span></a>';
211 }
212 }
213
214 ?>
215 </div>
216 </div>
217
218
219 </div>
220 <?php
221
222
223 }
224
225
226 /*--------------------------------------------------------------------------------------
227 *
228 * create_options
229 *
230 * @author Elliot Condon
231 * @since 2.0.6
232 * @updated 2.2.0
233 *
234 *-------------------------------------------------------------------------------------*/
235
236 function create_options($key, $field)
237 {
238 // defaults
239 $field['post_type'] = isset($field['post_type']) ? $field['post_type'] : '';
240 $field['max'] = isset($field['max']) ? $field['max'] : '-1';
241 $field['taxonomy'] = isset($field['taxonomy']) ? $field['taxonomy'] : array('all');
242 //$field['meta_key'] = isset($field['meta_key']) ? $field['meta_key'] : '';
243 //$field['meta_value'] = isset($field['meta_value']) ? $field['meta_value'] : '';
244 ?>
245 <tr class="field_option field_option_<?php echo $this->name; ?>">
246 <td class="label">
247 <label for=""><?php _e("Post Type",'acf'); ?></label>
248 </td>
249 <td>
250 <?php
251 $post_types = array('' => __("All",'acf'));
252
253 foreach (get_post_types(array('public' => true)) as $post_type ) {
254 $post_types[$post_type] = $post_type;
255 }
256
257 $this->parent->create_field(array(
258 'type' => 'select',
259 'name' => 'fields['.$key.'][post_type]',
260 'value' => $field['post_type'],
261 'choices' => $post_types,
262 'multiple' => '1',
263 ));
264 ?>
265 </td>
266 </tr>
267 <?php /*<tr class="field_option field_option_<?php echo $this->name; ?>">
268 <td class="label">
269 <label><?php _e("Filter Posts",'acf'); ?></label>
270 <p class="description"><?php _e("Where meta_key == meta_value",'acf'); ?></p>
271 </td>
272 <td>
273 <div style="width:45%; float:left">
274 <?php
275 $this->parent->create_field(array(
276 'type' => 'text',
277 'name' => 'fields['.$key.'][meta_key]',
278 'value' => $field['meta_key'],
279 ));
280 ?>
281 </div>
282 <div style="width:10%; float:left; text-align:center; padding:5px 0 0;">is equal to</div>
283 <div style="width:45%; float:left">
284 <?php
285 $this->parent->create_field(array(
286 'type' => 'text',
287 'name' => 'fields['.$key.'][meta_value]',
288 'value' => $field['meta_value'],
289 ));
290 ?>
291 </div>
292 </td>
293 </tr>*/ ?>
294 <tr class="field_option field_option_<?php echo $this->name; ?>">
295 <td class="label">
296 <label><?php _e("Filter from Taxonomy",'acf'); ?></label>
297 </td>
298 <td>
299 <?php
300 $choices = array(
301 '' => array(
302 'all' => __("All",'acf')
303 )
304 );
305 $choices = array_merge($choices, $this->parent->get_taxonomies_for_select());
306 $this->parent->create_field(array(
307 'type' => 'select',
308 'name' => 'fields['.$key.'][taxonomy]',
309 'value' => $field['taxonomy'],
310 'choices' => $choices,
311 'optgroup' => true,
312 'multiple' => '1',
313 ));
314 ?>
315 </td>
316 </tr>
317 <tr class="field_option field_option_<?php echo $this->name; ?>">
318 <td class="label">
319 <label><?php _e("Maximum posts",'acf'); ?></label>
320 <p class="description"><?php _e("Set to -1 for infinite",'acf'); ?></p>
321 </td>
322 <td>
323 <?php
324 $this->parent->create_field(array(
325 'type' => 'text',
326 'name' => 'fields['.$key.'][max]',
327 'value' => $field['max'],
328 ));
329 ?>
330 </td>
331 </tr>
332
333
334
335 <?php
336 }
337
338
339 /*--------------------------------------------------------------------------------------
340 *
341 * get_value_for_api
342 *
343 * @author Elliot Condon
344 * @since 3.0.0
345 *
346 *-------------------------------------------------------------------------------------*/
347
348 function get_value_for_api($post_id, $field)
349 {
350 // vars
351 $value = parent::get_value($post_id, $field);
352 $return = false;
353
354 if(!$value || $value == "")
355 {
356 return $return;
357 }
358
359 $value = explode(',', $value);
360
361 if(is_array($value))
362 {
363 $return = array();
364 foreach($value as $v)
365 {
366 $p = get_post($v);
367
368 if( $p && in_array( $p->post_status, array('publish', 'private', 'draft', 'inherit')) )
369 {
370 $return[] = $p;
371 }
372 }
373 }
374 else
375 {
376 $return = array(get_post($value));
377 }
378
379 return $return;
380 }
381
382
383
384 }
385
386 ?>