Completed
Pull Request — 2.x (#3632)
by Scott Kingsley
06:38
created

PodsField_Pick::input()   F

Complexity

Conditions 23
Paths 320

Size

Total Lines 88
Code Lines 57

Duplication

Lines 28
Ratio 31.82 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 57
c 1
b 0
f 0
nc 320
nop 5
dl 28
loc 88
rs 3.5882

How to fix   Long Method    Complexity   

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
     * Add admin_init actions
106
     *
107
     * @since 2.3
108
     */
109
    public function admin_init () {
110
        //--!! Prototype testing only
111
        add_action( 'wp_ajax_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
112
        add_action( 'wp_ajax_nopriv_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
113
114
        // AJAX for Relationship lookups
115
        add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
116
        add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
117
    }
118
119
    /**
120
     * Add options and set defaults to
121
     *
122
     * @return array
123
     *
124
     * @since 2.0
125
     */
126
    public function options () {
127
        $options = array(
128
            self::$type . '_format_type' => array(
129
                'label' => __( 'Selection Type', 'pods' ),
130
                'help' => __( 'help', 'pods' ),
131
                'default' => 'single',
132
                'type' => 'pick',
133
                'data' => array(
134
                    'single' => __( 'Single Select', 'pods' ),
135
                    'multi' => __( 'Multiple Select', 'pods' )
136
                ),
137
                'dependency' => true
138
            ),
139
            self::$type . '_format_single' => array(
140
                'label' => __( 'Format', 'pods' ),
141
                'help' => __( 'help', 'pods' ),
142
                'depends-on' => array( self::$type . '_format_type' => 'single' ),
143
                'default' => 'dropdown',
144
                'type' => 'pick',
145
                'data' => apply_filters(
146
                    'pods_form_ui_field_pick_format_single_options',
147
                    array(
148
                        'dropdown' => __( 'Drop Down', 'pods' ),
149
                        'radio' => __( 'Radio Buttons', 'pods' ),
150
                        'autocomplete' => __( 'Autocomplete', 'pods' ),
151
                        'flexible' => __( 'Flexible', 'pods' ),
152
                    )
153
                ),
154
                'dependency' => true
155
            ),
156
            self::$type . '_format_multi' => array(
157
                'label' => __( 'Format', 'pods' ),
158
                'help' => __( 'help', 'pods' ),
159
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
160
                'default' => 'checkbox',
161
                'type' => 'pick',
162
                'data' => apply_filters(
163
                    'pods_form_ui_field_pick_format_multi_options',
164
                    array(
165
                        'checkbox' => __( 'Checkboxes', 'pods' ),
166
                        'multiselect' => __( 'Multi Select', 'pods' ),
167
                        'autocomplete' => __( 'Autocomplete', 'pods' ),
168
                        'flexible' => __( 'Flexible', 'pods' ),
169
                    )
170
                ),
171
                'dependency' => true
172
            ),
173
            self::$type . '_taggable' => array(
174
                'label' => __( 'Taggable', 'pods' ),
175
                'help' => __( 'Allow new values to be inserted when using an Autocomplete field', 'pods' ),
176
                'excludes-on' => array(
177
					self::$type . '_format_single' => array( 'dropdown', 'radio' ),
178
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect' ),
179
                    self::$type . '_object' => array_merge(
180
                        array( 'site', 'network' ),
181
                        self::simple_objects()
182
                    )
183
				),
184
                'type' => 'boolean',
185
                'default' => 0
186
            ),
187
            self::$type . '_show_icon' => array(
188
                'label' => __( 'Show Icons', 'pods' ),
189
                'excludes-on' => array(
190
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
191
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
192
                    self::$type . '_object' => array_merge(
193
                        array( 'site', 'network' ),
194
                        self::simple_objects()
195
                    )
196
				),
197
                'type' => 'boolean',
198
                'default' => 1
199
            ),
200
            self::$type . '_show_edit_link' => array(
201
                'label' => __( 'Show Edit Links', 'pods' ),
202
                'excludes-on' => array(
203
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
204
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
205
                    self::$type . '_object' => array_merge(
206
                        array( 'site', 'network' ),
207
                        self::simple_objects()
208
                    )
209
				),
210
                'type' => 'boolean',
211
                'default' => 1
212
            ),
213
            self::$type . '_show_view_link' => array(
214
                'label' => __( 'Show View Links', 'pods' ),
215
                'excludes-on' => array(
216
					self::$type . '_format_single' => array( 'dropdown', 'radio', 'autocomplete' ),
217
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect', 'autocomplete' ),
218
                    self::$type . '_object' => array_merge(
219
                        array( 'site', 'network' ),
220
                        self::simple_objects()
221
                    )
222
				),
223
                'type' => 'boolean',
224
                'default' => 1
225
            ),
226
			self::$type . '_select_text' => array(
227
                'label' => __( 'Default Select Text', 'pods' ),
228
                'help' => __( 'This is the text use for the default "no selection" dropdown item, if empty, it will default to "-- Select One --"', 'pods' ),
229
                'depends-on' => array(
230
					self::$type . '_format_type' => 'single',
231
					self::$type . '_format_single' => 'dropdown'
232
				),
233
                'default' => '',
234
                'type' => 'text'
235
			),
236
            self::$type . '_limit' => array(
237
                'label' => __( 'Selection Limit', 'pods' ),
238
                'help' => __( 'help', 'pods' ),
239
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
240
                'default' => 0,
241
                'type' => 'number'
242
            ),
243
            self::$type . '_table_id' => array(
244
                'label' => __( 'Table ID Column', 'pods' ),
245
                'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ),
246
                'depends-on' => array( self::$type . '_object' => 'table' ),
247
                'required' => 1,
248
                'default' => '',
249
                'type' => 'text'
250
            ),
251
            self::$type . '_table_index' => array(
252
                'label' => __( 'Table Index Column', 'pods' ),
253
                'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ),
254
                'depends-on' => array( self::$type . '_object' => 'table' ),
255
                'required' => 1,
256
                'default' => '',
257
                'type' => 'text'
258
            ),
259
            self::$type . '_display' => array(
260
                'label' => __( 'Display Field in Selection List', 'pods' ),
261
                'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ),
262
                'excludes-on' => array(
263
                    self::$type . '_object' => array_merge(
264
                        array( 'site', 'network' ),
265
                        self::simple_objects()
266
                    )
267
                ),
268
                'default' => '',
269
                'type' => 'text'
270
            ),
271
            self::$type . '_user_role' => array(
272
                'label' => __( 'Limit list to Role(s)', 'pods' ),
273
                'help' => __( 'help', 'pods' ),
274
                'depends-on' => array( self::$type . '_object' => 'user' ),
275
                'default' => '',
276
                'type' => 'pick',
277
                'pick_object' => 'role',
278
                'pick_format_type' => 'multi'
279
            ),/*
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...
280
            self::$type . '_user_site' => array(
281
                'label' => __( 'Limit list to Site(s)', 'pods' ),
282
                'help' => __( 'help', 'pods' ),
283
                'depends-on' => array( self::$type . '_object' => 'user' ),
284
                'default' => '',
285
                'type' => 'pick',
286
                'pick_object' => 'site',
287
                'pick_format_type' => 'multi'
288
            ),*/
289
            self::$type . '_where' => array(
290
                'label' => __( 'Customized <em>WHERE</em>', 'pods' ),
291
                'help' => __( 'help', 'pods' ),
292
                'excludes-on' => array(
293
                    self::$type . '_object' => array_merge(
294
                        array( 'site', 'network' ),
295
                        self::simple_objects()
296
                    )
297
                ),
298
                'default' => '',
299
                'type' => 'text'
300
            ),
301
            self::$type . '_orderby' => array(
302
                'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ),
303
                'help' => __( 'help', 'pods' ),
304
                'excludes-on' => array(
305
                    self::$type . '_object' => array_merge(
306
                        array( 'site', 'network' ),
307
                        self::simple_objects()
308
                    )
309
                ),
310
                'default' => '',
311
                'type' => 'text'
312
            ),
313
            self::$type . '_groupby' => array(
314
                'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ),
315
                'help' => __( 'help', 'pods' ),
316
                'excludes-on' => array(
317
                    self::$type . '_object' => array_merge(
318
                        array( 'site', 'network' ),
319
                        self::simple_objects()
320
                    )
321
                ),
322
                'default' => '',
323
                'type' => 'text'
324
            )
325
            /*,
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...
326
            self::$type . '_size' => array(
327
                'label' => __( 'Field Size', 'pods' ),
328
                'default' => 'medium',
329
                'type' => 'pick',
330
                'data' => array(
331
                    'small' => __( 'Small', 'pods' ),
332
                    'medium' => __( 'Medium', 'pods' ),
333
                    'large' => __( 'Large', 'pods' )
334
                )
335
            )*/
336
        );
337
338
        /*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...
339
            unset( $options[ self::$type . '_user_site' ] );*/
340
341
        return $options;
342
    }
343
344
    /**
345
     * Register a related object
346
     *
347
     * @param string $name Object name
348
     * @param string $label Object label
349
     * @param array $options Object options
350
     *
351
     * @return array|boolean Object array or false if unsuccessful
352
     * @since 2.3
353
     */
354
    public function register_related_object ( $name, $label, $options = null ) {
355
        if ( empty( $name ) || empty( $label ) )
356
            return false;
357
358
        $related_object = array(
359
            'label' => $label,
360
            'group' => 'Custom Relationships',
361
            'simple' => true,
362
            'bidirectional' => false,
363
            'data' => array(),
364
            'data_callback' => null
365
        );
366
367
        $related_object = array_merge( $related_object, $options );
368
369
        self::$custom_related_objects[ $name ] = $related_object;
370
371
        return true;
372
    }
373
374
	/**
375
	 * Setup related objects
376
	 *
377
	 * @param boolean $force Whether to force refresh of related objects
378
	 * @return bool True when data has been loaded
379
	 * @since 2.3
380
	 */
381
	public function setup_related_objects( $force = false ) {
382
383
		$new_data_loaded = false;
384
385
		if ( ! $force && empty( self::$related_objects ) ) {
386
			// Only load transient if we aren't forcing a refresh
387
			self::$related_objects = pods_transient_get( 'pods_related_objects' );
388
389
			if ( false !== self::$related_objects ) {
390
				$new_data_loaded = true;
391
			}
392
		} elseif ( $force ) {
393
			// If we are rebuilding, make sure we start with a clean slate
394
			self::$related_objects = array();
395
		}
396
397
		if ( empty( self::$related_objects ) ) {
398
			// Do a complete build of related_objects
399
			$new_data_loaded = true;
400
401
			// Custom
402
			self::$related_objects['custom-simple'] = array(
403
				'label'  => __( 'Simple (custom defined list)', 'pods' ),
404
				'group'  => __( 'Custom', 'pods' ),
405
				'simple' => true
406
			);
407
408
			// Pods
409
			$pod_options = array();
410
411
			// Include PodsMeta if not already included
412
			pods_meta();
413
414
			// Advanced Content Types
415
			$_pods = PodsMeta::$advanced_content_types;
416
417 View Code Duplication
			foreach ( $_pods as $pod ) {
418
				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')';
419
			}
420
421
			// Settings
422
			$_pods = PodsMeta::$settings;
423
424 View Code Duplication
			foreach ( $_pods as $pod ) {
425
				$pod_options[ $pod['name'] ] = $pod['label'] . ' (' . $pod['name'] . ')';
426
			}
427
428
			asort( $pod_options );
429
430
			foreach ( $pod_options as $pod => $label ) {
431
				self::$related_objects[ 'pod-' . $pod ] = array(
432
					'label'         => $label,
433
					'group'         => __( 'Pods', 'pods' ),
434
					'bidirectional' => true
435
				);
436
			}
437
438
			// Post Types
439
			$post_types = get_post_types();
440
			asort( $post_types );
441
442
			$ignore = array( 'attachment', 'revision', 'nav_menu_item' );
443
444
			foreach ( $post_types as $post_type => $label ) {
445
				if ( in_array( $post_type, $ignore ) || empty( $post_type ) ) {
446
					unset( $post_types[ $post_type ] );
447
448
					continue;
449
				} elseif ( 0 === strpos( $post_type, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) {
450
					unset( $post_types[ $post_type ] );
451
452
					continue;
453
				}
454
455
				$post_type = get_post_type_object( $post_type );
456
457
				self::$related_objects[ 'post_type-' . $post_type->name ] = array(
458
					'label'         => $post_type->label . ' (' . $post_type->name . ')',
459
					'group'         => __( 'Post Types', 'pods' ),
460
					'bidirectional' => true
461
				);
462
			}
463
464
			// Taxonomies
465
			$taxonomies = get_taxonomies();
466
			asort( $taxonomies );
467
468
			$ignore = array( 'nav_menu', 'post_format' );
469
470
			foreach ( $taxonomies as $taxonomy => $label ) {
471
				/**
472
				 * Prevent ability to extend core Pods content types.
473
				 *
474
				 * @param bool Default is true, when set to false Pods internal content types can not be extended.
475
				 *
476
				 * @since 2.3.19
477
				 */
478
				$ignore_internal = apply_filters( 'pods_pick_ignore_internal', true );
479
480
				if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) ) {
481
					unset( $taxonomies[ $taxonomy ] );
482
483
					continue;
484
				} elseif ( 0 === strpos( $taxonomy, '_pods_' ) && $ignore_internal ) {
485
					unset( $taxonomies[ $taxonomy ] );
486
487
					continue;
488
				}
489
490
				$taxonomy = get_taxonomy( $taxonomy );
491
492
				self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array(
493
					'label'         => $taxonomy->label . ' (' . $taxonomy->name . ')',
494
					'group'         => __( 'Taxonomies', 'pods' ),
495
					'bidirectional' => true
496
				);
497
			}
498
499
			// Other WP Objects
500
			self::$related_objects['user'] = array(
501
				'label'         => __( 'Users', 'pods' ),
502
				'group'         => __( 'Other WP Objects', 'pods' ),
503
				'bidirectional' => true
504
			);
505
506
			self::$related_objects['role'] = array(
507
				'label'         => __( 'User Roles', 'pods' ),
508
				'group'         => __( 'Other WP Objects', 'pods' ),
509
				'simple'        => true,
510
				'data_callback' => array( $this, 'data_roles' )
511
			);
512
513
			self::$related_objects['capability'] = array(
514
				'label'         => __( 'User Capabilities', 'pods' ),
515
				'group'         => __( 'Other WP Objects', 'pods' ),
516
				'simple'        => true,
517
				'data_callback' => array( $this, 'data_capabilities' )
518
			);
519
520
			self::$related_objects['media'] = array(
521
				'label'         => __( 'Media', 'pods' ),
522
				'group'         => __( 'Other WP Objects', 'pods' ),
523
				'bidirectional' => true
524
			);
525
526
			self::$related_objects['comment'] = array(
527
				'label'         => __( 'Comments', 'pods' ),
528
				'group'         => __( 'Other WP Objects', 'pods' ),
529
				'bidirectional' => true
530
			);
531
532
			self::$related_objects['image-size'] = array(
533
				'label'         => __( 'Image Sizes', 'pods' ),
534
				'group'         => __( 'Other WP Objects', 'pods' ),
535
				'simple'        => true,
536
				'data_callback' => array( $this, 'data_image_sizes' )
537
			);
538
539
			self::$related_objects['nav_menu'] = array(
540
				'label' => __( 'Navigation Menus', 'pods' ),
541
				'group' => __( 'Other WP Objects', 'pods' )
542
			);
543
544
			self::$related_objects['post_format'] = array(
545
				'label' => __( 'Post Formats', 'pods' ),
546
				'group' => __( 'Other WP Objects', 'pods' )
547
			);
548
549
			self::$related_objects['post-status'] = array(
550
				'label'         => __( 'Post Status', 'pods' ),
551
				'group'         => __( 'Other WP Objects', 'pods' ),
552
				'simple'        => true,
553
				'data_callback' => array( $this, 'data_post_stati' )
554
			);
555
556
			do_action( 'pods_form_ui_field_pick_related_objects_other' );
557
558
			self::$related_objects['country'] = array(
559
				'label'         => __( 'Countries', 'pods' ),
560
				'group'         => __( 'Predefined Lists', 'pods' ),
561
				'simple'        => true,
562
				'data_callback' => array( $this, 'data_countries' )
563
			);
564
565
			self::$related_objects['us_state'] = array(
566
				'label'         => __( 'US States', 'pods' ),
567
				'group'         => __( 'Predefined Lists', 'pods' ),
568
				'simple'        => true,
569
				'data_callback' => array( $this, 'data_us_states' )
570
			);
571
572
			self::$related_objects['days_of_week'] = array(
573
				'label'         => __( 'Calendar - Days of Week', 'pods' ),
574
				'group'         => __( 'Predefined Lists', 'pods' ),
575
				'simple'        => true,
576
				'data_callback' => array( $this, 'data_days_of_week' )
577
			);
578
579
			self::$related_objects['months_of_year'] = array(
580
				'label'         => __( 'Calendar - Months of Year', 'pods' ),
581
				'group'         => __( 'Predefined Lists', 'pods' ),
582
				'simple'        => true,
583
				'data_callback' => array( $this, 'data_months_of_year' )
584
			);
585
586
			do_action( 'pods_form_ui_field_pick_related_objects_predefined' );
587
588
			if ( did_action( 'init' ) ) {
589
				pods_transient_set( 'pods_related_objects', self::$related_objects );
590
			}
591
		}
592
593
		/**
594
		 * Allow custom related objects to be defined
595
		 */
596
		do_action( 'pods_form_ui_field_pick_related_objects_custom' );
597
598
		foreach ( self::$custom_related_objects as $object => $related_object ) {
599
			if ( ! isset( self::$related_objects[ $object ] ) ) {
600
				$new_data_loaded = true;
601
602
				self::$related_objects[ $object ] = $related_object;
603
			}
604
		}
605
606
		return $new_data_loaded;
607
608
	}
609
610
    /**
611
     * Return available related objects
612
     *
613
     * @param boolean $force Whether to force refresh of related objects
614
     *
615
     * @return array Field selection array
616
     * @since 2.3
617
     */
618
    public function related_objects ( $force = false ) {
619
        if ( $this->setup_related_objects( $force ) || null === self::$names_related ) {
620
	        $related_objects = array();
621
622
	        foreach ( self::$related_objects as $related_object_name => $related_object ) {
623
		        if ( ! isset( $related_objects[ $related_object[ 'group' ] ] ) ) {
624
			        $related_objects[ $related_object[ 'group' ] ] = array();
625
		        }
626
627
		        $related_objects[ $related_object[ 'group' ] ][ $related_object_name ] = $related_object[ 'label' ];
628
	        }
629
630
	        self::$names_related = (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects );
631
        }
632
633
	    return self::$names_related;
634
    }
635
636
    /**
637
     * Return available simple object names
638
     *
639
     * @return array Simple object names
640
     * @since 2.3
641
     */
642 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...
643
		if ( $this->setup_related_objects() || null === self::$names_simple ) {
644
			$simple_objects = array();
645
646
			foreach ( self::$related_objects as $object => $related_object ) {
647
				if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] )
648
					continue;
649
650
				$simple_objects[] = $object;
651
			}
652
653
			self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects );
654
		}
655
656
	    return self::$names_simple;
657
    }
658
659
    /**
660
     * Return available bidirectional object names
661
     *
662
     * @return array Bidirectional object names
663
     * @since 2.3.4
664
     */
665 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...
666
        if ( $this->setup_related_objects() || null === self::$names_bidirectional ) {
667
	        $bidirectional_objects = array();
668
669
	        foreach ( self::$related_objects as $object => $related_object ) {
670
		        if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] )
671
			        continue;
672
673
		        $bidirectional_objects[] = $object;
674
	        }
675
676
	        self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects );
677
        }
678
679
	    return self::$names_bidirectional;
680
    }
681
682
    /**
683
     * Define the current field's schema for DB table storage
684
     *
685
     * @param array $options
686
     *
687
     * @return array
688
     * @since 2.0
689
     */
690
    public function schema ( $options = null ) {
691
        $schema = false;
692
693
        $simple_tableless_objects = $this->simple_objects();
694
695
        if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) )
696
            $schema = 'LONGTEXT';
697
698
        return $schema;
699
    }
700
701
    /**
702
     * Change the way the value of the field is displayed with Pods::get
703
     *
704
     * @param mixed $value
705
     * @param string $name
706
     * @param array $options
707
     * @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...
708
     * @param array $pod
709
     * @param int $id
710
     *
711
     * @since 2.0
712
     */
713
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
714
        $fields = null;
715
716
        if ( is_object( $pod ) && isset( $pod->fields ) ) {
717
	        $fields = $pod->fields;
718
719
	        if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) {
720
		        $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] );
721
	        }
722 View Code Duplication
        } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) {
723
	        $fields = $pod[ 'fields' ];
724
725
	        if ( ! empty( $pod[ 'object_fields' ] ) ) {
726
		        $fields = array_merge( $fields, $pod[ 'object_fields' ] );
727
	        }
728
        }
729
730
        return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) );
731
    }
732
733
    /**
734
     * Customize output of the form field
735
     *
736
     * @param string $name
737
     * @param mixed $value
738
     * @param array $options
739
     * @param array $pod
740
     * @param int $id
741
     *
742
     * @since 2.0
743
     */
744
    public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
745
        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...
746
747
        $options = (array) $options;
748
        $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...
749
750
        $options[ 'grouped' ] = 1;
751
752
        $options[ 'table_info' ] = array();
753
754
        $custom = pods_var_raw( self::$type . '_custom', $options, false );
755
756
        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
757
758
        $ajax = false;
759
760
        if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) )
761
            $ajax = true;
762
763
        if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) {
764
            $ajax = (boolean) self::$field_data[ 'autocomplete' ];
765
        }
766
767
        $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id );
768
769
        if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) )
770
            $ajax = false;
771
772
	    $format_type = pods_v( self::$type . '_format_type', $options, 'single' );
773
774
        if ( 'single' == $format_type ) {
775 View Code Duplication
            if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
776
                $field_type = 'select';
777
            elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
778
                $field_type = 'radio';
779
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
780
                $field_type = 'select2';
781
            elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
782
                $field_type = 'flexible';
783
            else {
784
                // Support custom integration
785
                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 );
786
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
787
                return;
788
            }
789
        }
790
        elseif ( 'multi' == $format_type ) {
791
            if ( !empty( $value ) && !is_array( $value ) )
792
                $value = explode( ',', $value );
793
794 View Code Duplication
            if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
795
                $field_type = 'checkbox';
796
            elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
797
                $field_type = 'select';
798
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
799
                $field_type = 'select2';
800
            elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
801
                $field_type = 'flexible';
802
            else {
803
                // Support custom integration
804
                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 );
805
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
806
                return;
807
            }
808
        }
809
        else {
810
            // Support custom integration
811
            do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
812
            return;
813
        }
814
815
	    // Flexible relationships only support certain related objects
816
	    if ( 'flexible' == $field_type || ( 'select2' == $field_type && 1 == pods_v( self::$type . '_taggable', $options, 0 ) ) ) {
817
		    $pick_object = pods_v( 'pick_object', $options );
818
819
		    if ( ! in_array( $pick_object, array( 'post_type', 'taxonomy', 'user', 'pod' ) ) ) {
820
			    // Default all others to Select2
821
				$field_type = 'select2';
822
		    } else {
823
		        // @todo: Fix location when merging
824
		        $field_type = 'pick';
825
		        pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) );
826
		        return;
827
		    }
828
	    }
829
830
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
831
    }
832
833
    /**
834
     * Validate a value before it's saved
835
     *
836
     * @param mixed $value
837
     * @param string $name
838
     * @param array $options
839
     * @param array $fields
840
     * @param array $pod
841
     * @param int $id
842
     *
843
     * @param null $params
844
     * @return array|bool
845
     * @since 2.0
846
     */
847
    public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
848
        if ( empty( self::$api ) )
849
            self::$api = pods_api();
850
851
        $simple_tableless_objects = $this->simple_objects();
852
853
        $related_pick_limit = 0;
854
        $related_field = $related_pod = $current_related_ids = false;
855
856
        // Bidirectional relationship requirement checks
857
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
858
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
859
        if ( empty( $related_val ) ) {
860
            $related_val = $related_object;
861
        }
862
863
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
864
865
        $options[ 'id' ] = (int) $options[ 'id' ];
866
867
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) || empty( self::$related_data[ $options[ 'id' ] ] ) )
868
            self::$related_data[ $options[ 'id' ] ] = array();
869
870
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
871
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
872
873
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
874
                $related_field = false;
875
876
                // Ensure sister_id exists on related Pod
877 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
878
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
879
                        $related_field = $related_pod_field;
880
881
                        break;
882
                    }
883
                }
884
885
                if ( !empty( $related_field ) ) {
886
                    $current_ids = self::$api->lookup_related_items( $fields[ $name ][ 'id' ], $pod[ 'id' ], $id, $fields[ $name ], $pod );
887
888
                    self::$related_data[ $options[ 'id' ] ][ 'current_ids' ] = $current_ids;
889
890
                    $value_ids = $value;
891
892
                    // Convert values from a comma-separated string into an array
893
                    if ( !is_array( $value_ids ) )
894
                        $value_ids = explode( ',', $value_ids );
895
896
                    $value_ids = array_unique( array_filter( $value_ids ) );
897
898
                    // Get ids to remove
899
                    $remove_ids = array_diff( $current_ids, $value_ids );
900
901
                    $related_required = (boolean) pods_var( 'required', $related_field[ 'options' ], 0 );
902
                    $related_pick_limit = (int) pods_var( self::$type . '_limit', $related_field[ 'options' ], 0 );
903
904
                    if ( 'single' == pods_var_raw( self::$type . '_format_type', $related_field[ 'options' ] ) )
905
                        $related_pick_limit = 1;
906
907
                    // Validate Required
908
                    if ( $related_required && !empty( $remove_ids ) ) {
909
                        foreach ( $remove_ids as $related_id ) {
910
                            $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
911
912
                            self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] = $bidirectional_ids;
913
914
                            if ( empty( $bidirectional_ids ) || ( in_array( $id, $bidirectional_ids ) && 1 == count( $bidirectional_ids ) ) )
915
                                return sprintf( __( 'The %s field is required and cannot be removed by the %s field', 'pods' ), $related_field[ 'label' ], $options[ 'label' ] );
916
                        }
917
                    }
918
                }
919
                else
920
                    $related_pod = false;
921
            }
922
            else
923
                $related_pod = false;
924
        }
925
926
        if ( empty( self::$related_data[ $options[ 'id' ] ] ) )
927
            unset( self::$related_data[ $options[ 'id' ] ] );
928
        else {
929
            self::$related_data[ $options[ 'id' ] ][ 'related_pod' ] = $related_pod;
930
            self::$related_data[ $options[ 'id' ] ][ 'related_field' ] = $related_field;
931
            self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ] = $related_pick_limit;
932
933
            $pick_limit = (int) pods_var( self::$type . '_limit', $options[ 'options' ], 0 );
934
935
            if ( 'single' == pods_var_raw( self::$type . '_format_type', $options[ 'options' ] ) )
936
                $pick_limit = 1;
937
938
            $related_field[ 'id' ] = (int) $related_field[ 'id' ];
939
940
            if ( !isset( self::$related_data[ $related_field[ 'id' ] ] ) || empty( self::$related_data[ $related_field[ 'id' ] ] ) ) {
941
                self::$related_data[ $related_field[ 'id' ] ] = array(
942
                    'related_pod' => $pod,
943
                    'related_field' => $options,
944
                    'related_pick_limit' => $pick_limit
945
                );
946
            }
947
        }
948
949
        return true;
950
    }
951
952
    /**
953
     * Save the value to the DB
954
     *
955
     * @param mixed $value
956
     * @param int $id
957
     * @param string $name
958
     * @param array $options
959
     * @param array $fields
960
     * @param array $pod
961
     * @param object $params
962
     *
963
     * @since 2.3
964
     */
965
    public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
966
        if ( empty( self::$api ) )
967
            self::$api = pods_api();
968
969
        $options[ 'id' ] = (int) $options[ 'id' ];
970
971
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) )
972
            return;
973
974
        $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ];
975
        $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ];
976
        $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ];
977
978
        // Bidirectional relationship updates
979
        if ( !empty( $related_field ) ) {
980
            // Don't use no conflict mode unless this isn't the current pod type
981
            $no_conflict = true;
982
983
            if ( $related_pod[ 'type' ] != $pod[ 'type' ] )
984
                $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
985
986
            if ( !$no_conflict )
987
                pods_no_conflict_on( $related_pod[ 'type' ] );
988
989
            $value = array_filter( $value );
990
991
            foreach ( $value as $related_id ) {
992
                if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) )
993
                    $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ];
994
                else
995
                    $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
996
997
                $bidirectional_ids = array_filter( $bidirectional_ids );
998
999
                if ( empty( $bidirectional_ids ) )
1000
                    $bidirectional_ids = array();
1001
1002
                $remove_ids = array();
1003
1004
                if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) {
1005
                    while ( $related_pick_limit <= count( $bidirectional_ids ) ) {
1006
                        $remove_ids[] = (int) array_pop( $bidirectional_ids );
1007
                    }
1008
                }
1009
1010
                // Remove this item from related items no longer related to
1011
                $remove_ids = array_unique( array_filter( $remove_ids ) );
1012
1013
                // Add to related items
1014
                if ( !in_array( $id, $bidirectional_ids ) )
1015
                    $bidirectional_ids[] = $id;
1016
                // Nothing to change
1017
                elseif ( empty( $remove_ids ) )
1018
                    continue;
1019
1020
                self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field );
1021
1022
                if ( !empty( $remove_ids ) )
1023
                    self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options );
1024
            }
1025
1026
            if ( !$no_conflict )
1027
                pods_no_conflict_off( $related_pod[ 'type' ] );
1028
        }
1029
    }
1030
1031
    /**
1032
     * Delete the value from the DB
1033
     *
1034
     * @param int $id
1035
     * @param string $name
1036
     * @param array $options
1037
     * @param array $pod
1038
     *
1039
     * @since 2.3
1040
     */
1041
    public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
1042
        if ( empty( self::$api ) )
1043
            self::$api = pods_api();
1044
1045
        $simple_tableless_objects = $this->simple_objects();
1046
1047
        // Bidirectional relationship requirement checks
1048
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
1049
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
1050
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
1051
1052
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
1053
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
1054
1055
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
1056
                $related_field = false;
1057
1058
                // Ensure sister_id exists on related Pod
1059 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
1060
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
1061
                        $related_field = $related_pod_field;
1062
1063
                        break;
1064
                    }
1065
                }
1066
1067
                if ( !empty( $related_field ) ) {
1068
                    $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod );
1069
1070
                    if ( !empty( $values ) ) {
1071
                        $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1072
1073
                        if ( !$no_conflict )
1074
                            pods_no_conflict_on( $related_pod[ 'type' ] );
1075
1076
                        self::$api->delete_relationships( $values, $id, $related_pod, $related_field );
1077
1078
                        if ( !$no_conflict )
1079
                            pods_no_conflict_off( $related_pod[ 'type' ] );
1080
                    }
1081
                }
1082
            }
1083
        }
1084
    }
1085
1086
    /**
1087
     * Customize the Pods UI manage table column output
1088
     *
1089
     * @param int $id
1090
     * @param mixed $value
1091
     * @param string $name
1092
     * @param array $options
1093
     * @param array $fields
1094
     * @param array $pod
1095
     *
1096
     * @since 2.0
1097
     */
1098
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
1099
        $value = $this->simple_value( $name, $value, $options, $pod, $id );
1100
1101
        return $this->display( $value, $name, $options, $pod, $id );
1102
    }
1103
1104
    /**
1105
     * Get the data from the field
1106
     *
1107
     * @param string $name The name of the field
1108
     * @param string|array $value The value of the field
1109
     * @param array $options Field options
1110
     * @param array $pod Pod data
1111
     * @param int $id Item ID
1112
     * @param boolean $in_form
1113
     *
1114
     * @return array Array of possible field data
1115
     *
1116
     * @since 2.0
1117
     */
1118
    public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
1119 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1120
            $options = array_merge( $options, $options[ 'options' ] );
1121
1122
            unset( $options[ 'options' ] );
1123
        }
1124
1125
        $data = pods_var_raw( 'data', $options, null, null, true );
1126
1127
        $object_params = array(
1128
            'name' => $name, // The name of the field
1129
            'value' => $value, // The value of the field
1130
            'options' => $options, // Field options
1131
            'pod' => $pod, // Pod data
1132
            'id' => $id, // Item ID
1133
            'context' => 'data', // Data context
1134
        );
1135
1136
        if ( null !== $data )
1137
            $data = (array) $data;
1138
        else
1139
            $data = $this->get_object_data( $object_params );
1140
1141
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1142
            $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data;
1143
1144
        $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
1145
1146
        return $data;
1147
    }
1148
1149
    /**
1150
     * Convert a simple value to the correct value
1151
     *
1152
     * @param string $name The name of the field
1153
     * @param string|array $value The value of the field
1154
     * @param array $options Field options
1155
     * @param array $pod Pod data
1156
     * @param int $id Item ID
1157
     * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false)
1158
     *
1159
     * @return mixed Corrected value
1160
     */
1161
    public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) {
1162
        if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) {
1163 View Code Duplication
            if ( isset( $options[ 'options' ] ) ) {
1164
                $options = array_merge( $options, $options[ 'options' ] );
1165
1166
                unset( $options[ 'options' ] );
1167
            }
1168
1169
            if ( !is_array( $value ) && 0 < strlen( $value ) ) {
1170
                $simple = @json_decode( $value, true );
1171
1172
                if ( is_array( $simple ) )
1173
                    $value = $simple;
1174
            }
1175
1176
            $data = pods_var_raw( 'data', $options, null, null, true );
1177
1178
            $object_params = array(
1179
                'name' => $name, // The name of the field
1180
                'value' => $value, // The value of the field
1181
                'options' => $options, // Field options
1182
                'pod' => $pod, // Pod data
1183
                'id' => $id, // Item ID
1184
                'context' => 'simple_value', // Data context
1185
            );
1186
1187
            if ( null === $data )
1188
                $data = $this->get_object_data( $object_params );
1189
1190
            $data = (array) $data;
1191
1192
            $key = 0;
1193
1194
            if ( is_array( $value ) ) {
1195
                if ( !empty( $data ) ) {
1196
                    $val = array();
1197
1198
                    foreach ( $value as $k => $v ) {
1199
                        if ( isset( $data[ $v ] ) ) {
1200
                            if ( false === $raw ) {
1201
                                $k = $v;
1202
                                $v = $data[ $v ];
1203
                            }
1204
1205
                            $val[ $k ] = $v;
1206
                        }
1207
                    }
1208
1209
                    $value = $val;
1210
                }
1211
            }
1212
            elseif ( isset( $data[ $value ] ) && false === $raw ) {
1213
                $key = $value;
1214
                $value = $data[ $value ];
1215
            }
1216
1217
            $single_multi = pods_var( self::$type . '_format_type', $options, 'single' );
1218
1219
            if ( 'multi' == $single_multi )
1220
                $limit = (int) pods_var( self::$type . '_limit', $options, 0 );
1221
            else
1222
                $limit = 1;
1223
1224
            if ( is_array( $value ) && 0 < $limit ) {
1225
                if ( 1 == $limit )
1226
                    $value = current( $value );
1227
                else
1228
                    $value = array_slice( $value, 0, $limit, true );
1229
            }
1230
            elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) {
1231
                if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) {
1232
                    $value = array(
1233
                        $key => $value
1234
                    );
1235
                }
1236
            }
1237
        }
1238
1239
        return $value;
1240
    }
1241
1242
    /**
1243
     * Get the label from a pick value
1244
     *
1245
     * @param string $name The name of the field
1246
     * @param string|array $value The value of the field
1247
     * @param array $options Field options
1248
     * @param array $pod Pod data
1249
     * @param int $id Item ID
1250
     *
1251
     * @return string
1252
     *
1253
     * @since 2.2
1254
     */
1255
    public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) {
1256 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1257
            $options = array_merge( $options, $options[ 'options' ] );
1258
1259
            unset( $options[ 'options' ] );
1260
        }
1261
1262
        $data = pods_var_raw( 'data', $options, null, null, true );
1263
1264
        $object_params = array(
1265
            'name' => $name, // The name of the field
1266
            'value' => $value, // The value of the field
1267
            'options' => $options, // Field options
1268
            'pod' => $pod, // Pod data
1269
            'id' => $id, // Item ID
1270
            'context' => 'value_to_label', // Data context
1271
        );
1272
1273
        if ( null !== $data )
1274
            $data = (array) $data;
1275
        else
1276
            $data = $this->get_object_data( $object_params );
1277
1278
        $labels = array();
1279
1280
        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...
1281
            if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) )
1282
                $labels[] = $l;
1283
        }
1284
1285
        $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id );
1286
1287
        $labels = pods_serial_comma( $labels );
1288
1289
        return $labels;
1290
    }
1291
1292
	/**
1293
	 * Get available items from a relationship field
1294
	 *
1295
	 * @param array|string $field Field array or field name
1296
	 * @param array $options [optional] Field options array overrides
1297
	 * @param array $object_params [optional] Additional get_object_data options
1298
	 *
1299
	 * @return array An array of available items from a relationship field
1300
	 */
1301
	public function get_field_data( $field, $options = array(), $object_params = array() ) {
1302
1303
		// Handle field array overrides
1304
		if ( is_array( $field ) ) {
1305
			$options = array_merge( $field, $options );
1306
		}
1307
1308
		// Get field name from array
1309
		$field = pods_var_raw( 'name', $options, $field, null, true );
1310
1311
		// Field name or options not set
1312
		if ( empty( $field ) || empty( $options ) ) {
1313
			return array();
1314
		}
1315
1316
		// Options normalization
1317
		$options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) );
1318
1319
		// Setup object params
1320
        $object_params = array_merge(
1321
			array(
1322
				'name' => $field, // The name of the field
1323
				'options' => $options, // Field options
1324
			),
1325
			$object_params
1326
        );
1327
1328
		// Get data override
1329
        $data = pods_var_raw( 'data', $options, null, null, true );
1330
1331
		// Return data override
1332
        if ( null !== $data ) {
1333
            $data = (array) $data;
1334
		}
1335
		// Get object data
1336
        else {
1337
            $data = $this->get_object_data( $object_params );
1338
		}
1339
1340
		return $data;
1341
1342
	}
1343
1344
    /**
1345
     * Get data from relationship objects
1346
     *
1347
     * @param array $object_params Object data parameters
1348
     *
1349
     * @return array|bool Object data
1350
     */
1351
    public function get_object_data ( $object_params = null ) {
1352
1353
	    /**
1354
	     * @var $wpdb wpdb
1355
	     */
1356
        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...
1357
1358
        $object_params = array_merge(
1359
            array(
1360
                'name' => '', // The name of the field
1361
                'value' => '', // The value of the field
1362
                'options' => array(), // Field options
1363
                'pod' => '', // Pod data
1364
                'id' => '', // Item ID
1365
                'context' => '', // Data context
1366
                'data_params' => array(
1367
                    'query' => '' // Query being searched
1368
                ),
1369
                'page' => 1, // Page number of results to get
1370
				'limit' => 0 // How many data items to limit to (autocomplete defaults to 30, set to -1 or 1+ to override)
1371
            ),
1372
            $object_params
1373
        );
1374
1375
        $name = $object_params[ 'name' ];
1376
        $value = $object_params[ 'value' ];
1377
        $options = $object_params[ 'options' ] = (array) $object_params[ 'options' ];
1378
        $pod = $object_params[ 'pod' ];
1379
        $id = $object_params[ 'id' ];
1380
        $context = $object_params[ 'context' ];
1381
        $data_params = $object_params[ 'data_params' ] = (array) $object_params[ 'data_params' ];
1382
        $page = min( 1, (int) $object_params[ 'page' ] );
1383
        $limit = (int) $object_params[ 'limit' ];
1384
1385 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1386
            $options = array_merge( $options, $options[ 'options' ] );
1387
1388
            unset( $options[ 'options' ] );
1389
        }
1390
1391
        $data = apply_filters( 'pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params );
1392
        $items = array();
1393
1394
        if ( !isset( $options[ self::$type . '_object' ] ) )
1395
            $data = pods_var_raw( 'data', $options, array(), null, true );
1396
1397
		$simple = false;
1398
1399
        if ( null === $data ) {
1400
            $data = array();
1401
1402
            if ( 'custom-simple' == $options[ self::$type . '_object' ] ) {
1403
                $custom = pods_var_raw( self::$type . '_custom', $options, '' );
1404
1405
                $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params );
1406
1407
                if ( !empty( $custom ) ) {
1408
                    if ( !is_array( $custom ) ) {
1409
                        $data = array();
1410
1411
                        $custom = explode( "\n", trim( $custom ) );
1412
1413
                        foreach ( $custom as $custom_value ) {
1414
                            $custom_label = explode( '|', $custom_value );
1415
1416
                            if ( empty( $custom_label ) )
1417
                                continue;
1418
1419
                            if ( 1 == count( $custom_label ) )
1420
                                $custom_label = $custom_value;
1421
                            else {
1422
                                $custom_value = $custom_label[ 0 ];
1423
                                $custom_label = $custom_label[ 1 ];
1424
                            }
1425
1426
							$custom_value = trim( (string) $custom_value );
1427
							$custom_label = trim( (string) $custom_label );
1428
1429
                            $data[ $custom_value ] = $custom_label;
1430
                        }
1431
                    }
1432
                    else
1433
                        $data = $custom;
1434
1435
					$simple = true;
1436
                }
1437
            }
1438
            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' ] ) ) {
1439
                $data = self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ];
1440
1441
				$simple = true;
1442
			}
1443
            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' ] ) ) {
1444
                $data = call_user_func_array(
1445
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ],
1446
                    array( $name, $value, $options, $pod, $id )
1447
                );
1448
1449
				$simple = true;
1450
1451
                // Cache data from callback
1452
                if ( !empty( $data ) )
1453
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] = $data;
1454
            }
1455
            elseif ( 'simple_value' != $context ) {
1456
                $pick_val = pods_var( self::$type . '_val', $options );
1457
1458
                if ( 'table' == pods_var( self::$type . '_object', $options ) )
1459
                    $pick_val = pods_var( self::$type . '_table', $options, $pick_val, null, true );
1460
1461 View Code Duplication
                if ( '__current__' == $pick_val ) {
1462
                    if ( is_object( $pod ) )
1463
                        $pick_val = $pod->pod;
1464
                    elseif ( is_array( $pod ) )
1465
                        $pick_val = $pod[ 'name' ];
1466
                    elseif ( 0 < strlen( $pod ) )
1467
                        $pick_val = $pod;
1468
                }
1469
1470
                $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( self::$type . '_object', $options ), $pick_val, null, null, $options );
1471
1472
                $search_data = pods_data();
1473
                $search_data->table( $options[ 'table_info' ] );
1474
1475
                if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
1476
                    $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
1477
                    $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
1478
                }
1479
1480
                $params = array(
1481
                    'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
1482
                    'table' => $search_data->table,
1483
                    'where' => pods_var_raw( self::$type . '_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
1484
                    'orderby' => pods_var_raw( self::$type . '_orderby', $options, null, null, true ),
1485
                    'groupby' => pods_var_raw( self::$type . '_groupby', $options, null, null, true ),
1486
                    //'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...
1487
					'pagination' => false,
1488
					'search' => false
1489
                );
1490
1491
                if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1492
                    $params[ 'select' ] .= ', `t`.`path`';
1493
1494
                if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
1495
                    $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
1496
1497
                if ( empty( $params[ 'where' ] ) || ( !is_array( $params[ 'where' ] ) && strlen( trim( $params[ 'where' ] ) ) < 1 ) )
1498
                    $params[ 'where' ] = array();
1499
                elseif ( !is_array( $params[ 'where' ] ) )
1500
                    $params[ 'where' ] = (array) $params[ 'where' ];
1501
1502
                if ( 'value_to_label' == $context )
1503
                    $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' );
1504
1505
                /* 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...
1506
                if ( !empty( $params[ 'orderby' ] ) )
1507
                    $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
1508
1509
                if ( !empty( $params[ 'groupby' ] ) )
1510
                    $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
1511
1512
                $display = trim( pods_var( self::$type . '_display', $options ), ' {@}' );
1513
1514
                if ( 0 < strlen( $display ) ) {
1515
                    if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
1516
                        if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
1517
                            $search_data->field_index = $display;
1518
1519
                            $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1520
                        }
1521
                        elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
1522
                            $search_data->field_index = $display;
1523
1524
                            if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
1525
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
1526
                            elseif ( 'meta' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] )
1527
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
1528
                            else
1529
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1530
                        }
1531
                    }
1532
                    elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
1533
                        $search_data->field_index = $display;
1534
1535
                        $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1536
                    }
1537
                }
1538
1539
                $autocomplete = false;
1540
1541
                if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1542
                    $autocomplete = true;
1543 View Code Duplication
                elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
1544
                    $autocomplete = true;
1545
1546
                $hierarchy = false;
1547
1548
                if ( 'data' == $context && !$autocomplete ) {
1549
                    if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_single', $options, 'dropdown' ), array( 'dropdown', 'radio' ) ) )
1550
                        $hierarchy = true;
1551 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' ) ) )
1552
                        $hierarchy = true;
1553
                }
1554
1555
                if ( $hierarchy && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) )
1556
                    $params[ 'select' ] .= ', ' . $options[ 'table_info' ][ 'field_parent_select' ];
1557
1558
                if ( $autocomplete ) {
1559
					if ( 0 == $limit ) {
1560
						$limit = 30;
1561
					}
1562
1563
                    $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params );
1564
1565
					if ( is_array( $value ) && $params[ 'limit' ] < count( $value ) ) {
1566
						$params[ 'limit' ] = count( $value );
1567
					}
1568
1569
                    $params[ 'page' ] = $page;
1570
1571
                    if ( 'admin_ajax_relationship' == $context ) {
1572
                        $lookup_where = array(
1573
                            $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"
1574
                        );
1575
1576
                        // @todo Hook into WPML for each table
1577
                        if ( $wpdb->users == $search_data->table ) {
1578
                            $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1579
                            $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1580
                            $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1581
                        }
1582
                        elseif ( $wpdb->posts == $search_data->table ) {
1583
                            $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1584
                            $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1585
                            $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1586
                            $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1587
                        }
1588
                        elseif ( $wpdb->terms == $search_data->table ) {
1589
                            $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1590
                            $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1591
                        }
1592
                        elseif ( $wpdb->comments == $search_data->table ) {
1593
                            $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1594
                            $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1595
                            $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1596
                        }
1597
1598
                        $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 );
1599
1600
                        if ( !empty( $lookup_where ) )
1601
                            $params[ 'where' ][] = implode( ' OR ', $lookup_where );
1602
1603
                        $orderby = array();
1604
                        $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%' ) DESC";
1605
1606
                        $pick_orderby = pods_var_raw( self::$type . '_orderby', $options, null, null, true );
1607
1608
                        if ( 0 < strlen( $pick_orderby ) )
1609
                            $orderby[] = $pick_orderby;
1610
1611
                        $orderby[] = "`t`.`{$search_data->field_index}`";
1612
                        $orderby[] = "`t`.`{$search_data->field_id}`";
1613
1614
                        $params[ 'orderby' ] = $orderby;
1615
                    }
1616
                }
1617
				elseif ( 0 < $limit ) {
1618
                    $params[ 'limit' ] = $limit;
1619
                    $params[ 'page' ] = $page;
1620
				}
1621
1622
                $extra = '';
1623
1624
                if ( $wpdb->posts == $search_data->table )
1625
                    $extra = ', `t`.`post_type`';
1626
                elseif ( $wpdb->terms == $search_data->table )
1627
                    $extra = ', `tt`.`taxonomy`';
1628
                elseif ( $wpdb->comments == $search_data->table )
1629
                    $extra = ', `t`.`comment_type`';
1630
1631
                $params[ 'select' ] .= $extra;
1632
1633
                if ( 'user' == pods_var( self::$type . '_object', $options ) ) {
1634
                    $roles = pods_var( self::$type . '_user_role', $options );
1635
1636
                    if ( !empty( $roles ) ) {
1637
                        $where = array();
1638
1639
                        foreach ( (array) $roles as $role ) {
1640
                            if ( empty( $role ) || ( pods_clean_name( $role ) != $role && sanitize_title( $role ) != $role ) )
1641
                                continue;
1642
1643
                            $where[] = $wpdb->base_prefix . ( ( is_multisite() && !is_main_site() ) ? get_current_blog_id() . '_' : '' ) . 'capabilities.meta_value LIKE "%\"' . pods_sanitize_like( $role ) . '\"%"';
1644
                        }
1645
1646
                        if ( !empty( $where ) ) {
1647
                            $params[ 'where' ][] = implode( ' OR ', $where );
1648
                        }
1649
                    }
1650
                }
1651
1652
                $results = $search_data->select( $params );
1653
1654
                if ( $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
1655
                    if ( !empty( $value ) ) {
1656
                        $ids = $value;
1657
1658
						if ( is_array( $ids ) && isset( $ids[ 0 ] ) && is_array( $ids[ 0 ] ) ) {
1659
							$ids = wp_list_pluck( $ids, $search_data->field_id );
1660
						}
1661
1662
                        if ( is_array( $ids ) )
1663
                            $ids = implode( ', ', $ids );
1664
1665
                        if ( is_array( $params[ 'where' ] ) )
1666
                            $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
1667
                        if ( !empty( $params[ 'where' ] ) )
1668
                            $params[ 'where' ] .= ' AND ';
1669
1670
                        $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
1671
1672
                        $results = $search_data->select( $params );
1673
                    }
1674
                }
1675
                else
1676
                    $autocomplete = false;
1677
1678
                if ( 'data' == $context ) {
1679
                    self::$field_data = array(
1680
                        'field' => $name,
1681
                        'id' => $options[ 'id' ],
1682
                        'autocomplete' => $autocomplete
1683
                    );
1684
                }
1685
1686
                if ( $hierarchy && !$autocomplete && !empty( $results ) && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) {
1687
                    $args = array(
1688
                        'id' => $options[ 'table_info' ][ 'field_id' ],
1689
                        'index' => $options[ 'table_info' ][ 'field_index' ],
1690
                        'parent' => $options[ 'table_info' ][ 'field_parent' ],
1691
                    );
1692
1693
                    $results = pods_hierarchical_select( $results, $args );
1694
                }
1695
1696
                $ids = array();
1697
1698
                if ( !empty( $results ) ) {
1699
                    $display_filter = pods_var( 'display_filter', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) );
1700
1701
                    foreach ( $results as $result ) {
1702
                        $result = get_object_vars( $result );
1703
1704
                        if ( !isset( $result[ $search_data->field_id ] ) || !isset( $result[ $search_data->field_index ] ) )
1705
                            continue;
1706
1707
                        $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
1708
1709
                        $object = $object_type = '';
1710
1711
                        if ( $wpdb->posts == $search_data->table && isset( $result[ 'post_type' ] ) ) {
1712
                            $object = $result[ 'post_type' ];
1713
                            $object_type = 'post_type';
1714
                        }
1715
                        elseif ( $wpdb->terms == $search_data->table && isset( $result[ 'taxonomy' ] ) ) {
1716
                            $object = $result[ 'taxonomy' ];
1717
                            $object_type = 'taxonomy';
1718
                        }
1719
1720
                        if ( 0 < strlen( $display_filter ) ) {
1721
                            $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' ] ) ) );
1722
1723
                            $args = array(
1724
                                $display_filter,
1725
                                $result[ $search_data->field_index ]
1726
                            );
1727
1728
                            if ( !empty( $display_filter_args ) ) {
1729
                                foreach ( (array) $display_filter_args as $display_filter_arg ) {
1730
                                    if ( isset( $result[ $display_filter_arg ] ) )
1731
                                        $args[] = $result[ $display_filter_arg ];
1732
                                }
1733
                            }
1734
1735
                            $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args );
1736
                        }
1737
1738
                        if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1739
                            $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
1740
                        elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
1741
                            $result[ $search_data->field_index ] = '(No Title)';
1742
1743
                        if ( 'admin_ajax_relationship' == $context ) {
1744
                            $items[] = array(
1745
                                'id' => $result[ $search_data->field_id ],
1746
                                'text' => $result[ $search_data->field_index ],
1747
                                'image' => ''
1748
                            );
1749
                        }
1750
                        else
1751
                            $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
1752
1753
                        $ids[] = $result[ $search_data->field_id ];
1754
                    }
1755
                }
1756
            }
1757
1758
			if ( $simple && 'admin_ajax_relationship' == $context ) {
1759
				$found_data = array();
1760
1761
				foreach ( $data as $k => $v ) {
1762
					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) {
1763
						$found_data[ $k ] = $v;
1764
					}
1765
				}
1766
1767
				$data = $found_data;
1768
			}
1769
        }
1770
1771
        if ( 'admin_ajax_relationship' == $context ) {
1772
            if ( empty( $items ) && !empty( $data ) ) {
1773
                foreach ( $data as $k => $v ) {
1774
                    $items[] = array(
1775
                        'id' => $k,
1776
                        'text' => $v,
1777
                        'image' => ''
1778
                    );
1779
                }
1780
            }
1781
1782
            return $items;
1783
        }
1784
1785
        return $data;
1786
    }
1787
1788
    /**
1789
     * AJAX call to refresh relationship field markup (supports adding new records modally)
1790
     *
1791
     * @since 2.7
1792
     */
1793
    public function admin_ajax_relationship_popup () {
1794
1795
        $data = pods_unslash( (array) $_POST );
1796
1797
        // Get the field information
1798
        $params = array(
1799
            'pod_id' => $data[ 'pod_id' ],
1800
            'id'     => $data[ 'field_id' ]
1801
        );
1802
        $field = pods_api()->load_field( $params );
1803
1804
        // Get Pods object for this item
1805
        $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] );
1806
1807
        // Get the relationship field's value(s)
1808
        $field_name = $field[ 'name' ];
1809
        $params = array(
1810
            'name'    => $field_name,
1811
            'in_form' => true
1812
        );
1813
        $value = $pod->field( $params);
1814
1815
        // Build the markup and return it to the caller
1816
        $meta_field_name = 'pods_meta_' . $field_name;
1817
        $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] );
1818
        echo $output;
1819
1820
        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...
1821
    }
1822
1823
    /**
1824
     * Handle autocomplete AJAX
1825
     *
1826
     * @since 2.3
1827
     */
1828
    public function admin_ajax_relationship () {
1829
		pods_session_start();
1830
1831
        // Sanitize input
1832
        $params = pods_unslash( (array) $_POST );
1833
1834 View Code Duplication
        foreach ( $params as $key => $value ) {
1835
            if ( 'action' == $key )
1836
                continue;
1837
1838
            unset( $params[ $key ] );
1839
1840
            $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
1841
        }
1842
1843
        $params = (object) $params;
1844
1845
        $uid = @session_id();
1846
1847
        if ( is_user_logged_in() )
1848
            $uid = 'user_' . get_current_user_id();
1849
1850
        $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
1851
1852 View Code Duplication
        if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
1853
            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...
1854
1855
        $api = pods_api();
1856
1857
        $pod = $api->load_pod( array( 'id' => (int) $params->pod ) );
1858
        $field = $api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
1859
        $id = (int) $params->id;
1860
1861
        $limit = 15;
1862
1863
        if ( isset( $params->limit ) )
1864
            $limit = (int) $params->limit;
1865
1866
        $page = 1;
1867
1868
        if ( isset( $params->page ) )
1869
            $page = (int) $params->page;
1870
1871
        if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
1872
            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...
1873 View Code Duplication
        elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
1874
            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...
1875
        elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
1876
            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...
1877 View Code Duplication
        elseif ( 'single' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_single', $field ) )
1878
            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...
1879 View Code Duplication
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $field ) )
1880
            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...
1881
1882
        $object_params = array(
1883
            'name' => $field[ 'name' ], // The name of the field
1884
            'value' => null, // The value of the field
1885
            'options' => array_merge( $field, $field[ 'options' ] ), // Field options
1886
            'pod' => $pod, // Pod data
1887
            'id' => $id, // Item ID
1888
            'context' => 'admin_ajax_relationship', // Data context
1889
            'data_params' => $params,
1890
            'page' => $page,
1891
            'limit' => $limit
1892
        );
1893
1894
        $pick_data = apply_filters( 'pods_field_pick_data_ajax', null, $field[ 'name' ], null, $field, $pod, $id );
1895
1896
        if ( null !== $pick_data )
1897
            $items = $pick_data;
1898
        else
1899
            $items = $this->get_object_data( $object_params );
1900
1901
        if ( !empty( $items ) && isset( $items[ 0 ] ) && !is_array( $items[ 0 ] ) ) {
1902
            $new_items = array();
1903
1904
            foreach ( $items as $id => $text ) {
1905
                $new_items[] = array(
1906
                    'id' => $id,
1907
                    'text' => $text,
1908
                    'image' => ''
1909
                );
1910
            }
1911
1912
            $items = $new_items;
1913
        }
1914
1915
        $items = apply_filters( 'pods_field_pick_data_ajax_items', $items, $field[ 'name' ], null, $field, $pod, $id );
1916
1917
        $items = array(
1918
            'results' => $items
1919
        );
1920
1921
        wp_send_json( $items );
1922
1923
        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...
1924
    }
1925
1926
    /**
1927
     * Data callback for Post Stati
1928
     *
1929
     * @param string $name The name of the field
1930
     * @param string|array $value The value of the field
1931
     * @param array $options Field options
1932
     * @param array $pod Pod data
1933
     * @param int $id Item ID
1934
     *
1935
     * @return array
1936
     *
1937
     * @since 2.3
1938
     */
1939
    public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1940
        $data = array();
1941
1942
        $post_stati = get_post_stati( array(), 'objects' );
1943
1944
        foreach ( $post_stati as $post_status ) {
1945
            $data[ $post_status->name ] = $post_status->label;
1946
        }
1947
1948
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1949
    }
1950
1951
    /**
1952
     * Data callback for User Roles
1953
     *
1954
     * @param string $name The name of the field
1955
     * @param string|array $value The value of the field
1956
     * @param array $options Field options
1957
     * @param array $pod Pod data
1958
     * @param int $id Item ID
1959
     *
1960
     * @return array
1961
     *
1962
     * @since 2.3
1963
     */
1964
    public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1965
        $data = array();
1966
1967
        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...
1968
1969
        foreach ( $wp_roles->role_objects as $key => $role ) {
1970
            $data[ $key ] = $wp_roles->role_names[ $key ];
1971
        }
1972
1973
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1974
    }
1975
1976
    /**
1977
     * Data callback for User Capabilities
1978
     *
1979
     * @param string $name The name of the field
1980
     * @param string|array $value The value of the field
1981
     * @param array $options Field options
1982
     * @param array $pod Pod data
1983
     * @param int $id Item ID
1984
     *
1985
     * @return array
1986
     *
1987
     * @since 2.3
1988
     */
1989
    public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1990
        $data = array();
1991
1992
        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...
1993
1994
        $default_caps = array(
1995
            'activate_plugins',
1996
            'add_users',
1997
            'create_users',
1998
            'delete_others_pages',
1999
            'delete_others_posts',
2000
            'delete_pages',
2001
            'delete_plugins',
2002
            'delete_posts',
2003
            'delete_private_pages',
2004
            'delete_private_posts',
2005
            'delete_published_pages',
2006
            'delete_published_posts',
2007
            'delete_users',
2008
            'edit_dashboard',
2009
            'edit_files',
2010
            'edit_others_pages',
2011
            'edit_others_posts',
2012
            'edit_pages',
2013
            'edit_plugins',
2014
            'edit_posts',
2015
            'edit_private_pages',
2016
            'edit_private_posts',
2017
            'edit_published_pages',
2018
            'edit_published_posts',
2019
            'edit_theme_options',
2020
            'edit_themes',
2021
            'edit_users',
2022
            'import',
2023
            'install_plugins',
2024
            'install_themes',
2025
            'list_users',
2026
            'manage_categories',
2027
            'manage_links',
2028
            'manage_options',
2029
            'moderate_comments',
2030
            'promote_users',
2031
            'publish_pages',
2032
            'publish_posts',
2033
            'read',
2034
            'read_private_pages',
2035
            'read_private_posts',
2036
            'remove_users',
2037
            'switch_themes',
2038
            'unfiltered_html',
2039
            'unfiltered_upload',
2040
            'update_core',
2041
            'update_plugins',
2042
            'update_themes',
2043
            'upload_files'
2044
        );
2045
2046
        $role_caps = array();
2047
2048 View Code Duplication
        foreach ( $wp_roles->role_objects as $key => $role ) {
2049
            if ( is_array( $role->capabilities ) ) {
2050
                foreach ( $role->capabilities as $cap => $grant ) {
2051
                    $role_caps[ $cap ] = $cap;
2052
                }
2053
            }
2054
        }
2055
2056
        $role_caps = array_unique( $role_caps );
2057
2058
        $capabilities = array_merge( $default_caps, $role_caps );
2059
2060
        // To support Members filters
2061
        $capabilities = apply_filters( 'members_get_capabilities', $capabilities );
2062
2063
        $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities );
2064
2065
        sort( $capabilities );
2066
2067
        $capabilities = array_unique( $capabilities );
2068
2069
        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...
2070
2071
        foreach ( $capabilities as $capability ) {
2072
            $data[ $capability ] = $capability;
2073
        }
2074
2075
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2076
    }
2077
2078
    /**
2079
     * Data callback for Image Sizes
2080
     *
2081
     * @param string $name The name of the field
2082
     * @param string|array $value The value of the field
2083
     * @param array $options Field options
2084
     * @param array $pod Pod data
2085
     * @param int $id Item ID
2086
     *
2087
     * @return array
2088
     *
2089
     * @since 2.3
2090
     */
2091
    public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2092
        $data = array();
2093
2094
        $image_sizes = get_intermediate_image_sizes();
2095
2096
        foreach ( $image_sizes as $image_size ) {
2097
            $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
2098
        }
2099
2100
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2101
    }
2102
2103
    /**
2104
     * Data callback for Countries
2105
     *
2106
     * @param string $name The name of the field
2107
     * @param string|array $value The value of the field
2108
     * @param array $options Field options
2109
     * @param array $pod Pod data
2110
     * @param int $id Item ID
2111
     *
2112
     * @return array
2113
     *
2114
     * @since 2.3
2115
     */
2116
    public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2117
        $data = array(
2118
            'AF' => __( 'Afghanistan' ),
2119
            'AL' => __( 'Albania' ),
2120
            'DZ' => __( 'Algeria' ),
2121
            'AS' => __( 'American Samoa' ),
2122
            'AD' => __( 'Andorra' ),
2123
            'AO' => __( 'Angola' ),
2124
            'AI' => __( 'Anguilla' ),
2125
            'AQ' => __( 'Antarctica' ),
2126
            'AG' => __( 'Antigua and Barbuda' ),
2127
            'AR' => __( 'Argentina' ),
2128
            'AM' => __( 'Armenia' ),
2129
            'AW' => __( 'Aruba' ),
2130
            'AU' => __( 'Australia' ),
2131
            'AT' => __( 'Austria' ),
2132
            'AZ' => __( 'Azerbaijan' ),
2133
            'BS' => __( 'Bahamas' ),
2134
            'BH' => __( 'Bahrain' ),
2135
            'BD' => __( 'Bangladesh' ),
2136
            'BB' => __( 'Barbados' ),
2137
            'BY' => __( 'Belarus' ),
2138
            'BE' => __( 'Belgium' ),
2139
            'BZ' => __( 'Belize' ),
2140
            'BJ' => __( 'Benin' ),
2141
            'BM' => __( 'Bermuda' ),
2142
            'BT' => __( 'Bhutan' ),
2143
            'BO' => __( 'Bolivia' ),
2144
            'BA' => __( 'Bosnia and Herzegovina' ),
2145
            'BW' => __( 'Botswana' ),
2146
            'BV' => __( 'Bouvet Island' ),
2147
            'BR' => __( 'Brazil' ),
2148
            'BQ' => __( 'British Antarctic Territory' ),
2149
            'IO' => __( 'British Indian Ocean Territory' ),
2150
            'VG' => __( 'British Virgin Islands' ),
2151
            'BN' => __( 'Brunei' ),
2152
            'BG' => __( 'Bulgaria' ),
2153
            'BF' => __( 'Burkina Faso' ),
2154
            'BI' => __( 'Burundi' ),
2155
            'KH' => __( 'Cambodia' ),
2156
            'CM' => __( 'Cameroon' ),
2157
            'CA' => __( 'Canada' ),
2158
            'CT' => __( 'Canton and Enderbury Islands' ),
2159
            'CV' => __( 'Cape Verde' ),
2160
            'KY' => __( 'Cayman Islands' ),
2161
            'CF' => __( 'Central African Republic' ),
2162
            'TD' => __( 'Chad' ),
2163
            'CL' => __( 'Chile' ),
2164
            'CN' => __( 'China' ),
2165
            'CX' => __( 'Christmas Island' ),
2166
            'CC' => __( 'Cocos [Keeling] Islands' ),
2167
            'CO' => __( 'Colombia' ),
2168
            'KM' => __( 'Comoros' ),
2169
            'CG' => __( 'Congo - Brazzaville' ),
2170
            'CD' => __( 'Congo - Kinshasa' ),
2171
            'CK' => __( 'Cook Islands' ),
2172
            'CR' => __( 'Costa Rica' ),
2173
            'HR' => __( 'Croatia' ),
2174
            'CU' => __( 'Cuba' ),
2175
            'CY' => __( 'Cyprus' ),
2176
            'CZ' => __( 'Czech Republic' ),
2177
            'CI' => __( 'Côte d’Ivoire' ),
2178
            'DK' => __( 'Denmark' ),
2179
            'DJ' => __( 'Djibouti' ),
2180
            'DM' => __( 'Dominica' ),
2181
            'DO' => __( 'Dominican Republic' ),
2182
            'NQ' => __( 'Dronning Maud Land' ),
2183
            'DD' => __( 'East Germany' ),
2184
            'EC' => __( 'Ecuador' ),
2185
            'EG' => __( 'Egypt' ),
2186
            'SV' => __( 'El Salvador' ),
2187
            'GQ' => __( 'Equatorial Guinea' ),
2188
            'ER' => __( 'Eritrea' ),
2189
            'EE' => __( 'Estonia' ),
2190
            'ET' => __( 'Ethiopia' ),
2191
            'FK' => __( 'Falkland Islands' ),
2192
            'FO' => __( 'Faroe Islands' ),
2193
            'FJ' => __( 'Fiji' ),
2194
            'FI' => __( 'Finland' ),
2195
            'FR' => __( 'France' ),
2196
            'GF' => __( 'French Guiana' ),
2197
            'PF' => __( 'French Polynesia' ),
2198
            'TF' => __( 'French Southern Territories' ),
2199
            'FQ' => __( 'French Southern and Antarctic Territories' ),
2200
            'GA' => __( 'Gabon' ),
2201
            'GM' => __( 'Gambia' ),
2202
            'GE' => __( 'Georgia' ),
2203
            'DE' => __( 'Germany' ),
2204
            'GH' => __( 'Ghana' ),
2205
            'GI' => __( 'Gibraltar' ),
2206
            'GR' => __( 'Greece' ),
2207
            'GL' => __( 'Greenland' ),
2208
            'GD' => __( 'Grenada' ),
2209
            'GP' => __( 'Guadeloupe' ),
2210
            'GU' => __( 'Guam' ),
2211
            'GT' => __( 'Guatemala' ),
2212
            'GG' => __( 'Guernsey' ),
2213
            'GN' => __( 'Guinea' ),
2214
            'GW' => __( 'Guinea-Bissau' ),
2215
            'GY' => __( 'Guyana' ),
2216
            'HT' => __( 'Haiti' ),
2217
            'HM' => __( 'Heard Island and McDonald Islands' ),
2218
            'HN' => __( 'Honduras' ),
2219
            'HK' => __( 'Hong Kong SAR China' ),
2220
            'HU' => __( 'Hungary' ),
2221
            'IS' => __( 'Iceland' ),
2222
            'IN' => __( 'India' ),
2223
            'ID' => __( 'Indonesia' ),
2224
            'IR' => __( 'Iran' ),
2225
            'IQ' => __( 'Iraq' ),
2226
            'IE' => __( 'Ireland' ),
2227
            'IM' => __( 'Isle of Man' ),
2228
            'IL' => __( 'Israel' ),
2229
            'IT' => __( 'Italy' ),
2230
            'JM' => __( 'Jamaica' ),
2231
            'JP' => __( 'Japan' ),
2232
            'JE' => __( 'Jersey' ),
2233
            'JT' => __( 'Johnston Island' ),
2234
            'JO' => __( 'Jordan' ),
2235
            'KZ' => __( 'Kazakhstan' ),
2236
            'KE' => __( 'Kenya' ),
2237
            'KI' => __( 'Kiribati' ),
2238
            'KW' => __( 'Kuwait' ),
2239
            'KG' => __( 'Kyrgyzstan' ),
2240
            'LA' => __( 'Laos' ),
2241
            'LV' => __( 'Latvia' ),
2242
            'LB' => __( 'Lebanon' ),
2243
            'LS' => __( 'Lesotho' ),
2244
            'LR' => __( 'Liberia' ),
2245
            'LY' => __( 'Libya' ),
2246
            'LI' => __( 'Liechtenstein' ),
2247
            'LT' => __( 'Lithuania' ),
2248
            'LU' => __( 'Luxembourg' ),
2249
            'MO' => __( 'Macau SAR China' ),
2250
            'MK' => __( 'Macedonia' ),
2251
            'MG' => __( 'Madagascar' ),
2252
            'MW' => __( 'Malawi' ),
2253
            'MY' => __( 'Malaysia' ),
2254
            'MV' => __( 'Maldives' ),
2255
            'ML' => __( 'Mali' ),
2256
            'MT' => __( 'Malta' ),
2257
            'MH' => __( 'Marshall Islands' ),
2258
            'MQ' => __( 'Martinique' ),
2259
            'MR' => __( 'Mauritania' ),
2260
            'MU' => __( 'Mauritius' ),
2261
            'YT' => __( 'Mayotte' ),
2262
            'FX' => __( 'Metropolitan France' ),
2263
            'MX' => __( 'Mexico' ),
2264
            'FM' => __( 'Micronesia' ),
2265
            'MI' => __( 'Midway Islands' ),
2266
            'MD' => __( 'Moldova' ),
2267
            'MC' => __( 'Monaco' ),
2268
            'MN' => __( 'Mongolia' ),
2269
            'ME' => __( 'Montenegro' ),
2270
            'MS' => __( 'Montserrat' ),
2271
            'MA' => __( 'Morocco' ),
2272
            'MZ' => __( 'Mozambique' ),
2273
            'MM' => __( 'Myanmar [Burma]' ),
2274
            'NA' => __( 'Namibia' ),
2275
            'NR' => __( 'Nauru' ),
2276
            'NP' => __( 'Nepal' ),
2277
            'NL' => __( 'Netherlands' ),
2278
            'AN' => __( 'Netherlands Antilles' ),
2279
            'NT' => __( 'Neutral Zone' ),
2280
            'NC' => __( 'New Caledonia' ),
2281
            'NZ' => __( 'New Zealand' ),
2282
            'NI' => __( 'Nicaragua' ),
2283
            'NE' => __( 'Niger' ),
2284
            'NG' => __( 'Nigeria' ),
2285
            'NU' => __( 'Niue' ),
2286
            'NF' => __( 'Norfolk Island' ),
2287
            'KP' => __( 'North Korea' ),
2288
            'VD' => __( 'North Vietnam' ),
2289
            'MP' => __( 'Northern Mariana Islands' ),
2290
            'NO' => __( 'Norway' ),
2291
            'OM' => __( 'Oman' ),
2292
            'PC' => __( 'Pacific Islands Trust Territory' ),
2293
            'PK' => __( 'Pakistan' ),
2294
            'PW' => __( 'Palau' ),
2295
            'PS' => __( 'Palestinian Territories' ),
2296
            'PA' => __( 'Panama' ),
2297
            'PZ' => __( 'Panama Canal Zone' ),
2298
            'PG' => __( 'Papua New Guinea' ),
2299
            'PY' => __( 'Paraguay' ),
2300
            'YD' => __( "People's Democratic Republic of Yemen" ),
2301
            'PE' => __( 'Peru' ),
2302
            'PH' => __( 'Philippines' ),
2303
            'PN' => __( 'Pitcairn Islands' ),
2304
            'PL' => __( 'Poland' ),
2305
            'PT' => __( 'Portugal' ),
2306
            'PR' => __( 'Puerto Rico' ),
2307
            'QA' => __( 'Qatar' ),
2308
            'RO' => __( 'Romania' ),
2309
            'RU' => __( 'Russia' ),
2310
            'RW' => __( 'Rwanda' ),
2311
            'RE' => __( 'Réunion' ),
2312
            'BL' => __( 'Saint Barthélemy' ),
2313
            'SH' => __( 'Saint Helena' ),
2314
            'KN' => __( 'Saint Kitts and Nevis' ),
2315
            'LC' => __( 'Saint Lucia' ),
2316
            'MF' => __( 'Saint Martin' ),
2317
            'PM' => __( 'Saint Pierre and Miquelon' ),
2318
            'VC' => __( 'Saint Vincent and the Grenadines' ),
2319
            'WS' => __( 'Samoa' ),
2320
            'SM' => __( 'San Marino' ),
2321
            'SA' => __( 'Saudi Arabia' ),
2322
            'SN' => __( 'Senegal' ),
2323
            'RS' => __( 'Serbia' ),
2324
            'CS' => __( 'Serbia and Montenegro' ),
2325
            'SC' => __( 'Seychelles' ),
2326
            'SL' => __( 'Sierra Leone' ),
2327
            'SG' => __( 'Singapore' ),
2328
            'SK' => __( 'Slovakia' ),
2329
            'SI' => __( 'Slovenia' ),
2330
            'SB' => __( 'Solomon Islands' ),
2331
            'SO' => __( 'Somalia' ),
2332
            'ZA' => __( 'South Africa' ),
2333
            'GS' => __( 'South Georgia and the South Sandwich Islands' ),
2334
            'KR' => __( 'South Korea' ),
2335
            'ES' => __( 'Spain' ),
2336
            'LK' => __( 'Sri Lanka' ),
2337
            'SD' => __( 'Sudan' ),
2338
            'SR' => __( 'Suriname' ),
2339
            'SJ' => __( 'Svalbard and Jan Mayen' ),
2340
            'SZ' => __( 'Swaziland' ),
2341
            'SE' => __( 'Sweden' ),
2342
            'CH' => __( 'Switzerland' ),
2343
            'SY' => __( 'Syria' ),
2344
            'ST' => __( 'São Tomé and Príncipe' ),
2345
            'TW' => __( 'Taiwan' ),
2346
            'TJ' => __( 'Tajikistan' ),
2347
            'TZ' => __( 'Tanzania' ),
2348
            'TH' => __( 'Thailand' ),
2349
            'TL' => __( 'Timor-Leste' ),
2350
            'TG' => __( 'Togo' ),
2351
            'TK' => __( 'Tokelau' ),
2352
            'TO' => __( 'Tonga' ),
2353
            'TT' => __( 'Trinidad and Tobago' ),
2354
            'TN' => __( 'Tunisia' ),
2355
            'TR' => __( 'Turkey' ),
2356
            'TM' => __( 'Turkmenistan' ),
2357
            'TC' => __( 'Turks and Caicos Islands' ),
2358
            'TV' => __( 'Tuvalu' ),
2359
            'UM' => __( 'U.S. Minor Outlying Islands' ),
2360
            'PU' => __( 'U.S. Miscellaneous Pacific Islands' ),
2361
            'VI' => __( 'U.S. Virgin Islands' ),
2362
            'UG' => __( 'Uganda' ),
2363
            'UA' => __( 'Ukraine' ),
2364
            'SU' => __( 'Union of Soviet Socialist Republics' ),
2365
            'AE' => __( 'United Arab Emirates' ),
2366
            'GB' => __( 'United Kingdom' ),
2367
            'US' => __( 'United States' ),
2368
            'ZZ' => __( 'Unknown or Invalid Region' ),
2369
            'UY' => __( 'Uruguay' ),
2370
            'UZ' => __( 'Uzbekistan' ),
2371
            'VU' => __( 'Vanuatu' ),
2372
            'VA' => __( 'Vatican City' ),
2373
            'VE' => __( 'Venezuela' ),
2374
            'VN' => __( 'Vietnam' ),
2375
            'WK' => __( 'Wake Island' ),
2376
            'WF' => __( 'Wallis and Futuna' ),
2377
            'EH' => __( 'Western Sahara' ),
2378
            'YE' => __( 'Yemen' ),
2379
            'ZM' => __( 'Zambia' ),
2380
            'ZW' => __( 'Zimbabwe' ),
2381
            'AX' => __( 'Åland Islands' )
2382
        );
2383
2384
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2385
    }
2386
2387
    /**
2388
     * Data callback for US States
2389
     *
2390
     * @param string $name The name of the field
2391
     * @param string|array $value The value of the field
2392
     * @param array $options Field options
2393
     * @param array $pod Pod data
2394
     * @param int $id Item ID
2395
     *
2396
     * @return array
2397
     *
2398
     * @since 2.3
2399
     */
2400
    public function data_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2401
        $data = array(
2402
            'AL' => __( 'Alabama' ),
2403
            'AK' => __( 'Alaska' ),
2404
            'AZ' => __( 'Arizona' ),
2405
            'AR' => __( 'Arkansas' ),
2406
            'CA' => __( 'California' ),
2407
            'CO' => __( 'Colorado' ),
2408
            'CT' => __( 'Connecticut' ),
2409
            'DE' => __( 'Delaware' ),
2410
            'DC' => __( 'District Of Columbia' ),
2411
            'FL' => __( 'Florida' ),
2412
            'GA' => __( 'Georgia' ),
2413
            'HI' => __( 'Hawaii' ),
2414
            'ID' => __( 'Idaho' ),
2415
            'IL' => __( 'Illinois' ),
2416
            'IN' => __( 'Indiana' ),
2417
            'IA' => __( 'Iowa' ),
2418
            'KS' => __( 'Kansas' ),
2419
            'KY' => __( 'Kentucky' ),
2420
            'LA' => __( 'Louisiana' ),
2421
            'ME' => __( 'Maine' ),
2422
            'MD' => __( 'Maryland' ),
2423
            'MA' => __( 'Massachusetts' ),
2424
            'MI' => __( 'Michigan' ),
2425
            'MN' => __( 'Minnesota' ),
2426
            'MS' => __( 'Mississippi' ),
2427
            'MO' => __( 'Missouri' ),
2428
            'MT' => __( 'Montana' ),
2429
            'NE' => __( 'Nebraska' ),
2430
            'NV' => __( 'Nevada' ),
2431
            'NH' => __( 'New Hampshire' ),
2432
            'NJ' => __( 'New Jersey' ),
2433
            'NM' => __( 'New Mexico' ),
2434
            'NY' => __( 'New York' ),
2435
            'NC' => __( 'North Carolina' ),
2436
            'ND' => __( 'North Dakota' ),
2437
            'OH' => __( 'Ohio' ),
2438
            'OK' => __( 'Oklahoma' ),
2439
            'OR' => __( 'Oregon' ),
2440
            'PA' => __( 'Pennsylvania' ),
2441
            'RI' => __( 'Rhode Island' ),
2442
            'SC' => __( 'South Carolina' ),
2443
            'SD' => __( 'South Dakota' ),
2444
            'TN' => __( 'Tennessee' ),
2445
            'TX' => __( 'Texas' ),
2446
            'UT' => __( 'Utah' ),
2447
            'VT' => __( 'Vermont' ),
2448
            'VA' => __( 'Virginia' ),
2449
            'WA' => __( 'Washington' ),
2450
            'WV' => __( 'West Virginia' ),
2451
            'WI' => __( 'Wisconsin' ),
2452
            'WY' => __( 'Wyoming' )
2453
        );
2454
2455
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2456
    }
2457
2458
    /**
2459
     * Data callback for US States
2460
     *
2461
     * @param string $name The name of the field
2462
     * @param string|array $value The value of the field
2463
     * @param array $options Field options
2464
     * @param array $pod Pod data
2465
     * @param int $id Item ID
2466
     *
2467
     * @return array
2468
     *
2469
     * @since 2.3
2470
     */
2471
    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...
2472
2473
		/**
2474
		 * @var WP_Locale
2475
		 */
2476
		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...
2477
2478
		return $wp_locale->weekday;
2479
2480
    }
2481
2482
    /**
2483
     * Data callback for US States
2484
     *
2485
     * @param string $name The name of the field
2486
     * @param string|array $value The value of the field
2487
     * @param array $options Field options
2488
     * @param array $pod Pod data
2489
     * @param int $id Item ID
2490
     *
2491
     * @return array
2492
     *
2493
     * @since 2.3
2494
     */
2495
    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...
2496
2497
		/**
2498
		 * @var WP_Locale
2499
		 */
2500
		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...
2501
2502
		return $wp_locale->month;
2503
2504
    }
2505
}
2506