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

PodsField_Pick::validate()   F

Complexity

Conditions 26
Paths 1272

Size

Total Lines 104
Code Lines 60

Duplication

Lines 7
Ratio 6.73 %
Metric Value
dl 7
loc 104
rs 2
cc 26
eloc 60
nc 1272
nop 7

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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