Completed
Pull Request — 2.x (#3632)
by Phil
05:40
created

PodsField_Pick::build_model_data()   C

Complexity

Conditions 9
Paths 11

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 44
nc 11
nop 3
dl 0
loc 66
rs 6.4099
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
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 () {
101
102
    }
103
104
	/**
105
	 * @param $data
106
	 * @param $selected_values
107
	 * @param $options
108
	 *
109
	 * @return array
110
	 */
111
	public static function build_model_data( $data, $selected_values, $options ) {
112
		$model_data = array();
113
114
		foreach ( $data as $this_id => $this_title ) {
115
			$icon = '';
116
			$edit_link = '';
117
			$link = '';
118
119
			switch ( $options[ 'pick_object' ] ) {
120
				case 'post_type':
121
					if ( null === $options[ 'supports_thumbnails' ] ) {
122
						$options[ 'supports_thumbnails' ] = post_type_supports( $options['pick_val'], 'thumbnail' );
123
					}
124
125
					if ( true === $options[ 'supports_thumbnails' ] ) {
126
						$thumb = wp_get_attachment_image_src( $this_id, 'thumbnail', true );
127
128
						if ( ! empty( $thumb[0] ) ) {
129
							$icon = $thumb[0];
130
						}
131
					}
132
133
					$edit_link = get_edit_post_link( $this_id, 'raw' );
134
					$link = get_permalink( $this_id );
135
					break;
136
137
				case 'taxonomy':
138
					$edit_link = get_edit_term_link( $this_id, $options['pick_val'] );
139
					$link = get_term_link( $this_id, $options['pick_val'] );
140
					break;
141
142
				case 'user':
143
					$icon = get_avatar_url( $this_id, array( 'size' => 150 ) );
144
					$edit_link = get_edit_user_link( $this_id );
145
					$link = get_author_posts_url( $this_id );
146
					break;
147
148
				case 'pod':
149
					$file_name = 'admin.php';
150
					$query_args = array(
151
						'page'   => 'pods-manage-' . $options[ 'pick_val' ],
152
						'action' => 'edit',
153
						'id'     => $this_id,
154
					);
155
156
					$edit_link = add_query_arg( $query_args, admin_url( $file_name ) );
157
					// @todo Add $link support
158
					break;
159
160
				// Something unsupported
161
				default:
162
					break;
163
			}
164
165
			$model_data[] = array(
166
				'id'        => $this_id,
167
				'icon'      => $icon,
168
				'name'      => $this_title,
169
				'edit_link' => $edit_link,
170
				'link'      => $link,
171
				'selected'  => ( isset( $selected_values[ $this_id ] ) ),
172
			);
173
		}
174
175
		return $model_data;
176
	}
177
178
	/**
179
     * Add admin_init actions
180
     *
181
     * @since 2.3
182
     */
183
    public function admin_init () {
184
        //--!! Prototype testing only
185
        add_action( 'wp_ajax_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
186
        add_action( 'wp_ajax_nopriv_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
187
188
        // AJAX for Relationship lookups
189
        add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
190
        add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
191
    }
192
193
    /**
194
     * Add options and set defaults to
195
     *
196
     * @return array
197
     *
198
     * @since 2.0
199
     */
200
    public function options () {
201
        $options = array(
202
            self::$type . '_format_type' => array(
203
                'label' => __( 'Selection Type', 'pods' ),
204
                'help' => __( 'help', 'pods' ),
205
                'default' => 'single',
206
                'type' => 'pick',
207
                'data' => array(
208
                    'single' => __( 'Single Select', 'pods' ),
209
                    'multi' => __( 'Multiple Select', 'pods' )
210
                ),
211
                'dependency' => true
212
            ),
213
            self::$type . '_format_single' => array(
214
                'label' => __( 'Format', 'pods' ),
215
                'help' => __( 'help', 'pods' ),
216
                'depends-on' => array( self::$type . '_format_type' => 'single' ),
217
                'default' => 'dropdown',
218
                'type' => 'pick',
219
                'data' => apply_filters(
220
                    'pods_form_ui_field_pick_format_single_options',
221
                    array(
222
                        'dropdown' => __( 'Drop Down', 'pods' ),
223
                        'radio' => __( 'Radio Buttons', 'pods' ),
224
                        'autocomplete' => __( 'Autocomplete', 'pods' ),
225
                        'flexible' => __( 'Flexible', 'pods' ),
226
                    )
227
                ),
228
                'dependency' => true
229
            ),
230
            self::$type . '_format_multi' => array(
231
                'label' => __( 'Format', 'pods' ),
232
                'help' => __( 'help', 'pods' ),
233
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
234
                'default' => 'checkbox',
235
                'type' => 'pick',
236
                'data' => apply_filters(
237
                    'pods_form_ui_field_pick_format_multi_options',
238
                    array(
239
                        'checkbox' => __( 'Checkboxes', 'pods' ),
240
                        'multiselect' => __( 'Multi Select', 'pods' ),
241
                        'autocomplete' => __( 'Autocomplete', 'pods' ),
242
                        'flexible' => __( 'Flexible', 'pods' ),
243
                    )
244
                ),
245
                'dependency' => true
246
            ),
247
            self::$type . '_taggable' => array(
248
                'label' => __( 'Taggable', 'pods' ),
249
                'help' => __( 'Allow new values to be inserted when using an Autocomplete field', 'pods' ),
250
                'excludes-on' => array(
251
					self::$type . '_format_single' => array( 'dropdown', 'radio' ),
252
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect' ),
253
                    self::$type . '_object' => array_merge(
254
                        array( 'site', 'network' ),
255
                        self::simple_objects()
256
                    )
257
				),
258
                'type' => 'boolean',
259
                'default' => 0
260
            ),
261
            self::$type . '_show_icon' => array(
262
                'label' => __( 'Show Icons', 'pods' ),
263
                'excludes-on' => array(
264
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
265
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
266
                    self::$type . '_object' => array_merge(
267
                        array( 'site', 'network' ),
268
                        self::simple_objects()
269
                    )
270
				),
271
                'type' => 'boolean',
272
                'default' => 1
273
            ),
274
            self::$type . '_show_edit_link' => array(
275
                'label' => __( 'Show Edit Links', 'pods' ),
276
                'excludes-on' => array(
277
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
278
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
279
                    self::$type . '_object' => array_merge(
280
                        array( 'site', 'network' ),
281
                        self::simple_objects()
282
                    )
283
				),
284
                'type' => 'boolean',
285
                'default' => 1
286
            ),
287
            self::$type . '_show_view_link' => array(
288
                'label' => __( 'Show View Links', 'pods' ),
289
                'excludes-on' => array(
290
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
291
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
292
                    self::$type . '_object' => array_merge(
293
                        array( 'site', 'network' ),
294
                        self::simple_objects()
295
                    )
296
				),
297
                'type' => 'boolean',
298
                'default' => 1
299
            ),
300
			self::$type . '_select_text' => array(
301
                'label' => __( 'Default Select Text', 'pods' ),
302
                'help' => __( 'This is the text use for the default "no selection" dropdown item, if empty, it will default to "-- Select One --"', 'pods' ),
303
                'depends-on' => array(
304
					self::$type . '_format_type' => 'single',
305
					self::$type . '_format_single' => 'dropdown'
306
				),
307
                'default' => '',
308
                'type' => 'text'
309
			),
310
            self::$type . '_limit' => array(
311
                'label' => __( 'Selection Limit', 'pods' ),
312
                'help' => __( 'help', 'pods' ),
313
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
314
                'default' => 0,
315
                'type' => 'number'
316
            ),
317
            self::$type . '_table_id' => array(
318
                'label' => __( 'Table ID Column', 'pods' ),
319
                'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ),
320
                'depends-on' => array( self::$type . '_object' => 'table' ),
321
                'required' => 1,
322
                'default' => '',
323
                'type' => 'text'
324
            ),
325
            self::$type . '_table_index' => array(
326
                'label' => __( 'Table Index Column', 'pods' ),
327
                'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ),
328
                'depends-on' => array( self::$type . '_object' => 'table' ),
329
                'required' => 1,
330
                'default' => '',
331
                'type' => 'text'
332
            ),
333
            self::$type . '_display' => array(
334
                'label' => __( 'Display Field in Selection List', 'pods' ),
335
                'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ),
336
                'excludes-on' => array(
337
                    self::$type . '_object' => array_merge(
338
                        array( 'site', 'network' ),
339
                        self::simple_objects()
340
                    )
341
                ),
342
                'default' => '',
343
                'type' => 'text'
344
            ),
345
            self::$type . '_user_role' => array(
346
                'label' => __( 'Limit list to Role(s)', 'pods' ),
347
                'help' => __( 'help', 'pods' ),
348
                'depends-on' => array( self::$type . '_object' => 'user' ),
349
                'default' => '',
350
                'type' => 'pick',
351
                'pick_object' => 'role',
352
                'pick_format_type' => 'multi'
353
            ),/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
354
            self::$type . '_user_site' => array(
355
                'label' => __( 'Limit list to Site(s)', 'pods' ),
356
                'help' => __( 'help', 'pods' ),
357
                'depends-on' => array( self::$type . '_object' => 'user' ),
358
                'default' => '',
359
                'type' => 'pick',
360
                'pick_object' => 'site',
361
                'pick_format_type' => 'multi'
362
            ),*/
363
            self::$type . '_where' => array(
364
                'label' => __( 'Customized <em>WHERE</em>', 'pods' ),
365
                'help' => __( 'help', 'pods' ),
366
                'excludes-on' => array(
367
                    self::$type . '_object' => array_merge(
368
                        array( 'site', 'network' ),
369
                        self::simple_objects()
370
                    )
371
                ),
372
                'default' => '',
373
                'type' => 'text'
374
            ),
375
            self::$type . '_orderby' => array(
376
                'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ),
377
                'help' => __( 'help', 'pods' ),
378
                'excludes-on' => array(
379
                    self::$type . '_object' => array_merge(
380
                        array( 'site', 'network' ),
381
                        self::simple_objects()
382
                    )
383
                ),
384
                'default' => '',
385
                'type' => 'text'
386
            ),
387
            self::$type . '_groupby' => array(
388
                'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ),
389
                'help' => __( 'help', 'pods' ),
390
                'excludes-on' => array(
391
                    self::$type . '_object' => array_merge(
392
                        array( 'site', 'network' ),
393
                        self::simple_objects()
394
                    )
395
                ),
396
                'default' => '',
397
                'type' => 'text'
398
            )
399
            /*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
400
            self::$type . '_size' => array(
401
                'label' => __( 'Field Size', 'pods' ),
402
                'default' => 'medium',
403
                'type' => 'pick',
404
                'data' => array(
405
                    'small' => __( 'Small', 'pods' ),
406
                    'medium' => __( 'Medium', 'pods' ),
407
                    'large' => __( 'Large', 'pods' )
408
                )
409
            )*/
410
        );
411
412
        $post_type_pick_objects = array();
413
        foreach ( get_post_types( '', 'names' ) as $post_type ) {
414
            $post_type_pick_objects[] = 'post-type_' .$post_type;
415
        }
416
        $options[ self::$type . '_post_status' ] = array(
417
            'name' => 'post_status',
418
            'label' => __( 'Post Status', 'pods' ),
419
            'help' => __( 'help', 'pods' ),
420
            'type' => 'pick',
421
            'pick_object' => 'post-status',
422
            'pick_format_type' => 'multi',
423
            'default' => 'publish',
424
            'depends-on' => array(
425
                self::$type . '_object' => $post_type_pick_objects
426
            )
427
        );
428
429
        /*if ( !is_multisite() )
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
430
            unset( $options[ self::$type . '_user_site' ] );*/
431
432
        return $options;
433
    }
434
435
    /**
436
     * Register a related object
437
     *
438
     * @param string $name Object name
439
     * @param string $label Object label
440
     * @param array $options Object options
441
     *
442
     * @return array|boolean Object array or false if unsuccessful
443
     * @since 2.3
444
     */
445
    public function register_related_object ( $name, $label, $options = null ) {
446
        if ( empty( $name ) || empty( $label ) )
447
            return false;
448
449
        $related_object = array(
450
            'label' => $label,
451
            'group' => 'Custom Relationships',
452
            'simple' => true,
453
            'bidirectional' => false,
454
            'data' => array(),
455
            'data_callback' => null
456
        );
457
458
        $related_object = array_merge( $related_object, $options );
459
460
        self::$custom_related_objects[ $name ] = $related_object;
461
462
        return true;
463
    }
464
465
	/**
466
	 * Setup related objects
467
	 *
468
	 * @param boolean $force Whether to force refresh of related objects
469
	 * @return bool True when data has been loaded
470
	 * @since 2.3
471
	 */
472
	public function setup_related_objects( $force = false ) {
473
474
		$new_data_loaded = false;
475
476
		if ( ! $force && empty( self::$related_objects ) ) {
477
			// Only load transient if we aren't forcing a refresh
478
			self::$related_objects = pods_transient_get( 'pods_related_objects' );
479
480
			if ( false !== self::$related_objects ) {
481
				$new_data_loaded = true;
482
			}
483
		} elseif ( $force ) {
484
			// If we are rebuilding, make sure we start with a clean slate
485
			self::$related_objects = array();
486
		}
487
488
		if ( empty( self::$related_objects ) ) {
489
			// Do a complete build of related_objects
490
			$new_data_loaded = true;
491
492
			// Custom
493
			self::$related_objects['custom-simple'] = array(
494
				'label'  => __( 'Simple (custom defined list)', 'pods' ),
495
				'group'  => __( 'Custom', 'pods' ),
496
				'simple' => true
497
			);
498
499
			// Pods
500
			$pod_options = array();
501
502
			// Include PodsMeta if not already included
503
			pods_meta();
504
505
			// Advanced Content Types
506
			$_pods = PodsMeta::$advanced_content_types;
507
508 View Code Duplication
			foreach ( $_pods as $pod ) {
509
				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')';
510
			}
511
512
			// Settings
513
			$_pods = PodsMeta::$settings;
514
515 View Code Duplication
			foreach ( $_pods as $pod ) {
516
				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')';
517
			}
518
519
			asort( $pod_options );
520
521
			foreach ( $pod_options as $pod => $label ) {
522
				self::$related_objects[ 'pod-' . $pod ] = array(
523
					'label'         => $label,
524
					'group'         => __( 'Pods', 'pods' ),
525
					'bidirectional' => true
526
				);
527
			}
528
529
			// Post Types
530
			$post_types = get_post_types();
531
			asort( $post_types );
532
533
			$ignore = array( 'attachment', 'revision', 'nav_menu_item' );
534
535
			foreach ( $post_types as $post_type => $label ) {
536
				if ( in_array( $post_type, $ignore ) || empty( $post_type ) ) {
537
					unset( $post_types[ $post_type ] );
538
539
					continue;
540
				} elseif ( 0 === strpos( $post_type, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) {
541
					unset( $post_types[ $post_type ] );
542
543
					continue;
544
				}
545
546
				$post_type = get_post_type_object( $post_type );
547
548
				self::$related_objects[ 'post_type-' . $post_type->name ] = array(
549
					'label'         => $post_type->label . ' (' . $post_type->name . ')',
550
					'group'         => __( 'Post Types', 'pods' ),
551
					'bidirectional' => true
552
				);
553
			}
554
555
			// Taxonomies
556
			$taxonomies = get_taxonomies();
557
			asort( $taxonomies );
558
559
			$ignore = array( 'nav_menu', 'post_format' );
560
561
			foreach ( $taxonomies as $taxonomy => $label ) {
562
				/**
563
				 * Prevent ability to extend core Pods content types.
564
				 *
565
				 * @param bool Default is true, when set to false Pods internal content types can not be extended.
566
				 *
567
				 * @since 2.3.19
568
				 */
569
				$ignore_internal = apply_filters( 'pods_pick_ignore_internal', true );
570
571
				if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) ) {
572
					unset( $taxonomies[ $taxonomy ] );
573
574
					continue;
575
				} elseif ( 0 === strpos( $taxonomy, '_pods_' ) && $ignore_internal ) {
576
					unset( $taxonomies[ $taxonomy ] );
577
578
					continue;
579
				}
580
581
				$taxonomy = get_taxonomy( $taxonomy );
582
583
				self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array(
584
					'label'         => $taxonomy->label . ' (' . $taxonomy->name . ')',
585
					'group'         => __( 'Taxonomies', 'pods' ),
586
					'bidirectional' => true
587
				);
588
			}
589
590
			// Other WP Objects
591
			self::$related_objects['user'] = array(
592
				'label'         => __( 'Users', 'pods' ),
593
				'group'         => __( 'Other WP Objects', 'pods' ),
594
				'bidirectional' => true
595
			);
596
597
			self::$related_objects['role'] = array(
598
				'label'         => __( 'User Roles', 'pods' ),
599
				'group'         => __( 'Other WP Objects', 'pods' ),
600
				'simple'        => true,
601
				'data_callback' => array( $this, 'data_roles' )
602
			);
603
604
			self::$related_objects['capability'] = array(
605
				'label'         => __( 'User Capabilities', 'pods' ),
606
				'group'         => __( 'Other WP Objects', 'pods' ),
607
				'simple'        => true,
608
				'data_callback' => array( $this, 'data_capabilities' )
609
			);
610
611
			self::$related_objects['media'] = array(
612
				'label'         => __( 'Media', 'pods' ),
613
				'group'         => __( 'Other WP Objects', 'pods' ),
614
				'bidirectional' => true
615
			);
616
617
			self::$related_objects['comment'] = array(
618
				'label'         => __( 'Comments', 'pods' ),
619
				'group'         => __( 'Other WP Objects', 'pods' ),
620
				'bidirectional' => true
621
			);
622
623
			self::$related_objects['image-size'] = array(
624
				'label'         => __( 'Image Sizes', 'pods' ),
625
				'group'         => __( 'Other WP Objects', 'pods' ),
626
				'simple'        => true,
627
				'data_callback' => array( $this, 'data_image_sizes' )
628
			);
629
630
			self::$related_objects['nav_menu'] = array(
631
				'label' => __( 'Navigation Menus', 'pods' ),
632
				'group' => __( 'Other WP Objects', 'pods' )
633
			);
634
635
			self::$related_objects['post_format'] = array(
636
				'label' => __( 'Post Formats', 'pods' ),
637
				'group' => __( 'Other WP Objects', 'pods' )
638
			);
639
640
			self::$related_objects['post-status'] = array(
641
				'label'         => __( 'Post Status', 'pods' ),
642
				'group'         => __( 'Other WP Objects', 'pods' ),
643
				'simple'        => true,
644
				'data_callback' => array( $this, 'data_post_stati' )
645
			);
646
647
			do_action( 'pods_form_ui_field_pick_related_objects_other' );
648
649
			self::$related_objects['country'] = array(
650
				'label'         => __( 'Countries', 'pods' ),
651
				'group'         => __( 'Predefined Lists', 'pods' ),
652
				'simple'        => true,
653
				'data_callback' => array( $this, 'data_countries' )
654
			);
655
656
			self::$related_objects['us_state'] = array(
657
				'label'         => __( 'US States', 'pods' ),
658
				'group'         => __( 'Predefined Lists', 'pods' ),
659
				'simple'        => true,
660
				'data_callback' => array( $this, 'data_us_states' )
661
			);
662
663
			self::$related_objects['days_of_week'] = array(
664
				'label'         => __( 'Calendar - Days of Week', 'pods' ),
665
				'group'         => __( 'Predefined Lists', 'pods' ),
666
				'simple'        => true,
667
				'data_callback' => array( $this, 'data_days_of_week' )
668
			);
669
670
			self::$related_objects['months_of_year'] = array(
671
				'label'         => __( 'Calendar - Months of Year', 'pods' ),
672
				'group'         => __( 'Predefined Lists', 'pods' ),
673
				'simple'        => true,
674
				'data_callback' => array( $this, 'data_months_of_year' )
675
			);
676
677
			do_action( 'pods_form_ui_field_pick_related_objects_predefined' );
678
679
			if ( did_action( 'init' ) ) {
680
				pods_transient_set( 'pods_related_objects', self::$related_objects );
681
			}
682
		}
683
684
		/**
685
		 * Allow custom related objects to be defined
686
		 */
687
		do_action( 'pods_form_ui_field_pick_related_objects_custom' );
688
689
		foreach ( self::$custom_related_objects as $object => $related_object ) {
690
			if ( ! isset( self::$related_objects[ $object ] ) ) {
691
				$new_data_loaded = true;
692
693
				self::$related_objects[ $object ] = $related_object;
694
			}
695
		}
696
697
		return $new_data_loaded;
698
699
	}
700
701
    /**
702
     * Return available related objects
703
     *
704
     * @param boolean $force Whether to force refresh of related objects
705
     *
706
     * @return array Field selection array
707
     * @since 2.3
708
     */
709
    public function related_objects ( $force = false ) {
710
        if ( $this->setup_related_objects( $force ) || null === self::$names_related ) {
711
	        $related_objects = array();
712
713
	        foreach ( self::$related_objects as $related_object_name => $related_object ) {
714
		        if ( ! isset( $related_objects[ $related_object[ 'group' ] ] ) ) {
715
			        $related_objects[ $related_object[ 'group' ] ] = array();
716
		        }
717
718
		        $related_objects[ $related_object[ 'group' ] ][ $related_object_name ] = $related_object[ 'label' ];
719
	        }
720
721
	        self::$names_related = (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects );
722
        }
723
724
	    return self::$names_related;
725
    }
726
727
    /**
728
     * Return available simple object names
729
     *
730
     * @return array Simple object names
731
     * @since 2.3
732
     */
733 View Code Duplication
    public function simple_objects () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
734
		if ( $this->setup_related_objects() || null === self::$names_simple ) {
735
			$simple_objects = array();
736
737
			foreach ( self::$related_objects as $object => $related_object ) {
738
				if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] )
739
					continue;
740
741
				$simple_objects[] = $object;
742
			}
743
744
			self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects );
745
		}
746
747
	    return self::$names_simple;
748
    }
749
750
    /**
751
     * Return available bidirectional object names
752
     *
753
     * @return array Bidirectional object names
754
     * @since 2.3.4
755
     */
756 View Code Duplication
    public function bidirectional_objects () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
757
        if ( $this->setup_related_objects() || null === self::$names_bidirectional ) {
758
	        $bidirectional_objects = array();
759
760
	        foreach ( self::$related_objects as $object => $related_object ) {
761
		        if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] )
762
			        continue;
763
764
		        $bidirectional_objects[] = $object;
765
	        }
766
767
	        self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects );
768
        }
769
770
	    return self::$names_bidirectional;
771
    }
772
773
    /**
774
     * Define the current field's schema for DB table storage
775
     *
776
     * @param array $options
777
     *
778
     * @return array
779
     * @since 2.0
780
     */
781
    public function schema ( $options = null ) {
782
        $schema = false;
783
784
        $simple_tableless_objects = $this->simple_objects();
785
786
        if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) )
787
            $schema = 'LONGTEXT';
788
789
        return $schema;
790
    }
791
792
    /**
793
     * Change the way the value of the field is displayed with Pods::get
794
     *
795
     * @param mixed $value
796
     * @param string $name
797
     * @param array $options
798
     * @param array $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
799
     * @param array $pod
800
     * @param int $id
801
     *
802
     * @since 2.0
803
     */
804
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
805
        $fields = null;
806
807
        if ( is_object( $pod ) && isset( $pod->fields ) ) {
808
	        $fields = $pod->fields;
809
810
	        if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) {
811
		        $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] );
812
	        }
813 View Code Duplication
        } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) {
814
	        $fields = $pod[ 'fields' ];
815
816
	        if ( ! empty( $pod[ 'object_fields' ] ) ) {
817
		        $fields = array_merge( $fields, $pod[ 'object_fields' ] );
818
	        }
819
        }
820
821
        return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) );
822
    }
823
824
    /**
825
     * Customize output of the form field
826
     *
827
     * @param string $name
828
     * @param mixed $value
829
     * @param array $options
830
     * @param array $pod
831
     * @param int $id
832
     *
833
     * @since 2.0
834
     */
835
    public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
836
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
837
838
        $options = (array) $options;
839
        $form_field_type = PodsForm::$field_type;
0 ignored issues
show
Bug introduced by
The property field_type cannot be accessed from this context as it is declared private in class PodsForm.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
840
841
        $options[ 'grouped' ] = 1;
842
843
        $options[ 'table_info' ] = array();
844
845
        $custom = pods_var_raw( self::$type . '_custom', $options, false );
846
847
        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
848
849
        $ajax = false;
850
851
        if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) )
852
            $ajax = true;
853
854
        if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) {
855
            $ajax = (boolean) self::$field_data[ 'autocomplete' ];
856
        }
857
858
        $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id );
859
860
        if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) )
861
            $ajax = false;
862
863
	    $format_type = pods_v( self::$type . '_format_type', $options, 'single' );
864
865
        if ( 'single' == $format_type ) {
866 View Code Duplication
            if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) {
867
	            $options[ 'view_name' ] = 'select';
868
            } elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) {
869
	            $options[ 'view_name' ] = 'radio';
870
            } elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) {
871
	            $options[ 'view_name' ] = 'select2';
872
            } elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) ) {
873
	            $options[ 'view_name' ] = 'flexible';
874
            } else {
875
                // Support custom integration
876
                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 );
877
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
878
                return;
879
            }
880
        }
881
        elseif ( 'multi' == $format_type ) {
882
            if ( !empty( $value ) && !is_array( $value ) ) {
883
	            $value = explode( ',', $value );
884
            }
885
886 View Code Duplication
            if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) {
887
	            $options[ 'view_name' ] = 'checkbox';
888
            } elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) {
889
	            $options[ 'view_name' ] = 'select';
890
            } elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) {
891
	            $options[ 'view_name' ] = 'select2';
892
            } elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) ) {
893
	            $options[ 'view_name' ] = 'flexible';
894
            } else {
895
                // Support custom integration
896
                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 );
897
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
898
                return;
899
            }
900
        }
901
        else {
902
            // Support custom integration
903
            do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
904
            return;
905
        }
906
907
	    $field_type = 'pick';
908
	    pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) );
909
910
    }
911
912
    /**
913
     * Validate a value before it's saved
914
     *
915
     * @param mixed $value
916
     * @param string $name
917
     * @param array $options
918
     * @param array $fields
919
     * @param array $pod
920
     * @param int $id
921
     *
922
     * @param null $params
923
     * @return array|bool
924
     * @since 2.0
925
     */
926
    public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
927
        if ( empty( self::$api ) )
928
            self::$api = pods_api();
929
930
        $simple_tableless_objects = $this->simple_objects();
931
932
        $related_pick_limit = 0;
933
        $related_field = $related_pod = $current_related_ids = false;
934
935
        // Bidirectional relationship requirement checks
936
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
937
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
938
        if ( empty( $related_val ) ) {
939
            $related_val = $related_object;
940
        }
941
942
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
943
944
        $options[ 'id' ] = (int) $options[ 'id' ];
945
946
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) || empty( self::$related_data[ $options[ 'id' ] ] ) )
947
            self::$related_data[ $options[ 'id' ] ] = array();
948
949
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
950
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
951
952
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
953
                $related_field = false;
954
955
                // Ensure sister_id exists on related Pod
956 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
957
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
958
                        $related_field = $related_pod_field;
959
960
                        break;
961
                    }
962
                }
963
964
                if ( !empty( $related_field ) ) {
965
                    $current_ids = self::$api->lookup_related_items( $fields[ $name ][ 'id' ], $pod[ 'id' ], $id, $fields[ $name ], $pod );
966
967
                    self::$related_data[ $options[ 'id' ] ][ 'current_ids' ] = $current_ids;
968
969
                    $value_ids = $value;
970
971
                    // Convert values from a comma-separated string into an array
972
                    if ( !is_array( $value_ids ) )
973
                        $value_ids = explode( ',', $value_ids );
974
975
                    $value_ids = array_unique( array_filter( $value_ids ) );
976
977
                    // Get ids to remove
978
                    $remove_ids = array_diff( $current_ids, $value_ids );
979
980
                    $related_required = (boolean) pods_var( 'required', $related_field[ 'options' ], 0 );
981
                    $related_pick_limit = (int) pods_var( self::$type . '_limit', $related_field[ 'options' ], 0 );
982
983
                    if ( 'single' == pods_var_raw( self::$type . '_format_type', $related_field[ 'options' ] ) )
984
                        $related_pick_limit = 1;
985
986
                    // Validate Required
987
                    if ( $related_required && !empty( $remove_ids ) ) {
988
                        foreach ( $remove_ids as $related_id ) {
989
                            $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
990
991
                            self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] = $bidirectional_ids;
992
993
                            if ( empty( $bidirectional_ids ) || ( in_array( $id, $bidirectional_ids ) && 1 == count( $bidirectional_ids ) ) )
994
                                return sprintf( __( 'The %s field is required and cannot be removed by the %s field', 'pods' ), $related_field[ 'label' ], $options[ 'label' ] );
995
                        }
996
                    }
997
                }
998
                else
999
                    $related_pod = false;
1000
            }
1001
            else
1002
                $related_pod = false;
1003
        }
1004
1005
        if ( empty( self::$related_data[ $options[ 'id' ] ] ) )
1006
            unset( self::$related_data[ $options[ 'id' ] ] );
1007
        else {
1008
            self::$related_data[ $options[ 'id' ] ][ 'related_pod' ] = $related_pod;
1009
            self::$related_data[ $options[ 'id' ] ][ 'related_field' ] = $related_field;
1010
            self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ] = $related_pick_limit;
1011
1012
            $pick_limit = (int) pods_var( self::$type . '_limit', $options[ 'options' ], 0 );
1013
1014
            if ( 'single' == pods_var_raw( self::$type . '_format_type', $options[ 'options' ] ) )
1015
                $pick_limit = 1;
1016
1017
            $related_field[ 'id' ] = (int) $related_field[ 'id' ];
1018
1019
            if ( !isset( self::$related_data[ $related_field[ 'id' ] ] ) || empty( self::$related_data[ $related_field[ 'id' ] ] ) ) {
1020
                self::$related_data[ $related_field[ 'id' ] ] = array(
1021
                    'related_pod' => $pod,
1022
                    'related_field' => $options,
1023
                    'related_pick_limit' => $pick_limit
1024
                );
1025
            }
1026
        }
1027
1028
        return true;
1029
    }
1030
1031
    /**
1032
     * Save the value to the DB
1033
     *
1034
     * @param mixed $value
1035
     * @param int $id
1036
     * @param string $name
1037
     * @param array $options
1038
     * @param array $fields
1039
     * @param array $pod
1040
     * @param object $params
1041
     *
1042
     * @since 2.3
1043
     */
1044
    public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
1045
        if ( empty( self::$api ) )
1046
            self::$api = pods_api();
1047
1048
        $options[ 'id' ] = (int) $options[ 'id' ];
1049
1050
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) )
1051
            return;
1052
1053
        $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ];
1054
        $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ];
1055
        $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ];
1056
1057
        // Bidirectional relationship updates
1058
        if ( !empty( $related_field ) ) {
1059
            // Don't use no conflict mode unless this isn't the current pod type
1060
            $no_conflict = true;
1061
1062
            if ( $related_pod[ 'type' ] != $pod[ 'type' ] )
1063
                $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1064
1065
            if ( !$no_conflict )
1066
                pods_no_conflict_on( $related_pod[ 'type' ] );
1067
1068
            $value = array_filter( $value );
1069
1070
            foreach ( $value as $related_id ) {
1071
                if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) )
1072
                    $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ];
1073
                else
1074
                    $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
1075
1076
                $bidirectional_ids = array_filter( $bidirectional_ids );
1077
1078
                if ( empty( $bidirectional_ids ) )
1079
                    $bidirectional_ids = array();
1080
1081
                $remove_ids = array();
1082
1083
                if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) {
1084
                    while ( $related_pick_limit <= count( $bidirectional_ids ) ) {
1085
                        $remove_ids[] = (int) array_pop( $bidirectional_ids );
1086
                    }
1087
                }
1088
1089
                // Remove this item from related items no longer related to
1090
                $remove_ids = array_unique( array_filter( $remove_ids ) );
1091
1092
                // Add to related items
1093
                if ( !in_array( $id, $bidirectional_ids ) )
1094
                    $bidirectional_ids[] = $id;
1095
                // Nothing to change
1096
                elseif ( empty( $remove_ids ) )
1097
                    continue;
1098
1099
                self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field );
1100
1101
                if ( !empty( $remove_ids ) )
1102
                    self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options );
1103
            }
1104
1105
            if ( !$no_conflict )
1106
                pods_no_conflict_off( $related_pod[ 'type' ] );
1107
        }
1108
    }
1109
1110
    /**
1111
     * Delete the value from the DB
1112
     *
1113
     * @param int $id
1114
     * @param string $name
1115
     * @param array $options
1116
     * @param array $pod
1117
     *
1118
     * @since 2.3
1119
     */
1120
    public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
1121
        if ( empty( self::$api ) )
1122
            self::$api = pods_api();
1123
1124
        $simple_tableless_objects = $this->simple_objects();
1125
1126
        // Bidirectional relationship requirement checks
1127
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
1128
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
1129
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
1130
1131
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
1132
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
1133
1134
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
1135
                $related_field = false;
1136
1137
                // Ensure sister_id exists on related Pod
1138 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
1139
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
1140
                        $related_field = $related_pod_field;
1141
1142
                        break;
1143
                    }
1144
                }
1145
1146
                if ( !empty( $related_field ) ) {
1147
                    $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod );
1148
1149
                    if ( !empty( $values ) ) {
1150
                        $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1151
1152
                        if ( !$no_conflict )
1153
                            pods_no_conflict_on( $related_pod[ 'type' ] );
1154
1155
                        self::$api->delete_relationships( $values, $id, $related_pod, $related_field );
1156
1157
                        if ( !$no_conflict )
1158
                            pods_no_conflict_off( $related_pod[ 'type' ] );
1159
                    }
1160
                }
1161
            }
1162
        }
1163
    }
1164
1165
    /**
1166
     * Customize the Pods UI manage table column output
1167
     *
1168
     * @param int $id
1169
     * @param mixed $value
1170
     * @param string $name
1171
     * @param array $options
1172
     * @param array $fields
1173
     * @param array $pod
1174
     *
1175
     * @since 2.0
1176
     */
1177
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
1178
        $value = $this->simple_value( $name, $value, $options, $pod, $id );
1179
1180
        return $this->display( $value, $name, $options, $pod, $id );
1181
    }
1182
1183
    /**
1184
     * Get the data from the field
1185
     *
1186
     * @param string $name The name of the field
1187
     * @param string|array $value The value of the field
1188
     * @param array $options Field options
1189
     * @param array $pod Pod data
1190
     * @param int $id Item ID
1191
     * @param boolean $in_form
1192
     *
1193
     * @return array Array of possible field data
1194
     *
1195
     * @since 2.0
1196
     */
1197
    public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
1198 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1199
            $options = array_merge( $options, $options[ 'options' ] );
1200
1201
            unset( $options[ 'options' ] );
1202
        }
1203
1204
        $data = pods_var_raw( 'data', $options, null, null, true );
1205
1206
        $object_params = array(
1207
            'name' => $name, // The name of the field
1208
            'value' => $value, // The value of the field
1209
            'options' => $options, // Field options
1210
            'pod' => $pod, // Pod data
1211
            'id' => $id, // Item ID
1212
            'context' => 'data', // Data context
1213
        );
1214
1215
        if ( null !== $data )
1216
            $data = (array) $data;
1217
        else
1218
            $data = $this->get_object_data( $object_params );
1219
1220
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1221
            $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data;
1222
1223
        $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
1224
1225
        return $data;
1226
    }
1227
1228
    /**
1229
     * Convert a simple value to the correct value
1230
     *
1231
     * @param string $name The name of the field
1232
     * @param string|array $value The value of the field
1233
     * @param array $options Field options
1234
     * @param array $pod Pod data
1235
     * @param int $id Item ID
1236
     * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false)
1237
     *
1238
     * @return mixed Corrected value
1239
     */
1240
    public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) {
1241
        if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) {
1242 View Code Duplication
            if ( isset( $options[ 'options' ] ) ) {
1243
                $options = array_merge( $options, $options[ 'options' ] );
1244
1245
                unset( $options[ 'options' ] );
1246
            }
1247
1248
            if ( !is_array( $value ) && 0 < strlen( $value ) ) {
1249
                $simple = @json_decode( $value, true );
1250
1251
                if ( is_array( $simple ) )
1252
                    $value = $simple;
1253
            }
1254
1255
            $data = pods_var_raw( 'data', $options, null, null, true );
1256
1257
            $object_params = array(
1258
                'name' => $name, // The name of the field
1259
                'value' => $value, // The value of the field
1260
                'options' => $options, // Field options
1261
                'pod' => $pod, // Pod data
1262
                'id' => $id, // Item ID
1263
                'context' => 'simple_value', // Data context
1264
            );
1265
1266
            if ( null === $data )
1267
                $data = $this->get_object_data( $object_params );
1268
1269
            $data = (array) $data;
1270
1271
            $key = 0;
1272
1273
            if ( is_array( $value ) ) {
1274
                if ( !empty( $data ) ) {
1275
                    $val = array();
1276
1277
                    foreach ( $value as $k => $v ) {
1278
                        if ( isset( $data[ $v ] ) ) {
1279
                            if ( false === $raw ) {
1280
                                $k = $v;
1281
                                $v = $data[ $v ];
1282
                            }
1283
1284
                            $val[ $k ] = $v;
1285
                        }
1286
                    }
1287
1288
                    $value = $val;
1289
                }
1290
            }
1291
            elseif ( isset( $data[ $value ] ) && false === $raw ) {
1292
                $key = $value;
1293
                $value = $data[ $value ];
1294
            }
1295
1296
            $single_multi = pods_var( self::$type . '_format_type', $options, 'single' );
1297
1298
            if ( 'multi' == $single_multi )
1299
                $limit = (int) pods_var( self::$type . '_limit', $options, 0 );
1300
            else
1301
                $limit = 1;
1302
1303
            if ( is_array( $value ) && 0 < $limit ) {
1304
                if ( 1 == $limit )
1305
                    $value = current( $value );
1306
                else
1307
                    $value = array_slice( $value, 0, $limit, true );
1308
            }
1309
            elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) {
1310
                if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) {
1311
                    $value = array(
1312
                        $key => $value
1313
                    );
1314
                }
1315
            }
1316
        }
1317
1318
        return $value;
1319
    }
1320
1321
    /**
1322
     * Get the label from a pick value
1323
     *
1324
     * @param string $name The name of the field
1325
     * @param string|array $value The value of the field
1326
     * @param array $options Field options
1327
     * @param array $pod Pod data
1328
     * @param int $id Item ID
1329
     *
1330
     * @return string
1331
     *
1332
     * @since 2.2
1333
     */
1334
    public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) {
1335 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1336
            $options = array_merge( $options, $options[ 'options' ] );
1337
1338
            unset( $options[ 'options' ] );
1339
        }
1340
1341
        $data = pods_var_raw( 'data', $options, null, null, true );
1342
1343
        $object_params = array(
1344
            'name' => $name, // The name of the field
1345
            'value' => $value, // The value of the field
1346
            'options' => $options, // Field options
1347
            'pod' => $pod, // Pod data
1348
            'id' => $id, // Item ID
1349
            'context' => 'value_to_label', // Data context
1350
        );
1351
1352
        if ( null !== $data )
1353
            $data = (array) $data;
1354
        else
1355
            $data = $this->get_object_data( $object_params );
1356
1357
        $labels = array();
1358
1359
        foreach ( $data as $v => $l ) {
0 ignored issues
show
Bug introduced by
The expression $data of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1360
            if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) )
1361
                $labels[] = $l;
1362
        }
1363
1364
        $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id );
1365
1366
        $labels = pods_serial_comma( $labels );
1367
1368
        return $labels;
1369
    }
1370
1371
	/**
1372
	 * Get available items from a relationship field
1373
	 *
1374
	 * @param array|string $field Field array or field name
1375
	 * @param array $options [optional] Field options array overrides
1376
	 * @param array $object_params [optional] Additional get_object_data options
1377
	 *
1378
	 * @return array An array of available items from a relationship field
1379
	 */
1380
	public function get_field_data( $field, $options = array(), $object_params = array() ) {
1381
1382
		// Handle field array overrides
1383
		if ( is_array( $field ) ) {
1384
			$options = array_merge( $field, $options );
1385
		}
1386
1387
		// Get field name from array
1388
		$field = pods_var_raw( 'name', $options, $field, null, true );
1389
1390
		// Field name or options not set
1391
		if ( empty( $field ) || empty( $options ) ) {
1392
			return array();
1393
		}
1394
1395
		// Options normalization
1396
		$options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) );
1397
1398
		// Setup object params
1399
        $object_params = array_merge(
1400
			array(
1401
				'name' => $field, // The name of the field
1402
				'options' => $options, // Field options
1403
			),
1404
			$object_params
1405
        );
1406
1407
		// Get data override
1408
        $data = pods_var_raw( 'data', $options, null, null, true );
1409
1410
		// Return data override
1411
        if ( null !== $data ) {
1412
            $data = (array) $data;
1413
		}
1414
		// Get object data
1415
        else {
1416
            $data = $this->get_object_data( $object_params );
1417
		}
1418
1419
		return $data;
1420
1421
	}
1422
1423
    /**
1424
     * Get data from relationship objects
1425
     *
1426
     * @param array $object_params Object data parameters
1427
     *
1428
     * @return array|bool Object data
1429
     */
1430
    public function get_object_data ( $object_params = null ) {
1431
1432
	    /**
1433
	     * @var $wpdb wpdb
1434
	     */
1435
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
1436
1437
        $object_params = array_merge(
1438
            array(
1439
                'name' => '', // The name of the field
1440
                'value' => '', // The value of the field
1441
                'options' => array(), // Field options
1442
                'pod' => '', // Pod data
1443
                'id' => '', // Item ID
1444
                'context' => '', // Data context
1445
                'data_params' => array(
1446
                    'query' => '' // Query being searched
1447
                ),
1448
                'page' => 1, // Page number of results to get
1449
				'limit' => 0 // How many data items to limit to (autocomplete defaults to 30, set to -1 or 1+ to override)
1450
            ),
1451
            $object_params
1452
        );
1453
1454
        $name = $object_params[ 'name' ];
1455
        $value = $object_params[ 'value' ];
1456
        $options = $object_params[ 'options' ] = (array) $object_params[ 'options' ];
1457
        $pod = $object_params[ 'pod' ];
1458
        $id = $object_params[ 'id' ];
1459
        $context = $object_params[ 'context' ];
1460
        $data_params = $object_params[ 'data_params' ] = (array) $object_params[ 'data_params' ];
1461
        $page = min( 1, (int) $object_params[ 'page' ] );
1462
        $limit = (int) $object_params[ 'limit' ];
1463
1464 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1465
            $options = array_merge( $options, $options[ 'options' ] );
1466
1467
            unset( $options[ 'options' ] );
1468
        }
1469
1470
        $data = apply_filters( 'pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params );
1471
        $items = array();
1472
1473
        if ( !isset( $options[ self::$type . '_object' ] ) )
1474
            $data = pods_var_raw( 'data', $options, array(), null, true );
1475
1476
		$simple = false;
1477
1478
        if ( null === $data ) {
1479
            $data = array();
1480
1481
            if ( 'custom-simple' == $options[ self::$type . '_object' ] ) {
1482
                $custom = pods_var_raw( self::$type . '_custom', $options, '' );
1483
1484
                $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params );
1485
1486
                if ( !empty( $custom ) ) {
1487
                    if ( !is_array( $custom ) ) {
1488
                        $data = array();
1489
1490
                        $custom = explode( "\n", trim( $custom ) );
1491
1492
                        foreach ( $custom as $custom_value ) {
1493
                            $custom_label = explode( '|', $custom_value );
1494
1495
                            if ( empty( $custom_label ) )
1496
                                continue;
1497
1498
                            if ( 1 == count( $custom_label ) )
1499
                                $custom_label = $custom_value;
1500
                            else {
1501
                                $custom_value = $custom_label[ 0 ];
1502
                                $custom_label = $custom_label[ 1 ];
1503
                            }
1504
1505
							$custom_value = trim( (string) $custom_value );
1506
							$custom_label = trim( (string) $custom_label );
1507
1508
                            $data[ $custom_value ] = $custom_label;
1509
                        }
1510
                    }
1511
                    else
1512
                        $data = $custom;
1513
1514
					$simple = true;
1515
                }
1516
            }
1517
            elseif ( isset( self::$related_objects[ $options[ self::$type . '_object' ] ] ) && isset( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] ) && !empty( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] ) ) {
1518
                $data = self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ];
1519
1520
				$simple = true;
1521
			}
1522
            elseif ( isset( self::$related_objects[ $options[ self::$type . '_object' ] ] ) && isset( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ] ) && is_callable( self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ] ) ) {
1523
                $data = call_user_func_array(
1524
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ],
1525
                    array( $name, $value, $options, $pod, $id )
1526
                );
1527
1528
				$simple = true;
1529
1530
                // Cache data from callback
1531
                if ( !empty( $data ) )
1532
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] = $data;
1533
            }
1534
            elseif ( 'simple_value' != $context ) {
1535
                $pick_val = pods_var( self::$type . '_val', $options );
1536
1537
                if ( 'table' == pods_var( self::$type . '_object', $options ) )
1538
                    $pick_val = pods_var( self::$type . '_table', $options, $pick_val, null, true );
1539
1540 View Code Duplication
                if ( '__current__' == $pick_val ) {
1541
                    if ( is_object( $pod ) )
1542
                        $pick_val = $pod->pod;
1543
                    elseif ( is_array( $pod ) )
1544
                        $pick_val = $pod[ 'name' ];
1545
                    elseif ( 0 < strlen( $pod ) )
1546
                        $pick_val = $pod;
1547
                }
1548
1549
                $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( self::$type . '_object', $options ), $pick_val, null, null, $options );
1550
1551
                $search_data = pods_data();
1552
                $search_data->table( $options[ 'table_info' ] );
1553
1554
                if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
1555
                    $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
1556
                    $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
1557
                }
1558
1559
                $params = array(
1560
                    'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
1561
                    'table' => $search_data->table,
1562
                    'where' => pods_var_raw( self::$type . '_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
1563
                    'orderby' => pods_var_raw( self::$type . '_orderby', $options, null, null, true ),
1564
                    'groupby' => pods_var_raw( self::$type . '_groupby', $options, null, null, true ),
1565
                    //'having' => pods_var_raw( self::$type . '_having', $options, null, null, true ),
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
1566
					'pagination' => false,
1567
					'search' => false
1568
                );
1569
1570
                if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1571
                    $params[ 'select' ] .= ', `t`.`path`';
1572
1573
                if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
1574
                    $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
1575
1576
                if ( empty( $params[ 'where' ] ) || ( !is_array( $params[ 'where' ] ) && strlen( trim( $params[ 'where' ] ) ) < 1 ) )
1577
                    $params[ 'where' ] = array();
1578
                elseif ( !is_array( $params[ 'where' ] ) )
1579
                    $params[ 'where' ] = (array) $params[ 'where' ];
1580
1581
                if ( 'value_to_label' == $context )
1582
                    $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' );
1583
1584
                /* not needed yet
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
1585
                if ( !empty( $params[ 'orderby' ] ) )
1586
                    $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
1587
1588
                if ( !empty( $params[ 'groupby' ] ) )
1589
                    $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
1590
1591
                $display = trim( pods_var( self::$type . '_display', $options ), ' {@}' );
1592
1593
                if ( 0 < strlen( $display ) ) {
1594
                    if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
1595
                        if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
1596
                            $search_data->field_index = $display;
1597
1598
                            $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1599
                        }
1600
                        elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
1601
                            $search_data->field_index = $display;
1602
1603
                            if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
1604
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
1605
                            elseif ( 'meta' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] )
1606
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
1607
                            else
1608
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1609
                        }
1610
                    }
1611
                    elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
1612
                        $search_data->field_index = $display;
1613
1614
                        $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1615
                    }
1616
                }
1617
1618
                $autocomplete = false;
1619
1620
                if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1621
                    $autocomplete = true;
1622 View Code Duplication
                elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
1623
                    $autocomplete = true;
1624
1625
                $hierarchy = false;
1626
1627
                if ( 'data' == $context && !$autocomplete ) {
1628
                    if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_single', $options, 'dropdown' ), array( 'dropdown', 'radio' ) ) )
1629
                        $hierarchy = true;
1630 View Code Duplication
                    elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_multi', $options, 'checkbox' ), array( 'multiselect', 'checkbox' ) ) )
1631
                        $hierarchy = true;
1632
                }
1633
1634
                if ( $hierarchy && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) )
1635
                    $params[ 'select' ] .= ', ' . $options[ 'table_info' ][ 'field_parent_select' ];
1636
1637
                if ( $autocomplete ) {
1638
					if ( 0 == $limit ) {
1639
						$limit = 30;
1640
					}
1641
1642
                    $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params );
1643
1644
					if ( is_array( $value ) && $params[ 'limit' ] < count( $value ) ) {
1645
						$params[ 'limit' ] = count( $value );
1646
					}
1647
1648
                    $params[ 'page' ] = $page;
1649
1650
                    if ( 'admin_ajax_relationship' == $context ) {
1651
                        $lookup_where = array(
1652
                            $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"
1653
                        );
1654
1655
                        // @todo Hook into WPML for each table
1656
                        if ( $wpdb->users == $search_data->table ) {
1657
                            $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1658
                            $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1659
                            $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1660
                        }
1661
                        elseif ( $wpdb->posts == $search_data->table ) {
1662
                            $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1663
                            $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1664
                            $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1665
                            $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1666
                        }
1667
                        elseif ( $wpdb->terms == $search_data->table ) {
1668
                            $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1669
                            $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1670
                        }
1671
                        elseif ( $wpdb->comments == $search_data->table ) {
1672
                            $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1673
                            $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1674
                            $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1675
                        }
1676
1677
                        $lookup_where = apply_filters( 'pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params[ 'query' ], $name, $value, $options, $pod, $id, $object_params, $search_data );
1678
1679
                        if ( !empty( $lookup_where ) )
1680
                            $params[ 'where' ][] = implode( ' OR ', $lookup_where );
1681
1682
                        $orderby = array();
1683
                        $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%' ) DESC";
1684
1685
                        $pick_orderby = pods_var_raw( self::$type . '_orderby', $options, null, null, true );
1686
1687
                        if ( 0 < strlen( $pick_orderby ) )
1688
                            $orderby[] = $pick_orderby;
1689
1690
                        $orderby[] = "`t`.`{$search_data->field_index}`";
1691
                        $orderby[] = "`t`.`{$search_data->field_id}`";
1692
1693
                        $params[ 'orderby' ] = $orderby;
1694
                    }
1695
                }
1696
				elseif ( 0 < $limit ) {
1697
                    $params[ 'limit' ] = $limit;
1698
                    $params[ 'page' ] = $page;
1699
				}
1700
1701
                $extra = '';
1702
1703
                if ( $wpdb->posts == $search_data->table )
1704
                    $extra = ', `t`.`post_type`';
1705
                elseif ( $wpdb->terms == $search_data->table )
1706
                    $extra = ', `tt`.`taxonomy`';
1707
                elseif ( $wpdb->comments == $search_data->table )
1708
                    $extra = ', `t`.`comment_type`';
1709
1710
                $params[ 'select' ] .= $extra;
1711
1712
                if ( 'user' == pods_var( self::$type . '_object', $options ) ) {
1713
                    $roles = pods_var( self::$type . '_user_role', $options );
1714
1715
                    if ( !empty( $roles ) ) {
1716
                        $where = array();
1717
1718
                        foreach ( (array) $roles as $role ) {
1719
                            if ( empty( $role ) || ( pods_clean_name( $role ) != $role && sanitize_title( $role ) != $role ) )
1720
                                continue;
1721
1722
                            $where[] = $wpdb->base_prefix . ( ( is_multisite() && !is_main_site() ) ? get_current_blog_id() . '_' : '' ) . 'capabilities.meta_value LIKE "%\"' . pods_sanitize_like( $role ) . '\"%"';
1723
                        }
1724
1725
                        if ( !empty( $where ) ) {
1726
                            $params[ 'where' ][] = implode( ' OR ', $where );
1727
                        }
1728
                    }
1729
                }
1730
1731
                $results = $search_data->select( $params );
1732
1733
                if ( $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
1734
                    if ( !empty( $value ) ) {
1735
                        $ids = $value;
1736
1737
						if ( is_array( $ids ) && isset( $ids[ 0 ] ) && is_array( $ids[ 0 ] ) ) {
1738
							$ids = wp_list_pluck( $ids, $search_data->field_id );
1739
						}
1740
1741
                        if ( is_array( $ids ) )
1742
                            $ids = implode( ', ', $ids );
1743
1744
                        if ( is_array( $params[ 'where' ] ) )
1745
                            $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
1746
                        if ( !empty( $params[ 'where' ] ) )
1747
                            $params[ 'where' ] .= ' AND ';
1748
1749
                        $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
1750
1751
                        $results = $search_data->select( $params );
1752
                    }
1753
                }
1754
                else
1755
                    $autocomplete = false;
1756
1757
                if ( 'data' == $context ) {
1758
                    self::$field_data = array(
1759
                        'field' => $name,
1760
                        'id' => $options[ 'id' ],
1761
                        'autocomplete' => $autocomplete
1762
                    );
1763
                }
1764
1765
                if ( $hierarchy && !$autocomplete && !empty( $results ) && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) {
1766
                    $args = array(
1767
                        'id' => $options[ 'table_info' ][ 'field_id' ],
1768
                        'index' => $options[ 'table_info' ][ 'field_index' ],
1769
                        'parent' => $options[ 'table_info' ][ 'field_parent' ],
1770
                    );
1771
1772
                    $results = pods_hierarchical_select( $results, $args );
1773
                }
1774
1775
                $ids = array();
1776
1777
                if ( !empty( $results ) ) {
1778
                    $display_filter = pods_var( 'display_filter', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) );
1779
1780
                    foreach ( $results as $result ) {
1781
                        $result = get_object_vars( $result );
1782
1783
                        if ( !isset( $result[ $search_data->field_id ] ) || !isset( $result[ $search_data->field_index ] ) )
1784
                            continue;
1785
1786
                        $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
1787
1788
                        $object = $object_type = '';
1789
1790
                        if ( $wpdb->posts == $search_data->table && isset( $result[ 'post_type' ] ) ) {
1791
                            $object = $result[ 'post_type' ];
1792
                            $object_type = 'post_type';
1793
                        }
1794
                        elseif ( $wpdb->terms == $search_data->table && isset( $result[ 'taxonomy' ] ) ) {
1795
                            $object = $result[ 'taxonomy' ];
1796
                            $object_type = 'taxonomy';
1797
                        }
1798
1799
                        if ( 0 < strlen( $display_filter ) ) {
1800
                            $display_filter_args = pods_var( 'display_filter_args', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) );
1801
1802
                            $args = array(
1803
                                $display_filter,
1804
                                $result[ $search_data->field_index ]
1805
                            );
1806
1807
                            if ( !empty( $display_filter_args ) ) {
1808
                                foreach ( (array) $display_filter_args as $display_filter_arg ) {
1809
                                    if ( isset( $result[ $display_filter_arg ] ) )
1810
                                        $args[] = $result[ $display_filter_arg ];
1811
                                }
1812
                            }
1813
1814
                            $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args );
1815
                        }
1816
1817
                        if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1818
                            $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
1819
                        elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
1820
                            $result[ $search_data->field_index ] = '(No Title)';
1821
1822
                        if ( 'admin_ajax_relationship' == $context ) {
1823
                            $items[] = array(
1824
                                'id' => $result[ $search_data->field_id ],
1825
                                'text' => $result[ $search_data->field_index ],
1826
                                'image' => ''
1827
                            );
1828
                        }
1829
                        else
1830
                            $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
1831
1832
                        $ids[] = $result[ $search_data->field_id ];
1833
                    }
1834
                }
1835
            }
1836
1837
			if ( $simple && 'admin_ajax_relationship' == $context ) {
1838
				$found_data = array();
1839
1840
				foreach ( $data as $k => $v ) {
1841
					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) {
1842
						$found_data[ $k ] = $v;
1843
					}
1844
				}
1845
1846
				$data = $found_data;
1847
			}
1848
        }
1849
1850
        if ( 'admin_ajax_relationship' == $context ) {
1851
            if ( empty( $items ) && !empty( $data ) ) {
1852
                foreach ( $data as $k => $v ) {
1853
                    $items[] = array(
1854
                        'id' => $k,
1855
                        'text' => $v,
1856
                        'image' => ''
1857
                    );
1858
                }
1859
            }
1860
1861
            return $items;
1862
        }
1863
1864
        return $data;
1865
    }
1866
1867
    /**
1868
     * AJAX call to refresh relationship field markup (supports adding new records modally)
1869
     *
1870
     * @since 2.7
1871
     */
1872
    public function admin_ajax_relationship_popup () {
1873
1874
        $data = pods_unslash( (array) $_POST );
1875
1876
        // Get the field information
1877
        $params = array(
1878
            'pod_id' => $data[ 'pod_id' ],
1879
            'id'     => $data[ 'field_id' ]
1880
        );
1881
        $field = pods_api()->load_field( $params );
1882
1883
        // Get Pods object for this item
1884
        $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] );
1885
1886
        // Get the relationship field's value(s)
1887
        $field_name = $field[ 'name' ];
1888
        $params = array(
1889
            'name'    => $field_name,
1890
            'in_form' => true
1891
        );
1892
        $value = $pod->field( $params);
1893
1894
        // Build the markup and return it to the caller
1895
        $meta_field_name = 'pods_meta_' . $field_name;
1896
        $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] );
1897
        echo $output;
1898
1899
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_ajax_relationship_popup() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1900
    }
1901
1902
    /**
1903
     * Handle autocomplete AJAX
1904
     *
1905
     * @since 2.3
1906
     */
1907
    public function admin_ajax_relationship () {
1908
		pods_session_start();
1909
1910
        // Sanitize input
1911
        $params = pods_unslash( (array) $_POST );
1912
1913 View Code Duplication
        foreach ( $params as $key => $value ) {
1914
            if ( 'action' == $key )
1915
                continue;
1916
1917
            unset( $params[ $key ] );
1918
1919
            $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
1920
        }
1921
1922
        $params = (object) $params;
1923
1924
        $uid = @session_id();
1925
1926
        if ( is_user_logged_in() )
1927
            $uid = 'user_' . get_current_user_id();
1928
1929
        $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
1930
1931 View Code Duplication
        if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
1932
            pods_error( __( 'Unauthorized request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1933
1934
        $api = pods_api();
1935
1936
        $pod = $api->load_pod( array( 'id' => (int) $params->pod ) );
1937
        $field = $api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
1938
        $id = (int) $params->id;
1939
1940
        $limit = 15;
1941
1942
        if ( isset( $params->limit ) )
1943
            $limit = (int) $params->limit;
1944
1945
        $page = 1;
1946
1947
        if ( isset( $params->page ) )
1948
            $page = (int) $params->page;
1949
1950
        if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
1951
            pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1952 View Code Duplication
        elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
1953
            pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1954
        elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
1955
            pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1956 View Code Duplication
        elseif ( 'single' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_single', $field ) )
1957
            pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1958 View Code Duplication
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $field ) )
1959
            pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1960
1961
        $object_params = array(
1962
            'name' => $field[ 'name' ], // The name of the field
1963
            'value' => null, // The value of the field
1964
            'options' => array_merge( $field, $field[ 'options' ] ), // Field options
1965
            'pod' => $pod, // Pod data
1966
            'id' => $id, // Item ID
1967
            'context' => 'admin_ajax_relationship', // Data context
1968
            'data_params' => $params,
1969
            'page' => $page,
1970
            'limit' => $limit
1971
        );
1972
1973
        $pick_data = apply_filters( 'pods_field_pick_data_ajax', null, $field[ 'name' ], null, $field, $pod, $id );
1974
1975
        if ( null !== $pick_data )
1976
            $items = $pick_data;
1977
        else
1978
            $items = $this->get_object_data( $object_params );
1979
1980
        if ( !empty( $items ) && isset( $items[ 0 ] ) && !is_array( $items[ 0 ] ) ) {
1981
            $new_items = array();
1982
1983
            foreach ( $items as $id => $text ) {
1984
                $new_items[] = array(
1985
                    'id' => $id,
1986
                    'text' => $text,
1987
                    'image' => ''
1988
                );
1989
            }
1990
1991
            $items = $new_items;
1992
        }
1993
1994
        $items = apply_filters( 'pods_field_pick_data_ajax_items', $items, $field[ 'name' ], null, $field, $pod, $id );
1995
1996
        $items = array(
1997
            'results' => $items
1998
        );
1999
2000
        wp_send_json( $items );
2001
2002
        die(); // KBAI!
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_ajax_relationship() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
2003
    }
2004
2005
    /**
2006
     * Data callback for Post Stati
2007
     *
2008
     * @param string $name The name of the field
2009
     * @param string|array $value The value of the field
2010
     * @param array $options Field options
2011
     * @param array $pod Pod data
2012
     * @param int $id Item ID
2013
     *
2014
     * @return array
2015
     *
2016
     * @since 2.3
2017
     */
2018
    public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2019
        $data = array();
2020
2021
        $post_stati = get_post_stati( array(), 'objects' );
2022
2023
        foreach ( $post_stati as $post_status ) {
2024
            $data[ $post_status->name ] = $post_status->label;
2025
        }
2026
2027
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2028
    }
2029
2030
    /**
2031
     * Data callback for User Roles
2032
     *
2033
     * @param string $name The name of the field
2034
     * @param string|array $value The value of the field
2035
     * @param array $options Field options
2036
     * @param array $pod Pod data
2037
     * @param int $id Item ID
2038
     *
2039
     * @return array
2040
     *
2041
     * @since 2.3
2042
     */
2043
    public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2044
        $data = array();
2045
2046
        global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2047
2048
        foreach ( $wp_roles->role_objects as $key => $role ) {
2049
            $data[ $key ] = $wp_roles->role_names[ $key ];
2050
        }
2051
2052
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2053
    }
2054
2055
    /**
2056
     * Data callback for User Capabilities
2057
     *
2058
     * @param string $name The name of the field
2059
     * @param string|array $value The value of the field
2060
     * @param array $options Field options
2061
     * @param array $pod Pod data
2062
     * @param int $id Item ID
2063
     *
2064
     * @return array
2065
     *
2066
     * @since 2.3
2067
     */
2068
    public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2069
        $data = array();
2070
2071
        global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2072
2073
        $default_caps = array(
2074
            'activate_plugins',
2075
            'add_users',
2076
            'create_users',
2077
            'delete_others_pages',
2078
            'delete_others_posts',
2079
            'delete_pages',
2080
            'delete_plugins',
2081
            'delete_posts',
2082
            'delete_private_pages',
2083
            'delete_private_posts',
2084
            'delete_published_pages',
2085
            'delete_published_posts',
2086
            'delete_users',
2087
            'edit_dashboard',
2088
            'edit_files',
2089
            'edit_others_pages',
2090
            'edit_others_posts',
2091
            'edit_pages',
2092
            'edit_plugins',
2093
            'edit_posts',
2094
            'edit_private_pages',
2095
            'edit_private_posts',
2096
            'edit_published_pages',
2097
            'edit_published_posts',
2098
            'edit_theme_options',
2099
            'edit_themes',
2100
            'edit_users',
2101
            'import',
2102
            'install_plugins',
2103
            'install_themes',
2104
            'list_users',
2105
            'manage_categories',
2106
            'manage_links',
2107
            'manage_options',
2108
            'moderate_comments',
2109
            'promote_users',
2110
            'publish_pages',
2111
            'publish_posts',
2112
            'read',
2113
            'read_private_pages',
2114
            'read_private_posts',
2115
            'remove_users',
2116
            'switch_themes',
2117
            'unfiltered_html',
2118
            'unfiltered_upload',
2119
            'update_core',
2120
            'update_plugins',
2121
            'update_themes',
2122
            'upload_files'
2123
        );
2124
2125
        $role_caps = array();
2126
2127 View Code Duplication
        foreach ( $wp_roles->role_objects as $key => $role ) {
2128
            if ( is_array( $role->capabilities ) ) {
2129
                foreach ( $role->capabilities as $cap => $grant ) {
2130
                    $role_caps[ $cap ] = $cap;
2131
                }
2132
            }
2133
        }
2134
2135
        $role_caps = array_unique( $role_caps );
2136
2137
        $capabilities = array_merge( $default_caps, $role_caps );
2138
2139
        // To support Members filters
2140
        $capabilities = apply_filters( 'members_get_capabilities', $capabilities );
2141
2142
        $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities );
2143
2144
        sort( $capabilities );
2145
2146
        $capabilities = array_unique( $capabilities );
2147
2148
        global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2149
2150
        foreach ( $capabilities as $capability ) {
2151
            $data[ $capability ] = $capability;
2152
        }
2153
2154
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2155
    }
2156
2157
    /**
2158
     * Data callback for Image Sizes
2159
     *
2160
     * @param string $name The name of the field
2161
     * @param string|array $value The value of the field
2162
     * @param array $options Field options
2163
     * @param array $pod Pod data
2164
     * @param int $id Item ID
2165
     *
2166
     * @return array
2167
     *
2168
     * @since 2.3
2169
     */
2170
    public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2171
        $data = array();
2172
2173
        $image_sizes = get_intermediate_image_sizes();
2174
2175
        foreach ( $image_sizes as $image_size ) {
2176
            $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
2177
        }
2178
2179
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2180
    }
2181
2182
    /**
2183
     * Data callback for Countries
2184
     *
2185
     * @param string $name The name of the field
2186
     * @param string|array $value The value of the field
2187
     * @param array $options Field options
2188
     * @param array $pod Pod data
2189
     * @param int $id Item ID
2190
     *
2191
     * @return array
2192
     *
2193
     * @since 2.3
2194
     */
2195
    public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2196
        $data = array(
2197
            'AF' => __( 'Afghanistan' ),
2198
            'AL' => __( 'Albania' ),
2199
            'DZ' => __( 'Algeria' ),
2200
            'AS' => __( 'American Samoa' ),
2201
            'AD' => __( 'Andorra' ),
2202
            'AO' => __( 'Angola' ),
2203
            'AI' => __( 'Anguilla' ),
2204
            'AQ' => __( 'Antarctica' ),
2205
            'AG' => __( 'Antigua and Barbuda' ),
2206
            'AR' => __( 'Argentina' ),
2207
            'AM' => __( 'Armenia' ),
2208
            'AW' => __( 'Aruba' ),
2209
            'AU' => __( 'Australia' ),
2210
            'AT' => __( 'Austria' ),
2211
            'AZ' => __( 'Azerbaijan' ),
2212
            'BS' => __( 'Bahamas' ),
2213
            'BH' => __( 'Bahrain' ),
2214
            'BD' => __( 'Bangladesh' ),
2215
            'BB' => __( 'Barbados' ),
2216
            'BY' => __( 'Belarus' ),
2217
            'BE' => __( 'Belgium' ),
2218
            'BZ' => __( 'Belize' ),
2219
            'BJ' => __( 'Benin' ),
2220
            'BM' => __( 'Bermuda' ),
2221
            'BT' => __( 'Bhutan' ),
2222
            'BO' => __( 'Bolivia' ),
2223
            'BA' => __( 'Bosnia and Herzegovina' ),
2224
            'BW' => __( 'Botswana' ),
2225
            'BV' => __( 'Bouvet Island' ),
2226
            'BR' => __( 'Brazil' ),
2227
            'BQ' => __( 'British Antarctic Territory' ),
2228
            'IO' => __( 'British Indian Ocean Territory' ),
2229
            'VG' => __( 'British Virgin Islands' ),
2230
            'BN' => __( 'Brunei' ),
2231
            'BG' => __( 'Bulgaria' ),
2232
            'BF' => __( 'Burkina Faso' ),
2233
            'BI' => __( 'Burundi' ),
2234
            'KH' => __( 'Cambodia' ),
2235
            'CM' => __( 'Cameroon' ),
2236
            'CA' => __( 'Canada' ),
2237
            'CT' => __( 'Canton and Enderbury Islands' ),
2238
            'CV' => __( 'Cape Verde' ),
2239
            'KY' => __( 'Cayman Islands' ),
2240
            'CF' => __( 'Central African Republic' ),
2241
            'TD' => __( 'Chad' ),
2242
            'CL' => __( 'Chile' ),
2243
            'CN' => __( 'China' ),
2244
            'CX' => __( 'Christmas Island' ),
2245
            'CC' => __( 'Cocos [Keeling] Islands' ),
2246
            'CO' => __( 'Colombia' ),
2247
            'KM' => __( 'Comoros' ),
2248
            'CG' => __( 'Congo - Brazzaville' ),
2249
            'CD' => __( 'Congo - Kinshasa' ),
2250
            'CK' => __( 'Cook Islands' ),
2251
            'CR' => __( 'Costa Rica' ),
2252
            'HR' => __( 'Croatia' ),
2253
            'CU' => __( 'Cuba' ),
2254
            'CY' => __( 'Cyprus' ),
2255
            'CZ' => __( 'Czech Republic' ),
2256
            'CI' => __( 'Côte d’Ivoire' ),
2257
            'DK' => __( 'Denmark' ),
2258
            'DJ' => __( 'Djibouti' ),
2259
            'DM' => __( 'Dominica' ),
2260
            'DO' => __( 'Dominican Republic' ),
2261
            'NQ' => __( 'Dronning Maud Land' ),
2262
            'DD' => __( 'East Germany' ),
2263
            'EC' => __( 'Ecuador' ),
2264
            'EG' => __( 'Egypt' ),
2265
            'SV' => __( 'El Salvador' ),
2266
            'GQ' => __( 'Equatorial Guinea' ),
2267
            'ER' => __( 'Eritrea' ),
2268
            'EE' => __( 'Estonia' ),
2269
            'ET' => __( 'Ethiopia' ),
2270
            'FK' => __( 'Falkland Islands' ),
2271
            'FO' => __( 'Faroe Islands' ),
2272
            'FJ' => __( 'Fiji' ),
2273
            'FI' => __( 'Finland' ),
2274
            'FR' => __( 'France' ),
2275
            'GF' => __( 'French Guiana' ),
2276
            'PF' => __( 'French Polynesia' ),
2277
            'TF' => __( 'French Southern Territories' ),
2278
            'FQ' => __( 'French Southern and Antarctic Territories' ),
2279
            'GA' => __( 'Gabon' ),
2280
            'GM' => __( 'Gambia' ),
2281
            'GE' => __( 'Georgia' ),
2282
            'DE' => __( 'Germany' ),
2283
            'GH' => __( 'Ghana' ),
2284
            'GI' => __( 'Gibraltar' ),
2285
            'GR' => __( 'Greece' ),
2286
            'GL' => __( 'Greenland' ),
2287
            'GD' => __( 'Grenada' ),
2288
            'GP' => __( 'Guadeloupe' ),
2289
            'GU' => __( 'Guam' ),
2290
            'GT' => __( 'Guatemala' ),
2291
            'GG' => __( 'Guernsey' ),
2292
            'GN' => __( 'Guinea' ),
2293
            'GW' => __( 'Guinea-Bissau' ),
2294
            'GY' => __( 'Guyana' ),
2295
            'HT' => __( 'Haiti' ),
2296
            'HM' => __( 'Heard Island and McDonald Islands' ),
2297
            'HN' => __( 'Honduras' ),
2298
            'HK' => __( 'Hong Kong SAR China' ),
2299
            'HU' => __( 'Hungary' ),
2300
            'IS' => __( 'Iceland' ),
2301
            'IN' => __( 'India' ),
2302
            'ID' => __( 'Indonesia' ),
2303
            'IR' => __( 'Iran' ),
2304
            'IQ' => __( 'Iraq' ),
2305
            'IE' => __( 'Ireland' ),
2306
            'IM' => __( 'Isle of Man' ),
2307
            'IL' => __( 'Israel' ),
2308
            'IT' => __( 'Italy' ),
2309
            'JM' => __( 'Jamaica' ),
2310
            'JP' => __( 'Japan' ),
2311
            'JE' => __( 'Jersey' ),
2312
            'JT' => __( 'Johnston Island' ),
2313
            'JO' => __( 'Jordan' ),
2314
            'KZ' => __( 'Kazakhstan' ),
2315
            'KE' => __( 'Kenya' ),
2316
            'KI' => __( 'Kiribati' ),
2317
            'KW' => __( 'Kuwait' ),
2318
            'KG' => __( 'Kyrgyzstan' ),
2319
            'LA' => __( 'Laos' ),
2320
            'LV' => __( 'Latvia' ),
2321
            'LB' => __( 'Lebanon' ),
2322
            'LS' => __( 'Lesotho' ),
2323
            'LR' => __( 'Liberia' ),
2324
            'LY' => __( 'Libya' ),
2325
            'LI' => __( 'Liechtenstein' ),
2326
            'LT' => __( 'Lithuania' ),
2327
            'LU' => __( 'Luxembourg' ),
2328
            'MO' => __( 'Macau SAR China' ),
2329
            'MK' => __( 'Macedonia' ),
2330
            'MG' => __( 'Madagascar' ),
2331
            'MW' => __( 'Malawi' ),
2332
            'MY' => __( 'Malaysia' ),
2333
            'MV' => __( 'Maldives' ),
2334
            'ML' => __( 'Mali' ),
2335
            'MT' => __( 'Malta' ),
2336
            'MH' => __( 'Marshall Islands' ),
2337
            'MQ' => __( 'Martinique' ),
2338
            'MR' => __( 'Mauritania' ),
2339
            'MU' => __( 'Mauritius' ),
2340
            'YT' => __( 'Mayotte' ),
2341
            'FX' => __( 'Metropolitan France' ),
2342
            'MX' => __( 'Mexico' ),
2343
            'FM' => __( 'Micronesia' ),
2344
            'MI' => __( 'Midway Islands' ),
2345
            'MD' => __( 'Moldova' ),
2346
            'MC' => __( 'Monaco' ),
2347
            'MN' => __( 'Mongolia' ),
2348
            'ME' => __( 'Montenegro' ),
2349
            'MS' => __( 'Montserrat' ),
2350
            'MA' => __( 'Morocco' ),
2351
            'MZ' => __( 'Mozambique' ),
2352
            'MM' => __( 'Myanmar [Burma]' ),
2353
            'NA' => __( 'Namibia' ),
2354
            'NR' => __( 'Nauru' ),
2355
            'NP' => __( 'Nepal' ),
2356
            'NL' => __( 'Netherlands' ),
2357
            'AN' => __( 'Netherlands Antilles' ),
2358
            'NT' => __( 'Neutral Zone' ),
2359
            'NC' => __( 'New Caledonia' ),
2360
            'NZ' => __( 'New Zealand' ),
2361
            'NI' => __( 'Nicaragua' ),
2362
            'NE' => __( 'Niger' ),
2363
            'NG' => __( 'Nigeria' ),
2364
            'NU' => __( 'Niue' ),
2365
            'NF' => __( 'Norfolk Island' ),
2366
            'KP' => __( 'North Korea' ),
2367
            'VD' => __( 'North Vietnam' ),
2368
            'MP' => __( 'Northern Mariana Islands' ),
2369
            'NO' => __( 'Norway' ),
2370
            'OM' => __( 'Oman' ),
2371
            'PC' => __( 'Pacific Islands Trust Territory' ),
2372
            'PK' => __( 'Pakistan' ),
2373
            'PW' => __( 'Palau' ),
2374
            'PS' => __( 'Palestinian Territories' ),
2375
            'PA' => __( 'Panama' ),
2376
            'PZ' => __( 'Panama Canal Zone' ),
2377
            'PG' => __( 'Papua New Guinea' ),
2378
            'PY' => __( 'Paraguay' ),
2379
            'YD' => __( "People's Democratic Republic of Yemen" ),
2380
            'PE' => __( 'Peru' ),
2381
            'PH' => __( 'Philippines' ),
2382
            'PN' => __( 'Pitcairn Islands' ),
2383
            'PL' => __( 'Poland' ),
2384
            'PT' => __( 'Portugal' ),
2385
            'PR' => __( 'Puerto Rico' ),
2386
            'QA' => __( 'Qatar' ),
2387
            'RO' => __( 'Romania' ),
2388
            'RU' => __( 'Russia' ),
2389
            'RW' => __( 'Rwanda' ),
2390
            'RE' => __( 'Réunion' ),
2391
            'BL' => __( 'Saint Barthélemy' ),
2392
            'SH' => __( 'Saint Helena' ),
2393
            'KN' => __( 'Saint Kitts and Nevis' ),
2394
            'LC' => __( 'Saint Lucia' ),
2395
            'MF' => __( 'Saint Martin' ),
2396
            'PM' => __( 'Saint Pierre and Miquelon' ),
2397
            'VC' => __( 'Saint Vincent and the Grenadines' ),
2398
            'WS' => __( 'Samoa' ),
2399
            'SM' => __( 'San Marino' ),
2400
            'SA' => __( 'Saudi Arabia' ),
2401
            'SN' => __( 'Senegal' ),
2402
            'RS' => __( 'Serbia' ),
2403
            'CS' => __( 'Serbia and Montenegro' ),
2404
            'SC' => __( 'Seychelles' ),
2405
            'SL' => __( 'Sierra Leone' ),
2406
            'SG' => __( 'Singapore' ),
2407
            'SK' => __( 'Slovakia' ),
2408
            'SI' => __( 'Slovenia' ),
2409
            'SB' => __( 'Solomon Islands' ),
2410
            'SO' => __( 'Somalia' ),
2411
            'ZA' => __( 'South Africa' ),
2412
            'GS' => __( 'South Georgia and the South Sandwich Islands' ),
2413
            'KR' => __( 'South Korea' ),
2414
            'ES' => __( 'Spain' ),
2415
            'LK' => __( 'Sri Lanka' ),
2416
            'SD' => __( 'Sudan' ),
2417
            'SR' => __( 'Suriname' ),
2418
            'SJ' => __( 'Svalbard and Jan Mayen' ),
2419
            'SZ' => __( 'Swaziland' ),
2420
            'SE' => __( 'Sweden' ),
2421
            'CH' => __( 'Switzerland' ),
2422
            'SY' => __( 'Syria' ),
2423
            'ST' => __( 'São Tomé and Príncipe' ),
2424
            'TW' => __( 'Taiwan' ),
2425
            'TJ' => __( 'Tajikistan' ),
2426
            'TZ' => __( 'Tanzania' ),
2427
            'TH' => __( 'Thailand' ),
2428
            'TL' => __( 'Timor-Leste' ),
2429
            'TG' => __( 'Togo' ),
2430
            'TK' => __( 'Tokelau' ),
2431
            'TO' => __( 'Tonga' ),
2432
            'TT' => __( 'Trinidad and Tobago' ),
2433
            'TN' => __( 'Tunisia' ),
2434
            'TR' => __( 'Turkey' ),
2435
            'TM' => __( 'Turkmenistan' ),
2436
            'TC' => __( 'Turks and Caicos Islands' ),
2437
            'TV' => __( 'Tuvalu' ),
2438
            'UM' => __( 'U.S. Minor Outlying Islands' ),
2439
            'PU' => __( 'U.S. Miscellaneous Pacific Islands' ),
2440
            'VI' => __( 'U.S. Virgin Islands' ),
2441
            'UG' => __( 'Uganda' ),
2442
            'UA' => __( 'Ukraine' ),
2443
            'SU' => __( 'Union of Soviet Socialist Republics' ),
2444
            'AE' => __( 'United Arab Emirates' ),
2445
            'GB' => __( 'United Kingdom' ),
2446
            'US' => __( 'United States' ),
2447
            'ZZ' => __( 'Unknown or Invalid Region' ),
2448
            'UY' => __( 'Uruguay' ),
2449
            'UZ' => __( 'Uzbekistan' ),
2450
            'VU' => __( 'Vanuatu' ),
2451
            'VA' => __( 'Vatican City' ),
2452
            'VE' => __( 'Venezuela' ),
2453
            'VN' => __( 'Vietnam' ),
2454
            'WK' => __( 'Wake Island' ),
2455
            'WF' => __( 'Wallis and Futuna' ),
2456
            'EH' => __( 'Western Sahara' ),
2457
            'YE' => __( 'Yemen' ),
2458
            'ZM' => __( 'Zambia' ),
2459
            'ZW' => __( 'Zimbabwe' ),
2460
            'AX' => __( 'Åland Islands' )
2461
        );
2462
2463
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2464
    }
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_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2480
        $data = array(
2481
            'AL' => __( 'Alabama' ),
2482
            'AK' => __( 'Alaska' ),
2483
            'AZ' => __( 'Arizona' ),
2484
            'AR' => __( 'Arkansas' ),
2485
            'CA' => __( 'California' ),
2486
            'CO' => __( 'Colorado' ),
2487
            'CT' => __( 'Connecticut' ),
2488
            'DE' => __( 'Delaware' ),
2489
            'DC' => __( 'District Of Columbia' ),
2490
            'FL' => __( 'Florida' ),
2491
            'GA' => __( 'Georgia' ),
2492
            'HI' => __( 'Hawaii' ),
2493
            'ID' => __( 'Idaho' ),
2494
            'IL' => __( 'Illinois' ),
2495
            'IN' => __( 'Indiana' ),
2496
            'IA' => __( 'Iowa' ),
2497
            'KS' => __( 'Kansas' ),
2498
            'KY' => __( 'Kentucky' ),
2499
            'LA' => __( 'Louisiana' ),
2500
            'ME' => __( 'Maine' ),
2501
            'MD' => __( 'Maryland' ),
2502
            'MA' => __( 'Massachusetts' ),
2503
            'MI' => __( 'Michigan' ),
2504
            'MN' => __( 'Minnesota' ),
2505
            'MS' => __( 'Mississippi' ),
2506
            'MO' => __( 'Missouri' ),
2507
            'MT' => __( 'Montana' ),
2508
            'NE' => __( 'Nebraska' ),
2509
            'NV' => __( 'Nevada' ),
2510
            'NH' => __( 'New Hampshire' ),
2511
            'NJ' => __( 'New Jersey' ),
2512
            'NM' => __( 'New Mexico' ),
2513
            'NY' => __( 'New York' ),
2514
            'NC' => __( 'North Carolina' ),
2515
            'ND' => __( 'North Dakota' ),
2516
            'OH' => __( 'Ohio' ),
2517
            'OK' => __( 'Oklahoma' ),
2518
            'OR' => __( 'Oregon' ),
2519
            'PA' => __( 'Pennsylvania' ),
2520
            'RI' => __( 'Rhode Island' ),
2521
            'SC' => __( 'South Carolina' ),
2522
            'SD' => __( 'South Dakota' ),
2523
            'TN' => __( 'Tennessee' ),
2524
            'TX' => __( 'Texas' ),
2525
            'UT' => __( 'Utah' ),
2526
            'VT' => __( 'Vermont' ),
2527
            'VA' => __( 'Virginia' ),
2528
            'WA' => __( 'Washington' ),
2529
            'WV' => __( 'West Virginia' ),
2530
            'WI' => __( 'Wisconsin' ),
2531
            'WY' => __( 'Wyoming' )
2532
        );
2533
2534
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2535
    }
2536
2537
    /**
2538
     * Data callback for US States
2539
     *
2540
     * @param string $name The name of the field
2541
     * @param string|array $value The value of the field
2542
     * @param array $options Field options
2543
     * @param array $pod Pod data
2544
     * @param int $id Item ID
2545
     *
2546
     * @return array
2547
     *
2548
     * @since 2.3
2549
     */
2550
    public function data_days_of_week ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2551
2552
		/**
2553
		 * @var WP_Locale
2554
		 */
2555
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2556
2557
		return $wp_locale->weekday;
2558
2559
    }
2560
2561
    /**
2562
     * Data callback for US States
2563
     *
2564
     * @param string $name The name of the field
2565
     * @param string|array $value The value of the field
2566
     * @param array $options Field options
2567
     * @param array $pod Pod data
2568
     * @param int $id Item ID
2569
     *
2570
     * @return array
2571
     *
2572
     * @since 2.3
2573
     */
2574
    public function data_months_of_year ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
2575
2576
		/**
2577
		 * @var WP_Locale
2578
		 */
2579
		global $wp_locale;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
2580
2581
		return $wp_locale->month;
2582
2583
    }
2584
}
2585