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

PodsField_Pick::admin_ajax_relationship_popup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
/**
3
 * @package Pods\Fields
4
 */
5
class PodsField_Pick extends PodsField {
6
7
    /**
8
     * Field Type Group
9
     *
10
     * @var string
11
     * @since 2.0
12
     */
13
    public static $group = 'Relationships / Media';
14
15
    /**
16
     * Field Type Identifier
17
     *
18
     * @var string
19
     * @since 2.0
20
     */
21
    public static $type = 'pick';
22
23
    /**
24
     * Field Type Label
25
     *
26
     * @var string
27
     * @since 2.0
28
     */
29
    public static $label = 'Relationship';
30
31
    /**
32
     * Available Related Objects
33
     *
34
     * @var array
35
     * @since 2.3
36
     */
37
    public static $related_objects = array();
38
39
    /**
40
     * Custom Related Objects
41
     *
42
     * @var array
43
     * @since 2.3
44
     */
45
    public static $custom_related_objects = array();
46
47
    /**
48
     * Data used during validate / save to avoid extra queries
49
     *
50
     * @var array
51
     * @since 2.3
52
     */
53
    public static $related_data = array();
54
55
    /**
56
     * Data used during input method (mainly for autocomplete)
57
     *
58
     * @var array
59
     * @since 2.3
60
     */
61
    public static $field_data = array();
62
63
    /**
64
     * API caching for fields that need it during validate/save
65
     *
66
     * @var \PodsAPI
67
     * @since 2.3
68
     */
69
    protected static $api = false;
70
71
	/**
72
	 * Saved array of simple relationship names
73
	 *
74
	 * @var array
75
	 * @since 2.5
76
	 */
77
	private static $names_simple = null;
78
79
	/**
80
	 * Saved array of relationship names
81
	 *
82
	 * @var array
83
	 * @since 2.5
84
	 */
85
	private static $names_related = null;
86
87
	/**
88
	 * Saved array of bidirectional relationship names
89
	 *
90
	 * @var array
91
	 * @since 2.5
92
	 */
93
	private static $names_bidirectional = null;
94
95
   /**
96
     * Setup related objects list
97
     *
98
     * @since 2.0
99
     */
100
    public function __construct () {
101
102
    }
103
104
    /**
105
     * Add admin_init actions
106
     *
107
     * @since 2.3
108
     */
109
    public function admin_init () {
110
        //--!! Prototype testing only
111
        add_action( 'wp_ajax_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
112
        add_action( 'wp_ajax_nopriv_pods_relationship_popup', array( $this, 'admin_ajax_relationship_popup' ) );
113
114
        // AJAX for Relationship lookups
115
        add_action( 'wp_ajax_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
116
        add_action( 'wp_ajax_nopriv_pods_relationship', array( $this, 'admin_ajax_relationship' ) );
117
    }
118
119
    /**
120
     * Add options and set defaults to
121
     *
122
     * @return array
123
     *
124
     * @since 2.0
125
     */
126
    public function options () {
127
        $options = array(
128
            self::$type . '_format_type' => array(
129
                'label' => __( 'Selection Type', 'pods' ),
130
                'help' => __( 'help', 'pods' ),
131
                'default' => 'single',
132
                'type' => 'pick',
133
                'data' => array(
134
                    'single' => __( 'Single Select', 'pods' ),
135
                    'multi' => __( 'Multiple Select', 'pods' )
136
                ),
137
                'dependency' => true
138
            ),
139
            self::$type . '_format_single' => array(
140
                'label' => __( 'Format', 'pods' ),
141
                'help' => __( 'help', 'pods' ),
142
                'depends-on' => array( self::$type . '_format_type' => 'single' ),
143
                'default' => 'dropdown',
144
                'type' => 'pick',
145
                'data' => apply_filters(
146
                    'pods_form_ui_field_pick_format_single_options',
147
                    array(
148
                        'dropdown' => __( 'Drop Down', 'pods' ),
149
                        'radio' => __( 'Radio Buttons', 'pods' ),
150
                        'autocomplete' => __( 'Autocomplete', 'pods' )
151
                    ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() )
152
                ),
153
                'dependency' => true
154
            ),
155
            self::$type . '_format_multi' => array(
156
                'label' => __( 'Format', 'pods' ),
157
                'help' => __( 'help', 'pods' ),
158
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
159
                'default' => 'checkbox',
160
                'type' => 'pick',
161
                'data' => apply_filters(
162
                    'pods_form_ui_field_pick_format_multi_options',
163
                    array(
164
                        'checkbox' => __( 'Checkboxes', 'pods' ),
165
                        'multiselect' => __( 'Multi Select', 'pods' ),
166
                        'autocomplete' => __( 'Autocomplete', 'pods' )
167
                    ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() )
168
                ),
169
                'dependency' => true
170
            ),
171
            self::$type . '_taggable' => array(
172
                'label' => __( 'Taggable', 'pods' ),
173
                'help' => __( 'Allow new values to be inserted when using an Autocomplete field', 'pods' ),
174
                'excludes-on' => array(
175
					self::$type . '_format_single' => array( 'dropdown', 'radio' ),
176
					self::$type . '_format_multi' => array( 'checkbox', 'multiselect' ),
177
                    self::$type . '_object' => array_merge(
178
                        array( 'site', 'network' ),
179
                        self::simple_objects()
180
                    )
181
				),
182
                'type' => 'boolean',
183
                'default' => 0
184
            ),
185
			self::$type . '_select_text' => array(
186
                'label' => __( 'Default Select Text', 'pods' ),
187
                'help' => __( 'This is the text use for the default "no selection" dropdown item, if empty, it will default to "-- Select One --"', 'pods' ),
188
                'depends-on' => array(
189
					self::$type . '_format_type' => 'single',
190
					self::$type . '_format_single' => 'dropdown'
191
				),
192
                'default' => '',
193
                'type' => 'text'
194
			),
195
            self::$type . '_limit' => array(
196
                'label' => __( 'Selection Limit', 'pods' ),
197
                'help' => __( 'help', 'pods' ),
198
                'depends-on' => array( self::$type . '_format_type' => 'multi' ),
199
                'default' => 0,
200
                'type' => 'number'
201
            ),
202
			self::$type . '_allow_html' => array(
203
				'label' => __('Allow HTML','pods'),
204
				'type' => 'boolean',
205
				'default' => 0
206
			),
207
            self::$type . '_table_id' => array(
208
                'label' => __( 'Table ID Column', 'pods' ),
209
                'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ),
210
                'depends-on' => array( self::$type . '_object' => 'table' ),
211
                'required' => 1,
212
                'default' => '',
213
                'type' => 'text'
214
            ),
215
            self::$type . '_table_index' => array(
216
                'label' => __( 'Table Index Column', 'pods' ),
217
                'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ),
218
                'depends-on' => array( self::$type . '_object' => 'table' ),
219
                'required' => 1,
220
                'default' => '',
221
                'type' => 'text'
222
            ),
223
            self::$type . '_display' => array(
224
                'label' => __( 'Display Field in Selection List', 'pods' ),
225
                'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ),
226
                'excludes-on' => array(
227
                    self::$type . '_object' => array_merge(
228
                        array( 'site', 'network' ),
229
                        self::simple_objects()
230
                    )
231
                ),
232
                'default' => '',
233
                'type' => 'text'
234
            ),
235
            self::$type . '_user_role' => array(
236
                'label' => __( 'Limit list to Role(s)', 'pods' ),
237
                'help' => __( 'help', 'pods' ),
238
                'depends-on' => array( self::$type . '_object' => 'user' ),
239
                'default' => '',
240
                'type' => 'pick',
241
                'pick_object' => 'role',
242
                'pick_format_type' => 'multi'
243
            ),/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
244
            self::$type . '_user_site' => array(
245
                'label' => __( 'Limit list to Site(s)', 'pods' ),
246
                'help' => __( 'help', 'pods' ),
247
                'depends-on' => array( self::$type . '_object' => 'user' ),
248
                'default' => '',
249
                'type' => 'pick',
250
                'pick_object' => 'site',
251
                'pick_format_type' => 'multi'
252
            ),*/
253
            self::$type . '_where' => array(
254
                'label' => __( 'Customized <em>WHERE</em>', 'pods' ),
255
                'help' => __( 'help', 'pods' ),
256
                'excludes-on' => array(
257
                    self::$type . '_object' => array_merge(
258
                        array( 'site', 'network' ),
259
                        self::simple_objects()
260
                    )
261
                ),
262
                'default' => '',
263
                'type' => 'text'
264
            ),
265
            self::$type . '_orderby' => array(
266
                'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ),
267
                'help' => __( 'help', 'pods' ),
268
                'excludes-on' => array(
269
                    self::$type . '_object' => array_merge(
270
                        array( 'site', 'network' ),
271
                        self::simple_objects()
272
                    )
273
                ),
274
                'default' => '',
275
                'type' => 'text'
276
            ),
277
            self::$type . '_groupby' => array(
278
                'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ),
279
                'help' => __( 'help', 'pods' ),
280
                'excludes-on' => array(
281
                    self::$type . '_object' => array_merge(
282
                        array( 'site', 'network' ),
283
                        self::simple_objects()
284
                    )
285
                ),
286
                'default' => '',
287
                'type' => 'text'
288
            )
289
            /*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
290
            self::$type . '_size' => array(
291
                'label' => __( 'Field Size', 'pods' ),
292
                'default' => 'medium',
293
                'type' => 'pick',
294
                'data' => array(
295
                    'small' => __( 'Small', 'pods' ),
296
                    'medium' => __( 'Medium', 'pods' ),
297
                    'large' => __( 'Large', 'pods' )
298
                )
299
            )*/
300
        );
301
302
        /*if ( !is_multisite() )
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
303
            unset( $options[ self::$type . '_user_site' ] );*/
304
305
        return $options;
306
    }
307
308
    /**
309
     * Register a related object
310
     *
311
     * @param string $name Object name
312
     * @param string $label Object label
313
     * @param array $options Object options
314
     *
315
     * @return array|boolean Object array or false if unsuccessful
316
     * @since 2.3
317
     */
318
    public function register_related_object ( $name, $label, $options = null ) {
319
        if ( empty( $name ) || empty( $label ) )
320
            return false;
321
322
        $related_object = array(
323
            'label' => $label,
324
            'group' => 'Custom Relationships',
325
            'simple' => true,
326
            'bidirectional' => false,
327
            'data' => array(),
328
            'data_callback' => null
329
        );
330
331
        $related_object = array_merge( $related_object, $options );
332
333
        self::$custom_related_objects[ $name ] = $related_object;
334
335
        return true;
336
    }
337
338
    /**
339
     * Setup related objects
340
     *
341
     * @param boolean $force Whether to force refresh of related objects
342
     * @return bool True when data has been loaded
343
     * @since 2.3
344
     */
345
    public function setup_related_objects ( $force = false ) {
346
        $new_data_loaded = false;
347
348
        if ( ! $force && empty( self::$related_objects ) ) {
349
            // Only load transient if we aren't forcing a refresh
350
            self::$related_objects = pods_transient_get( 'pods_related_objects' );
351
            if ( false !== self::$related_objects ) {
352
                $new_data_loaded = true;
353
            }
354
        } elseif ( $force ) {
355
	        // If we are rebuilding, make sure we start with a clean slate
356
	        self::$related_objects = array();
357
        }
358
359
        if ( empty( self::$related_objects ) ) {
360
                // Do a complete build of related_objects
361
            $new_data_loaded = true;
362
363
            // Custom
364
            self::$related_objects[ 'custom-simple' ] = array(
365
                'label' => __( 'Simple (custom defined list)', 'pods' ),
366
                'group' => __( 'Custom', 'pods' ),
367
                'simple' => true
368
            );
369
370
            // Pods
371
            $pod_options = array();
372
373
			// Include PodsMeta if not already included
374
			pods_meta();
375
376
            // Advanced Content Types
377
            $_pods = PodsMeta::$advanced_content_types;
378
379 View Code Duplication
            foreach ( $_pods as $pod ) {
380
                $pod_options[ $pod[ 'name' ] ] = $pod[ 'label' ] . ' (' . $pod[ 'name' ] . ')';
381
            }
382
383
            // Settings
384
            $_pods = PodsMeta::$settings;
385
386 View Code Duplication
            foreach ( $_pods as $pod ) {
387
                $pod_options[ $pod[ 'name' ] ] = $pod[ 'label' ] . ' (' . $pod[ 'name' ] . ')';
388
            }
389
390
            asort( $pod_options );
391
392
            foreach ( $pod_options as $pod => $label ) {
393
                self::$related_objects[ 'pod-' . $pod ] = array(
394
                    'label' => $label,
395
                    'group' => __( 'Pods', 'pods' ),
396
                    'bidirectional' => true
397
                );
398
            }
399
400
            // Post Types
401
            $post_types = get_post_types();
402
            asort( $post_types );
403
404
            $ignore = array( 'attachment', 'revision', 'nav_menu_item' );
405
406 View Code Duplication
            foreach ( $post_types as $post_type => $label ) {
407
                if ( in_array( $post_type, $ignore ) || empty( $post_type ) ) {
408
                    unset( $post_types[ $post_type ] );
409
410
                    continue;
411
                }
412
                elseif ( 0 === strpos( $post_type, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) {
413
                    unset( $post_types[ $post_type ] );
414
415
                    continue;
416
                }
417
418
                $post_type = get_post_type_object( $post_type );
419
420
                self::$related_objects[ 'post_type-' . $post_type->name ] = array(
421
                    'label' => $post_type->label . ' (' . $post_type->name . ')',
422
                    'group' => __( 'Post Types', 'pods' ),
423
                    'bidirectional' => true
424
                );
425
            }
426
427
            // Taxonomies
428
            $taxonomies = get_taxonomies();
429
            asort( $taxonomies );
430
431
            $ignore = array( 'nav_menu', 'post_format' );
432
433 View Code Duplication
            foreach ( $taxonomies as $taxonomy => $label ) {
434
                if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) ) {
435
                    unset( $taxonomies[ $taxonomy ] );
436
437
                    continue;
438
                }
439
440
				/**
441
				 * Prevent ability to extend core Pods content types.
442
				 *
443
				 * @param bool. Default is true, when set to false Pods internal content types can not be extended.
444
				 *
445
				 * @since 2.3.19
446
				 */
447
				elseif ( 0 === strpos( $taxonomy, '_pods_' ) && apply_filters( 'pods_pick_ignore_internal', true ) ) {
448
                    unset( $taxonomies[ $taxonomy ] );
449
450
                    continue;
451
                }
452
453
                $taxonomy = get_taxonomy( $taxonomy );
454
455
                self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array(
456
                    'label' => $taxonomy->label . ' (' . $taxonomy->name . ')',
457
                    'group' => __( 'Taxonomies', 'pods' ),
458
                    'bidirectional' => true
459
                );
460
            }
461
462
            // Other WP Objects
463
            self::$related_objects[ 'user' ] = array(
464
                'label' => __( 'Users', 'pods' ),
465
                'group' => __( 'Other WP Objects', 'pods' ),
466
                'bidirectional' => true
467
            );
468
469
            self::$related_objects[ 'role' ] = array(
470
                'label' => __( 'User Roles', 'pods' ),
471
                'group' => __( 'Other WP Objects', 'pods' ),
472
                'simple' => true,
473
                'data_callback' => array( $this, 'data_roles' )
474
            );
475
476
            self::$related_objects[ 'capability' ] = array(
477
                'label' => __( 'User Capabilities', 'pods' ),
478
                'group' => __( 'Other WP Objects', 'pods' ),
479
                'simple' => true,
480
                'data_callback' => array( $this, 'data_capabilities' )
481
            );
482
483
            self::$related_objects[ 'media' ] = array(
484
                'label' => __( 'Media', 'pods' ),
485
                'group' => __( 'Other WP Objects', 'pods' ),
486
                'bidirectional' => true
487
            );
488
489
            self::$related_objects[ 'comment' ] = array(
490
                'label' => __( 'Comments', 'pods' ),
491
                'group' => __( 'Other WP Objects', 'pods' ),
492
                'bidirectional' => true
493
            );
494
495
            self::$related_objects[ 'image-size' ] = array(
496
                'label' => __( 'Image Sizes', 'pods' ),
497
                'group' => __( 'Other WP Objects', 'pods' ),
498
                'simple' => true,
499
                'data_callback' => array( $this, 'data_image_sizes' )
500
            );
501
502
            self::$related_objects[ 'nav_menu' ] = array(
503
                'label' => __( 'Navigation Menus', 'pods' ),
504
                'group' => __( 'Other WP Objects', 'pods' )
505
            );
506
507
            self::$related_objects[ 'post_format' ] = array(
508
                'label' => __( 'Post Formats', 'pods' ),
509
                'group' => __( 'Other WP Objects', 'pods' )
510
            );
511
512
            self::$related_objects[ 'post-status' ] = array(
513
                'label' => __( 'Post Status', 'pods' ),
514
                'group' => __( 'Other WP Objects', 'pods' ),
515
                'simple' => true,
516
                'data_callback' => array( $this, 'data_post_stati' )
517
            );
518
519
            do_action( 'pods_form_ui_field_pick_related_objects_other' );
520
521
            self::$related_objects[ 'country' ] = array(
522
                'label' => __( 'Countries', 'pods' ),
523
                'group' => __( 'Predefined Lists', 'pods' ),
524
                'simple' => true,
525
                'data_callback' => array( $this, 'data_countries' )
526
            );
527
528
            self::$related_objects[ 'us_state' ] = array(
529
                'label' => __( 'US States', 'pods' ),
530
                'group' => __( 'Predefined Lists', 'pods' ),
531
                'simple' => true,
532
                'data_callback' => array( $this, 'data_us_states' )
533
            );
534
535
            self::$related_objects[ 'days_of_week' ] = array(
536
                'label' => __( 'Calendar - Days of Week', 'pods' ),
537
                'group' => __( 'Predefined Lists', 'pods' ),
538
                'simple' => true,
539
                'data_callback' => array( $this, 'data_days_of_week' )
540
            );
541
542
            self::$related_objects[ 'months_of_year' ] = array(
543
                'label' => __( 'Calendar - Months of Year', 'pods' ),
544
                'group' => __( 'Predefined Lists', 'pods' ),
545
                'simple' => true,
546
                'data_callback' => array( $this, 'data_months_of_year' )
547
            );
548
549
            do_action( 'pods_form_ui_field_pick_related_objects_predefined' );
550
551
            if ( did_action( 'init' ) )
552
                pods_transient_set( 'pods_related_objects', self::$related_objects );
553
        }
554
555
        foreach ( self::$custom_related_objects as $object => $related_object ) {
556
            if ( ! isset( self::$related_objects[ $object ] ) ) {
557
                $new_data_loaded = true;
558
                self::$related_objects[ $object ] = $related_object;
559
            }
560
        }
561
	    return $new_data_loaded;
562
    }
563
564
    /**
565
     * Return available related objects
566
     *
567
     * @param boolean $force Whether to force refresh of related objects
568
     *
569
     * @return array Field selection array
570
     * @since 2.3
571
     */
572
    public function related_objects ( $force = false ) {
573
        if ( $this->setup_related_objects( $force ) || null === self::$names_related ) {
574
	        $related_objects = array();
575
576
	        foreach ( self::$related_objects as $related_object_name => $related_object ) {
577
		        if ( ! isset( $related_objects[ $related_object[ 'group' ] ] ) ) {
578
			        $related_objects[ $related_object[ 'group' ] ] = array();
579
		        }
580
581
		        $related_objects[ $related_object[ 'group' ] ][ $related_object_name ] = $related_object[ 'label' ];
582
	        }
583
584
	        self::$names_related = (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects );
585
        }
586
587
	    return self::$names_related;
588
    }
589
590
    /**
591
     * Return available simple object names
592
     *
593
     * @return array Simple object names
594
     * @since 2.3
595
     */
596 View Code Duplication
    public function simple_objects () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
597
		if ( $this->setup_related_objects() || null === self::$names_simple ) {
598
			$simple_objects = array();
599
600
			foreach ( self::$related_objects as $object => $related_object ) {
601
				if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] )
602
					continue;
603
604
				$simple_objects[] = $object;
605
			}
606
607
			self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects );
608
		}
609
610
	    return self::$names_simple;
611
    }
612
613
    /**
614
     * Return available bidirectional object names
615
     *
616
     * @return array Bidirectional object names
617
     * @since 2.3.4
618
     */
619 View Code Duplication
    public function bidirectional_objects () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
620
        if ( $this->setup_related_objects() || null === self::$names_bidirectional ) {
621
	        $bidirectional_objects = array();
622
623
	        foreach ( self::$related_objects as $object => $related_object ) {
624
		        if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] )
625
			        continue;
626
627
		        $bidirectional_objects[] = $object;
628
	        }
629
630
	        self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects );
631
        }
632
633
	    return self::$names_bidirectional;
634
    }
635
636
    /**
637
     * Define the current field's schema for DB table storage
638
     *
639
     * @param array $options
640
     *
641
     * @return array
642
     * @since 2.0
643
     */
644
    public function schema ( $options = null ) {
645
        $schema = false;
646
647
        $simple_tableless_objects = $this->simple_objects();
648
649
        if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) )
650
            $schema = 'LONGTEXT';
651
652
        return $schema;
653
    }
654
655
    /**
656
     * Change the way the value of the field is displayed with Pods::get
657
     *
658
     * @param mixed $value
659
     * @param string $name
660
     * @param array $options
661
     * @param array $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. Was it maybe removed?

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

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

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

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

Loading history...
662
     * @param array $pod
663
     * @param int $id
664
     *
665
     * @since 2.0
666
     */
667
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
668
        $fields = null;
669
670
        if ( is_object( $pod ) && isset( $pod->fields ) ) {
671
	        $fields = $pod->fields;
672
673
	        if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) {
674
		        $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] );
675
	        }
676 View Code Duplication
        } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) {
677
	        $fields = $pod[ 'fields' ];
678
679
	        if ( ! empty( $pod[ 'object_fields' ] ) ) {
680
		        $fields = array_merge( $fields, $pod[ 'object_fields' ] );
681
	        }
682
        }
683
684
        return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) );
685
    }
686
687
    /**
688
     * Customize output of the form field
689
     *
690
     * @param string $name
691
     * @param mixed $value
692
     * @param array $options
693
     * @param array $pod
694
     * @param int $id
695
     *
696
     * @since 2.0
697
     */
698
    public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
699
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

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

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

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

Loading history...
703
704
        $options[ 'grouped' ] = 1;
705
706
        $options[ 'table_info' ] = array();
707
708
        $custom = pods_var_raw( self::$type . '_custom', $options, false );
709
710
        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
711
712
        $ajax = false;
713
714
        if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) )
715
            $ajax = true;
716
717
        if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) {
718
            $ajax = (boolean) self::$field_data[ 'autocomplete' ];
719
        }
720
721
        $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id );
722
723
        if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) )
724
            $ajax = false;
725
726
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) ) {
727 View Code Duplication
            if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
728
                $field_type = 'select';
729
            elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
730
                $field_type = 'radio';
731
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
732
                $field_type = 'select2';
733
            elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
734
                $field_type = 'flexible';
735
            else {
736
                // Support custom integration
737
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_single', $options, 'dropdown' ), $name, $value, $options, $pod, $id );
738
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
739
                return;
740
            }
741
        }
742
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) ) {
743
            if ( !empty( $value ) && !is_array( $value ) )
744
                $value = explode( ',', $value );
745
746 View Code Duplication
            if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
747
                $field_type = 'checkbox';
748
            elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
749
                $field_type = 'select';
750
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
751
                $field_type = 'select2';
752
            elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
753
                $field_type = 'flexible';
754
            else {
755
                // Support custom integration
756
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_multi', $options, 'checkbox' ), $name, $value, $options, $pod, $id );
757
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
758
                return;
759
            }
760
        }
761
        else {
762
            // Support custom integration
763
            do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
764
            return;
765
        }
766
767
	    if ( 'flexible' == $field_type || ( 'select2' == $field_type && 1 == pods_v( self::$type . '_taggable', $options, 0 ) ) ) {
768
	        // @todo: Fix location when merging
769
	        $field_type = 'pick';
770
	        pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) );
771
	        return;
772
	    }
773
774
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
775
    }
776
777
    /**
778
     * Validate a value before it's saved
779
     *
780
     * @param mixed $value
781
     * @param string $name
782
     * @param array $options
783
     * @param array $fields
784
     * @param array $pod
785
     * @param int $id
786
     *
787
     * @param null $params
788
     * @return array|bool
789
     * @since 2.0
790
     */
791
    public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
792
        if ( empty( self::$api ) )
793
            self::$api = pods_api();
794
795
        $simple_tableless_objects = $this->simple_objects();
796
797
        $related_pick_limit = 0;
798
        $related_field = $related_pod = $current_related_ids = false;
799
800
        // Bidirectional relationship requirement checks
801
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
802
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
803
        if ( empty( $related_val ) ) {
804
            $related_val = $related_object;
805
        }
806
807
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
808
809
        $options[ 'id' ] = (int) $options[ 'id' ];
810
811
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) || empty( self::$related_data[ $options[ 'id' ] ] ) )
812
            self::$related_data[ $options[ 'id' ] ] = array();
813
814
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
815
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
816
817
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
818
                $related_field = false;
819
820
                // Ensure sister_id exists on related Pod
821 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
822
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
823
                        $related_field = $related_pod_field;
824
825
                        break;
826
                    }
827
                }
828
829
                if ( !empty( $related_field ) ) {
830
                    $current_ids = self::$api->lookup_related_items( $fields[ $name ][ 'id' ], $pod[ 'id' ], $id, $fields[ $name ], $pod );
831
832
                    self::$related_data[ $options[ 'id' ] ][ 'current_ids' ] = $current_ids;
833
834
                    $value_ids = $value;
835
836
                    // Convert values from a comma-separated string into an array
837
                    if ( !is_array( $value_ids ) )
838
                        $value_ids = explode( ',', $value_ids );
839
840
                    $value_ids = array_unique( array_filter( $value_ids ) );
841
842
                    // Get ids to remove
843
                    $remove_ids = array_diff( $current_ids, $value_ids );
844
845
                    $related_required = (boolean) pods_var( 'required', $related_field[ 'options' ], 0 );
846
                    $related_pick_limit = (int) pods_var( self::$type . '_limit', $related_field[ 'options' ], 0 );
847
848
                    if ( 'single' == pods_var_raw( self::$type . '_format_type', $related_field[ 'options' ] ) )
849
                        $related_pick_limit = 1;
850
851
                    // Validate Required
852
                    if ( $related_required && !empty( $remove_ids ) ) {
853
                        foreach ( $remove_ids as $related_id ) {
854
                            $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
855
856
                            self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] = $bidirectional_ids;
857
858
                            if ( empty( $bidirectional_ids ) || ( in_array( $id, $bidirectional_ids ) && 1 == count( $bidirectional_ids ) ) )
859
                                return sprintf( __( 'The %s field is required and cannot be removed by the %s field', 'pods' ), $related_field[ 'label' ], $options[ 'label' ] );
860
                        }
861
                    }
862
                }
863
                else
864
                    $related_pod = false;
865
            }
866
            else
867
                $related_pod = false;
868
        }
869
870
        if ( empty( self::$related_data[ $options[ 'id' ] ] ) )
871
            unset( self::$related_data[ $options[ 'id' ] ] );
872
        else {
873
            self::$related_data[ $options[ 'id' ] ][ 'related_pod' ] = $related_pod;
874
            self::$related_data[ $options[ 'id' ] ][ 'related_field' ] = $related_field;
875
            self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ] = $related_pick_limit;
876
877
            $pick_limit = (int) pods_var( self::$type . '_limit', $options[ 'options' ], 0 );
878
879
            if ( 'single' == pods_var_raw( self::$type . '_format_type', $options[ 'options' ] ) )
880
                $pick_limit = 1;
881
882
            $related_field[ 'id' ] = (int) $related_field[ 'id' ];
883
884
            if ( !isset( self::$related_data[ $related_field[ 'id' ] ] ) || empty( self::$related_data[ $related_field[ 'id' ] ] ) ) {
885
                self::$related_data[ $related_field[ 'id' ] ] = array(
886
                    'related_pod' => $pod,
887
                    'related_field' => $options,
888
                    'related_pick_limit' => $pick_limit
889
                );
890
            }
891
        }
892
893
        return true;
894
    }
895
896
    /**
897
     * Save the value to the DB
898
     *
899
     * @param mixed $value
900
     * @param int $id
901
     * @param string $name
902
     * @param array $options
903
     * @param array $fields
904
     * @param array $pod
905
     * @param object $params
906
     *
907
     * @since 2.3
908
     */
909
    public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
910
        if ( empty( self::$api ) )
911
            self::$api = pods_api();
912
913
        $options[ 'id' ] = (int) $options[ 'id' ];
914
915
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) )
916
            return;
917
918
        $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ];
919
        $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ];
920
        $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ];
921
922
        // Bidirectional relationship updates
923
        if ( !empty( $related_field ) ) {
924
            // Don't use no conflict mode unless this isn't the current pod type
925
            $no_conflict = true;
926
927
            if ( $related_pod[ 'type' ] != $pod[ 'type' ] )
928
                $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
929
930
            if ( !$no_conflict )
931
                pods_no_conflict_on( $related_pod[ 'type' ] );
932
933
            $value = array_filter( $value );
934
935
            foreach ( $value as $related_id ) {
936
                if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) )
937
                    $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ];
938
                else
939
                    $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
940
941
                $bidirectional_ids = array_filter( $bidirectional_ids );
942
943
                if ( empty( $bidirectional_ids ) )
944
                    $bidirectional_ids = array();
945
946
                $remove_ids = array();
947
948
                if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) {
949
                    while ( $related_pick_limit <= count( $bidirectional_ids ) ) {
950
                        $remove_ids[] = (int) array_pop( $bidirectional_ids );
951
                    }
952
                }
953
954
                // Remove this item from related items no longer related to
955
                $remove_ids = array_unique( array_filter( $remove_ids ) );
956
957
                // Add to related items
958
                if ( !in_array( $id, $bidirectional_ids ) )
959
                    $bidirectional_ids[] = $id;
960
                // Nothing to change
961
                elseif ( empty( $remove_ids ) )
962
                    continue;
963
964
                self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field );
965
966
                if ( !empty( $remove_ids ) )
967
                    self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options );
968
            }
969
970
            if ( !$no_conflict )
971
                pods_no_conflict_off( $related_pod[ 'type' ] );
972
        }
973
    }
974
975
    /**
976
     * Delete the value from the DB
977
     *
978
     * @param int $id
979
     * @param string $name
980
     * @param array $options
981
     * @param array $pod
982
     *
983
     * @since 2.3
984
     */
985
    public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
986
        if ( empty( self::$api ) )
987
            self::$api = pods_api();
988
989
        $simple_tableless_objects = $this->simple_objects();
990
991
        // Bidirectional relationship requirement checks
992
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
993
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
994
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
995
996
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
997
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
998
999
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
1000
                $related_field = false;
1001
1002
                // Ensure sister_id exists on related Pod
1003 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
1004
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
1005
                        $related_field = $related_pod_field;
1006
1007
                        break;
1008
                    }
1009
                }
1010
1011
                if ( !empty( $related_field ) ) {
1012
                    $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod );
1013
1014
                    if ( !empty( $values ) ) {
1015
                        $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1016
1017
                        if ( !$no_conflict )
1018
                            pods_no_conflict_on( $related_pod[ 'type' ] );
1019
1020
                        self::$api->delete_relationships( $values, $id, $related_pod, $related_field );
1021
1022
                        if ( !$no_conflict )
1023
                            pods_no_conflict_off( $related_pod[ 'type' ] );
1024
                    }
1025
                }
1026
            }
1027
        }
1028
    }
1029
1030
    /**
1031
     * Customize the Pods UI manage table column output
1032
     *
1033
     * @param int $id
1034
     * @param mixed $value
1035
     * @param string $name
1036
     * @param array $options
1037
     * @param array $fields
1038
     * @param array $pod
1039
     *
1040
     * @since 2.0
1041
     */
1042
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
1043
        $value = $this->simple_value( $name, $value, $options, $pod, $id );
1044
1045
        return $this->display( $value, $name, $options, $pod, $id );
1046
    }
1047
1048
    /**
1049
     * Get the data from the field
1050
     *
1051
     * @param string $name The name of the field
1052
     * @param string|array $value The value of the field
1053
     * @param array $options Field options
1054
     * @param array $pod Pod data
1055
     * @param int $id Item ID
1056
     * @param boolean $in_form
1057
     *
1058
     * @return array Array of possible field data
1059
     *
1060
     * @since 2.0
1061
     */
1062
    public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
1063 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1064
            $options = array_merge( $options, $options[ 'options' ] );
1065
1066
            unset( $options[ 'options' ] );
1067
        }
1068
1069
        $data = pods_var_raw( 'data', $options, null, null, true );
1070
1071
        $object_params = array(
1072
            'name' => $name, // The name of the field
1073
            'value' => $value, // The value of the field
1074
            'options' => $options, // Field options
1075
            'pod' => $pod, // Pod data
1076
            'id' => $id, // Item ID
1077
            'context' => 'data', // Data context
1078
        );
1079
1080
        if ( null !== $data )
1081
            $data = (array) $data;
1082
        else
1083
            $data = $this->get_object_data( $object_params );
1084
1085
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1086
            $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data;
1087
1088
        $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
1089
1090
        return $data;
1091
    }
1092
1093
    /**
1094
     * Convert a simple value to the correct value
1095
     *
1096
     * @param string $name The name of the field
1097
     * @param string|array $value The value of the field
1098
     * @param array $options Field options
1099
     * @param array $pod Pod data
1100
     * @param int $id Item ID
1101
     * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false)
1102
     *
1103
     * @return mixed Corrected value
1104
     */
1105
    public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) {
1106
        if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) {
1107 View Code Duplication
            if ( isset( $options[ 'options' ] ) ) {
1108
                $options = array_merge( $options, $options[ 'options' ] );
1109
1110
                unset( $options[ 'options' ] );
1111
            }
1112
1113
            if ( !is_array( $value ) && 0 < strlen( $value ) ) {
1114
                $simple = @json_decode( $value, true );
1115
1116
                if ( is_array( $simple ) )
1117
                    $value = $simple;
1118
            }
1119
1120
            $data = pods_var_raw( 'data', $options, null, null, true );
1121
1122
            $object_params = array(
1123
                'name' => $name, // The name of the field
1124
                'value' => $value, // The value of the field
1125
                'options' => $options, // Field options
1126
                'pod' => $pod, // Pod data
1127
                'id' => $id, // Item ID
1128
                'context' => 'simple_value', // Data context
1129
            );
1130
1131
            if ( null === $data )
1132
                $data = $this->get_object_data( $object_params );
1133
1134
            $data = (array) $data;
1135
1136
            $key = 0;
1137
1138
            if ( is_array( $value ) ) {
1139
                if ( !empty( $data ) ) {
1140
                    $val = array();
1141
1142
                    foreach ( $value as $k => $v ) {
1143
                        if ( isset( $data[ $v ] ) ) {
1144
                            if ( false === $raw ) {
1145
                                $k = $v;
1146
                                $v = $data[ $v ];
1147
                            }
1148
1149
                            $val[ $k ] = $v;
1150
                        }
1151
                    }
1152
1153
                    $value = $val;
1154
                }
1155
            }
1156
            elseif ( isset( $data[ $value ] ) && false === $raw ) {
1157
                $key = $value;
1158
                $value = $data[ $value ];
1159
            }
1160
1161
            $single_multi = pods_var( self::$type . '_format_type', $options, 'single' );
1162
1163
            if ( 'multi' == $single_multi )
1164
                $limit = (int) pods_var( self::$type . '_limit', $options, 0 );
1165
            else
1166
                $limit = 1;
1167
1168
            if ( is_array( $value ) && 0 < $limit ) {
1169
                if ( 1 == $limit )
1170
                    $value = current( $value );
1171
                else
1172
                    $value = array_slice( $value, 0, $limit, true );
1173
            }
1174
            elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) {
1175
                if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) {
1176
                    $value = array(
1177
                        $key => $value
1178
                    );
1179
                }
1180
            }
1181
        }
1182
1183
        return $value;
1184
    }
1185
1186
    /**
1187
     * Get the label from a pick value
1188
     *
1189
     * @param string $name The name of the field
1190
     * @param string|array $value The value of the field
1191
     * @param array $options Field options
1192
     * @param array $pod Pod data
1193
     * @param int $id Item ID
1194
     *
1195
     * @return string
1196
     *
1197
     * @since 2.2
1198
     */
1199
    public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) {
1200 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1201
            $options = array_merge( $options, $options[ 'options' ] );
1202
1203
            unset( $options[ 'options' ] );
1204
        }
1205
1206
        $data = pods_var_raw( 'data', $options, null, null, true );
1207
1208
        $object_params = array(
1209
            'name' => $name, // The name of the field
1210
            'value' => $value, // The value of the field
1211
            'options' => $options, // Field options
1212
            'pod' => $pod, // Pod data
1213
            'id' => $id, // Item ID
1214
            'context' => 'value_to_label', // Data context
1215
        );
1216
1217
        if ( null !== $data )
1218
            $data = (array) $data;
1219
        else
1220
            $data = $this->get_object_data( $object_params );
1221
1222
        $labels = array();
1223
1224
        foreach ( $data as $v => $l ) {
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...
1225
            if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) )
1226
                $labels[] = $l;
1227
        }
1228
1229
        $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id );
1230
1231
        $labels = pods_serial_comma( $labels );
1232
1233
        return $labels;
1234
    }
1235
1236
	/**
1237
	 * Get available items from a relationship field
1238
	 *
1239
	 * @param array|string $field Field array or field name
1240
	 * @param array $options [optional] Field options array overrides
1241
	 * @param array $object_params [optional] Additional get_object_data options
1242
	 *
1243
	 * @return array An array of available items from a relationship field
1244
	 */
1245
	public function get_field_data( $field, $options = array(), $object_params = array() ) {
1246
1247
		// Handle field array overrides
1248
		if ( is_array( $field ) ) {
1249
			$options = array_merge( $field, $options );
1250
		}
1251
1252
		// Get field name from array
1253
		$field = pods_var_raw( 'name', $options, $field, null, true );
1254
1255
		// Field name or options not set
1256
		if ( empty( $field ) || empty( $options ) ) {
1257
			return array();
1258
		}
1259
1260
		// Options normalization
1261
		$options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) );
1262
1263
		// Setup object params
1264
        $object_params = array_merge(
1265
			array(
1266
				'name' => $field, // The name of the field
1267
				'options' => $options, // Field options
1268
			),
1269
			$object_params
1270
        );
1271
1272
		// Get data override
1273
        $data = pods_var_raw( 'data', $options, null, null, true );
1274
1275
		// Return data override
1276
        if ( null !== $data ) {
1277
            $data = (array) $data;
1278
		}
1279
		// Get object data
1280
        else {
1281
            $data = $this->get_object_data( $object_params );
1282
		}
1283
1284
		return $data;
1285
1286
	}
1287
1288
    /**
1289
     * Get data from relationship objects
1290
     *
1291
     * @param array $object_params Object data parameters
1292
     *
1293
     * @return array|bool Object data
1294
     */
1295
    public function get_object_data ( $object_params = null ) {
1296
        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...
1297
1298
        $current_language = false;
1299
1300
        // WPML support
1301 View Code Duplication
        if ( is_object( $sitepress ) && !$icl_adjust_id_url_filter_off )
1302
            $current_language = pods_sanitize( ICL_LANGUAGE_CODE );
1303
        // Polylang support
1304
        elseif ( function_exists( 'pll_current_language' ) )
1305
            $current_language = pll_current_language( 'slug' );
1306
1307
        $object_params = array_merge(
1308
            array(
1309
                'name' => '', // The name of the field
1310
                'value' => '', // The value of the field
1311
                'options' => array(), // Field options
1312
                'pod' => '', // Pod data
1313
                'id' => '', // Item ID
1314
                'context' => '', // Data context
1315
                'data_params' => array(
1316
                    'query' => '' // Query being searched
1317
                ),
1318
                'page' => 1, // Page number of results to get
1319
				'limit' => 0 // How many data items to limit to (autocomplete defaults to 30, set to -1 or 1+ to override)
1320
            ),
1321
            $object_params
1322
        );
1323
1324
        $name = $object_params[ 'name' ];
1325
        $value = $object_params[ 'value' ];
1326
        $options = $object_params[ 'options' ] = (array) $object_params[ 'options' ];
1327
        $pod = $object_params[ 'pod' ];
1328
        $id = $object_params[ 'id' ];
1329
        $context = $object_params[ 'context' ];
1330
        $data_params = $object_params[ 'data_params' ] = (array) $object_params[ 'data_params' ];
1331
        $page = min( 1, (int) $object_params[ 'page' ] );
1332
        $limit = (int) $object_params[ 'limit' ];
1333
1334 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1335
            $options = array_merge( $options, $options[ 'options' ] );
1336
1337
            unset( $options[ 'options' ] );
1338
        }
1339
1340
        $data = apply_filters( 'pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params );
1341
        $items = array();
1342
1343
        if ( !isset( $options[ self::$type . '_object' ] ) )
1344
            $data = pods_var_raw( 'data', $options, array(), null, true );
1345
1346
		$simple = false;
1347
1348
        if ( null === $data ) {
1349
            $data = array();
1350
1351
            if ( 'custom-simple' == $options[ self::$type . '_object' ] ) {
1352
                $custom = pods_var_raw( self::$type . '_custom', $options, '' );
1353
1354
                $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params );
1355
1356
                if ( !empty( $custom ) ) {
1357
                    if ( !is_array( $custom ) ) {
1358
                        $data = array();
1359
1360
                        $custom = explode( "\n", trim( $custom ) );
1361
1362
                        foreach ( $custom as $custom_value ) {
1363
                            $custom_label = explode( '|', $custom_value );
1364
1365
                            if ( empty( $custom_label ) )
1366
                                continue;
1367
1368
                            if ( 1 == count( $custom_label ) )
1369
                                $custom_label = $custom_value;
1370
                            else {
1371
                                $custom_value = $custom_label[ 0 ];
1372
                                $custom_label = $custom_label[ 1 ];
1373
                            }
1374
1375
							$custom_value = trim( (string) $custom_value );
1376
							$custom_label = trim( (string) $custom_label );
1377
1378
                            $data[ $custom_value ] = $custom_label;
1379
                        }
1380
                    }
1381
                    else
1382
                        $data = $custom;
1383
1384
					$simple = true;
1385
                }
1386
            }
1387
            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' ] ) ) {
1388
                $data = self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ];
1389
1390
				$simple = true;
1391
			}
1392
            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' ] ) ) {
1393
                $data = call_user_func_array(
1394
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ],
1395
                    array( $name, $value, $options, $pod, $id )
1396
                );
1397
1398
				$simple = true;
1399
1400
                // Cache data from callback
1401
                if ( !empty( $data ) )
1402
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] = $data;
1403
            }
1404
            elseif ( 'simple_value' != $context ) {
1405
                $pick_val = pods_var( self::$type . '_val', $options );
1406
1407
                if ( 'table' == pods_var( self::$type . '_object', $options ) )
1408
                    $pick_val = pods_var( self::$type . '_table', $options, $pick_val, null, true );
1409
1410 View Code Duplication
                if ( '__current__' == $pick_val ) {
1411
                    if ( is_object( $pod ) )
1412
                        $pick_val = $pod->pod;
1413
                    elseif ( is_array( $pod ) )
1414
                        $pick_val = $pod[ 'name' ];
1415
                    elseif ( 0 < strlen( $pod ) )
1416
                        $pick_val = $pod;
1417
                }
1418
1419
                $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( self::$type . '_object', $options ), $pick_val, null, null, $options );
1420
1421
                $search_data = pods_data();
1422
                $search_data->table( $options[ 'table_info' ] );
1423
1424
                if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
1425
                    $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
1426
                    $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
1427
                }
1428
1429
                $params = array(
1430
                    'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
1431
                    'table' => $search_data->table,
1432
                    'where' => pods_var_raw( self::$type . '_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
1433
                    'orderby' => pods_var_raw( self::$type . '_orderby', $options, null, null, true ),
1434
                    'groupby' => pods_var_raw( self::$type . '_groupby', $options, null, null, true ),
1435
                    //'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...
1436
					'pagination' => false,
1437
					'search' => false
1438
                );
1439
1440
                if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1441
                    $params[ 'select' ] .= ', `t`.`path`';
1442
1443
                if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
1444
                    $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
1445
1446
                if ( empty( $params[ 'where' ] ) || ( !is_array( $params[ 'where' ] ) && strlen( trim( $params[ 'where' ] ) ) < 1 ) )
1447
                    $params[ 'where' ] = array();
1448
                elseif ( !is_array( $params[ 'where' ] ) )
1449
                    $params[ 'where' ] = (array) $params[ 'where' ];
1450
1451
                if ( 'value_to_label' == $context )
1452
                    $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' );
1453
1454
                /* 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...
1455
                if ( !empty( $params[ 'orderby' ] ) )
1456
                    $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
1457
1458
                if ( !empty( $params[ 'groupby' ] ) )
1459
                    $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
1460
1461
                $display = trim( pods_var( self::$type . '_display', $options ), ' {@}' );
1462
1463
                if ( 0 < strlen( $display ) ) {
1464
                    if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
1465
                        if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
1466
                            $search_data->field_index = $display;
1467
1468
                            $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1469
                        }
1470
                        elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
1471
                            $search_data->field_index = $display;
1472
1473
                            if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
1474
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
1475
                            elseif ( 'meta' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] )
1476
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
1477
                            else
1478
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1479
                        }
1480
                    }
1481
                    elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
1482
                        $search_data->field_index = $display;
1483
1484
                        $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1485
                    }
1486
                }
1487
1488
                $autocomplete = false;
1489
1490
                if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1491
                    $autocomplete = true;
1492 View Code Duplication
                elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
1493
                    $autocomplete = true;
1494
1495
                $hierarchy = false;
1496
1497
                if ( 'data' == $context && !$autocomplete ) {
1498
                    if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_single', $options, 'dropdown' ), array( 'dropdown', 'radio' ) ) )
1499
                        $hierarchy = true;
1500 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' ) ) )
1501
                        $hierarchy = true;
1502
                }
1503
1504
                if ( $hierarchy && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) )
1505
                    $params[ 'select' ] .= ', ' . $options[ 'table_info' ][ 'field_parent_select' ];
1506
1507
                if ( $autocomplete ) {
1508
					if ( 0 == $limit ) {
1509
						$limit = 30;
1510
					}
1511
1512
                    $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params );
1513
1514
					if ( is_array( $value ) && $params[ 'limit' ] < count( $value ) ) {
1515
						$params[ 'limit' ] = count( $value );
1516
					}
1517
1518
                    $params[ 'page' ] = $page;
1519
1520
                    if ( 'admin_ajax_relationship' == $context ) {
1521
                        $lookup_where = array(
1522
                            $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"
1523
                        );
1524
1525
                        // @todo Hook into WPML for each table
1526
                        if ( $wpdb->users == $search_data->table ) {
1527
                            $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1528
                            $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1529
                            $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1530
                        }
1531
                        elseif ( $wpdb->posts == $search_data->table ) {
1532
                            $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1533
                            $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1534
                            $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1535
                            $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1536
                        }
1537
                        elseif ( $wpdb->terms == $search_data->table ) {
1538
                            $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1539
                            $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1540
                        }
1541
                        elseif ( $wpdb->comments == $search_data->table ) {
1542
                            $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1543
                            $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1544
                            $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1545
                        }
1546
1547
                        $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 );
1548
1549
                        if ( !empty( $lookup_where ) )
1550
                            $params[ 'where' ][] = implode( ' OR ', $lookup_where );
1551
1552
                        $orderby = array();
1553
                        $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%' ) DESC";
1554
1555
                        $pick_orderby = pods_var_raw( self::$type . '_orderby', $options, null, null, true );
1556
1557
                        if ( 0 < strlen( $pick_orderby ) )
1558
                            $orderby[] = $pick_orderby;
1559
1560
                        $orderby[] = "`t`.`{$search_data->field_index}`";
1561
                        $orderby[] = "`t`.`{$search_data->field_id}`";
1562
1563
                        $params[ 'orderby' ] = $orderby;
1564
                    }
1565
                }
1566
				elseif ( 0 < $limit ) {
1567
                    $params[ 'limit' ] = $limit;
1568
                    $params[ 'page' ] = $page;
1569
				}
1570
1571
                $extra = '';
1572
1573
                if ( $wpdb->posts == $search_data->table )
1574
                    $extra = ', `t`.`post_type`';
1575
                elseif ( $wpdb->terms == $search_data->table )
1576
                    $extra = ', `tt`.`taxonomy`';
1577
                elseif ( $wpdb->comments == $search_data->table )
1578
                    $extra = ', `t`.`comment_type`';
1579
1580
                $params[ 'select' ] .= $extra;
1581
1582
                if ( 'user' == pods_var( self::$type . '_object', $options ) ) {
1583
                    $roles = pods_var( self::$type . '_user_role', $options );
1584
1585
                    if ( !empty( $roles ) ) {
1586
                        $where = array();
1587
1588
                        foreach ( (array) $roles as $role ) {
1589
                            if ( empty( $role ) || ( pods_clean_name( $role ) != $role && sanitize_title( $role ) != $role ) )
1590
                                continue;
1591
1592
                            $where[] = $wpdb->base_prefix . ( ( is_multisite() && !is_main_site() ) ? get_current_blog_id() . '_' : '' ) . 'capabilities.meta_value LIKE "%\"' . pods_sanitize_like( $role ) . '\"%"';
1593
                        }
1594
1595
                        if ( !empty( $where ) ) {
1596
                            $params[ 'where' ][] = implode( ' OR ', $where );
1597
                        }
1598
                    }
1599
                }
1600
1601
                $results = $search_data->select( $params );
1602
1603
                if ( $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
1604
                    if ( !empty( $value ) ) {
1605
                        $ids = $value;
1606
1607
						if ( is_array( $ids ) && isset( $ids[ 0 ] ) && is_array( $ids[ 0 ] ) ) {
1608
							$ids = wp_list_pluck( $ids, $search_data->field_id );
1609
						}
1610
1611
                        if ( is_array( $ids ) )
1612
                            $ids = implode( ', ', $ids );
1613
1614
                        if ( is_array( $params[ 'where' ] ) )
1615
                            $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
1616
                        if ( !empty( $params[ 'where' ] ) )
1617
                            $params[ 'where' ] .= ' AND ';
1618
1619
                        $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
1620
1621
                        $results = $search_data->select( $params );
1622
                    }
1623
                }
1624
                else
1625
                    $autocomplete = false;
1626
1627
                if ( 'data' == $context ) {
1628
                    self::$field_data = array(
1629
                        'field' => $name,
1630
                        'id' => $options[ 'id' ],
1631
                        'autocomplete' => $autocomplete
1632
                    );
1633
                }
1634
1635
                if ( $hierarchy && !$autocomplete && !empty( $results ) && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) {
1636
                    $args = array(
1637
                        'id' => $options[ 'table_info' ][ 'field_id' ],
1638
                        'index' => $options[ 'table_info' ][ 'field_index' ],
1639
                        'parent' => $options[ 'table_info' ][ 'field_parent' ],
1640
                    );
1641
1642
                    $results = pods_hierarchical_select( $results, $args );
1643
                }
1644
1645
                $ids = array();
1646
1647
                if ( !empty( $results ) ) {
1648
                    $display_filter = pods_var( 'display_filter', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) );
1649
1650
                    foreach ( $results as $result ) {
1651
                        $result = get_object_vars( $result );
1652
1653
                        if ( !isset( $result[ $search_data->field_id ] ) || !isset( $result[ $search_data->field_index ] ) )
1654
                            continue;
1655
1656
                        $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
1657
1658
                        $object = $object_type = '';
1659
1660
                        if ( $wpdb->posts == $search_data->table && isset( $result[ 'post_type' ] ) ) {
1661
                            $object = $result[ 'post_type' ];
1662
                            $object_type = 'post_type';
1663
                        }
1664
                        elseif ( $wpdb->terms == $search_data->table && isset( $result[ 'taxonomy' ] ) ) {
1665
                            $object = $result[ 'taxonomy' ];
1666
                            $object_type = 'taxonomy';
1667
                        }
1668
1669
                        // WPML integration for Post Types and Taxonomies
1670
                        if ( is_object( $sitepress ) && in_array( $object_type, array( 'post_type', 'taxonomy' ) ) ) {
1671
                            $translated = false;
1672
1673
                            if ( 'post_type' == $object_type && $sitepress->is_translated_post_type( $object ) )
1674
                                $translated = true;
1675
                            elseif ( 'taxonomy' == $object_type && $sitepress->is_translated_taxonomy( $object ) )
1676
                                $translated = true;
1677
1678
                            if ( $translated ) {
1679
                                $object_id = icl_object_id( $result[ $search_data->field_id ], $object, false, $current_language );
1680
1681
                                if ( 0 < $object_id && !in_array( $object_id, $ids ) ) {
1682
                                    $text = $result[ $search_data->field_index ];
1683
1684
                                    if ( $result[ $search_data->field_id ] != $object_id ) {
1685
                                        if ( $wpdb->posts == $search_data->table )
1686
                                            $text = trim( get_the_title( $object_id ) );
1687
                                        elseif ( $wpdb->terms == $search_data->table )
1688
                                            $text = trim( get_term( $object_id, $object )->name );
1689
                                    }
1690
1691
                                    $result[ $search_data->field_id ] = $object_id;
1692
                                    $result[ $search_data->field_index ] = $text;
1693
                                }
1694
                                else
1695
                                    continue;
1696
                            }
1697
                        }
1698
                        // Polylang integration for Post Types and Taxonomies
1699
                        elseif ( function_exists( 'PLL' ) || ( is_object( $polylang ) ) && in_array( $object_type, array( 'post_type', 'taxonomy' ) ) ) {
1700
                            $translated = false;
1701
1702
                            if ( 'post_type' == $object_type && pll_is_translated_post_type( $object ) )
1703
                                $translated = true;
1704
                            elseif ( 'taxonomy' == $object_type && pll_is_translated_taxonomy( $object ) )
1705
                                $translated = true;
1706
1707
                            if ( $translated ) {
1708
                            	$object_id = 0; // default
1709
                            	if ( function_exists( 'PLL' ) && isset( PLL()->model ) && method_exists( PLL()->model, 'get_translation' ) ) {
1710
                            		// Polylang 1.8 and newer
1711
                            		$object_id = PLL()->model->get_translation( $object, $result[ $search_data->field_id ], $current_language );
1712
                            	} elseif ( is_object( $polylang ) && isset( $polylang->model ) && method_exists( $polylang->model, 'get_translation' ) ) {
1713
                            		// Polylang 1.2 - 1.7.x
1714
                            		$object_id = $polylang->model->get_translation( $object, $result[ $search_data->field_id ], $current_language );
1715
                            	} elseif ( is_object( $polylang ) && method_exists( $polylang, 'get_translation' ) ) {
1716
                            		// Polylang 1.1.x and older
1717
                                	$object_id = $polylang->get_translation( $object, $result[ $search_data->field_id ], $current_language );
1718
                            	}
1719
1720
                                if ( 0 < $object_id && !in_array( $object_id, $ids ) ) {
1721
                                    $text = $result[ $search_data->field_index ];
1722
1723
                                    if ( $result[ $search_data->field_id ] != $object_id ) {
1724
                                        if ( $wpdb->posts == $search_data->table )
1725
                                            $text = trim( get_the_title( $object_id ) );
1726
                                        elseif ( $wpdb->terms == $search_data->table )
1727
                                            $text = trim( get_term( $object_id, $object )->name );
1728
                                    }
1729
1730
                                    $result[ $search_data->field_id ] = $object_id;
1731
                                    $result[ $search_data->field_index ] = $text;
1732
                                }
1733
                                else
1734
                                    continue;
1735
                            }
1736
                        }
1737
1738
                        if ( 0 < strlen( $display_filter ) ) {
1739
                            $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' ] ) ) );
1740
1741
                            $args = array(
1742
                                $display_filter,
1743
                                $result[ $search_data->field_index ]
1744
                            );
1745
1746
                            if ( !empty( $display_filter_args ) ) {
1747
                                foreach ( (array) $display_filter_args as $display_filter_arg ) {
1748
                                    if ( isset( $result[ $display_filter_arg ] ) )
1749
                                        $args[] = $result[ $display_filter_arg ];
1750
                                }
1751
                            }
1752
1753
                            $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args );
1754
                        }
1755
1756
                        if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1757
                            $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
1758
                        elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
1759
                            $result[ $search_data->field_index ] = '(No Title)';
1760
1761
                        if ( 'admin_ajax_relationship' == $context ) {
1762
                            $items[] = array(
1763
                                'id' => $result[ $search_data->field_id ],
1764
                                'text' => $result[ $search_data->field_index ],
1765
                                'image' => ''
1766
                            );
1767
                        }
1768
                        else
1769
                            $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
1770
1771
                        $ids[] = $result[ $search_data->field_id ];
1772
                    }
1773
                }
1774
            }
1775
1776
			if ( $simple && 'admin_ajax_relationship' == $context ) {
1777
				$found_data = array();
1778
1779
				foreach ( $data as $k => $v ) {
1780
					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) {
1781
						$found_data[ $k ] = $v;
1782
					}
1783
				}
1784
1785
				$data = $found_data;
1786
			}
1787
        }
1788
1789
        if ( 'admin_ajax_relationship' == $context ) {
1790
            if ( empty( $items ) && !empty( $data ) ) {
1791
                foreach ( $data as $k => $v ) {
1792
                    $items[] = array(
1793
                        'id' => $k,
1794
                        'text' => $v,
1795
                        'image' => ''
1796
                    );
1797
                }
1798
            }
1799
1800
            return $items;
1801
        }
1802
1803
        return $data;
1804
    }
1805
1806
    /**
1807
     * AJAX call to refresh relationship field markup (supports adding new records modally)
1808
     *
1809
     * @since 2.7
1810
     */
1811
    public function admin_ajax_relationship_popup () {
1812
1813
        $data = pods_unslash( (array) $_POST );
1814
1815
        // Get the field information
1816
        $params = array(
1817
            'pod_id' => $data[ 'pod_id' ],
1818
            'id'     => $data[ 'field_id' ]
1819
        );
1820
        $field = pods_api()->load_field( $params );
1821
1822
        // Get Pods object for this item
1823
        $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] );
1824
1825
        // Get the relationship field's value(s)
1826
        $field_name = $field[ 'name' ];
1827
        $params = array(
1828
            'name'    => $field_name,
1829
            'in_form' => true
1830
        );
1831
        $value = $pod->field( $params);
1832
1833
        // Build the markup and return it to the caller
1834
        $meta_field_name = 'pods_meta_' . $field_name;
1835
        $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] );
1836
        echo $output;
1837
1838
        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...
1839
    }
1840
1841
    /**
1842
     * Handle autocomplete AJAX
1843
     *
1844
     * @since 2.3
1845
     */
1846
    public function admin_ajax_relationship () {
1847
		pods_session_start();
1848
1849
        // Sanitize input
1850
        $params = pods_unslash( (array) $_POST );
1851
1852 View Code Duplication
        foreach ( $params as $key => $value ) {
1853
            if ( 'action' == $key )
1854
                continue;
1855
1856
            unset( $params[ $key ] );
1857
1858
            $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
1859
        }
1860
1861
        $params = (object) $params;
1862
1863
        $uid = @session_id();
1864
1865
        if ( is_user_logged_in() )
1866
            $uid = 'user_' . get_current_user_id();
1867
1868
        $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
1869
1870 View Code Duplication
        if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
1871
            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...
1872
1873
        $api = pods_api();
1874
1875
        $pod = $api->load_pod( array( 'id' => (int) $params->pod ) );
1876
        $field = $api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
1877
        $id = (int) $params->id;
1878
1879
        $limit = 15;
1880
1881
        if ( isset( $params->limit ) )
1882
            $limit = (int) $params->limit;
1883
1884
        $page = 1;
1885
1886
        if ( isset( $params->page ) )
1887
            $page = (int) $params->page;
1888
1889
        if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
1890
            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...
1891 View Code Duplication
        elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
1892
            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...
1893
        elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
1894
            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...
1895 View Code Duplication
        elseif ( 'single' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_single', $field ) )
1896
            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...
1897 View Code Duplication
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $field ) )
1898
            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...
1899
1900
        $object_params = array(
1901
            'name' => $field[ 'name' ], // The name of the field
1902
            'value' => null, // The value of the field
1903
            'options' => array_merge( $field, $field[ 'options' ] ), // Field options
1904
            'pod' => $pod, // Pod data
1905
            'id' => $id, // Item ID
1906
            'context' => 'admin_ajax_relationship', // Data context
1907
            'data_params' => $params,
1908
            'page' => $page,
1909
            'limit' => $limit
1910
        );
1911
1912
        $pick_data = apply_filters( 'pods_field_pick_data_ajax', null, $field[ 'name' ], null, $field, $pod, $id );
1913
1914
        if ( null !== $pick_data )
1915
            $items = $pick_data;
1916
        else
1917
            $items = $this->get_object_data( $object_params );
1918
1919
        if ( !empty( $items ) && isset( $items[ 0 ] ) && !is_array( $items[ 0 ] ) ) {
1920
            $new_items = array();
1921
1922
            foreach ( $items as $id => $text ) {
1923
                $new_items[] = array(
1924
                    'id' => $id,
1925
                    'text' => $text,
1926
                    'image' => ''
1927
                );
1928
            }
1929
1930
            $items = $new_items;
1931
        }
1932
1933
        $items = apply_filters( 'pods_field_pick_data_ajax_items', $items, $field[ 'name' ], null, $field, $pod, $id );
1934
1935
        $items = array(
1936
            'results' => $items
1937
        );
1938
1939
        wp_send_json( $items );
1940
1941
        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...
1942
    }
1943
1944
    /**
1945
     * Data callback for Post Stati
1946
     *
1947
     * @param string $name The name of the field
1948
     * @param string|array $value The value of the field
1949
     * @param array $options Field options
1950
     * @param array $pod Pod data
1951
     * @param int $id Item ID
1952
     *
1953
     * @return array
1954
     *
1955
     * @since 2.3
1956
     */
1957
    public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1958
        $data = array();
1959
1960
        $post_stati = get_post_stati( array(), 'objects' );
1961
1962
        foreach ( $post_stati as $post_status ) {
1963
            $data[ $post_status->name ] = $post_status->label;
1964
        }
1965
1966
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1967
    }
1968
1969
    /**
1970
     * Data callback for User Roles
1971
     *
1972
     * @param string $name The name of the field
1973
     * @param string|array $value The value of the field
1974
     * @param array $options Field options
1975
     * @param array $pod Pod data
1976
     * @param int $id Item ID
1977
     *
1978
     * @return array
1979
     *
1980
     * @since 2.3
1981
     */
1982
    public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1983
        $data = array();
1984
1985
        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...
1986
1987
        foreach ( $wp_roles->role_objects as $key => $role ) {
1988
            $data[ $key ] = $wp_roles->role_names[ $key ];
1989
        }
1990
1991
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1992
    }
1993
1994
    /**
1995
     * Data callback for User Capabilities
1996
     *
1997
     * @param string $name The name of the field
1998
     * @param string|array $value The value of the field
1999
     * @param array $options Field options
2000
     * @param array $pod Pod data
2001
     * @param int $id Item ID
2002
     *
2003
     * @return array
2004
     *
2005
     * @since 2.3
2006
     */
2007
    public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2008
        $data = array();
2009
2010
        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...
2011
2012
        $default_caps = array(
2013
            'activate_plugins',
2014
            'add_users',
2015
            'create_users',
2016
            'delete_others_pages',
2017
            'delete_others_posts',
2018
            'delete_pages',
2019
            'delete_plugins',
2020
            'delete_posts',
2021
            'delete_private_pages',
2022
            'delete_private_posts',
2023
            'delete_published_pages',
2024
            'delete_published_posts',
2025
            'delete_users',
2026
            'edit_dashboard',
2027
            'edit_files',
2028
            'edit_others_pages',
2029
            'edit_others_posts',
2030
            'edit_pages',
2031
            'edit_plugins',
2032
            'edit_posts',
2033
            'edit_private_pages',
2034
            'edit_private_posts',
2035
            'edit_published_pages',
2036
            'edit_published_posts',
2037
            'edit_theme_options',
2038
            'edit_themes',
2039
            'edit_users',
2040
            'import',
2041
            'install_plugins',
2042
            'install_themes',
2043
            'list_users',
2044
            'manage_categories',
2045
            'manage_links',
2046
            'manage_options',
2047
            'moderate_comments',
2048
            'promote_users',
2049
            'publish_pages',
2050
            'publish_posts',
2051
            'read',
2052
            'read_private_pages',
2053
            'read_private_posts',
2054
            'remove_users',
2055
            'switch_themes',
2056
            'unfiltered_html',
2057
            'unfiltered_upload',
2058
            'update_core',
2059
            'update_plugins',
2060
            'update_themes',
2061
            'upload_files'
2062
        );
2063
2064
        $role_caps = array();
2065
2066 View Code Duplication
        foreach ( $wp_roles->role_objects as $key => $role ) {
2067
            if ( is_array( $role->capabilities ) ) {
2068
                foreach ( $role->capabilities as $cap => $grant ) {
2069
                    $role_caps[ $cap ] = $cap;
2070
                }
2071
            }
2072
        }
2073
2074
        $role_caps = array_unique( $role_caps );
2075
2076
        $capabilities = array_merge( $default_caps, $role_caps );
2077
2078
        // To support Members filters
2079
        $capabilities = apply_filters( 'members_get_capabilities', $capabilities );
2080
2081
        $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities );
2082
2083
        sort( $capabilities );
2084
2085
        $capabilities = array_unique( $capabilities );
2086
2087
        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...
2088
2089
        foreach ( $capabilities as $capability ) {
2090
            $data[ $capability ] = $capability;
2091
        }
2092
2093
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2094
    }
2095
2096
    /**
2097
     * Data callback for Image Sizes
2098
     *
2099
     * @param string $name The name of the field
2100
     * @param string|array $value The value of the field
2101
     * @param array $options Field options
2102
     * @param array $pod Pod data
2103
     * @param int $id Item ID
2104
     *
2105
     * @return array
2106
     *
2107
     * @since 2.3
2108
     */
2109
    public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2110
        $data = array();
2111
2112
        $image_sizes = get_intermediate_image_sizes();
2113
2114
        foreach ( $image_sizes as $image_size ) {
2115
            $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
2116
        }
2117
2118
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2119
    }
2120
2121
    /**
2122
     * Data callback for Countries
2123
     *
2124
     * @param string $name The name of the field
2125
     * @param string|array $value The value of the field
2126
     * @param array $options Field options
2127
     * @param array $pod Pod data
2128
     * @param int $id Item ID
2129
     *
2130
     * @return array
2131
     *
2132
     * @since 2.3
2133
     */
2134
    public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2135
        $data = array(
2136
            'AF' => __( 'Afghanistan' ),
2137
            'AL' => __( 'Albania' ),
2138
            'DZ' => __( 'Algeria' ),
2139
            'AS' => __( 'American Samoa' ),
2140
            'AD' => __( 'Andorra' ),
2141
            'AO' => __( 'Angola' ),
2142
            'AI' => __( 'Anguilla' ),
2143
            'AQ' => __( 'Antarctica' ),
2144
            'AG' => __( 'Antigua and Barbuda' ),
2145
            'AR' => __( 'Argentina' ),
2146
            'AM' => __( 'Armenia' ),
2147
            'AW' => __( 'Aruba' ),
2148
            'AU' => __( 'Australia' ),
2149
            'AT' => __( 'Austria' ),
2150
            'AZ' => __( 'Azerbaijan' ),
2151
            'BS' => __( 'Bahamas' ),
2152
            'BH' => __( 'Bahrain' ),
2153
            'BD' => __( 'Bangladesh' ),
2154
            'BB' => __( 'Barbados' ),
2155
            'BY' => __( 'Belarus' ),
2156
            'BE' => __( 'Belgium' ),
2157
            'BZ' => __( 'Belize' ),
2158
            'BJ' => __( 'Benin' ),
2159
            'BM' => __( 'Bermuda' ),
2160
            'BT' => __( 'Bhutan' ),
2161
            'BO' => __( 'Bolivia' ),
2162
            'BA' => __( 'Bosnia and Herzegovina' ),
2163
            'BW' => __( 'Botswana' ),
2164
            'BV' => __( 'Bouvet Island' ),
2165
            'BR' => __( 'Brazil' ),
2166
            'BQ' => __( 'British Antarctic Territory' ),
2167
            'IO' => __( 'British Indian Ocean Territory' ),
2168
            'VG' => __( 'British Virgin Islands' ),
2169
            'BN' => __( 'Brunei' ),
2170
            'BG' => __( 'Bulgaria' ),
2171
            'BF' => __( 'Burkina Faso' ),
2172
            'BI' => __( 'Burundi' ),
2173
            'KH' => __( 'Cambodia' ),
2174
            'CM' => __( 'Cameroon' ),
2175
            'CA' => __( 'Canada' ),
2176
            'CT' => __( 'Canton and Enderbury Islands' ),
2177
            'CV' => __( 'Cape Verde' ),
2178
            'KY' => __( 'Cayman Islands' ),
2179
            'CF' => __( 'Central African Republic' ),
2180
            'TD' => __( 'Chad' ),
2181
            'CL' => __( 'Chile' ),
2182
            'CN' => __( 'China' ),
2183
            'CX' => __( 'Christmas Island' ),
2184
            'CC' => __( 'Cocos [Keeling] Islands' ),
2185
            'CO' => __( 'Colombia' ),
2186
            'KM' => __( 'Comoros' ),
2187
            'CG' => __( 'Congo - Brazzaville' ),
2188
            'CD' => __( 'Congo - Kinshasa' ),
2189
            'CK' => __( 'Cook Islands' ),
2190
            'CR' => __( 'Costa Rica' ),
2191
            'HR' => __( 'Croatia' ),
2192
            'CU' => __( 'Cuba' ),
2193
            'CY' => __( 'Cyprus' ),
2194
            'CZ' => __( 'Czech Republic' ),
2195
            'CI' => __( 'Côte d’Ivoire' ),
2196
            'DK' => __( 'Denmark' ),
2197
            'DJ' => __( 'Djibouti' ),
2198
            'DM' => __( 'Dominica' ),
2199
            'DO' => __( 'Dominican Republic' ),
2200
            'NQ' => __( 'Dronning Maud Land' ),
2201
            'DD' => __( 'East Germany' ),
2202
            'EC' => __( 'Ecuador' ),
2203
            'EG' => __( 'Egypt' ),
2204
            'SV' => __( 'El Salvador' ),
2205
            'GQ' => __( 'Equatorial Guinea' ),
2206
            'ER' => __( 'Eritrea' ),
2207
            'EE' => __( 'Estonia' ),
2208
            'ET' => __( 'Ethiopia' ),
2209
            'FK' => __( 'Falkland Islands' ),
2210
            'FO' => __( 'Faroe Islands' ),
2211
            'FJ' => __( 'Fiji' ),
2212
            'FI' => __( 'Finland' ),
2213
            'FR' => __( 'France' ),
2214
            'GF' => __( 'French Guiana' ),
2215
            'PF' => __( 'French Polynesia' ),
2216
            'TF' => __( 'French Southern Territories' ),
2217
            'FQ' => __( 'French Southern and Antarctic Territories' ),
2218
            'GA' => __( 'Gabon' ),
2219
            'GM' => __( 'Gambia' ),
2220
            'GE' => __( 'Georgia' ),
2221
            'DE' => __( 'Germany' ),
2222
            'GH' => __( 'Ghana' ),
2223
            'GI' => __( 'Gibraltar' ),
2224
            'GR' => __( 'Greece' ),
2225
            'GL' => __( 'Greenland' ),
2226
            'GD' => __( 'Grenada' ),
2227
            'GP' => __( 'Guadeloupe' ),
2228
            'GU' => __( 'Guam' ),
2229
            'GT' => __( 'Guatemala' ),
2230
            'GG' => __( 'Guernsey' ),
2231
            'GN' => __( 'Guinea' ),
2232
            'GW' => __( 'Guinea-Bissau' ),
2233
            'GY' => __( 'Guyana' ),
2234
            'HT' => __( 'Haiti' ),
2235
            'HM' => __( 'Heard Island and McDonald Islands' ),
2236
            'HN' => __( 'Honduras' ),
2237
            'HK' => __( 'Hong Kong SAR China' ),
2238
            'HU' => __( 'Hungary' ),
2239
            'IS' => __( 'Iceland' ),
2240
            'IN' => __( 'India' ),
2241
            'ID' => __( 'Indonesia' ),
2242
            'IR' => __( 'Iran' ),
2243
            'IQ' => __( 'Iraq' ),
2244
            'IE' => __( 'Ireland' ),
2245
            'IM' => __( 'Isle of Man' ),
2246
            'IL' => __( 'Israel' ),
2247
            'IT' => __( 'Italy' ),
2248
            'JM' => __( 'Jamaica' ),
2249
            'JP' => __( 'Japan' ),
2250
            'JE' => __( 'Jersey' ),
2251
            'JT' => __( 'Johnston Island' ),
2252
            'JO' => __( 'Jordan' ),
2253
            'KZ' => __( 'Kazakhstan' ),
2254
            'KE' => __( 'Kenya' ),
2255
            'KI' => __( 'Kiribati' ),
2256
            'KW' => __( 'Kuwait' ),
2257
            'KG' => __( 'Kyrgyzstan' ),
2258
            'LA' => __( 'Laos' ),
2259
            'LV' => __( 'Latvia' ),
2260
            'LB' => __( 'Lebanon' ),
2261
            'LS' => __( 'Lesotho' ),
2262
            'LR' => __( 'Liberia' ),
2263
            'LY' => __( 'Libya' ),
2264
            'LI' => __( 'Liechtenstein' ),
2265
            'LT' => __( 'Lithuania' ),
2266
            'LU' => __( 'Luxembourg' ),
2267
            'MO' => __( 'Macau SAR China' ),
2268
            'MK' => __( 'Macedonia' ),
2269
            'MG' => __( 'Madagascar' ),
2270
            'MW' => __( 'Malawi' ),
2271
            'MY' => __( 'Malaysia' ),
2272
            'MV' => __( 'Maldives' ),
2273
            'ML' => __( 'Mali' ),
2274
            'MT' => __( 'Malta' ),
2275
            'MH' => __( 'Marshall Islands' ),
2276
            'MQ' => __( 'Martinique' ),
2277
            'MR' => __( 'Mauritania' ),
2278
            'MU' => __( 'Mauritius' ),
2279
            'YT' => __( 'Mayotte' ),
2280
            'FX' => __( 'Metropolitan France' ),
2281
            'MX' => __( 'Mexico' ),
2282
            'FM' => __( 'Micronesia' ),
2283
            'MI' => __( 'Midway Islands' ),
2284
            'MD' => __( 'Moldova' ),
2285
            'MC' => __( 'Monaco' ),
2286
            'MN' => __( 'Mongolia' ),
2287
            'ME' => __( 'Montenegro' ),
2288
            'MS' => __( 'Montserrat' ),
2289
            'MA' => __( 'Morocco' ),
2290
            'MZ' => __( 'Mozambique' ),
2291
            'MM' => __( 'Myanmar [Burma]' ),
2292
            'NA' => __( 'Namibia' ),
2293
            'NR' => __( 'Nauru' ),
2294
            'NP' => __( 'Nepal' ),
2295
            'NL' => __( 'Netherlands' ),
2296
            'AN' => __( 'Netherlands Antilles' ),
2297
            'NT' => __( 'Neutral Zone' ),
2298
            'NC' => __( 'New Caledonia' ),
2299
            'NZ' => __( 'New Zealand' ),
2300
            'NI' => __( 'Nicaragua' ),
2301
            'NE' => __( 'Niger' ),
2302
            'NG' => __( 'Nigeria' ),
2303
            'NU' => __( 'Niue' ),
2304
            'NF' => __( 'Norfolk Island' ),
2305
            'KP' => __( 'North Korea' ),
2306
            'VD' => __( 'North Vietnam' ),
2307
            'MP' => __( 'Northern Mariana Islands' ),
2308
            'NO' => __( 'Norway' ),
2309
            'OM' => __( 'Oman' ),
2310
            'PC' => __( 'Pacific Islands Trust Territory' ),
2311
            'PK' => __( 'Pakistan' ),
2312
            'PW' => __( 'Palau' ),
2313
            'PS' => __( 'Palestinian Territories' ),
2314
            'PA' => __( 'Panama' ),
2315
            'PZ' => __( 'Panama Canal Zone' ),
2316
            'PG' => __( 'Papua New Guinea' ),
2317
            'PY' => __( 'Paraguay' ),
2318
            'YD' => __( "People's Democratic Republic of Yemen" ),
2319
            'PE' => __( 'Peru' ),
2320
            'PH' => __( 'Philippines' ),
2321
            'PN' => __( 'Pitcairn Islands' ),
2322
            'PL' => __( 'Poland' ),
2323
            'PT' => __( 'Portugal' ),
2324
            'PR' => __( 'Puerto Rico' ),
2325
            'QA' => __( 'Qatar' ),
2326
            'RO' => __( 'Romania' ),
2327
            'RU' => __( 'Russia' ),
2328
            'RW' => __( 'Rwanda' ),
2329
            'RE' => __( 'Réunion' ),
2330
            'BL' => __( 'Saint Barthélemy' ),
2331
            'SH' => __( 'Saint Helena' ),
2332
            'KN' => __( 'Saint Kitts and Nevis' ),
2333
            'LC' => __( 'Saint Lucia' ),
2334
            'MF' => __( 'Saint Martin' ),
2335
            'PM' => __( 'Saint Pierre and Miquelon' ),
2336
            'VC' => __( 'Saint Vincent and the Grenadines' ),
2337
            'WS' => __( 'Samoa' ),
2338
            'SM' => __( 'San Marino' ),
2339
            'SA' => __( 'Saudi Arabia' ),
2340
            'SN' => __( 'Senegal' ),
2341
            'RS' => __( 'Serbia' ),
2342
            'CS' => __( 'Serbia and Montenegro' ),
2343
            'SC' => __( 'Seychelles' ),
2344
            'SL' => __( 'Sierra Leone' ),
2345
            'SG' => __( 'Singapore' ),
2346
            'SK' => __( 'Slovakia' ),
2347
            'SI' => __( 'Slovenia' ),
2348
            'SB' => __( 'Solomon Islands' ),
2349
            'SO' => __( 'Somalia' ),
2350
            'ZA' => __( 'South Africa' ),
2351
            'GS' => __( 'South Georgia and the South Sandwich Islands' ),
2352
            'KR' => __( 'South Korea' ),
2353
            'ES' => __( 'Spain' ),
2354
            'LK' => __( 'Sri Lanka' ),
2355
            'SD' => __( 'Sudan' ),
2356
            'SR' => __( 'Suriname' ),
2357
            'SJ' => __( 'Svalbard and Jan Mayen' ),
2358
            'SZ' => __( 'Swaziland' ),
2359
            'SE' => __( 'Sweden' ),
2360
            'CH' => __( 'Switzerland' ),
2361
            'SY' => __( 'Syria' ),
2362
            'ST' => __( 'São Tomé and Príncipe' ),
2363
            'TW' => __( 'Taiwan' ),
2364
            'TJ' => __( 'Tajikistan' ),
2365
            'TZ' => __( 'Tanzania' ),
2366
            'TH' => __( 'Thailand' ),
2367
            'TL' => __( 'Timor-Leste' ),
2368
            'TG' => __( 'Togo' ),
2369
            'TK' => __( 'Tokelau' ),
2370
            'TO' => __( 'Tonga' ),
2371
            'TT' => __( 'Trinidad and Tobago' ),
2372
            'TN' => __( 'Tunisia' ),
2373
            'TR' => __( 'Turkey' ),
2374
            'TM' => __( 'Turkmenistan' ),
2375
            'TC' => __( 'Turks and Caicos Islands' ),
2376
            'TV' => __( 'Tuvalu' ),
2377
            'UM' => __( 'U.S. Minor Outlying Islands' ),
2378
            'PU' => __( 'U.S. Miscellaneous Pacific Islands' ),
2379
            'VI' => __( 'U.S. Virgin Islands' ),
2380
            'UG' => __( 'Uganda' ),
2381
            'UA' => __( 'Ukraine' ),
2382
            'SU' => __( 'Union of Soviet Socialist Republics' ),
2383
            'AE' => __( 'United Arab Emirates' ),
2384
            'GB' => __( 'United Kingdom' ),
2385
            'US' => __( 'United States' ),
2386
            'ZZ' => __( 'Unknown or Invalid Region' ),
2387
            'UY' => __( 'Uruguay' ),
2388
            'UZ' => __( 'Uzbekistan' ),
2389
            'VU' => __( 'Vanuatu' ),
2390
            'VA' => __( 'Vatican City' ),
2391
            'VE' => __( 'Venezuela' ),
2392
            'VN' => __( 'Vietnam' ),
2393
            'WK' => __( 'Wake Island' ),
2394
            'WF' => __( 'Wallis and Futuna' ),
2395
            'EH' => __( 'Western Sahara' ),
2396
            'YE' => __( 'Yemen' ),
2397
            'ZM' => __( 'Zambia' ),
2398
            'ZW' => __( 'Zimbabwe' ),
2399
            'AX' => __( 'Åland Islands' )
2400
        );
2401
2402
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2403
    }
2404
2405
    /**
2406
     * Data callback for US States
2407
     *
2408
     * @param string $name The name of the field
2409
     * @param string|array $value The value of the field
2410
     * @param array $options Field options
2411
     * @param array $pod Pod data
2412
     * @param int $id Item ID
2413
     *
2414
     * @return array
2415
     *
2416
     * @since 2.3
2417
     */
2418
    public function data_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2419
        $data = array(
2420
            'AL' => __( 'Alabama' ),
2421
            'AK' => __( 'Alaska' ),
2422
            'AZ' => __( 'Arizona' ),
2423
            'AR' => __( 'Arkansas' ),
2424
            'CA' => __( 'California' ),
2425
            'CO' => __( 'Colorado' ),
2426
            'CT' => __( 'Connecticut' ),
2427
            'DE' => __( 'Delaware' ),
2428
            'DC' => __( 'District Of Columbia' ),
2429
            'FL' => __( 'Florida' ),
2430
            'GA' => __( 'Georgia' ),
2431
            'HI' => __( 'Hawaii' ),
2432
            'ID' => __( 'Idaho' ),
2433
            'IL' => __( 'Illinois' ),
2434
            'IN' => __( 'Indiana' ),
2435
            'IA' => __( 'Iowa' ),
2436
            'KS' => __( 'Kansas' ),
2437
            'KY' => __( 'Kentucky' ),
2438
            'LA' => __( 'Louisiana' ),
2439
            'ME' => __( 'Maine' ),
2440
            'MD' => __( 'Maryland' ),
2441
            'MA' => __( 'Massachusetts' ),
2442
            'MI' => __( 'Michigan' ),
2443
            'MN' => __( 'Minnesota' ),
2444
            'MS' => __( 'Mississippi' ),
2445
            'MO' => __( 'Missouri' ),
2446
            'MT' => __( 'Montana' ),
2447
            'NE' => __( 'Nebraska' ),
2448
            'NV' => __( 'Nevada' ),
2449
            'NH' => __( 'New Hampshire' ),
2450
            'NJ' => __( 'New Jersey' ),
2451
            'NM' => __( 'New Mexico' ),
2452
            'NY' => __( 'New York' ),
2453
            'NC' => __( 'North Carolina' ),
2454
            'ND' => __( 'North Dakota' ),
2455
            'OH' => __( 'Ohio' ),
2456
            'OK' => __( 'Oklahoma' ),
2457
            'OR' => __( 'Oregon' ),
2458
            'PA' => __( 'Pennsylvania' ),
2459
            'RI' => __( 'Rhode Island' ),
2460
            'SC' => __( 'South Carolina' ),
2461
            'SD' => __( 'South Dakota' ),
2462
            'TN' => __( 'Tennessee' ),
2463
            'TX' => __( 'Texas' ),
2464
            'UT' => __( 'Utah' ),
2465
            'VT' => __( 'Vermont' ),
2466
            'VA' => __( 'Virginia' ),
2467
            'WA' => __( 'Washington' ),
2468
            'WV' => __( 'West Virginia' ),
2469
            'WI' => __( 'Wisconsin' ),
2470
            'WY' => __( 'Wyoming' )
2471
        );
2472
2473
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2474
    }
2475
2476
    /**
2477
     * Data callback for US States
2478
     *
2479
     * @param string $name The name of the field
2480
     * @param string|array $value The value of the field
2481
     * @param array $options Field options
2482
     * @param array $pod Pod data
2483
     * @param int $id Item ID
2484
     *
2485
     * @return array
2486
     *
2487
     * @since 2.3
2488
     */
2489
    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...
2490
2491
		/**
2492
		 * @var WP_Locale
2493
		 */
2494
		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...
2495
2496
		return $wp_locale->weekday;
2497
2498
    }
2499
2500
    /**
2501
     * Data callback for US States
2502
     *
2503
     * @param string $name The name of the field
2504
     * @param string|array $value The value of the field
2505
     * @param array $options Field options
2506
     * @param array $pod Pod data
2507
     * @param int $id Item ID
2508
     *
2509
     * @return array
2510
     *
2511
     * @since 2.3
2512
     */
2513
    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...
2514
2515
		/**
2516
		 * @var WP_Locale
2517
		 */
2518
		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...
2519
2520
		return $wp_locale->month;
2521
2522
    }
2523
}
2524