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

PodsField_Pick::input()   F

Complexity

Conditions 23
Paths 320

Size

Total Lines 88
Code Lines 57

Duplication

Lines 28
Ratio 31.82 %

Importance

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

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

Loading history...
280
            self::$type . '_user_site' => array(
281
                'label' => __( 'Limit list to Site(s)', 'pods' ),
282
                'help' => __( 'help', 'pods' ),
283
                'depends-on' => array( self::$type . '_object' => 'user' ),
284
                'default' => '',
285
                'type' => 'pick',
286
                'pick_object' => 'site',
287
                'pick_format_type' => 'multi'
288
            ),*/
289
            self::$type . '_where' => array(
290
                'label' => __( 'Customized <em>WHERE</em>', 'pods' ),
291
                'help' => __( 'help', 'pods' ),
292
                'excludes-on' => array(
293
                    self::$type . '_object' => array_merge(
294
                        array( 'site', 'network' ),
295
                        self::simple_objects()
296
                    )
297
                ),
298
                'default' => '',
299
                'type' => 'text'
300
            ),
301
            self::$type . '_orderby' => array(
302
                'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ),
303
                'help' => __( 'help', 'pods' ),
304
                'excludes-on' => array(
305
                    self::$type . '_object' => array_merge(
306
                        array( 'site', 'network' ),
307
                        self::simple_objects()
308
                    )
309
                ),
310
                'default' => '',
311
                'type' => 'text'
312
            ),
313
            self::$type . '_groupby' => array(
314
                'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ),
315
                'help' => __( 'help', 'pods' ),
316
                'excludes-on' => array(
317
                    self::$type . '_object' => array_merge(
318
                        array( 'site', 'network' ),
319
                        self::simple_objects()
320
                    )
321
                ),
322
                'default' => '',
323
                'type' => 'text'
324
            )
325
            /*,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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