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

PodsField_Pick::options()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 190
Code Lines 134

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 190
rs 8.1463
cc 5
eloc 134
nc 1
nop 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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

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

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

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

Loading history...
299
            self::$type . '_size' => array(
300
                'label' => __( 'Field Size', 'pods' ),
301
                'default' => 'medium',
302
                'type' => 'pick',
303
                'data' => array(
304
                    'small' => __( 'Small', 'pods' ),
305
                    'medium' => __( 'Medium', 'pods' ),
306
                    'large' => __( 'Large', 'pods' )
307
                )
308
            )*/
309
        );
310
311
        /*if ( !is_multisite() )
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

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

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

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

Loading history...
712
713
        $options[ 'grouped' ] = 1;
714
715
        $options[ 'table_info' ] = array();
716
717
        $custom = pods_var_raw( self::$type . '_custom', $options, false );
718
719
        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
720
721
        $ajax = false;
722
723
        if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) )
724
            $ajax = true;
725
726
        if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) {
727
            $ajax = (boolean) self::$field_data[ 'autocomplete' ];
728
        }
729
730
        $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id );
731
732
        if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) )
733
            $ajax = false;
734
735
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) ) {
736 View Code Duplication
            if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
737
                $field_type = 'select';
738
            elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
739
                $field_type = 'radio';
740
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
741
                $field_type = 'select2';
742
            else {
743
                // Support custom integration
744
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_single', $options, 'dropdown' ), $name, $value, $options, $pod, $id );
745
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
746
                return;
747
            }
748
        }
749
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) ) {
750
            if ( !empty( $value ) && !is_array( $value ) )
751
                $value = explode( ',', $value );
752
753 View Code Duplication
            if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
754
                $field_type = 'checkbox';
755
            elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
756
                $field_type = 'select';
757
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
758
                $field_type = 'select2';
759
            else {
760
                // Support custom integration
761
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_multi', $options, 'checkbox' ), $name, $value, $options, $pod, $id );
762
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
763
                return;
764
            }
765
        }
766
        else {
767
            // Support custom integration
768
            do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
769
            return;
770
        }
771
772
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
773
774
        echo PodsForm::flex_relationship( $options, $id );
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 View Code Duplication
                            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 ( is_object( $polylang ) && in_array( $object_type, array( 'post_type', 'taxonomy' ) ) && method_exists( $polylang, 'get_translation' ) ) {
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 View Code Duplication
                            if ( $translated ) {
1708
                                $object_id = $polylang->get_translation( $object, $result[ $search_data->field_id ], $current_language );
1709
1710
                                if ( 0 < $object_id && !in_array( $object_id, $ids ) ) {
1711
                                    $text = $result[ $search_data->field_index ];
1712
1713
                                    if ( $result[ $search_data->field_id ] != $object_id ) {
1714
                                        if ( $wpdb->posts == $search_data->table )
1715
                                            $text = trim( get_the_title( $object_id ) );
1716
                                        elseif ( $wpdb->terms == $search_data->table )
1717
                                            $text = trim( get_term( $object_id, $object )->name );
1718
                                    }
1719
1720
                                    $result[ $search_data->field_id ] = $object_id;
1721
                                    $result[ $search_data->field_index ] = $text;
1722
                                }
1723
                                else
1724
                                    continue;
1725
                            }
1726
                        }
1727
1728
                        if ( 0 < strlen( $display_filter ) ) {
1729
                            $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' ] ) ) );
1730
1731
                            $args = array(
1732
                                $display_filter,
1733
                                $result[ $search_data->field_index ]
1734
                            );
1735
1736
                            if ( !empty( $display_filter_args ) ) {
1737
                                foreach ( (array) $display_filter_args as $display_filter_arg ) {
1738
                                    if ( isset( $result[ $display_filter_arg ] ) )
1739
                                        $args[] = $result[ $display_filter_arg ];
1740
                                }
1741
                            }
1742
1743
                            $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args );
1744
                        }
1745
1746
                        if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1747
                            $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
1748
                        elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
1749
                            $result[ $search_data->field_index ] = '(No Title)';
1750
1751
                        if ( 'admin_ajax_relationship' == $context ) {
1752
                            $items[] = array(
1753
                                'id' => $result[ $search_data->field_id ],
1754
                                'text' => $result[ $search_data->field_index ],
1755
                                'image' => ''
1756
                            );
1757
                        }
1758
                        else
1759
                            $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
1760
1761
                        $ids[] = $result[ $search_data->field_id ];
1762
                    }
1763
                }
1764
            }
1765
1766
			if ( $simple && 'admin_ajax_relationship' == $context ) {
1767
				$found_data = array();
1768
1769
				foreach ( $data as $k => $v ) {
1770
					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) {
1771
						$found_data[ $k ] = $v;
1772
					}
1773
				}
1774
1775
				$data = $found_data;
1776
			}
1777
        }
1778
1779
        if ( 'admin_ajax_relationship' == $context ) {
1780
            if ( empty( $items ) && !empty( $data ) ) {
1781
                foreach ( $data as $k => $v ) {
1782
                    $items[] = array(
1783
                        'id' => $k,
1784
                        'text' => $v,
1785
                        'image' => ''
1786
                    );
1787
                }
1788
            }
1789
1790
            return $items;
1791
        }
1792
1793
        return $data;
1794
    }
1795
1796
    /**
1797
     * AJAX call to refresh relationship field markup (supports adding new records modally)
1798
     *
1799
     * @since 2.7
1800
     */
1801
    public function admin_ajax_relationship_popup () {
1802
1803
        $data = pods_unslash( (array) $_POST );
1804
1805
        // Get the field information
1806
        $params = array(
1807
            'pod_id' => $data[ 'pod_id' ],
1808
            'id'     => $data[ 'field_id' ]
1809
        );
1810
        $field = pods_api()->load_field( $params );
1811
1812
        // Get Pods object for this item
1813
        $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] );
1814
1815
        // Get the relationship field's value(s)
1816
        $field_name = $field[ 'name' ];
1817
        $params = array(
1818
            'name'    => $field_name,
1819
            'in_form' => true
1820
        );
1821
        $value = $pod->field( $params);
1822
1823
        // Build the markup and return it to the caller
1824
        $meta_field_name = 'pods_meta_' . $field_name;
1825
        $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] );
1826
        echo $output;
1827
1828
        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...
1829
    }
1830
1831
    /**
1832
     * Handle autocomplete AJAX
1833
     *
1834
     * @since 2.3
1835
     */
1836
    public function admin_ajax_relationship () {
1837
		pods_session_start();
1838
1839
        // Sanitize input
1840
        $params = pods_unslash( (array) $_POST );
1841
1842 View Code Duplication
        foreach ( $params as $key => $value ) {
1843
            if ( 'action' == $key )
1844
                continue;
1845
1846
            unset( $params[ $key ] );
1847
1848
            $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
1849
        }
1850
1851
        $params = (object) $params;
1852
1853
        $uid = @session_id();
1854
1855
        if ( is_user_logged_in() )
1856
            $uid = 'user_' . get_current_user_id();
1857
1858
        $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
1859
1860 View Code Duplication
        if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
1861
            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...
1862
1863
        $api = pods_api();
1864
1865
        $pod = $api->load_pod( array( 'id' => (int) $params->pod ) );
1866
        $field = $api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
1867
        $id = (int) $params->id;
1868
1869
        $limit = 15;
1870
1871
        if ( isset( $params->limit ) )
1872
            $limit = (int) $params->limit;
1873
1874
        $page = 1;
1875
1876
        if ( isset( $params->page ) )
1877
            $page = (int) $params->page;
1878
1879
        if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
1880
            pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

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

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

Loading history...
1881 View Code Duplication
        elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
1882
            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...
1883
        elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
1884
            pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

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

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

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

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

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

Loading history...
1887 View Code Duplication
        elseif ( 'multi' == pods_var( self::$type . '_format_type', $field ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $field ) )
1888
            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...
1889
1890
        $object_params = array(
1891
            'name' => $field[ 'name' ], // The name of the field
1892
            'value' => null, // The value of the field
1893
            'options' => array_merge( $field, $field[ 'options' ] ), // Field options
1894
            'pod' => $pod, // Pod data
1895
            'id' => $id, // Item ID
1896
            'context' => 'admin_ajax_relationship', // Data context
1897
            'data_params' => $params,
1898
            'page' => $page,
1899
            'limit' => $limit
1900
        );
1901
1902
        $pick_data = apply_filters( 'pods_field_pick_data_ajax', null, $field[ 'name' ], null, $field, $pod, $id );
1903
1904
        if ( null !== $pick_data )
1905
            $items = $pick_data;
1906
        else
1907
            $items = $this->get_object_data( $object_params );
1908
1909
        if ( !empty( $items ) && isset( $items[ 0 ] ) && !is_array( $items[ 0 ] ) ) {
1910
            $new_items = array();
1911
1912
            foreach ( $items as $id => $text ) {
1913
                $new_items[] = array(
1914
                    'id' => $id,
1915
                    'text' => $text,
1916
                    'image' => ''
1917
                );
1918
            }
1919
1920
            $items = $new_items;
1921
        }
1922
1923
        $items = apply_filters( 'pods_field_pick_data_ajax_items', $items, $field[ 'name' ], null, $field, $pod, $id );
1924
1925
        $items = array(
1926
            'results' => $items
1927
        );
1928
1929
        wp_send_json( $items );
1930
1931
        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...
1932
    }
1933
1934
    /**
1935
     * Data callback for Post Stati
1936
     *
1937
     * @param string $name The name of the field
1938
     * @param string|array $value The value of the field
1939
     * @param array $options Field options
1940
     * @param array $pod Pod data
1941
     * @param int $id Item ID
1942
     *
1943
     * @return array
1944
     *
1945
     * @since 2.3
1946
     */
1947
    public function data_post_stati ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1948
        $data = array();
1949
1950
        $post_stati = get_post_stati( array(), 'objects' );
1951
1952
        foreach ( $post_stati as $post_status ) {
1953
            $data[ $post_status->name ] = $post_status->label;
1954
        }
1955
1956
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1957
    }
1958
1959
    /**
1960
     * Data callback for User Roles
1961
     *
1962
     * @param string $name The name of the field
1963
     * @param string|array $value The value of the field
1964
     * @param array $options Field options
1965
     * @param array $pod Pod data
1966
     * @param int $id Item ID
1967
     *
1968
     * @return array
1969
     *
1970
     * @since 2.3
1971
     */
1972
    public function data_roles ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1973
        $data = array();
1974
1975
        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...
1976
1977
        foreach ( $wp_roles->role_objects as $key => $role ) {
1978
            $data[ $key ] = $wp_roles->role_names[ $key ];
1979
        }
1980
1981
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
1982
    }
1983
1984
    /**
1985
     * Data callback for User Capabilities
1986
     *
1987
     * @param string $name The name of the field
1988
     * @param string|array $value The value of the field
1989
     * @param array $options Field options
1990
     * @param array $pod Pod data
1991
     * @param int $id Item ID
1992
     *
1993
     * @return array
1994
     *
1995
     * @since 2.3
1996
     */
1997
    public function data_capabilities ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
1998
        $data = array();
1999
2000
        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...
2001
2002
        $default_caps = array(
2003
            'activate_plugins',
2004
            'add_users',
2005
            'create_users',
2006
            'delete_others_pages',
2007
            'delete_others_posts',
2008
            'delete_pages',
2009
            'delete_plugins',
2010
            'delete_posts',
2011
            'delete_private_pages',
2012
            'delete_private_posts',
2013
            'delete_published_pages',
2014
            'delete_published_posts',
2015
            'delete_users',
2016
            'edit_dashboard',
2017
            'edit_files',
2018
            'edit_others_pages',
2019
            'edit_others_posts',
2020
            'edit_pages',
2021
            'edit_plugins',
2022
            'edit_posts',
2023
            'edit_private_pages',
2024
            'edit_private_posts',
2025
            'edit_published_pages',
2026
            'edit_published_posts',
2027
            'edit_theme_options',
2028
            'edit_themes',
2029
            'edit_users',
2030
            'import',
2031
            'install_plugins',
2032
            'install_themes',
2033
            'list_users',
2034
            'manage_categories',
2035
            'manage_links',
2036
            'manage_options',
2037
            'moderate_comments',
2038
            'promote_users',
2039
            'publish_pages',
2040
            'publish_posts',
2041
            'read',
2042
            'read_private_pages',
2043
            'read_private_posts',
2044
            'remove_users',
2045
            'switch_themes',
2046
            'unfiltered_html',
2047
            'unfiltered_upload',
2048
            'update_core',
2049
            'update_plugins',
2050
            'update_themes',
2051
            'upload_files'
2052
        );
2053
2054
        $role_caps = array();
2055
2056 View Code Duplication
        foreach ( $wp_roles->role_objects as $key => $role ) {
2057
            if ( is_array( $role->capabilities ) ) {
2058
                foreach ( $role->capabilities as $cap => $grant ) {
2059
                    $role_caps[ $cap ] = $cap;
2060
                }
2061
            }
2062
        }
2063
2064
        $role_caps = array_unique( $role_caps );
2065
2066
        $capabilities = array_merge( $default_caps, $role_caps );
2067
2068
        // To support Members filters
2069
        $capabilities = apply_filters( 'members_get_capabilities', $capabilities );
2070
2071
        $capabilities = apply_filters( 'pods_roles_get_capabilities', $capabilities );
2072
2073
        sort( $capabilities );
2074
2075
        $capabilities = array_unique( $capabilities );
2076
2077
        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...
2078
2079
        foreach ( $capabilities as $capability ) {
2080
            $data[ $capability ] = $capability;
2081
        }
2082
2083
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2084
    }
2085
2086
    /**
2087
     * Data callback for Image Sizes
2088
     *
2089
     * @param string $name The name of the field
2090
     * @param string|array $value The value of the field
2091
     * @param array $options Field options
2092
     * @param array $pod Pod data
2093
     * @param int $id Item ID
2094
     *
2095
     * @return array
2096
     *
2097
     * @since 2.3
2098
     */
2099
    public function data_image_sizes ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2100
        $data = array();
2101
2102
        $image_sizes = get_intermediate_image_sizes();
2103
2104
        foreach ( $image_sizes as $image_size ) {
2105
            $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
2106
        }
2107
2108
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2109
    }
2110
2111
    /**
2112
     * Data callback for Countries
2113
     *
2114
     * @param string $name The name of the field
2115
     * @param string|array $value The value of the field
2116
     * @param array $options Field options
2117
     * @param array $pod Pod data
2118
     * @param int $id Item ID
2119
     *
2120
     * @return array
2121
     *
2122
     * @since 2.3
2123
     */
2124
    public function data_countries ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2125
        $data = array(
2126
            'AF' => __( 'Afghanistan' ),
2127
            'AL' => __( 'Albania' ),
2128
            'DZ' => __( 'Algeria' ),
2129
            'AS' => __( 'American Samoa' ),
2130
            'AD' => __( 'Andorra' ),
2131
            'AO' => __( 'Angola' ),
2132
            'AI' => __( 'Anguilla' ),
2133
            'AQ' => __( 'Antarctica' ),
2134
            'AG' => __( 'Antigua and Barbuda' ),
2135
            'AR' => __( 'Argentina' ),
2136
            'AM' => __( 'Armenia' ),
2137
            'AW' => __( 'Aruba' ),
2138
            'AU' => __( 'Australia' ),
2139
            'AT' => __( 'Austria' ),
2140
            'AZ' => __( 'Azerbaijan' ),
2141
            'BS' => __( 'Bahamas' ),
2142
            'BH' => __( 'Bahrain' ),
2143
            'BD' => __( 'Bangladesh' ),
2144
            'BB' => __( 'Barbados' ),
2145
            'BY' => __( 'Belarus' ),
2146
            'BE' => __( 'Belgium' ),
2147
            'BZ' => __( 'Belize' ),
2148
            'BJ' => __( 'Benin' ),
2149
            'BM' => __( 'Bermuda' ),
2150
            'BT' => __( 'Bhutan' ),
2151
            'BO' => __( 'Bolivia' ),
2152
            'BA' => __( 'Bosnia and Herzegovina' ),
2153
            'BW' => __( 'Botswana' ),
2154
            'BV' => __( 'Bouvet Island' ),
2155
            'BR' => __( 'Brazil' ),
2156
            'BQ' => __( 'British Antarctic Territory' ),
2157
            'IO' => __( 'British Indian Ocean Territory' ),
2158
            'VG' => __( 'British Virgin Islands' ),
2159
            'BN' => __( 'Brunei' ),
2160
            'BG' => __( 'Bulgaria' ),
2161
            'BF' => __( 'Burkina Faso' ),
2162
            'BI' => __( 'Burundi' ),
2163
            'KH' => __( 'Cambodia' ),
2164
            'CM' => __( 'Cameroon' ),
2165
            'CA' => __( 'Canada' ),
2166
            'CT' => __( 'Canton and Enderbury Islands' ),
2167
            'CV' => __( 'Cape Verde' ),
2168
            'KY' => __( 'Cayman Islands' ),
2169
            'CF' => __( 'Central African Republic' ),
2170
            'TD' => __( 'Chad' ),
2171
            'CL' => __( 'Chile' ),
2172
            'CN' => __( 'China' ),
2173
            'CX' => __( 'Christmas Island' ),
2174
            'CC' => __( 'Cocos [Keeling] Islands' ),
2175
            'CO' => __( 'Colombia' ),
2176
            'KM' => __( 'Comoros' ),
2177
            'CG' => __( 'Congo - Brazzaville' ),
2178
            'CD' => __( 'Congo - Kinshasa' ),
2179
            'CK' => __( 'Cook Islands' ),
2180
            'CR' => __( 'Costa Rica' ),
2181
            'HR' => __( 'Croatia' ),
2182
            'CU' => __( 'Cuba' ),
2183
            'CY' => __( 'Cyprus' ),
2184
            'CZ' => __( 'Czech Republic' ),
2185
            'CI' => __( 'Côte d’Ivoire' ),
2186
            'DK' => __( 'Denmark' ),
2187
            'DJ' => __( 'Djibouti' ),
2188
            'DM' => __( 'Dominica' ),
2189
            'DO' => __( 'Dominican Republic' ),
2190
            'NQ' => __( 'Dronning Maud Land' ),
2191
            'DD' => __( 'East Germany' ),
2192
            'EC' => __( 'Ecuador' ),
2193
            'EG' => __( 'Egypt' ),
2194
            'SV' => __( 'El Salvador' ),
2195
            'GQ' => __( 'Equatorial Guinea' ),
2196
            'ER' => __( 'Eritrea' ),
2197
            'EE' => __( 'Estonia' ),
2198
            'ET' => __( 'Ethiopia' ),
2199
            'FK' => __( 'Falkland Islands' ),
2200
            'FO' => __( 'Faroe Islands' ),
2201
            'FJ' => __( 'Fiji' ),
2202
            'FI' => __( 'Finland' ),
2203
            'FR' => __( 'France' ),
2204
            'GF' => __( 'French Guiana' ),
2205
            'PF' => __( 'French Polynesia' ),
2206
            'TF' => __( 'French Southern Territories' ),
2207
            'FQ' => __( 'French Southern and Antarctic Territories' ),
2208
            'GA' => __( 'Gabon' ),
2209
            'GM' => __( 'Gambia' ),
2210
            'GE' => __( 'Georgia' ),
2211
            'DE' => __( 'Germany' ),
2212
            'GH' => __( 'Ghana' ),
2213
            'GI' => __( 'Gibraltar' ),
2214
            'GR' => __( 'Greece' ),
2215
            'GL' => __( 'Greenland' ),
2216
            'GD' => __( 'Grenada' ),
2217
            'GP' => __( 'Guadeloupe' ),
2218
            'GU' => __( 'Guam' ),
2219
            'GT' => __( 'Guatemala' ),
2220
            'GG' => __( 'Guernsey' ),
2221
            'GN' => __( 'Guinea' ),
2222
            'GW' => __( 'Guinea-Bissau' ),
2223
            'GY' => __( 'Guyana' ),
2224
            'HT' => __( 'Haiti' ),
2225
            'HM' => __( 'Heard Island and McDonald Islands' ),
2226
            'HN' => __( 'Honduras' ),
2227
            'HK' => __( 'Hong Kong SAR China' ),
2228
            'HU' => __( 'Hungary' ),
2229
            'IS' => __( 'Iceland' ),
2230
            'IN' => __( 'India' ),
2231
            'ID' => __( 'Indonesia' ),
2232
            'IR' => __( 'Iran' ),
2233
            'IQ' => __( 'Iraq' ),
2234
            'IE' => __( 'Ireland' ),
2235
            'IM' => __( 'Isle of Man' ),
2236
            'IL' => __( 'Israel' ),
2237
            'IT' => __( 'Italy' ),
2238
            'JM' => __( 'Jamaica' ),
2239
            'JP' => __( 'Japan' ),
2240
            'JE' => __( 'Jersey' ),
2241
            'JT' => __( 'Johnston Island' ),
2242
            'JO' => __( 'Jordan' ),
2243
            'KZ' => __( 'Kazakhstan' ),
2244
            'KE' => __( 'Kenya' ),
2245
            'KI' => __( 'Kiribati' ),
2246
            'KW' => __( 'Kuwait' ),
2247
            'KG' => __( 'Kyrgyzstan' ),
2248
            'LA' => __( 'Laos' ),
2249
            'LV' => __( 'Latvia' ),
2250
            'LB' => __( 'Lebanon' ),
2251
            'LS' => __( 'Lesotho' ),
2252
            'LR' => __( 'Liberia' ),
2253
            'LY' => __( 'Libya' ),
2254
            'LI' => __( 'Liechtenstein' ),
2255
            'LT' => __( 'Lithuania' ),
2256
            'LU' => __( 'Luxembourg' ),
2257
            'MO' => __( 'Macau SAR China' ),
2258
            'MK' => __( 'Macedonia' ),
2259
            'MG' => __( 'Madagascar' ),
2260
            'MW' => __( 'Malawi' ),
2261
            'MY' => __( 'Malaysia' ),
2262
            'MV' => __( 'Maldives' ),
2263
            'ML' => __( 'Mali' ),
2264
            'MT' => __( 'Malta' ),
2265
            'MH' => __( 'Marshall Islands' ),
2266
            'MQ' => __( 'Martinique' ),
2267
            'MR' => __( 'Mauritania' ),
2268
            'MU' => __( 'Mauritius' ),
2269
            'YT' => __( 'Mayotte' ),
2270
            'FX' => __( 'Metropolitan France' ),
2271
            'MX' => __( 'Mexico' ),
2272
            'FM' => __( 'Micronesia' ),
2273
            'MI' => __( 'Midway Islands' ),
2274
            'MD' => __( 'Moldova' ),
2275
            'MC' => __( 'Monaco' ),
2276
            'MN' => __( 'Mongolia' ),
2277
            'ME' => __( 'Montenegro' ),
2278
            'MS' => __( 'Montserrat' ),
2279
            'MA' => __( 'Morocco' ),
2280
            'MZ' => __( 'Mozambique' ),
2281
            'MM' => __( 'Myanmar [Burma]' ),
2282
            'NA' => __( 'Namibia' ),
2283
            'NR' => __( 'Nauru' ),
2284
            'NP' => __( 'Nepal' ),
2285
            'NL' => __( 'Netherlands' ),
2286
            'AN' => __( 'Netherlands Antilles' ),
2287
            'NT' => __( 'Neutral Zone' ),
2288
            'NC' => __( 'New Caledonia' ),
2289
            'NZ' => __( 'New Zealand' ),
2290
            'NI' => __( 'Nicaragua' ),
2291
            'NE' => __( 'Niger' ),
2292
            'NG' => __( 'Nigeria' ),
2293
            'NU' => __( 'Niue' ),
2294
            'NF' => __( 'Norfolk Island' ),
2295
            'KP' => __( 'North Korea' ),
2296
            'VD' => __( 'North Vietnam' ),
2297
            'MP' => __( 'Northern Mariana Islands' ),
2298
            'NO' => __( 'Norway' ),
2299
            'OM' => __( 'Oman' ),
2300
            'PC' => __( 'Pacific Islands Trust Territory' ),
2301
            'PK' => __( 'Pakistan' ),
2302
            'PW' => __( 'Palau' ),
2303
            'PS' => __( 'Palestinian Territories' ),
2304
            'PA' => __( 'Panama' ),
2305
            'PZ' => __( 'Panama Canal Zone' ),
2306
            'PG' => __( 'Papua New Guinea' ),
2307
            'PY' => __( 'Paraguay' ),
2308
            'YD' => __( "People's Democratic Republic of Yemen" ),
2309
            'PE' => __( 'Peru' ),
2310
            'PH' => __( 'Philippines' ),
2311
            'PN' => __( 'Pitcairn Islands' ),
2312
            'PL' => __( 'Poland' ),
2313
            'PT' => __( 'Portugal' ),
2314
            'PR' => __( 'Puerto Rico' ),
2315
            'QA' => __( 'Qatar' ),
2316
            'RO' => __( 'Romania' ),
2317
            'RU' => __( 'Russia' ),
2318
            'RW' => __( 'Rwanda' ),
2319
            'RE' => __( 'Réunion' ),
2320
            'BL' => __( 'Saint Barthélemy' ),
2321
            'SH' => __( 'Saint Helena' ),
2322
            'KN' => __( 'Saint Kitts and Nevis' ),
2323
            'LC' => __( 'Saint Lucia' ),
2324
            'MF' => __( 'Saint Martin' ),
2325
            'PM' => __( 'Saint Pierre and Miquelon' ),
2326
            'VC' => __( 'Saint Vincent and the Grenadines' ),
2327
            'WS' => __( 'Samoa' ),
2328
            'SM' => __( 'San Marino' ),
2329
            'SA' => __( 'Saudi Arabia' ),
2330
            'SN' => __( 'Senegal' ),
2331
            'RS' => __( 'Serbia' ),
2332
            'CS' => __( 'Serbia and Montenegro' ),
2333
            'SC' => __( 'Seychelles' ),
2334
            'SL' => __( 'Sierra Leone' ),
2335
            'SG' => __( 'Singapore' ),
2336
            'SK' => __( 'Slovakia' ),
2337
            'SI' => __( 'Slovenia' ),
2338
            'SB' => __( 'Solomon Islands' ),
2339
            'SO' => __( 'Somalia' ),
2340
            'ZA' => __( 'South Africa' ),
2341
            'GS' => __( 'South Georgia and the South Sandwich Islands' ),
2342
            'KR' => __( 'South Korea' ),
2343
            'ES' => __( 'Spain' ),
2344
            'LK' => __( 'Sri Lanka' ),
2345
            'SD' => __( 'Sudan' ),
2346
            'SR' => __( 'Suriname' ),
2347
            'SJ' => __( 'Svalbard and Jan Mayen' ),
2348
            'SZ' => __( 'Swaziland' ),
2349
            'SE' => __( 'Sweden' ),
2350
            'CH' => __( 'Switzerland' ),
2351
            'SY' => __( 'Syria' ),
2352
            'ST' => __( 'São Tomé and Príncipe' ),
2353
            'TW' => __( 'Taiwan' ),
2354
            'TJ' => __( 'Tajikistan' ),
2355
            'TZ' => __( 'Tanzania' ),
2356
            'TH' => __( 'Thailand' ),
2357
            'TL' => __( 'Timor-Leste' ),
2358
            'TG' => __( 'Togo' ),
2359
            'TK' => __( 'Tokelau' ),
2360
            'TO' => __( 'Tonga' ),
2361
            'TT' => __( 'Trinidad and Tobago' ),
2362
            'TN' => __( 'Tunisia' ),
2363
            'TR' => __( 'Turkey' ),
2364
            'TM' => __( 'Turkmenistan' ),
2365
            'TC' => __( 'Turks and Caicos Islands' ),
2366
            'TV' => __( 'Tuvalu' ),
2367
            'UM' => __( 'U.S. Minor Outlying Islands' ),
2368
            'PU' => __( 'U.S. Miscellaneous Pacific Islands' ),
2369
            'VI' => __( 'U.S. Virgin Islands' ),
2370
            'UG' => __( 'Uganda' ),
2371
            'UA' => __( 'Ukraine' ),
2372
            'SU' => __( 'Union of Soviet Socialist Republics' ),
2373
            'AE' => __( 'United Arab Emirates' ),
2374
            'GB' => __( 'United Kingdom' ),
2375
            'US' => __( 'United States' ),
2376
            'ZZ' => __( 'Unknown or Invalid Region' ),
2377
            'UY' => __( 'Uruguay' ),
2378
            'UZ' => __( 'Uzbekistan' ),
2379
            'VU' => __( 'Vanuatu' ),
2380
            'VA' => __( 'Vatican City' ),
2381
            'VE' => __( 'Venezuela' ),
2382
            'VN' => __( 'Vietnam' ),
2383
            'WK' => __( 'Wake Island' ),
2384
            'WF' => __( 'Wallis and Futuna' ),
2385
            'EH' => __( 'Western Sahara' ),
2386
            'YE' => __( 'Yemen' ),
2387
            'ZM' => __( 'Zambia' ),
2388
            'ZW' => __( 'Zimbabwe' ),
2389
            'AX' => __( 'Åland Islands' )
2390
        );
2391
2392
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2393
    }
2394
2395
    /**
2396
     * Data callback for US States
2397
     *
2398
     * @param string $name The name of the field
2399
     * @param string|array $value The value of the field
2400
     * @param array $options Field options
2401
     * @param array $pod Pod data
2402
     * @param int $id Item ID
2403
     *
2404
     * @return array
2405
     *
2406
     * @since 2.3
2407
     */
2408
    public function data_us_states ( $name = null, $value = null, $options = null, $pod = null, $id = null ) {
2409
        $data = array(
2410
            'AL' => __( 'Alabama' ),
2411
            'AK' => __( 'Alaska' ),
2412
            'AZ' => __( 'Arizona' ),
2413
            'AR' => __( 'Arkansas' ),
2414
            'CA' => __( 'California' ),
2415
            'CO' => __( 'Colorado' ),
2416
            'CT' => __( 'Connecticut' ),
2417
            'DE' => __( 'Delaware' ),
2418
            'DC' => __( 'District Of Columbia' ),
2419
            'FL' => __( 'Florida' ),
2420
            'GA' => __( 'Georgia' ),
2421
            'HI' => __( 'Hawaii' ),
2422
            'ID' => __( 'Idaho' ),
2423
            'IL' => __( 'Illinois' ),
2424
            'IN' => __( 'Indiana' ),
2425
            'IA' => __( 'Iowa' ),
2426
            'KS' => __( 'Kansas' ),
2427
            'KY' => __( 'Kentucky' ),
2428
            'LA' => __( 'Louisiana' ),
2429
            'ME' => __( 'Maine' ),
2430
            'MD' => __( 'Maryland' ),
2431
            'MA' => __( 'Massachusetts' ),
2432
            'MI' => __( 'Michigan' ),
2433
            'MN' => __( 'Minnesota' ),
2434
            'MS' => __( 'Mississippi' ),
2435
            'MO' => __( 'Missouri' ),
2436
            'MT' => __( 'Montana' ),
2437
            'NE' => __( 'Nebraska' ),
2438
            'NV' => __( 'Nevada' ),
2439
            'NH' => __( 'New Hampshire' ),
2440
            'NJ' => __( 'New Jersey' ),
2441
            'NM' => __( 'New Mexico' ),
2442
            'NY' => __( 'New York' ),
2443
            'NC' => __( 'North Carolina' ),
2444
            'ND' => __( 'North Dakota' ),
2445
            'OH' => __( 'Ohio' ),
2446
            'OK' => __( 'Oklahoma' ),
2447
            'OR' => __( 'Oregon' ),
2448
            'PA' => __( 'Pennsylvania' ),
2449
            'RI' => __( 'Rhode Island' ),
2450
            'SC' => __( 'South Carolina' ),
2451
            'SD' => __( 'South Dakota' ),
2452
            'TN' => __( 'Tennessee' ),
2453
            'TX' => __( 'Texas' ),
2454
            'UT' => __( 'Utah' ),
2455
            'VT' => __( 'Vermont' ),
2456
            'VA' => __( 'Virginia' ),
2457
            'WA' => __( 'Washington' ),
2458
            'WV' => __( 'West Virginia' ),
2459
            'WI' => __( 'Wisconsin' ),
2460
            'WY' => __( 'Wyoming' )
2461
        );
2462
2463
        return apply_filters( 'pods_form_ui_field_pick_' . __FUNCTION__, $data, $name, $value, $options, $pod, $id );
2464
    }
2465
2466
    /**
2467
     * Data callback for US States
2468
     *
2469
     * @param string $name The name of the field
2470
     * @param string|array $value The value of the field
2471
     * @param array $options Field options
2472
     * @param array $pod Pod data
2473
     * @param int $id Item ID
2474
     *
2475
     * @return array
2476
     *
2477
     * @since 2.3
2478
     */
2479
    public function data_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...
2480
2481
		/**
2482
		 * @var WP_Locale
2483
		 */
2484
		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...
2485
2486
		return $wp_locale->weekday;
2487
2488
    }
2489
2490
    /**
2491
     * Data callback for US States
2492
     *
2493
     * @param string $name The name of the field
2494
     * @param string|array $value The value of the field
2495
     * @param array $options Field options
2496
     * @param array $pod Pod data
2497
     * @param int $id Item ID
2498
     *
2499
     * @return array
2500
     *
2501
     * @since 2.3
2502
     */
2503
    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...
2504
2505
		/**
2506
		 * @var WP_Locale
2507
		 */
2508
		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...
2509
2510
		return $wp_locale->month;
2511
2512
    }
2513
}
2514