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

PodsField_Pick::input()   D

Complexity

Conditions 19
Paths 128

Size

Total Lines 76
Code Lines 50

Duplication

Lines 28
Ratio 36.84 %

Importance

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

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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