Completed
Pull Request — 2.x (#3260)
by Phil
05:35
created

PodsField_Pick::admin_ajax_relationship_popup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

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