Completed
Pull Request — 2.x (#3696)
by Phil
07:13
created

PodsField_Pick::input()   D

Complexity

Conditions 23
Paths 128

Size

Total Lines 92
Code Lines 60

Duplication

Lines 28
Ratio 30.43 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 23
eloc 60
c 1
b 0
f 1
nc 128
nop 5
dl 28
loc 92
rs 4.3705

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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

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

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

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

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

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

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

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

Loading history...
326
            self::$type . '_size' => array(
327
                'label' => __( 'Field Size', 'pods' ),
328
                'default' => 'medium',
329
                'type' => 'pick',
330
                'data' => array(
331
                    'small' => __( 'Small', 'pods' ),
332
                    'medium' => __( 'Medium', 'pods' ),
333
                    'large' => __( 'Large', 'pods' )
334
                )
335
            )*/
336
        );
337
338
        $post_type_pick_objects = array();
339
        foreach ( get_post_types( '', 'names' ) as $post_type ) {
340
            $post_type_pick_objects[] = 'post-type_' .$post_type;
341
        }
342
        $options[ self::$type . '_post_status' ] = array(
343
            'name' => 'post_status',
344
            'label' => __( 'Post Status', 'pods' ),
345
            'help' => __( 'help', 'pods' ),
346
            'type' => 'pick',
347
            'pick_object' => 'post-status',
348
            'pick_format_type' => 'multi',
349
            'default' => 'publish',
350
            'depends-on' => array(
351
                self::$type . '_object' => $post_type_pick_objects
352
            )
353
        );
354
355
        /*if ( !is_multisite() )
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

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

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

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

Loading history...
660
		if ( $this->setup_related_objects() || null === self::$names_simple ) {
661
			$simple_objects = array();
662
663
			foreach ( self::$related_objects as $object => $related_object ) {
664
				if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] )
665
					continue;
666
667
				$simple_objects[] = $object;
668
			}
669
670
			self::$names_simple = (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects );
671
		}
672
673
	    return self::$names_simple;
674
    }
675
676
    /**
677
     * Return available bidirectional object names
678
     *
679
     * @return array Bidirectional object names
680
     * @since 2.3.4
681
     */
682 View Code Duplication
    public function bidirectional_objects () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
683
        if ( $this->setup_related_objects() || null === self::$names_bidirectional ) {
684
	        $bidirectional_objects = array();
685
686
	        foreach ( self::$related_objects as $object => $related_object ) {
687
		        if ( !isset( $related_object[ 'bidirectional' ] ) || !$related_object[ 'bidirectional' ] )
688
			        continue;
689
690
		        $bidirectional_objects[] = $object;
691
	        }
692
693
	        self::$names_bidirectional = (array) apply_filters( 'pods_form_ui_field_pick_bidirectional_objects', $bidirectional_objects );
694
        }
695
696
	    return self::$names_bidirectional;
697
    }
698
699
    /**
700
     * Define the current field's schema for DB table storage
701
     *
702
     * @param array $options
703
     *
704
     * @return array
705
     * @since 2.0
706
     */
707
    public function schema ( $options = null ) {
708
        $schema = false;
709
710
        $simple_tableless_objects = $this->simple_objects();
711
712
        if ( in_array( pods_var( self::$type . '_object', $options ), $simple_tableless_objects ) )
713
            $schema = 'LONGTEXT';
714
715
        return $schema;
716
    }
717
718
    /**
719
     * Change the way the value of the field is displayed with Pods::get
720
     *
721
     * @param mixed $value
722
     * @param string $name
723
     * @param array $options
724
     * @param array $fields
0 ignored issues
show
Bug introduced by
There is no parameter named $fields. Was it maybe removed?

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

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

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

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

Loading history...
725
     * @param array $pod
726
     * @param int $id
727
     *
728
     * @since 2.0
729
     */
730
    public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
731
        $fields = null;
732
733
        if ( is_object( $pod ) && isset( $pod->fields ) ) {
734
	        $fields = $pod->fields;
735
736
	        if ( ! empty( $pod->pod_data[ 'object_fields' ] ) ) {
737
		        $fields = array_merge( $fields, $pod->pod_data[ 'object_fields' ] );
738
	        }
739 View Code Duplication
        } elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) ) {
740
	        $fields = $pod[ 'fields' ];
741
742
	        if ( ! empty( $pod[ 'object_fields' ] ) ) {
743
		        $fields = array_merge( $fields, $pod[ 'object_fields' ] );
744
	        }
745
        }
746
747
        return pods_serial_comma( $value, array( 'field' => $name, 'fields' => $fields ) );
748
    }
749
750
    /**
751
     * Customize output of the form field
752
     *
753
     * @param string $name
754
     * @param mixed $value
755
     * @param array $options
756
     * @param array $pod
757
     * @param int $id
758
     *
759
     * @since 2.0
760
     */
761
    public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
762
        global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

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

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

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

Loading history...
766
767
        $options[ 'grouped' ] = 1;
768
769
        $options[ 'table_info' ] = array();
770
771
        $custom = pods_var_raw( self::$type . '_custom', $options, false );
772
773
        $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
774
775
        $ajax = false;
776
777
        if ( ( 'custom-simple' != pods_var( self::$type . '_object', $options ) || empty( $custom ) ) && '' != pods_var( self::$type . '_object', $options, '', null, true ) )
778
            $ajax = true;
779
780
        if ( !empty( self::$field_data ) && self::$field_data[ 'id' ] == $options[ 'id' ] ) {
781
            $ajax = (boolean) self::$field_data[ 'autocomplete' ];
782
        }
783
784
        $ajax = apply_filters( 'pods_form_ui_field_pick_ajax', $ajax, $name, $value, $options, $pod, $id );
785
786
        if ( 0 == pods_var( self::$type . '_ajax', $options, 1 ) )
787
            $ajax = false;
788
789
	    $format_type = pods_v( self::$type . '_format_type', $options, 'single' );
790
791
        if ( 'single' == $format_type ) {
792 View Code Duplication
            if ( 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
793
                $field_type = 'select';
794
            elseif ( 'radio' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
795
                $field_type = 'radio';
796
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
797
                $field_type = 'select2';
798
            elseif ( 'flexible' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
799
                $field_type = 'flexible';
800
            else {
801
                // Support custom integration
802
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_single', $options, 'dropdown' ), $name, $value, $options, $pod, $id );
803
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
804
                return;
805
            }
806
        }
807
        elseif ( 'multi' == $format_type ) {
808
            if ( !empty( $value ) && !is_array( $value ) )
809
                $value = explode( ',', $value );
810
811 View Code Duplication
            if ( 'checkbox' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
812
                $field_type = 'checkbox';
813
            elseif ( 'multiselect' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
814
                $field_type = 'select';
815
            elseif ( 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
816
                $field_type = 'select2';
817
            elseif ( 'flexible' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
818
                $field_type = 'flexible';
819
            else {
820
                // Support custom integration
821
                do_action( 'pods_form_ui_field_pick_input_' . pods_var( self::$type . '_format_type', $options, 'single' ) . '_' . pods_var( self::$type . '_format_multi', $options, 'checkbox' ), $name, $value, $options, $pod, $id );
822
                do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
823
                return;
824
            }
825
        }
826
        else {
827
            // Support custom integration
828
            do_action( 'pods_form_ui_field_pick_input', pods_var( self::$type . '_format_type', $options, 'single' ), $name, $value, $options, $pod, $id );
829
            return;
830
        }
831
832
	    $field_type = 'pick';
833
	    pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) );
834
	    return;
835
836
	    // Flexible relationships only support certain related objects
837
	    if ( 'flexible' == $field_type || ( 'select2' == $field_type && 1 == pods_v( self::$type . '_taggable', $options, 0 ) ) ) {
0 ignored issues
show
Unused Code introduced by
// Flexible relationship... return; } } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $field_type seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $options seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
838
		    $pick_object = pods_v( 'pick_object', $options );
839
840
		    if ( ! in_array( $pick_object, array( 'post_type', 'taxonomy', 'user', 'pod' ) ) ) {
0 ignored issues
show
Bug introduced by
The variable $pick_object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
841
			    // Default all others to Select2
842
				$field_type = 'select2';
843
		    } else {
844
		        // @todo: Fix location when merging
845
		        $field_type = 'pick';
846
		        pods_view( PODS_DIR . 'ui/fields-mv/pick.php', compact( array_keys( get_defined_vars() ) ) );
847
		        return;
848
		    }
849
	    }
850
851
        pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
852
    }
853
854
    /**
855
     * Validate a value before it's saved
856
     *
857
     * @param mixed $value
858
     * @param string $name
859
     * @param array $options
860
     * @param array $fields
861
     * @param array $pod
862
     * @param int $id
863
     *
864
     * @param null $params
865
     * @return array|bool
866
     * @since 2.0
867
     */
868
    public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
869
        if ( empty( self::$api ) )
870
            self::$api = pods_api();
871
872
        $simple_tableless_objects = $this->simple_objects();
873
874
        $related_pick_limit = 0;
875
        $related_field = $related_pod = $current_related_ids = false;
876
877
        // Bidirectional relationship requirement checks
878
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
879
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
880
        if ( empty( $related_val ) ) {
881
            $related_val = $related_object;
882
        }
883
884
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
885
886
        $options[ 'id' ] = (int) $options[ 'id' ];
887
888
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) || empty( self::$related_data[ $options[ 'id' ] ] ) )
889
            self::$related_data[ $options[ 'id' ] ] = array();
890
891
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
892
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
893
894
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
895
                $related_field = false;
896
897
                // Ensure sister_id exists on related Pod
898 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
899
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
900
                        $related_field = $related_pod_field;
901
902
                        break;
903
                    }
904
                }
905
906
                if ( !empty( $related_field ) ) {
907
                    $current_ids = self::$api->lookup_related_items( $fields[ $name ][ 'id' ], $pod[ 'id' ], $id, $fields[ $name ], $pod );
908
909
                    self::$related_data[ $options[ 'id' ] ][ 'current_ids' ] = $current_ids;
910
911
                    $value_ids = $value;
912
913
                    // Convert values from a comma-separated string into an array
914
                    if ( !is_array( $value_ids ) )
915
                        $value_ids = explode( ',', $value_ids );
916
917
                    $value_ids = array_unique( array_filter( $value_ids ) );
918
919
                    // Get ids to remove
920
                    $remove_ids = array_diff( $current_ids, $value_ids );
921
922
                    $related_required = (boolean) pods_var( 'required', $related_field[ 'options' ], 0 );
923
                    $related_pick_limit = (int) pods_var( self::$type . '_limit', $related_field[ 'options' ], 0 );
924
925
                    if ( 'single' == pods_var_raw( self::$type . '_format_type', $related_field[ 'options' ] ) )
926
                        $related_pick_limit = 1;
927
928
                    // Validate Required
929
                    if ( $related_required && !empty( $remove_ids ) ) {
930
                        foreach ( $remove_ids as $related_id ) {
931
                            $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
932
933
                            self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] = $bidirectional_ids;
934
935
                            if ( empty( $bidirectional_ids ) || ( in_array( $id, $bidirectional_ids ) && 1 == count( $bidirectional_ids ) ) )
936
                                return sprintf( __( 'The %s field is required and cannot be removed by the %s field', 'pods' ), $related_field[ 'label' ], $options[ 'label' ] );
937
                        }
938
                    }
939
                }
940
                else
941
                    $related_pod = false;
942
            }
943
            else
944
                $related_pod = false;
945
        }
946
947
        if ( empty( self::$related_data[ $options[ 'id' ] ] ) )
948
            unset( self::$related_data[ $options[ 'id' ] ] );
949
        else {
950
            self::$related_data[ $options[ 'id' ] ][ 'related_pod' ] = $related_pod;
951
            self::$related_data[ $options[ 'id' ] ][ 'related_field' ] = $related_field;
952
            self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ] = $related_pick_limit;
953
954
            $pick_limit = (int) pods_var( self::$type . '_limit', $options[ 'options' ], 0 );
955
956
            if ( 'single' == pods_var_raw( self::$type . '_format_type', $options[ 'options' ] ) )
957
                $pick_limit = 1;
958
959
            $related_field[ 'id' ] = (int) $related_field[ 'id' ];
960
961
            if ( !isset( self::$related_data[ $related_field[ 'id' ] ] ) || empty( self::$related_data[ $related_field[ 'id' ] ] ) ) {
962
                self::$related_data[ $related_field[ 'id' ] ] = array(
963
                    'related_pod' => $pod,
964
                    'related_field' => $options,
965
                    'related_pick_limit' => $pick_limit
966
                );
967
            }
968
        }
969
970
        return true;
971
    }
972
973
    /**
974
     * Save the value to the DB
975
     *
976
     * @param mixed $value
977
     * @param int $id
978
     * @param string $name
979
     * @param array $options
980
     * @param array $fields
981
     * @param array $pod
982
     * @param object $params
983
     *
984
     * @since 2.3
985
     */
986
    public function save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
987
        if ( empty( self::$api ) )
988
            self::$api = pods_api();
989
990
        $options[ 'id' ] = (int) $options[ 'id' ];
991
992
        if ( !isset( self::$related_data[ $options[ 'id' ] ] ) )
993
            return;
994
995
        $related_pod = self::$related_data[ $options[ 'id' ] ][ 'related_pod' ];
996
        $related_field = self::$related_data[ $options[ 'id' ] ][ 'related_field' ];
997
        $related_pick_limit = self::$related_data[ $options[ 'id' ] ][ 'related_pick_limit' ];
998
999
        // Bidirectional relationship updates
1000
        if ( !empty( $related_field ) ) {
1001
            // Don't use no conflict mode unless this isn't the current pod type
1002
            $no_conflict = true;
1003
1004
            if ( $related_pod[ 'type' ] != $pod[ 'type' ] )
1005
                $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1006
1007
            if ( !$no_conflict )
1008
                pods_no_conflict_on( $related_pod[ 'type' ] );
1009
1010
            $value = array_filter( $value );
1011
1012
            foreach ( $value as $related_id ) {
1013
                if ( isset( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) && !empty( self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ] ) )
1014
                    $bidirectional_ids = self::$related_data[ $options[ 'id' ] ][ 'related_ids_' . $related_id ];
1015
                else
1016
                    $bidirectional_ids = self::$api->lookup_related_items( $related_field[ 'id' ], $related_pod[ 'id' ], $related_id, $related_field, $related_pod );
1017
1018
                $bidirectional_ids = array_filter( $bidirectional_ids );
1019
1020
                if ( empty( $bidirectional_ids ) )
1021
                    $bidirectional_ids = array();
1022
1023
                $remove_ids = array();
1024
1025
                if ( 0 < $related_pick_limit && !empty( $bidirectional_ids ) && !in_array( $id, $bidirectional_ids ) ) {
1026
                    while ( $related_pick_limit <= count( $bidirectional_ids ) ) {
1027
                        $remove_ids[] = (int) array_pop( $bidirectional_ids );
1028
                    }
1029
                }
1030
1031
                // Remove this item from related items no longer related to
1032
                $remove_ids = array_unique( array_filter( $remove_ids ) );
1033
1034
                // Add to related items
1035
                if ( !in_array( $id, $bidirectional_ids ) )
1036
                    $bidirectional_ids[] = $id;
1037
                // Nothing to change
1038
                elseif ( empty( $remove_ids ) )
1039
                    continue;
1040
1041
                self::$api->save_relationships( $related_id, $bidirectional_ids, $related_pod, $related_field );
1042
1043
                if ( !empty( $remove_ids ) )
1044
                    self::$api->delete_relationships( $remove_ids, $related_id, $pod, $options );
1045
            }
1046
1047
            if ( !$no_conflict )
1048
                pods_no_conflict_off( $related_pod[ 'type' ] );
1049
        }
1050
    }
1051
1052
    /**
1053
     * Delete the value from the DB
1054
     *
1055
     * @param int $id
1056
     * @param string $name
1057
     * @param array $options
1058
     * @param array $pod
1059
     *
1060
     * @since 2.3
1061
     */
1062
    public function delete ( $id = null, $name = null, $options = null, $pod = null ) {
1063
        if ( empty( self::$api ) )
1064
            self::$api = pods_api();
1065
1066
        $simple_tableless_objects = $this->simple_objects();
1067
1068
        // Bidirectional relationship requirement checks
1069
        $related_object = pods_var( self::$type . '_object', $options, '' ); // pod, post_type, taxonomy, etc..
1070
        $related_val = pods_var( self::$type . '_val', $options, $related_object, null, true ); // pod name, post type name, taxonomy name, etc..
1071
        $related_sister_id = (int) pods_var( 'sister_id', $options, 0 );
1072
1073
        if ( !empty( $related_sister_id ) && !in_array( $related_object, $simple_tableless_objects ) ) {
1074
            $related_pod = self::$api->load_pod( array( 'name' => $related_val, 'table_info' => false ), false );
1075
1076
            if ( false !== $related_pod && ( 'pod' == $related_object || $related_object == $related_pod[ 'type' ] ) ) {
1077
                $related_field = false;
1078
1079
                // Ensure sister_id exists on related Pod
1080 View Code Duplication
                foreach ( $related_pod[ 'fields' ] as $related_pod_field ) {
1081
                    if ( 'pick' == $related_pod_field[ 'type' ] && $related_sister_id == $related_pod_field[ 'id' ] ) {
1082
                        $related_field = $related_pod_field;
1083
1084
                        break;
1085
                    }
1086
                }
1087
1088
                if ( !empty( $related_field ) ) {
1089
                    $values = self::$api->lookup_related_items( $options[ 'id' ], $pod[ 'id' ], $id, $options, $pod );
1090
1091
                    if ( !empty( $values ) ) {
1092
                        $no_conflict = pods_no_conflict_check( $related_pod[ 'type' ] );
1093
1094
                        if ( !$no_conflict )
1095
                            pods_no_conflict_on( $related_pod[ 'type' ] );
1096
1097
                        self::$api->delete_relationships( $values, $id, $related_pod, $related_field );
1098
1099
                        if ( !$no_conflict )
1100
                            pods_no_conflict_off( $related_pod[ 'type' ] );
1101
                    }
1102
                }
1103
            }
1104
        }
1105
    }
1106
1107
    /**
1108
     * Customize the Pods UI manage table column output
1109
     *
1110
     * @param int $id
1111
     * @param mixed $value
1112
     * @param string $name
1113
     * @param array $options
1114
     * @param array $fields
1115
     * @param array $pod
1116
     *
1117
     * @since 2.0
1118
     */
1119
    public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
1120
        $value = $this->simple_value( $name, $value, $options, $pod, $id );
1121
1122
        return $this->display( $value, $name, $options, $pod, $id );
1123
    }
1124
1125
    /**
1126
     * Get the data from the field
1127
     *
1128
     * @param string $name The name of the field
1129
     * @param string|array $value The value of the field
1130
     * @param array $options Field options
1131
     * @param array $pod Pod data
1132
     * @param int $id Item ID
1133
     * @param boolean $in_form
1134
     *
1135
     * @return array Array of possible field data
1136
     *
1137
     * @since 2.0
1138
     */
1139
    public function data ( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
1140 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1141
            $options = array_merge( $options, $options[ 'options' ] );
1142
1143
            unset( $options[ 'options' ] );
1144
        }
1145
1146
        $data = pods_var_raw( 'data', $options, null, null, true );
1147
1148
        $object_params = array(
1149
            'name' => $name, // The name of the field
1150
            'value' => $value, // The value of the field
1151
            'options' => $options, // Field options
1152
            'pod' => $pod, // Pod data
1153
            'id' => $id, // Item ID
1154
            'context' => 'data', // Data context
1155
        );
1156
1157
        if ( null !== $data )
1158
            $data = (array) $data;
1159
        else
1160
            $data = $this->get_object_data( $object_params );
1161
1162
        if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'dropdown' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1163
            $data = array( '' => pods_var_raw( self::$type . '_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) ) + $data;
1164
1165
        $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
1166
1167
        return $data;
1168
    }
1169
1170
    /**
1171
     * Convert a simple value to the correct value
1172
     *
1173
     * @param string $name The name of the field
1174
     * @param string|array $value The value of the field
1175
     * @param array $options Field options
1176
     * @param array $pod Pod data
1177
     * @param int $id Item ID
1178
     * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false)
1179
     *
1180
     * @return mixed Corrected value
1181
     */
1182
    public function simple_value ( $name, $value = null, $options = null, $pod = null, $id = null, $raw = false ) {
1183
        if ( in_array( pods_var( self::$type . '_object', $options ), self::simple_objects() ) ) {
1184 View Code Duplication
            if ( isset( $options[ 'options' ] ) ) {
1185
                $options = array_merge( $options, $options[ 'options' ] );
1186
1187
                unset( $options[ 'options' ] );
1188
            }
1189
1190
            if ( !is_array( $value ) && 0 < strlen( $value ) ) {
1191
                $simple = @json_decode( $value, true );
1192
1193
                if ( is_array( $simple ) )
1194
                    $value = $simple;
1195
            }
1196
1197
            $data = pods_var_raw( 'data', $options, null, null, true );
1198
1199
            $object_params = array(
1200
                'name' => $name, // The name of the field
1201
                'value' => $value, // The value of the field
1202
                'options' => $options, // Field options
1203
                'pod' => $pod, // Pod data
1204
                'id' => $id, // Item ID
1205
                'context' => 'simple_value', // Data context
1206
            );
1207
1208
            if ( null === $data )
1209
                $data = $this->get_object_data( $object_params );
1210
1211
            $data = (array) $data;
1212
1213
            $key = 0;
1214
1215
            if ( is_array( $value ) ) {
1216
                if ( !empty( $data ) ) {
1217
                    $val = array();
1218
1219
                    foreach ( $value as $k => $v ) {
1220
                        if ( isset( $data[ $v ] ) ) {
1221
                            if ( false === $raw ) {
1222
                                $k = $v;
1223
                                $v = $data[ $v ];
1224
                            }
1225
1226
                            $val[ $k ] = $v;
1227
                        }
1228
                    }
1229
1230
                    $value = $val;
1231
                }
1232
            }
1233
            elseif ( isset( $data[ $value ] ) && false === $raw ) {
1234
                $key = $value;
1235
                $value = $data[ $value ];
1236
            }
1237
1238
            $single_multi = pods_var( self::$type . '_format_type', $options, 'single' );
1239
1240
            if ( 'multi' == $single_multi )
1241
                $limit = (int) pods_var( self::$type . '_limit', $options, 0 );
1242
            else
1243
                $limit = 1;
1244
1245
            if ( is_array( $value ) && 0 < $limit ) {
1246
                if ( 1 == $limit )
1247
                    $value = current( $value );
1248
                else
1249
                    $value = array_slice( $value, 0, $limit, true );
1250
            }
1251
            elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) {
1252
                if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) {
1253
                    $value = array(
1254
                        $key => $value
1255
                    );
1256
                }
1257
            }
1258
        }
1259
1260
        return $value;
1261
    }
1262
1263
    /**
1264
     * Get the label from a pick value
1265
     *
1266
     * @param string $name The name of the field
1267
     * @param string|array $value The value of the field
1268
     * @param array $options Field options
1269
     * @param array $pod Pod data
1270
     * @param int $id Item ID
1271
     *
1272
     * @return string
1273
     *
1274
     * @since 2.2
1275
     */
1276
    public function value_to_label ( $name, $value = null, $options = null, $pod = null, $id = null ) {
1277 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1278
            $options = array_merge( $options, $options[ 'options' ] );
1279
1280
            unset( $options[ 'options' ] );
1281
        }
1282
1283
        $data = pods_var_raw( 'data', $options, null, null, true );
1284
1285
        $object_params = array(
1286
            'name' => $name, // The name of the field
1287
            'value' => $value, // The value of the field
1288
            'options' => $options, // Field options
1289
            'pod' => $pod, // Pod data
1290
            'id' => $id, // Item ID
1291
            'context' => 'value_to_label', // Data context
1292
        );
1293
1294
        if ( null !== $data )
1295
            $data = (array) $data;
1296
        else
1297
            $data = $this->get_object_data( $object_params );
1298
1299
        $labels = array();
1300
1301
        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...
1302
            if ( !in_array( $l, $labels ) && ( $value == $v || ( is_array( $value ) && in_array( $v, $value ) ) ) )
1303
                $labels[] = $l;
1304
        }
1305
1306
        $labels = apply_filters( 'pods_field_pick_value_to_label', $labels, $name, $value, $options, $pod, $id );
1307
1308
        $labels = pods_serial_comma( $labels );
1309
1310
        return $labels;
1311
    }
1312
1313
	/**
1314
	 * Get available items from a relationship field
1315
	 *
1316
	 * @param array|string $field Field array or field name
1317
	 * @param array $options [optional] Field options array overrides
1318
	 * @param array $object_params [optional] Additional get_object_data options
1319
	 *
1320
	 * @return array An array of available items from a relationship field
1321
	 */
1322
	public function get_field_data( $field, $options = array(), $object_params = array() ) {
1323
1324
		// Handle field array overrides
1325
		if ( is_array( $field ) ) {
1326
			$options = array_merge( $field, $options );
1327
		}
1328
1329
		// Get field name from array
1330
		$field = pods_var_raw( 'name', $options, $field, null, true );
1331
1332
		// Field name or options not set
1333
		if ( empty( $field ) || empty( $options ) ) {
1334
			return array();
1335
		}
1336
1337
		// Options normalization
1338
		$options = array_merge( $options, pods_var_raw( 'options', $options, array(), null, true ) );
1339
1340
		// Setup object params
1341
        $object_params = array_merge(
1342
			array(
1343
				'name' => $field, // The name of the field
1344
				'options' => $options, // Field options
1345
			),
1346
			$object_params
1347
        );
1348
1349
		// Get data override
1350
        $data = pods_var_raw( 'data', $options, null, null, true );
1351
1352
		// Return data override
1353
        if ( null !== $data ) {
1354
            $data = (array) $data;
1355
		}
1356
		// Get object data
1357
        else {
1358
            $data = $this->get_object_data( $object_params );
1359
		}
1360
1361
		return $data;
1362
1363
	}
1364
1365
    /**
1366
     * Get data from relationship objects
1367
     *
1368
     * @param array $object_params Object data parameters
1369
     *
1370
     * @return array|bool Object data
1371
     */
1372
    public function get_object_data ( $object_params = null ) {
1373
1374
	    /**
1375
	     * @var $wpdb wpdb
1376
	     */
1377
        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...
1378
1379
        $object_params = array_merge(
1380
            array(
1381
                'name' => '', // The name of the field
1382
                'value' => '', // The value of the field
1383
                'options' => array(), // Field options
1384
                'pod' => '', // Pod data
1385
                'id' => '', // Item ID
1386
                'context' => '', // Data context
1387
                'data_params' => array(
1388
                    'query' => '' // Query being searched
1389
                ),
1390
                'page' => 1, // Page number of results to get
1391
				'limit' => 0 // How many data items to limit to (autocomplete defaults to 30, set to -1 or 1+ to override)
1392
            ),
1393
            $object_params
1394
        );
1395
1396
        $name = $object_params[ 'name' ];
1397
        $value = $object_params[ 'value' ];
1398
        $options = $object_params[ 'options' ] = (array) $object_params[ 'options' ];
1399
        $pod = $object_params[ 'pod' ];
1400
        $id = $object_params[ 'id' ];
1401
        $context = $object_params[ 'context' ];
1402
        $data_params = $object_params[ 'data_params' ] = (array) $object_params[ 'data_params' ];
1403
        $page = min( 1, (int) $object_params[ 'page' ] );
1404
        $limit = (int) $object_params[ 'limit' ];
1405
1406 View Code Duplication
        if ( isset( $options[ 'options' ] ) ) {
1407
            $options = array_merge( $options, $options[ 'options' ] );
1408
1409
            unset( $options[ 'options' ] );
1410
        }
1411
1412
        $data = apply_filters( 'pods_field_pick_object_data', null, $name, $value, $options, $pod, $id, $object_params );
1413
        $items = array();
1414
1415
        if ( !isset( $options[ self::$type . '_object' ] ) )
1416
            $data = pods_var_raw( 'data', $options, array(), null, true );
1417
1418
		$simple = false;
1419
1420
        if ( null === $data ) {
1421
            $data = array();
1422
1423
            if ( 'custom-simple' == $options[ self::$type . '_object' ] ) {
1424
                $custom = pods_var_raw( self::$type . '_custom', $options, '' );
1425
1426
                $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id, $object_params );
1427
1428
                if ( !empty( $custom ) ) {
1429
                    if ( !is_array( $custom ) ) {
1430
                        $data = array();
1431
1432
                        $custom = explode( "\n", trim( $custom ) );
1433
1434
                        foreach ( $custom as $custom_value ) {
1435
                            $custom_label = explode( '|', $custom_value );
1436
1437
                            if ( empty( $custom_label ) )
1438
                                continue;
1439
1440
                            if ( 1 == count( $custom_label ) )
1441
                                $custom_label = $custom_value;
1442
                            else {
1443
                                $custom_value = $custom_label[ 0 ];
1444
                                $custom_label = $custom_label[ 1 ];
1445
                            }
1446
1447
							$custom_value = trim( (string) $custom_value );
1448
							$custom_label = trim( (string) $custom_label );
1449
1450
                            $data[ $custom_value ] = $custom_label;
1451
                        }
1452
                    }
1453
                    else
1454
                        $data = $custom;
1455
1456
					$simple = true;
1457
                }
1458
            }
1459
            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' ] ) ) {
1460
                $data = self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ];
1461
1462
				$simple = true;
1463
			}
1464
            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' ] ) ) {
1465
                $data = call_user_func_array(
1466
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data_callback' ],
1467
                    array( $name, $value, $options, $pod, $id )
1468
                );
1469
1470
				$simple = true;
1471
1472
                // Cache data from callback
1473
                if ( !empty( $data ) )
1474
                    self::$related_objects[ $options[ self::$type . '_object' ] ][ 'data' ] = $data;
1475
            }
1476
            elseif ( 'simple_value' != $context ) {
1477
                $pick_val = pods_var( self::$type . '_val', $options );
1478
1479
                if ( 'table' == pods_var( self::$type . '_object', $options ) )
1480
                    $pick_val = pods_var( self::$type . '_table', $options, $pick_val, null, true );
1481
1482 View Code Duplication
                if ( '__current__' == $pick_val ) {
1483
                    if ( is_object( $pod ) )
1484
                        $pick_val = $pod->pod;
1485
                    elseif ( is_array( $pod ) )
1486
                        $pick_val = $pod[ 'name' ];
1487
                    elseif ( 0 < strlen( $pod ) )
1488
                        $pick_val = $pod;
1489
                }
1490
1491
                $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( self::$type . '_object', $options ), $pick_val, null, null, $options );
1492
1493
                $search_data = pods_data();
1494
                $search_data->table( $options[ 'table_info' ] );
1495
1496
                if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
1497
                    $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
1498
                    $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
1499
                }
1500
1501
                $params = array(
1502
                    'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
1503
                    'table' => $search_data->table,
1504
                    'where' => pods_var_raw( self::$type . '_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
1505
                    'orderby' => pods_var_raw( self::$type . '_orderby', $options, null, null, true ),
1506
                    'groupby' => pods_var_raw( self::$type . '_groupby', $options, null, null, true ),
1507
                    //'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...
1508
					'pagination' => false,
1509
					'search' => false
1510
                );
1511
1512
                if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1513
                    $params[ 'select' ] .= ', `t`.`path`';
1514
1515
                if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
1516
                    $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
1517
1518
                if ( empty( $params[ 'where' ] ) || ( !is_array( $params[ 'where' ] ) && strlen( trim( $params[ 'where' ] ) ) < 1 ) )
1519
                    $params[ 'where' ] = array();
1520
                elseif ( !is_array( $params[ 'where' ] ) )
1521
                    $params[ 'where' ] = (array) $params[ 'where' ];
1522
1523
                if ( 'value_to_label' == $context )
1524
                    $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' );
1525
1526
                /* 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...
1527
                if ( !empty( $params[ 'orderby' ] ) )
1528
                    $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
1529
1530
                if ( !empty( $params[ 'groupby' ] ) )
1531
                    $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
1532
1533
                $display = trim( pods_var( self::$type . '_display', $options ), ' {@}' );
1534
1535
                if ( 0 < strlen( $display ) ) {
1536
                    if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
1537
                        if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
1538
                            $search_data->field_index = $display;
1539
1540
                            $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1541
                        }
1542
                        elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
1543
                            $search_data->field_index = $display;
1544
1545
                            if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
1546
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
1547
                            elseif ( 'meta' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] )
1548
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `{$search_data->field_index}`.`meta_value` AS {$search_data->field_index}";
1549
                            else
1550
                                $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1551
                        }
1552
                    }
1553
                    elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
1554
                        $search_data->field_index = $display;
1555
1556
                        $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
1557
                    }
1558
                }
1559
1560
                $autocomplete = false;
1561
1562
                if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_single', $options, 'dropdown' ) )
1563
                    $autocomplete = true;
1564 View Code Duplication
                elseif ( 'multi' == pods_var( self::$type . '_format_type', $options, 'single' ) && 'autocomplete' == pods_var( self::$type . '_format_multi', $options, 'checkbox' ) )
1565
                    $autocomplete = true;
1566
1567
                $hierarchy = false;
1568
1569
                if ( 'data' == $context && !$autocomplete ) {
1570
                    if ( 'single' == pods_var( self::$type . '_format_type', $options, 'single' ) && in_array( pods_var( self::$type . '_format_single', $options, 'dropdown' ), array( 'dropdown', 'radio' ) ) )
1571
                        $hierarchy = true;
1572 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' ) ) )
1573
                        $hierarchy = true;
1574
                }
1575
1576
                if ( $hierarchy && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) )
1577
                    $params[ 'select' ] .= ', ' . $options[ 'table_info' ][ 'field_parent_select' ];
1578
1579
                if ( $autocomplete ) {
1580
					if ( 0 == $limit ) {
1581
						$limit = 30;
1582
					}
1583
1584
                    $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', $limit, $name, $value, $options, $pod, $id, $object_params );
1585
1586
					if ( is_array( $value ) && $params[ 'limit' ] < count( $value ) ) {
1587
						$params[ 'limit' ] = count( $value );
1588
					}
1589
1590
                    $params[ 'page' ] = $page;
1591
1592
                    if ( 'admin_ajax_relationship' == $context ) {
1593
                        $lookup_where = array(
1594
                            $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'"
1595
                        );
1596
1597
                        // @todo Hook into WPML for each table
1598
                        if ( $wpdb->users == $search_data->table ) {
1599
                            $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1600
                            $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1601
                            $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1602
                        }
1603
                        elseif ( $wpdb->posts == $search_data->table ) {
1604
                            $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1605
                            $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1606
                            $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1607
                            $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1608
                        }
1609
                        elseif ( $wpdb->terms == $search_data->table ) {
1610
                            $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1611
                            $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1612
                        }
1613
                        elseif ( $wpdb->comments == $search_data->table ) {
1614
                            $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1615
                            $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1616
                            $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%'";
1617
                        }
1618
1619
                        $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 );
1620
1621
                        if ( !empty( $lookup_where ) )
1622
                            $params[ 'where' ][] = implode( ' OR ', $lookup_where );
1623
1624
                        $orderby = array();
1625
                        $orderby[] = "(`t`.`{$search_data->field_index}` LIKE '%" . pods_sanitize_like( $data_params[ 'query' ] ) . "%' ) DESC";
1626
1627
                        $pick_orderby = pods_var_raw( self::$type . '_orderby', $options, null, null, true );
1628
1629
                        if ( 0 < strlen( $pick_orderby ) )
1630
                            $orderby[] = $pick_orderby;
1631
1632
                        $orderby[] = "`t`.`{$search_data->field_index}`";
1633
                        $orderby[] = "`t`.`{$search_data->field_id}`";
1634
1635
                        $params[ 'orderby' ] = $orderby;
1636
                    }
1637
                }
1638
				elseif ( 0 < $limit ) {
1639
                    $params[ 'limit' ] = $limit;
1640
                    $params[ 'page' ] = $page;
1641
				}
1642
1643
                $extra = '';
1644
1645
                if ( $wpdb->posts == $search_data->table )
1646
                    $extra = ', `t`.`post_type`';
1647
                elseif ( $wpdb->terms == $search_data->table )
1648
                    $extra = ', `tt`.`taxonomy`';
1649
                elseif ( $wpdb->comments == $search_data->table )
1650
                    $extra = ', `t`.`comment_type`';
1651
1652
                $params[ 'select' ] .= $extra;
1653
1654
                if ( 'user' == pods_var( self::$type . '_object', $options ) ) {
1655
                    $roles = pods_var( self::$type . '_user_role', $options );
1656
1657
                    if ( !empty( $roles ) ) {
1658
                        $where = array();
1659
1660
                        foreach ( (array) $roles as $role ) {
1661
                            if ( empty( $role ) || ( pods_clean_name( $role ) != $role && sanitize_title( $role ) != $role ) )
1662
                                continue;
1663
1664
                            $where[] = $wpdb->base_prefix . ( ( is_multisite() && !is_main_site() ) ? get_current_blog_id() . '_' : '' ) . 'capabilities.meta_value LIKE "%\"' . pods_sanitize_like( $role ) . '\"%"';
1665
                        }
1666
1667
                        if ( !empty( $where ) ) {
1668
                            $params[ 'where' ][] = implode( ' OR ', $where );
1669
                        }
1670
                    }
1671
                }
1672
1673
                $results = $search_data->select( $params );
1674
1675
                if ( $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
1676
                    if ( !empty( $value ) ) {
1677
                        $ids = $value;
1678
1679
						if ( is_array( $ids ) && isset( $ids[ 0 ] ) && is_array( $ids[ 0 ] ) ) {
1680
							$ids = wp_list_pluck( $ids, $search_data->field_id );
1681
						}
1682
1683
                        if ( is_array( $ids ) )
1684
                            $ids = implode( ', ', $ids );
1685
1686
                        if ( is_array( $params[ 'where' ] ) )
1687
                            $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
1688
                        if ( !empty( $params[ 'where' ] ) )
1689
                            $params[ 'where' ] .= ' AND ';
1690
1691
                        $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
1692
1693
                        $results = $search_data->select( $params );
1694
                    }
1695
                }
1696
                else
1697
                    $autocomplete = false;
1698
1699
                if ( 'data' == $context ) {
1700
                    self::$field_data = array(
1701
                        'field' => $name,
1702
                        'id' => $options[ 'id' ],
1703
                        'autocomplete' => $autocomplete
1704
                    );
1705
                }
1706
1707
                if ( $hierarchy && !$autocomplete && !empty( $results ) && $options[ 'table_info' ][ 'object_hierarchical' ] && !empty( $options[ 'table_info' ][ 'field_parent' ] ) ) {
1708
                    $args = array(
1709
                        'id' => $options[ 'table_info' ][ 'field_id' ],
1710
                        'index' => $options[ 'table_info' ][ 'field_index' ],
1711
                        'parent' => $options[ 'table_info' ][ 'field_parent' ],
1712
                    );
1713
1714
                    $results = pods_hierarchical_select( $results, $args );
1715
                }
1716
1717
                $ids = array();
1718
1719
                if ( !empty( $results ) ) {
1720
                    $display_filter = pods_var( 'display_filter', pods_var_raw( 'options', pods_var_raw( $search_data->field_index, $search_data->pod_data[ 'object_fields' ] ) ) );
1721
1722
                    foreach ( $results as $result ) {
1723
                        $result = get_object_vars( $result );
1724
1725
                        if ( !isset( $result[ $search_data->field_id ] ) || !isset( $result[ $search_data->field_index ] ) )
1726
                            continue;
1727
1728
                        $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
1729
1730
                        $object = $object_type = '';
1731
1732
                        if ( $wpdb->posts == $search_data->table && isset( $result[ 'post_type' ] ) ) {
1733
                            $object = $result[ 'post_type' ];
1734
                            $object_type = 'post_type';
1735
                        }
1736
                        elseif ( $wpdb->terms == $search_data->table && isset( $result[ 'taxonomy' ] ) ) {
1737
                            $object = $result[ 'taxonomy' ];
1738
                            $object_type = 'taxonomy';
1739
                        }
1740
1741
                        if ( 0 < strlen( $display_filter ) ) {
1742
                            $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' ] ) ) );
1743
1744
                            $args = array(
1745
                                $display_filter,
1746
                                $result[ $search_data->field_index ]
1747
                            );
1748
1749
                            if ( !empty( $display_filter_args ) ) {
1750
                                foreach ( (array) $display_filter_args as $display_filter_arg ) {
1751
                                    if ( isset( $result[ $display_filter_arg ] ) )
1752
                                        $args[] = $result[ $display_filter_arg ];
1753
                                }
1754
                            }
1755
1756
                            $result[ $search_data->field_index ] = call_user_func_array( 'apply_filters', $args );
1757
                        }
1758
1759
                        if ( in_array( $options[ self::$type . '_object' ], array( 'site', 'network' ) ) )
1760
                            $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
1761
                        elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
1762
                            $result[ $search_data->field_index ] = '(No Title)';
1763
1764
                        if ( 'admin_ajax_relationship' == $context ) {
1765
                            $items[] = array(
1766
                                'id' => $result[ $search_data->field_id ],
1767
                                'text' => $result[ $search_data->field_index ],
1768
                                'image' => ''
1769
                            );
1770
                        }
1771
                        else
1772
                            $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
1773
1774
                        $ids[] = $result[ $search_data->field_id ];
1775
                    }
1776
                }
1777
            }
1778
1779
			if ( $simple && 'admin_ajax_relationship' == $context ) {
1780
				$found_data = array();
1781
1782
				foreach ( $data as $k => $v ) {
1783
					if ( false !== stripos( $v, $data_params[ 'query' ] ) || false !== stripos( $k, $data_params[ 'query' ] ) ) {
1784
						$found_data[ $k ] = $v;
1785
					}
1786
				}
1787
1788
				$data = $found_data;
1789
			}
1790
        }
1791
1792
        if ( 'admin_ajax_relationship' == $context ) {
1793
            if ( empty( $items ) && !empty( $data ) ) {
1794
                foreach ( $data as $k => $v ) {
1795
                    $items[] = array(
1796
                        'id' => $k,
1797
                        'text' => $v,
1798
                        'image' => ''
1799
                    );
1800
                }
1801
            }
1802
1803
            return $items;
1804
        }
1805
1806
        return $data;
1807
    }
1808
1809
    /**
1810
     * AJAX call to refresh relationship field markup (supports adding new records modally)
1811
     *
1812
     * @since 2.7
1813
     */
1814
    public function admin_ajax_relationship_popup () {
1815
1816
        $data = pods_unslash( (array) $_POST );
1817
1818
        // Get the field information
1819
        $params = array(
1820
            'pod_id' => $data[ 'pod_id' ],
1821
            'id'     => $data[ 'field_id' ]
1822
        );
1823
        $field = pods_api()->load_field( $params );
1824
1825
        // Get Pods object for this item
1826
        $pod = pods( $field[ 'pod' ], $data[ 'item_id' ] );
1827
1828
        // Get the relationship field's value(s)
1829
        $field_name = $field[ 'name' ];
1830
        $params = array(
1831
            'name'    => $field_name,
1832
            'in_form' => true
1833
        );
1834
        $value = $pod->field( $params);
1835
1836
        // Build the markup and return it to the caller
1837
        $meta_field_name = 'pods_meta_' . $field_name;
1838
        $output = PodsForm::field( $meta_field_name, $value, 'pick', $field, $pod, $data[ 'item_id' ] );
1839
        echo $output;
1840
1841
        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...
1842
    }
1843
1844
    /**
1845
     * Handle autocomplete AJAX
1846
     *
1847
     * @since 2.3
1848
     */
1849
    public function admin_ajax_relationship () {
1850
		pods_session_start();
1851
1852
        // Sanitize input
1853
        $params = pods_unslash( (array) $_POST );
1854
1855 View Code Duplication
        foreach ( $params as $key => $value ) {
1856
            if ( 'action' == $key )
1857
                continue;
1858
1859
            unset( $params[ $key ] );
1860
1861
            $params[ str_replace( '_podsfix_', '', $key ) ] = $value;
1862
        }
1863
1864
        $params = (object) $params;
1865
1866
        $uid = @session_id();
1867
1868
        if ( is_user_logged_in() )
1869
            $uid = 'user_' . get_current_user_id();
1870
1871
        $nonce_check = 'pods_relationship_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field;
1872
1873 View Code Duplication
        if ( !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) )
1874
            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...
1875
1876
        $api = pods_api();
1877
1878
        $pod = $api->load_pod( array( 'id' => (int) $params->pod ) );
1879
        $field = $api->load_field( array( 'id' => (int) $params->field, 'table_info' => true ) );
1880
        $id = (int) $params->id;
1881
1882
        $limit = 15;
1883
1884
        if ( isset( $params->limit ) )
1885
            $limit = (int) $params->limit;
1886
1887
        $page = 1;
1888
1889
        if ( isset( $params->page ) )
1890
            $page = (int) $params->page;
1891
1892
        if ( !isset( $params->query ) || strlen( trim( $params->query ) ) < 1 )
1893
            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...
1894 View Code Duplication
        elseif ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) )
1895
            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...
1896
        elseif ( 'pick' != $field[ 'type' ] || empty( $field[ 'table_info' ] ) )
1897
            pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin );
0 ignored issues
show
Bug introduced by
The property admin cannot be accessed from this context as it is declared private in class PodsInit.

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

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

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