Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PodsField_Pick often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PodsField_Pick, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PodsField_Pick extends PodsField { |
||
6 | |||
7 | /** |
||
8 | * Field Type Group |
||
9 | * |
||
10 | * @var string |
||
11 | * @since 2.0 |
||
12 | */ |
||
13 | public static $group = 'Relationships / Media'; |
||
14 | |||
15 | /** |
||
16 | * Field Type Identifier |
||
17 | * |
||
18 | * @var string |
||
19 | * @since 2.0 |
||
20 | */ |
||
21 | public static $type = 'pick'; |
||
22 | |||
23 | /** |
||
24 | * Field Type Label |
||
25 | * |
||
26 | * @var string |
||
27 | * @since 2.0 |
||
28 | */ |
||
29 | public static $label = 'Relationship'; |
||
30 | |||
31 | /** |
||
32 | * Available Related Objects |
||
33 | * |
||
34 | * @var array |
||
35 | * @since 2.3 |
||
36 | */ |
||
37 | public static $related_objects = array(); |
||
38 | |||
39 | /** |
||
40 | * Custom Related Objects |
||
41 | * |
||
42 | * @var array |
||
43 | * @since 2.3 |
||
44 | */ |
||
45 | public static $custom_related_objects = array(); |
||
46 | |||
47 | /** |
||
48 | * Data used during validate / save to avoid extra queries |
||
49 | * |
||
50 | * @var array |
||
51 | * @since 2.3 |
||
52 | */ |
||
53 | public static $related_data = array(); |
||
54 | |||
55 | /** |
||
56 | * Data used during input method (mainly for autocomplete) |
||
57 | * |
||
58 | * @var array |
||
59 | * @since 2.3 |
||
60 | */ |
||
61 | public static $field_data = array(); |
||
62 | |||
63 | /** |
||
64 | * API caching for fields that need it during validate/save |
||
65 | * |
||
66 | * @var \PodsAPI |
||
67 | * @since 2.3 |
||
68 | */ |
||
69 | protected static $api = false; |
||
70 | |||
71 | /** |
||
72 | * Saved array of simple relationship names |
||
73 | * |
||
74 | * @var array |
||
75 | * @since 2.5 |
||
76 | */ |
||
77 | private static $names_simple = null; |
||
78 | |||
79 | /** |
||
80 | * Saved array of relationship names |
||
81 | * |
||
82 | * @var array |
||
83 | * @since 2.5 |
||
84 | */ |
||
85 | private static $names_related = null; |
||
86 | |||
87 | /** |
||
88 | * Saved array of bidirectional relationship names |
||
89 | * |
||
90 | * @var array |
||
91 | * @since 2.5 |
||
92 | */ |
||
93 | private static $names_bidirectional = null; |
||
94 | |||
95 | /** |
||
96 | * Setup related objects list |
||
97 | * |
||
98 | * @since 2.0 |
||
99 | */ |
||
100 | public function __construct () { |
||
103 | |||
104 | /** |
||
105 | * Add admin_init actions |
||
106 | * |
||
107 | * @since 2.3 |
||
108 | */ |
||
109 | public function admin_init () { |
||
110 | //--!! Prototype testing only |
||
111 | add_action( 'wp_ajax_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) ); |
||
112 | add_action( 'wp_ajax_nopriv_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) ); |
||
113 | |||
114 | // AJAX for Relationship lookups |
||
115 | add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) ); |
||
116 | add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Add options and set defaults to |
||
121 | * |
||
122 | * @return array |
||
123 | * |
||
124 | * @since 2.0 |
||
125 | */ |
||
126 | public function options () { |
||
127 | $options = array( |
||
128 | self::$type . '_format_type' => array( |
||
129 | 'label' => __( 'Selection Type', 'pods' ), |
||
130 | 'help' => __( 'help', 'pods' ), |
||
131 | 'default' => 'single', |
||
132 | 'type' => 'pick', |
||
133 | 'data' => array( |
||
134 | 'single' => __( 'Single Select', 'pods' ), |
||
135 | 'multi' => __( 'Multiple Select', 'pods' ) |
||
136 | ), |
||
137 | 'dependency' => true |
||
138 | ), |
||
139 | self::$type . '_format_single' => array( |
||
140 | 'label' => __( 'Format', 'pods' ), |
||
141 | 'help' => __( 'help', 'pods' ), |
||
142 | 'depends-on' => array( self::$type . '_format_type' => 'single' ), |
||
143 | 'default' => 'dropdown', |
||
144 | 'type' => 'pick', |
||
145 | 'data' => apply_filters( |
||
146 | 'pods_form_ui_field_pick_format_single_options', |
||
147 | array( |
||
148 | 'dropdown' => __( 'Drop Down', 'pods' ), |
||
149 | 'radio' => __( 'Radio Buttons', 'pods' ), |
||
150 | 'autocomplete' => __( 'Autocomplete', 'pods' ) |
||
151 | ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() ) |
||
152 | ), |
||
153 | 'dependency' => true |
||
154 | ), |
||
155 | self::$type . '_format_multi' => array( |
||
156 | 'label' => __( 'Format', 'pods' ), |
||
157 | 'help' => __( 'help', 'pods' ), |
||
158 | 'depends-on' => array( self::$type . '_format_type' => 'multi' ), |
||
159 | 'default' => 'checkbox', |
||
160 | 'type' => 'pick', |
||
161 | 'data' => apply_filters( |
||
162 | 'pods_form_ui_field_pick_format_multi_options', |
||
163 | array( |
||
164 | 'checkbox' => __( 'Checkboxes', 'pods' ), |
||
165 | 'multiselect' => __( 'Multi Select', 'pods' ), |
||
166 | 'autocomplete' => __( 'Autocomplete', 'pods' ) |
||
167 | ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() ) |
||
168 | ), |
||
169 | 'dependency' => true |
||
170 | ), |
||
171 | self::$type . '_taggable' => array( |
||
172 | 'label' => __( 'Taggable', 'pods' ), |
||
173 | 'help' => __( 'Allow new values to be inserted when using an Autocomplete field', 'pods' ), |
||
174 | 'excludes-on' => array( |
||
175 | self::$type . '_format_single' => array( 'dropdown', 'radio' ), |
||
176 | self::$type . '_format_multi' => array( 'checkbox', 'multiselect' ), |
||
177 | self::$type . '_object' => array_merge( |
||
178 | array( 'site', 'network' ), |
||
179 | self::simple_objects() |
||
180 | ) |
||
181 | ), |
||
182 | 'type' => 'boolean', |
||
183 | 'default' => 0 |
||
184 | ), |
||
185 | self::$type . '_select_text' => array( |
||
186 | 'label' => __( 'Default Select Text', 'pods' ), |
||
187 | 'help' => __( 'This is the text use for the default "no selection" dropdown item, if empty, it will default to "-- Select One --"', 'pods' ), |
||
188 | 'depends-on' => array( |
||
189 | self::$type . '_format_type' => 'single', |
||
190 | self::$type . '_format_single' => 'dropdown' |
||
191 | ), |
||
192 | 'default' => '', |
||
193 | 'type' => 'text' |
||
194 | ), |
||
195 | self::$type . '_limit' => array( |
||
196 | 'label' => __( 'Selection Limit', 'pods' ), |
||
197 | 'help' => __( 'help', 'pods' ), |
||
198 | 'depends-on' => array( self::$type . '_format_type' => 'multi' ), |
||
199 | 'default' => 0, |
||
200 | 'type' => 'number' |
||
201 | ), |
||
202 | self::$type . '_allow_html' => array( |
||
203 | 'label' => __('Allow HTML','pods'), |
||
204 | 'type' => 'boolean', |
||
205 | 'default' => 0 |
||
206 | ), |
||
207 | self::$type . '_table_id' => array( |
||
208 | 'label' => __( 'Table ID Column', 'pods' ), |
||
209 | 'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ), |
||
210 | 'depends-on' => array( self::$type . '_object' => 'table' ), |
||
211 | 'required' => 1, |
||
212 | 'default' => '', |
||
213 | 'type' => 'text' |
||
214 | ), |
||
215 | self::$type . '_table_index' => array( |
||
216 | 'label' => __( 'Table Index Column', 'pods' ), |
||
217 | 'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ), |
||
218 | 'depends-on' => array( self::$type . '_object' => 'table' ), |
||
219 | 'required' => 1, |
||
220 | 'default' => '', |
||
221 | 'type' => 'text' |
||
222 | ), |
||
223 | self::$type . '_display' => array( |
||
224 | 'label' => __( 'Display Field in Selection List', 'pods' ), |
||
225 | 'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ), |
||
226 | 'excludes-on' => array( |
||
227 | self::$type . '_object' => array_merge( |
||
228 | array( 'site', 'network' ), |
||
229 | self::simple_objects() |
||
230 | ) |
||
231 | ), |
||
232 | 'default' => '', |
||
233 | 'type' => 'text' |
||
234 | ), |
||
235 | self::$type . '_user_role' => array( |
||
236 | 'label' => __( 'Limit list to Role(s)', 'pods' ), |
||
237 | 'help' => __( 'help', 'pods' ), |
||
238 | 'depends-on' => array( self::$type . '_object' => 'user' ), |
||
239 | 'default' => '', |
||
240 | 'type' => 'pick', |
||
241 | 'pick_object' => 'role', |
||
242 | 'pick_format_type' => 'multi' |
||
243 | ),/* |
||
|
|||
244 | self::$type . '_user_site' => array( |
||
245 | 'label' => __( 'Limit list to Site(s)', 'pods' ), |
||
246 | 'help' => __( 'help', 'pods' ), |
||
247 | 'depends-on' => array( self::$type . '_object' => 'user' ), |
||
248 | 'default' => '', |
||
249 | 'type' => 'pick', |
||
250 | 'pick_object' => 'site', |
||
251 | 'pick_format_type' => 'multi' |
||
252 | ),*/ |
||
253 | self::$type . '_where' => array( |
||
254 | 'label' => __( 'Customized <em>WHERE</em>', 'pods' ), |
||
255 | 'help' => __( 'help', 'pods' ), |
||
256 | 'excludes-on' => array( |
||
257 | self::$type . '_object' => array_merge( |
||
258 | array( 'site', 'network' ), |
||
259 | self::simple_objects() |
||
260 | ) |
||
261 | ), |
||
262 | 'default' => '', |
||
263 | 'type' => 'text' |
||
264 | ), |
||
265 | self::$type . '_orderby' => array( |
||
266 | 'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ), |
||
267 | 'help' => __( 'help', 'pods' ), |
||
268 | 'excludes-on' => array( |
||
269 | self::$type . '_object' => array_merge( |
||
270 | array( 'site', 'network' ), |
||
271 | self::simple_objects() |
||
272 | ) |
||
273 | ), |
||
274 | 'default' => '', |
||
275 | 'type' => 'text' |
||
276 | ), |
||
277 | self::$type . '_groupby' => array( |
||
278 | 'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ), |
||
279 | 'help' => __( 'help', 'pods' ), |
||
280 | 'excludes-on' => array( |
||
281 | self::$type . '_object' => array_merge( |
||
282 | array( 'site', 'network' ), |
||
283 | self::simple_objects() |
||
284 | ) |
||
285 | ), |
||
286 | 'default' => '', |
||
287 | 'type' => 'text' |
||
288 | ) |
||
289 | /*, |
||
290 | self::$type . '_size' => array( |
||
291 | 'label' => __( 'Field Size', 'pods' ), |
||
292 | 'default' => 'medium', |
||
293 | 'type' => 'pick', |
||
294 | 'data' => array( |
||
295 | 'small' => __( 'Small', 'pods' ), |
||
296 | 'medium' => __( 'Medium', 'pods' ), |
||
297 | 'large' => __( 'Large', 'pods' ) |
||
298 | ) |
||
299 | )*/ |
||
300 | ); |
||
301 | |||
302 | /*if ( !is_multisite() ) |
||
303 | unset( $options[ self::$type . '_user_site' ] );*/ |
||
304 | |||
305 | return $options; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Register a related object |
||
310 | * |
||
311 | * @param string $name Object name |
||
312 | * @param string $label Object label |
||
313 | * @param array $options Object options |
||
314 | * |
||
315 | * @return array|boolean Object array or false if unsuccessful |
||
316 | * @since 2.3 |
||
317 | */ |
||
318 | public function register_related_object ( $name, $label, $options = null ) { |
||
319 | if ( empty( $name ) || empty( $label ) ) |
||
320 | return false; |
||
321 | |||
322 | $related_object = array( |
||
323 | 'label' => $label, |
||
324 | 'group' => 'Custom Relationships', |
||
325 | 'simple' => true, |
||
326 | 'bidirectional' => false, |
||
327 | 'data' => array(), |
||
328 | 'data_callback' => null |
||
329 | ); |
||
330 | |||
331 | $related_object = array_merge( $related_object, $options ); |
||
332 | |||
333 | self::$custom_related_objects[ $name ] = $related_object; |
||
334 | |||
335 | return true; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Setup related objects |
||
340 | * |
||
341 | * @param boolean $force Whether to force refresh of related objects |
||
342 | * @return bool True when data has been loaded |
||
343 | * @since 2.3 |
||
344 | */ |
||
345 | public function setup_related_objects ( $force = false ) { |
||
346 | $new_data_loaded = false; |
||
347 | |||
348 | if ( ! $force && empty( self::$related_objects ) ) { |
||
349 | // Only load transient if we aren't forcing a refresh |
||
350 | self::$related_objects = pods_transient_get( 'pods_related_objects' ); |
||
351 | if ( false !== self::$related_objects ) { |
||
352 | $new_data_loaded = true; |
||
353 | } |
||
354 | } elseif ( $force ) { |
||
355 | // If we are rebuilding, make sure we start with a clean slate |
||
356 | self::$related_objects = array(); |
||
357 | } |
||
358 | |||
359 | if ( empty( self::$related_objects ) ) { |
||
360 | // Do a complete build of related_objects |
||
361 | $new_data_loaded = true; |
||
362 | |||
363 | // Custom |
||
364 | self::$related_objects[ 'custom-simple' ] = array( |
||
365 | 'label' => __( 'Simple (custom defined list)', 'pods' ), |
||
366 | 'group' => __( 'Custom', 'pods' ), |
||
367 | 'simple' => true |
||
368 | ); |
||
369 | |||
370 | // Pods |
||
371 | $pod_options = array(); |
||
372 | |||
373 | // Include PodsMeta if not already included |
||
374 | pods_meta(); |
||
375 | |||
376 | // Advanced Content Types |
||
377 | $_pods = PodsMeta::$advanced_content_types; |
||
378 | |||
379 | View Code Duplication | foreach ( $_pods as $pod ) { |
|
380 | $pod_options[ $pod[ 'name' ] ] = $pod[ 'label' ] . ' (' . $pod[ 'name' ] . ')'; |
||
381 | } |
||
382 | |||
383 | // Settings |
||
384 | $_pods = PodsMeta::$settings; |
||
385 | |||
386 | View Code Duplication | foreach ( $_pods as $pod ) { |
|
387 | $pod_options[ $pod[ 'name' ] ] = $pod[ 'label' ] . ' (' . $pod[ 'name' ] . ')'; |
||
388 | } |
||
389 | |||
390 | asort( $pod_options ); |
||
391 | |||
392 | foreach ( $pod_options as $pod => $label ) { |
||
393 | self::$related_objects[ 'pod-' . $pod ] = array( |
||
394 | 'label' => $label, |
||
395 | 'group' => __( 'Pods', 'pods' ), |
||
396 | 'bidirectional' => true |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | // Post Types |
||
401 | $post_types = get_post_types(); |
||
402 | asort( $post_types ); |
||
403 | |||
404 | $ignore = array( 'attachment', 'revision', 'nav_menu_item' ); |
||
405 | |||
406 | View Code Duplication | foreach ( $post_types as $post_type => $label ) { |
|
407 | if ( in_array( $post_type, $ignore ) || empty( $post_type ) ) { |
||
408 | unset( $post_types[ $post_type ] ); |
||
409 | |||
410 | continue; |
||
411 | } |
||
412 | elseif ( 0 === strpos( $post_type, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) { |
||
413 | unset( $post_types[ $post_type ] ); |
||
414 | |||
415 | continue; |
||
416 | } |
||
417 | |||
418 | $post_type = get_post_type_object( $post_type ); |
||
419 | |||
420 | self::$related_objects[ 'post_type-' . $post_type->name ] = array( |
||
421 | 'label' => $post_type->label . ' (' . $post_type->name . ')', |
||
422 | 'group' => __( 'Post Types', 'pods' ), |
||
423 | 'bidirectional' => true |
||
424 | ); |
||
425 | } |
||
426 | |||
427 | // Taxonomies |
||
428 | $taxonomies = get_taxonomies(); |
||
429 | asort( $taxonomies ); |
||
430 | |||
431 | $ignore = array( 'nav_menu', 'post_format' ); |
||
432 | |||
433 | View Code Duplication | foreach ( $taxonomies as $taxonomy => $label ) { |
|
434 | if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) ) { |
||
435 | unset( $taxonomies[ $taxonomy ] ); |
||
436 | |||
437 | continue; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Prevent ability to extend core Pods content types. |
||
442 | * |
||
443 | * @param bool. Default is true, when set to false Pods internal content types can not be extended. |
||
444 | * |
||
445 | * @since 2.3.19 |
||
446 | */ |
||
447 | elseif ( 0 === strpos( $taxonomy, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) { |
||
448 | unset( $taxonomies[ $taxonomy ] ); |
||
449 | |||
450 | continue; |
||
451 | } |
||
452 | |||
453 | $taxonomy = get_taxonomy( $taxonomy ); |
||
454 | |||
455 | self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array( |
||
456 | 'label' => $taxonomy->label . ' (' . $taxonomy->name . ')', |
||
457 | 'group' => __( 'Taxonomies', 'pods' ), |
||
458 | 'bidirectional' => true |
||
459 | ); |
||
460 | } |
||
461 | |||
462 | // Other WP Objects |
||
463 | self::$related_objects[ 'user' ] = array( |
||
464 | 'label' => __( 'Users', 'pods' ), |
||
465 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
466 | 'bidirectional' => true |
||
467 | ); |
||
468 | |||
469 | self::$related_objects[ 'role' ] = array( |
||
470 | 'label' => __( 'User Roles', 'pods' ), |
||
471 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
472 | 'simple' => true, |
||
473 | 'data_callback' => array( $this, 'data_roles' ) |
||
474 | ); |
||
475 | |||
476 | self::$related_objects[ 'capability' ] = array( |
||
477 | 'label' => __( 'User Capabilities', 'pods' ), |
||
478 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
479 | 'simple' => true, |
||
480 | 'data_callback' => array( $this, 'data_capabilities' ) |
||
481 | ); |
||
482 | |||
483 | self::$related_objects[ 'media' ] = array( |
||
484 | 'label' => __( 'Media', 'pods' ), |
||
485 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
486 | 'bidirectional' => true |
||
487 | ); |
||
488 | |||
489 | self::$related_objects[ 'comment' ] = array( |
||
490 | 'label' => __( 'Comments', 'pods' ), |
||
491 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
492 | 'bidirectional' => true |
||
493 | ); |
||
494 | |||
495 | self::$related_objects[ 'image-size' ] = array( |
||
496 | 'label' => __( 'Image Sizes', 'pods' ), |
||
497 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
498 | 'simple' => true, |
||
499 | 'data_callback' => array( $this, 'data_image_sizes' ) |
||
500 | ); |
||
501 | |||
502 | self::$related_objects[ 'nav_menu' ] = array( |
||
503 | 'label' => __( 'Navigation Menus', 'pods' ), |
||
504 | 'group' => __( 'Other WP Objects', 'pods' ) |
||
505 | ); |
||
506 | |||
507 | self::$related_objects[ 'post_format' ] = array( |
||
508 | 'label' => __( 'Post Formats', 'pods' ), |
||
509 | 'group' => __( 'Other WP Objects', 'pods' ) |
||
510 | ); |
||
511 | |||
512 | self::$related_objects[ 'post-status' ] = array( |
||
513 | 'label' => __( 'Post Status', 'pods' ), |
||
514 | 'group' => __( 'Other WP Objects', 'pods' ), |
||
515 | 'simple' => true, |
||
516 | 'data_callback' => array( $this, 'data_post_stati' ) |
||
517 | ); |
||
518 | |||
519 | do_action( 'pods_form_ui_field_pick_related_objects_other' ); |
||
520 | |||
521 | self::$related_objects[ 'country' ] = array( |
||
522 | 'label' => __( 'Countries', 'pods' ), |
||
523 | 'group' => __( 'Predefined Lists', 'pods' ), |
||
524 | 'simple' => true, |
||
525 | 'data_callback' => array( $this, 'data_countries' ) |
||
526 | ); |
||
527 | |||
528 | self::$related_objects[ 'us_state' ] = array( |
||
529 | 'label' => __( 'US States', 'pods' ), |
||
530 | 'group' => __( 'Predefined Lists', 'pods' ), |
||
531 | 'simple' => true, |
||
532 | 'data_callback' => array( $this, 'data_us_states' ) |
||
533 | ); |
||
534 | |||
535 | self::$related_objects[ 'days_of_week' ] = array( |
||
536 | 'label' => __( 'Calendar - Days of Week', 'pods' ), |
||
537 | 'group' => __( 'Predefined Lists', 'pods' ), |
||
538 | 'simple' => true, |
||
539 | 'data_callback' => array( $this, 'data_days_of_week' ) |
||
540 | ); |
||
541 | |||
542 | self::$related_objects[ 'months_of_year' ] = array( |
||
543 | 'label' => __( 'Calendar - Months of Year', 'pods' ), |
||
544 | 'group' => __( 'Predefined Lists', 'pods' ), |
||
545 | 'simple' => true, |
||
546 | 'data_callback' => array( $this, 'data_months_of_year' ) |
||
547 | ); |
||
548 | |||
549 | do_action( 'pods_form_ui_field_pick_related_objects_predefined' ); |
||
550 | |||
551 | if ( did_action( 'init' ) ) |
||
552 | pods_transient_set( 'pods_related_objects', self::$related_objects ); |
||
553 | } |
||
554 | |||
555 | foreach ( self::$custom_related_objects as $object => $related_object ) { |
||
556 | if ( ! isset( self::$related_objects[ $object ] ) ) { |
||
557 | $new_data_loaded = true; |
||
558 | self::$related_objects[ $object ] = $related_object; |
||
559 | } |
||
560 | } |
||
561 | return $new_data_loaded; |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Return available related objects |
||
566 | * |
||
567 | * @param boolean $force Whether to force refresh of related objects |
||
568 | * |
||
569 | * @return array Field selection array |
||
570 | * @since 2.3 |
||
571 | */ |
||
572 | public function related_objects ( $force = false ) { |
||
573 | if ( $this->setup_related_objects( $force ) || null === self::$names_related ) { |
||
574 | $related_objects = array(); |
||
575 | |||
576 | foreach ( self::$related_objects as $related_object_name => $related_object ) { |
||
577 | if ( ! isset( $related_objects[ $related_object[ 'group' ] ] ) ) { |
||
578 | $related_objects[ $related_object[ 'group' ] ] = array(); |
||
579 | } |
||
580 | |||
581 | $related_objects[ $related_object[ 'group' ] ][ $related_object_name ] = $related_object[ 'label' ]; |
||
582 | } |
||
583 | |||
584 | self::$names_related = (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects ); |
||
585 | } |
||
586 | |||
587 | return self::$names_related; |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Return available simple object names |
||
592 | * |
||
593 | * @return array Simple object names |
||
594 | * @since 2.3 |
||
595 | */ |
||
596 | View Code Duplication | public function simple_objects () { |
|
597 | if ( $this->setup_related_objects() || null === self::$names_simple ) { |
||
598 | $simple_objects = array(); |
||
599 | |||
600 | foreach ( self::$related_objects as $object => $related_object ) { |
||
601 | if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] ) |
||
602 | continue; |
||
603 | |||
604 | $simple_objects[] = $object; |
||
605 | } |
||
606 | |||
607 | self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects ); |
||
608 | } |
||
609 | |||
610 | return self::$names_simple; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Return available bidirectional object names |
||
615 | * |
||
616 | * @return array Bidirectional object names |
||
617 | * @since 2.3.4 |
||
618 | */ |
||
619 | View Code Duplication | public function bidirectional_objects () { |
|
620 | if ( $this->setup_related_objects() || null === self::$names_bidirectional ) { |
||
621 | $bidirectional_objects = array(); |
||
622 | |||
623 | foreach ( self::$related_objects as $object => $related_object ) { |
||
624 | if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] ) |
||
625 | continue; |
||
626 | |||
627 | $bidirectional_objects[] = $object; |
||
628 | } |
||
629 | |||
630 | self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects ); |
||
631 | } |
||
632 | |||
633 | return self::$names_bidirectional; |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Define the current field's schema for DB table storage |
||
638 | * |
||
639 | * @param array $options |
||
640 | * |
||
641 | * @return array |
||
642 | * @since 2.0 |
||
643 | */ |
||
644 | public function schema ( $options = null ) { |
||
645 | $schema = false; |
||
646 | |||
647 | $simple_tableless_objects = $this->simple_objects(); |
||
648 | |||
649 | if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) ) |
||
650 | $schema = 'LONGTEXT'; |
||
651 | |||
652 | return $schema; |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * Change the way the value of the field is displayed with Pods::get |
||
657 | * |
||
658 | * @param mixed $value |
||
659 | * @param string $name |
||
660 | * @param array $options |
||
661 | * @param array $fields |
||
662 | * @param array $pod |
||
663 | * @param int $id |
||
664 | * |
||
665 | * @since 2.0 |
||
666 | */ |
||
667 | public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
668 | $fields = null; |
||
669 | |||
670 | if ( is_object( $pod ) && isset( $pod->fields ) ) { |
||
671 | $fields = $pod->fields; |
||
672 | |||
673 | if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) { |
||
674 | $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] ); |
||
675 | } |
||
676 | View Code Duplication | } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) { |
|
677 | $fields = $pod[ 'fields' ]; |
||
678 | |||
679 | if ( ! empty( $pod[ 'object_fields' ] ) ) { |
||
680 | $fields = array_merge( $fields, $pod[ 'object_fields' ] ); |
||
681 | } |
||
682 | } |
||
683 | |||
684 | return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) ); |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * Customize output of the form field |
||
689 | * |
||
690 | * @param string $name |
||
691 | * @param mixed $value |
||
692 | * @param array $options |
||
693 | * @param array $pod |
||
694 | * @param int $id |
||
695 | * |
||
696 | * @since 2.0 |
||
697 | */ |
||
698 | public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
699 | global $wpdb; |
||
700 | |||
701 | $options = (array) $options; |
||
702 | $form_field_type = PodsForm::$field_type; |
||
703 | |||
704 | $options[ 'grouped' ] = 1; |
||
705 | |||
706 | $options[ 'table_info' ] = array(); |
||
707 | |||
708 | $custom = pods_var_raw( self::$type . '_custom', $options, false ); |
||
709 | |||
710 | $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id ); |
||
711 | |||
712 | $ajax = false; |
||
713 | |||
714 | if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) ) |
||
715 | $ajax = true; |
||
716 | |||
717 | if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) { |
||
718 | $ajax = (boolean) self::$field_data[ 'autocomplete' ]; |
||
719 | } |
||
720 | |||
721 | $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id ); |
||
722 | |||
723 | if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) ) |
||
724 | $ajax = false; |
||
725 | |||
726 | if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) ) { |
||
727 | View Code Duplication | if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) |
|
728 | $field_type = 'select'; |
||
729 | elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) |
||
730 | $field_type = 'radio'; |
||
731 | elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) |
||
732 | $field_type = 'select2'; |
||
733 | elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) |
||
734 | $field_type = 'flexible'; |
||
735 | else { |
||
736 | // Support custom integration |
||
737 | do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_single', $options, 'dropdown' ), $name, $value, $options, $pod, $id ); |
||
738 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); |
||
739 | return; |
||
740 | } |
||
741 | } |
||
742 | elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) ) { |
||
743 | if ( !empty( $value ) && !is_array( $value ) ) |
||
744 | $value = explode( ',', $value ); |
||
745 | |||
746 | View Code Duplication | if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) |
|
747 | $field_type = 'checkbox'; |
||
748 | elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) |
||
749 | $field_type = 'select'; |
||
750 | elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) |
||
751 | $field_type = 'select2'; |
||
752 | elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) |
||
753 | $field_type = 'flexible'; |
||
754 | else { |
||
755 | // Support custom integration |
||
756 | do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_multi', $options, 'checkbox' ), $name, $value, $options, $pod, $id ); |
||
757 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); |
||
758 | return; |
||
759 | } |
||
760 | } |
||
761 | else { |
||
762 | // Support custom integration |
||
763 | do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id ); |
||
764 | return; |
||
765 | } |
||
766 | |||
767 | if ( 'flexible' == $field_type || ( 'select2' == $field_type && 1 == pods_v( self::$type . '_taggable', $options, 0 ) ) ) { |
||
768 | // @todo: Fix location when merging |
||
769 | $field_type = 'pick'; |
||
770 | pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) ); |
||
771 | return; |
||
772 | } |
||
773 | |||
774 | pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
||
775 | } |
||
776 | |||
777 | /** |
||
778 | * Validate a value before it's saved |
||
779 | * |
||
780 | * @param mixed $value |
||
781 | * @param string $name |
||
782 | * @param array $options |
||
783 | * @param array $fields |
||
784 | * @param array $pod |
||
785 | * @param int $id |
||
786 | * |
||
787 | * @param null $params |
||
788 | * @return array|bool |
||
789 | * @since 2.0 |
||
790 | */ |
||
791 | public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
895 | |||
896 | /** |
||
897 | * Save the value to the DB |
||
898 | * |
||
899 | * @param mixed $value |
||
900 | * @param int $id |
||
901 | * @param string $name |
||
902 | * @param array $options |
||
903 | * @param array $fields |
||
904 | * @param array $pod |
||
905 | * @param object $params |
||
906 | * |
||
907 | * @since 2.3 |
||
908 | */ |
||
909 | public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
910 | if ( empty( self::$api ) ) |
||
911 | self::$api = pods_api(); |
||
912 | |||
913 | $options[ 'id' ] = (int) $options[ 'id' ]; |
||
914 | |||
915 | if ( !isset( self::$related_data[ $options[ 'id' ] ] ) ) |
||
916 | return; |
||
917 | |||
918 | $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ]; |
||
919 | $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ]; |
||
920 | $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ]; |
||
921 | |||
922 | // Bidirectional relationship updates |
||
923 | if ( !empty( $related_field ) ) { |
||
924 | // Don't use no conflict mode unless this isn't the current pod type |
||
925 | $no_conflict = true; |
||
926 | |||
927 | if ( $related_pod[ 'type' ] != $pod[ 'type' ] ) |
||
928 | $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] ); |
||
929 | |||
930 | if ( !$no_conflict ) |
||
931 | pods_no_conflict_on( $related_pod[ 'type' ] ); |
||
932 | |||
933 | $value = array_filter( $value ); |
||
934 | |||
935 | foreach ( $value as $related_id ) { |
||
936 | if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) ) |
||
937 | $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ]; |
||
938 | else |
||
939 | $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod ); |
||
940 | |||
941 | $bidirectional_ids = array_filter( $bidirectional_ids ); |
||
942 | |||
943 | if ( empty( $bidirectional_ids ) ) |
||
944 | $bidirectional_ids = array(); |
||
945 | |||
946 | $remove_ids = array(); |
||
947 | |||
948 | if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) { |
||
949 | while ( $related_pick_limit <= count( $bidirectional_ids ) ) { |
||
950 | $remove_ids[] = (int) array_pop( $bidirectional_ids ); |
||
951 | } |
||
952 | } |
||
953 | |||
954 | // Remove this item from related items no longer related to |
||
955 | $remove_ids = array_unique( array_filter( $remove_ids ) ); |
||
956 | |||
957 | // Add to related items |
||
958 | if ( !in_array( $id, $bidirectional_ids ) ) |
||
959 | $bidirectional_ids[] = $id; |
||
960 | // Nothing to change |
||
961 | elseif ( empty( $remove_ids ) ) |
||
962 | continue; |
||
963 | |||
964 | self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field ); |
||
965 | |||
966 | if ( !empty( $remove_ids ) ) |
||
967 | self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options ); |
||
968 | } |
||
969 | |||
970 | if ( !$no_conflict ) |
||
971 | pods_no_conflict_off( $related_pod[ 'type' ] ); |
||
972 | } |
||
973 | } |
||
974 | |||
975 | /** |
||
976 | * Delete the value from the DB |
||
977 | * |
||
978 | * @param int $id |
||
979 | * @param string $name |
||
980 | * @param array $options |
||
981 | * @param array $pod |
||
982 | * |
||
983 | * @since 2.3 |
||
984 | */ |
||
985 | public function delete ( $id = null, $name = null, $options = null, $pod = null ) { |
||
986 | if ( empty( self::$api ) ) |
||
987 | self::$api = pods_api(); |
||
988 | |||
989 | $simple_tableless_objects = $this->simple_objects(); |
||
990 | |||
991 | // Bidirectional relationship requirement checks |
||
992 | $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc.. |
||
993 | $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc.. |
||
994 | $related_sister_id = (int) pods_var( 'sister_id', $options, 0 ); |
||
995 | |||
996 | if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) { |
||
997 | $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false ); |
||
998 | |||
999 | if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) { |
||
1000 | $related_field = false; |
||
1001 | |||
1002 | // Ensure sister_id exists on related Pod |
||
1003 | View Code Duplication | foreach ( $related_pod[ 'fields' ] as $related_pod_field ) { |
|
1004 | if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) { |
||
1005 | $related_field = $related_pod_field; |
||
1006 | |||
1007 | break; |
||
1008 | } |
||
1009 | } |
||
1010 | |||
1011 | if ( !empty( $related_field ) ) { |
||
1012 | $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod ); |
||
1013 | |||
1014 | if ( !empty( $values ) ) { |
||
1015 | $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] ); |
||
1016 | |||
1017 | if ( !$no_conflict ) |
||
1018 | pods_no_conflict_on( $related_pod[ 'type' ] ); |
||
1019 | |||
1020 | self::$api->delete_relationships( $values, $id, $related_pod, $related_field ); |
||
1021 | |||
1022 | if ( !$no_conflict ) |
||
1023 | pods_no_conflict_off( $related_pod[ 'type' ] ); |
||
1024 | } |
||
1025 | } |
||
1026 | } |
||
1027 | } |
||
1028 | } |
||
1029 | |||
1030 | /** |
||
1031 | * Customize the Pods UI manage table column output |
||
1032 | * |
||
1033 | * @param int $id |
||
1034 | * @param mixed $value |
||
1035 | * @param string $name |
||
1036 | * @param array $options |
||
1037 | * @param array $fields |
||
1038 | * @param array $pod |
||
1039 | * |
||
1040 | * @since 2.0 |
||
1041 | */ |
||
1042 | public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
1043 | $value = $this->simple_value( $name, $value, $options, $pod, $id ); |
||
1044 | |||
1045 | return $this->display( $value, $name, $options, $pod, $id ); |
||
1046 | } |
||
1047 | |||
1048 | /** |
||
1049 | * Get the data from the field |
||
1050 | * |
||
1051 | * @param string $name The name of the field |
||
1052 | * @param string|array $value The value of the field |
||
1053 | * @param array $options Field options |
||
1054 | * @param array $pod Pod data |
||
1055 | * @param int $id Item ID |
||
1056 | * @param boolean $in_form |
||
1057 | * |
||
1058 | * @return array Array of possible field data |
||
1059 | * |
||
1060 | * @since 2.0 |
||
1061 | */ |
||
1062 | public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) { |
||
1063 | View Code Duplication | if ( isset( $options[ 'options' ] ) ) { |
|
1064 | $options = array_merge( $options, $options[ 'options' ] ); |
||
1065 | |||
1066 | unset( $options[ 'options' ] ); |
||
1067 | } |
||
1068 | |||
1069 | $data = pods_var_raw( 'data', $options, null, null, true ); |
||
1070 | |||
1071 | $object_params = array( |
||
1072 | 'name' => $name, // The name of the field |
||
1073 | 'value' => $value, // The value of the field |
||
1074 | 'options' => $options, // Field options |
||
1075 | 'pod' => $pod, // Pod data |
||
1076 | 'id' => $id, // Item ID |
||
1077 | 'context' => 'data', // Data context |
||
1078 | ); |
||
1079 | |||
1080 | if ( null !== $data ) |
||
1081 | $data = (array) $data; |
||
1082 | else |
||
1083 | $data = $this->get_object_data( $object_params ); |
||
1084 | |||
1085 | if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) |
||
1086 | $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data; |
||
1087 | |||
1088 | $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id ); |
||
1089 | |||
1090 | return $data; |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * Convert a simple value to the correct value |
||
1095 | * |
||
1096 | * @param string $name The name of the field |
||
1097 | * @param string|array $value The value of the field |
||
1098 | * @param array $options Field options |
||
1099 | * @param array $pod Pod data |
||
1100 | * @param int $id Item ID |
||
1101 | * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false) |
||
1102 | * |
||
1103 | * @return mixed Corrected value |
||
1104 | */ |
||
1105 | public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) { |
||
1106 | if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) { |
||
1107 | View Code Duplication | if ( isset( $options[ 'options' ] ) ) { |
|
1108 | $options = array_merge( $options, $options[ 'options' ] ); |
||
1109 | |||
1110 | unset( $options[ 'options' ] ); |
||
1111 | } |
||
1112 | |||
1113 | if ( !is_array( $value ) && 0 < strlen( $value ) ) { |
||
1114 | $simple = @json_decode( $value, true ); |
||
1115 | |||
1116 | if ( is_array( $simple ) ) |
||
1117 | $value = $simple; |
||
1118 | } |
||
1119 | |||
1120 | $data = pods_var_raw( 'data', $options, null, null, true ); |
||
1121 | |||
1122 | $object_params = array( |
||
1123 | 'name' => $name, // The name of the field |
||
1124 | 'value' => $value, // The value of the field |
||
1125 | 'options' => $options, // Field options |
||
1126 | 'pod' => $pod, // Pod data |
||
1127 | 'id' => $id, // Item ID |
||
1128 | 'context' => 'simple_value', // Data context |
||
1129 | ); |
||
1130 | |||
1131 | if ( null === $data ) |
||
1132 | $data = $this->get_object_data( $object_params ); |
||
1133 | |||
1134 | $data = (array) $data; |
||
1135 | |||
1136 | $key = 0; |
||
1137 | |||
1138 | if ( is_array( $value ) ) { |
||
1139 | if ( !empty( $data ) ) { |
||
1140 | $val = array(); |
||
1141 | |||
1142 | foreach ( $value as $k => $v ) { |
||
1143 | if ( isset( $data[ $v ] ) ) { |
||
1144 | if ( false === $raw ) { |
||
1145 | $k = $v; |
||
1146 | $v = $data[ $v ]; |
||
1147 | } |
||
1148 | |||
1149 | $val[ $k ] = $v; |
||
1150 | } |
||
1151 | } |
||
1152 | |||
1153 | $value = $val; |
||
1154 | } |
||
1155 | } |
||
1156 | elseif ( isset( $data[ $value ] ) && false === $raw ) { |
||
1157 | $key = $value; |
||
1158 | $value = $data[ $value ]; |
||
1159 | } |
||
1160 | |||
1161 | $single_multi = pods_var( self::$type . '_format_type', $options, 'single' ); |
||
1162 | |||
1163 | if ( 'multi' == $single_multi ) |
||
1164 | $limit = (int) pods_var( self::$type . '_limit', $options, 0 ); |
||
1165 | else |
||
1166 | $limit = 1; |
||
1167 | |||
1168 | if ( is_array( $value ) && 0 < $limit ) { |
||
1169 | if ( 1 == $limit ) |
||
1170 | $value = current( $value ); |
||
1171 | else |
||
1172 | $value = array_slice( $value, 0, $limit, true ); |
||
1173 | } |
||
1174 | elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) { |
||
1175 | if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) { |
||
1176 | $value = array( |
||
1177 | $key => $value |
||
1178 | ); |
||
1179 | } |
||
1180 | } |
||
1181 | } |
||
1182 | |||
1183 | return $value; |
||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * Get the label from a pick value |
||
1188 | * |
||
1189 | * @param string $name The name of the field |
||
1190 | * @param string|array $value The value of the field |
||
1191 | * @param array $options Field options |
||
1192 | * @param array $pod Pod data |
||
1193 | * @param int $id Item ID |
||
1194 | * |
||
1195 | * @return string |
||
1196 | * |
||
1197 | * @since 2.2 |
||
1198 | */ |
||
1199 | public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
1200 | View Code Duplication | if ( isset( $options[ 'options' ] ) ) { |
|
1201 | $options = array_merge( $options, $options[ 'options' ] ); |
||
1202 | |||
1203 | unset( $options[ 'options' ] ); |
||
1204 | } |
||
1205 | |||
1206 | $data = pods_var_raw( 'data', $options, null, null, true ); |
||
1207 | |||
1208 | $object_params = array( |
||
1209 | 'name' => $name, // The name of the field |
||
1210 | 'value' => $value, // The value of the field |
||
1211 | 'options' => $options, // Field options |
||
1212 | 'pod' => $pod, // Pod data |
||
1213 | 'id' => $id, // Item ID |
||
1214 | 'context' => 'value_to_label', // Data context |
||
1215 | ); |
||
1216 | |||
1217 | if ( null !== $data ) |
||
1218 | $data = (array) $data; |
||
1219 | else |
||
1220 | $data = $this->get_object_data( $object_params ); |
||
1221 | |||
1222 | $labels = array(); |
||
1223 | |||
1224 | foreach ( $data as $v => $l ) { |
||
1225 | if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) ) |
||
1226 | $labels[] = $l; |
||
1227 | } |
||
1228 | |||
1229 | $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id ); |
||
1230 | |||
1231 | $labels = pods_serial_comma( $labels ); |
||
1232 | |||
1233 | return $labels; |
||
1234 | } |
||
1235 | |||
1236 | /** |
||
1237 | * Get available items from a relationship field |
||
1238 | * |
||
1239 | * @param array|string $field Field array or field name |
||
1240 | * @param array $options [optional] Field options array overrides |
||
1241 | * @param array $object_params [optional] Additional get_object_data options |
||
1242 | * |
||
1243 | * @return array An array of available items from a relationship field |
||
1244 | */ |
||
1245 | public function get_field_data( $field, $options = array(), $object_params = array() ) { |
||
1246 | |||
1247 | // Handle field array overrides |
||
1248 | if ( is_array( $field ) ) { |
||
1249 | $options = array_merge( $field, $options ); |
||
1250 | } |
||
1251 | |||
1252 | // Get field name from array |
||
1253 | $field = pods_var_raw( 'name', $options, $field, null, true ); |
||
1254 | |||
1255 | // Field name or options not set |
||
1256 | if ( empty( $field ) || empty( $options ) ) { |
||
1257 | return array(); |
||
1258 | } |
||
1259 | |||
1260 | // Options normalization |
||
1261 | $options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) ); |
||
1262 | |||
1263 | // Setup object params |
||
1264 | $object_params = array_merge( |
||
1265 | array( |
||
1266 | 'name' => $field, // The name of the field |
||
1267 | 'options' => $options, // Field options |
||
1268 | ), |
||
1269 | $object_params |
||
1270 | ); |
||
1271 | |||
1272 | // Get data override |
||
1273 | $data = pods_var_raw( 'data', $options, null, null, true ); |
||
1274 | |||
1275 | // Return data override |
||
1276 | if ( null !== $data ) { |
||
1277 | $data = (array) $data; |
||
1278 | } |
||
1279 | // Get object data |
||
1280 | else { |
||
1281 | $data = $this->get_object_data( $object_params ); |
||
1282 | } |
||
1283 | |||
1284 | return $data; |
||
1285 | |||
1286 | } |
||
1287 | |||
1288 | /** |
||
1289 | * Get data from relationship objects |
||
1290 | * |
||
1291 | * @param array $object_params Object data parameters |
||
1292 | * |
||
1293 | * @return array|bool Object data |
||
1294 | */ |
||
1295 | public function get_object_data ( $object_params = null ) { |
||
1795 | |||
1796 | /** |
||
1797 | * AJAX call to refresh relationship field markup (supports adding new records modally) |
||
1798 | * |
||
1799 | * @since 2.7 |
||
1800 | */ |
||
1801 | public function admin_ajax_relationship_popup () { |
||
1802 | |||
1803 | $data = pods_unslash( (array) $_POST ); |
||
1804 | |||
1805 | // Get the field information |
||
1806 | $params = array( |
||
1807 | 'pod_id' => $data[ 'pod_id' ], |
||
1808 | 'id' => $data[ 'field_id' ] |
||
1809 | ); |
||
1810 | $field = pods_api()->load_field( $params ); |
||
1811 | |||
1812 | // Get Pods object for this item |
||
1813 | $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] ); |
||
1814 | |||
1815 | // Get the relationship field's value(s) |
||
1816 | $field_name = $field[ 'name' ]; |
||
1817 | $params = array( |
||
1818 | 'name' => $field_name, |
||
1819 | 'in_form' => true |
||
1820 | ); |
||
1821 | $value = $pod->field( $params); |
||
1822 | |||
1823 | // Build the markup and return it to the caller |
||
1824 | $meta_field_name = 'pods_meta_' . $field_name; |
||
1825 | $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] ); |
||
1826 | echo $output; |
||
1827 | |||
1828 | die(); |
||
1829 | } |
||
1830 | |||
1831 | /** |
||
1832 | * Handle autocomplete AJAX |
||
1833 | * |
||
1834 | * @since 2.3 |
||
1835 | */ |
||
1836 | public function admin_ajax_relationship () { |
||
1933 | |||
1934 | /** |
||
1935 | * Data callback for Post Stati |
||
1936 | * |
||
1937 | * @param string $name The name of the field |
||
1938 | * @param string|array $value The value of the field |
||
1939 | * @param array $options Field options |
||
1940 | * @param array $pod Pod data |
||
1941 | * @param int $id Item ID |
||
1942 | * |
||
1943 | * @return array |
||
1944 | * |
||
1945 | * @since 2.3 |
||
1946 | */ |
||
1947 | public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
1948 | $data = array(); |
||
1949 | |||
1950 | $post_stati = get_post_stati( array(), 'objects' ); |
||
1951 | |||
1958 | |||
1959 | /** |
||
1960 | * Data callback for User Roles |
||
1961 | * |
||
1962 | * @param string $name The name of the field |
||
1963 | * @param string|array $value The value of the field |
||
1964 | * @param array $options Field options |
||
1965 | * @param array $pod Pod data |
||
1966 | * @param int $id Item ID |
||
1967 | * |
||
1968 | * @return array |
||
1969 | * |
||
1970 | * @since 2.3 |
||
1971 | */ |
||
1972 | public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
1983 | |||
1984 | /** |
||
1985 | * Data callback for User Capabilities |
||
1986 | * |
||
1987 | * @param string $name The name of the field |
||
1988 | * @param string|array $value The value of the field |
||
1989 | * @param array $options Field options |
||
1990 | * @param array $pod Pod data |
||
1991 | * @param int $id Item ID |
||
1992 | * |
||
1993 | * @return array |
||
1994 | * |
||
1995 | * @since 2.3 |
||
1996 | */ |
||
1997 | public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2085 | |||
2086 | /** |
||
2087 | * Data callback for Image Sizes |
||
2088 | * |
||
2089 | * @param string $name The name of the field |
||
2090 | * @param string|array $value The value of the field |
||
2091 | * @param array $options Field options |
||
2092 | * @param array $pod Pod data |
||
2093 | * @param int $id Item ID |
||
2094 | * |
||
2095 | * @return array |
||
2096 | * |
||
2097 | * @since 2.3 |
||
2098 | */ |
||
2099 | public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2110 | |||
2111 | /** |
||
2112 | * Data callback for Countries |
||
2113 | * |
||
2114 | * @param string $name The name of the field |
||
2115 | * @param string|array $value The value of the field |
||
2116 | * @param array $options Field options |
||
2117 | * @param array $pod Pod data |
||
2118 | * @param int $id Item ID |
||
2119 | * |
||
2120 | * @return array |
||
2121 | * |
||
2122 | * @since 2.3 |
||
2123 | */ |
||
2124 | public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2394 | |||
2395 | /** |
||
2396 | * Data callback for US States |
||
2397 | * |
||
2398 | * @param string $name The name of the field |
||
2399 | * @param string|array $value The value of the field |
||
2400 | * @param array $options Field options |
||
2401 | * @param array $pod Pod data |
||
2402 | * @param int $id Item ID |
||
2403 | * |
||
2404 | * @return array |
||
2405 | * |
||
2406 | * @since 2.3 |
||
2407 | */ |
||
2408 | public function data_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2465 | |||
2466 | /** |
||
2467 | * Data callback for US States |
||
2468 | * |
||
2469 | * @param string $name The name of the field |
||
2470 | * @param string|array $value The value of the field |
||
2471 | * @param array $options Field options |
||
2472 | * @param array $pod Pod data |
||
2473 | * @param int $id Item ID |
||
2474 | * |
||
2475 | * @return array |
||
2476 | * |
||
2477 | * @since 2.3 |
||
2478 | */ |
||
2479 | public function data_days_of_week ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2489 | |||
2490 | /** |
||
2491 | * Data callback for US States |
||
2492 | * |
||
2493 | * @param string $name The name of the field |
||
2494 | * @param string|array $value The value of the field |
||
2495 | * @param array $options Field options |
||
2496 | * @param array $pod Pod data |
||
2497 | * @param int $id Item ID |
||
2498 | * |
||
2499 | * @return array |
||
2500 | * |
||
2501 | * @since 2.3 |
||
2502 | */ |
||
2503 | public function data_months_of_year ( $name = null, $value = null, $options = null, $pod = null, $id = null ) { |
||
2513 | } |
||
2514 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.