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

PodsField_Pick::delete()   C

Complexity

Conditions 14
Paths 40

Size

Total Lines 44
Code Lines 24

Duplication

Lines 7
Ratio 15.91 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 14
eloc 24
c 2
b 1
f 0
nc 40
nop 4
dl 7
loc 44
rs 5.0864

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

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