Completed
Pull Request — 2.x (#3632)
by Phil
04:46
created

PodsField_Pick::input()   D

Complexity

Conditions 19
Paths 128

Size

Total Lines 76
Code Lines 50

Duplication

Lines 28
Ratio 36.84 %

Importance

Changes 0
Metric Value
cc 19
eloc 50
nc 128
nop 5
dl 28
loc 76
rs 4.8608
c 0
b 0
f 0

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