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

PodsField_Pick::admin_ajax_relationship_popup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1. Pass all data via parameters

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

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

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

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

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

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

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