Completed
Pull Request — 2.x (#4370)
by
unknown
05:02
created

data.php ➔ pods_unslash()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 32
Code Lines 17

Duplication

Lines 32
Ratio 100 %

Importance

Changes 0
Metric Value
cc 10
eloc 17
nc 5
nop 1
dl 32
loc 32
rs 4.8196
c 0
b 0
f 0

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\Global\Functions\Data
4
 */
5
/**
6
 * Filter input and return sanitized output
7
 *
8
 * @param mixed $input The string, array, or object to sanitize
9
 * @param array $params Additional options
10
 *
11
 * @return array|mixed|object|string|void
12
 *
13
 * @since 1.2.0
14
 *
15
 * @see wp_slash
16
 */
17
function pods_sanitize( $input, $params = array() ) {
18
19
	if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) {
20
		return $input;
21
	}
22
23
	$output = array();
24
25
	$defaults = array(
26
		'nested' => false,
27
		'type' => null // %s %d %f etc
28
	);
29
30 View Code Duplication
	if ( !is_array( $params ) ) {
31
		$defaults[ 'type' ] = $params;
32
33
		$params = $defaults;
34
	}
35
	else {
36
		$params = array_merge( $defaults, (array) $params );
37
	}
38
39
	if ( is_object( $input ) ) {
40
		$input = get_object_vars( $input );
41
42
		$n_params = $params;
43
		$n_params[ 'nested' ] = true;
44
45
		foreach ( $input as $key => $val ) {
46
			$output[ pods_sanitize( $key ) ] = pods_sanitize( $val, $n_params );
47
		}
48
49
		$output = (object) $output;
50
	}
51 View Code Duplication
	elseif ( is_array( $input ) ) {
52
		$n_params = $params;
53
		$n_params[ 'nested' ] = true;
54
55
		foreach ( $input as $key => $val ) {
56
			$output[ pods_sanitize( $key ) ] = pods_sanitize( $val, $n_params );
57
		}
58
	}
59 View Code Duplication
	elseif ( !empty( $params[ 'type' ] ) && false !== strpos( $params[ 'type' ], '%' ) ) {
60
		/**
61
		 * @var $wpdb wpdb
62
		 */
63
		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...
64
65
		$output = $wpdb->prepare( $params[ 'type' ], $output );
66
	}
67
	// @todo Switch this full over to esc_sql once we get sanitization sane again in PodsAPI so we *don't* have to unsanitize in various places
68
	elseif ( function_exists( 'wp_slash' ) ) {
69
		$output = wp_slash( $input );
70
	}
71
	else {
72
		$output = esc_sql( $input );
73
	}
74
75
	return $output;
76
77
}
78
79
/**
80
 * Filter input and return sanitized SQL LIKE output
81
 *
82
 * @param mixed $input The string, array, or object to sanitize
83
 *
84
 * @return array|mixed|object|string|void
85
 *
86
 * @since 2.3.9
87
 *
88
 * @see like_escape
89
 */
90 View Code Duplication
function pods_sanitize_like( $input ) {
0 ignored issues
show
Duplication introduced by
This function 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...
91
92
	if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) {
93
		return $input;
94
	}
95
96
	$output = array();
97
98
	if ( is_object( $input ) ) {
99
		$input = get_object_vars( $input );
100
101
		foreach ( $input as $key => $val ) {
102
			$output[ $key ] = pods_sanitize_like( $val );
103
		}
104
105
		$output = (object) $output;
106
	}
107
	elseif ( is_array( $input ) ) {
108
		foreach ( $input as $key => $val ) {
109
			$output[ $key ] = pods_sanitize_like( $val );
110
		}
111
	}
112
	else {
113
		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...
114
		$input = pods_unslash( $input );
115
116
		$output = pods_sanitize( $wpdb->esc_like( $input ) );
117
	}
118
119
	return $output;
120
121
}
122
123
/**
124
 * Filter input and return slashed output
125
 *
126
 * @param mixed $input The string, array, or object to sanitize
127
 * @param array $params Additional options
128
 *
129
 * @return array|mixed|object|string|void
130
 *
131
 * @since 2.3.9
132
 *
133
 * @see wp_slash
134
 */
135
function pods_slash( $input, $params = array() ) {
136
137
	if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) {
138
		return $input;
139
	}
140
141
	$output = array();
142
143
	$defaults = array(
144
		'type' => null // %s %d %f etc
145
	);
146
147 View Code Duplication
	if ( !is_array( $params ) ) {
148
		$defaults[ 'type' ] = $params;
149
150
		$params = $defaults;
151
	}
152
	else {
153
		$params = array_merge( $defaults, (array) $params );
154
	}
155
156
	if ( empty( $input ) ) {
157
		$output = $input;
158
	}
159
	elseif ( is_object( $input ) ) {
160
		$input = get_object_vars( $input );
161
162
		foreach ( $input as $key => $val ) {
163
			$output[ $key ] = pods_slash( $val, $params );
164
		}
165
166
		$output = (object) $output;
167
	}
168
	elseif ( is_array( $input ) ) {
169
		foreach ( $input as $key => $val ) {
170
			$output[ $key ] = pods_slash( $val, $params );
171
		}
172
	}
173 View Code Duplication
	elseif ( !empty( $params[ 'type' ] ) && false !== strpos( $params[ 'type' ], '%' ) ) {
174
		/**
175
		 * @var $wpdb wpdb
176
		 */
177
		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...
178
179
		$output = $wpdb->prepare( $params[ 'type' ], $output );
180
	}
181
	elseif ( function_exists( 'wp_slash' ) ) {
182
		$output = wp_slash( $input );
183
	}
184
	else {
185
		$output = addslashes( $input );
186
	}
187
188
	return $output;
189
190
}
191
192
/**
193
 * Filter input and return unsanitized output
194
 *
195
 * @param mixed $input The string, array, or object to unsanitize
196
 * @param array $params Additional options
197
 *
198
 * @return array|mixed|object|string|void
199
 *
200
 * @since 1.2.0
201
 */
202
function pods_unsanitize( $input, $params = array() ) {
203
204
	if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) {
205
		return $input;
206
	}
207
208
	$output = array();
209
210
	if ( empty( $input ) ) {
211
		$output = $input;
212
	}
213
	elseif ( is_object( $input ) ) {
214
		$input = get_object_vars( $input );
215
216
		$n_params = (array) $params;
217
		$n_params[ 'nested' ] = true;
218
219
		foreach ( $input as $key => $val ) {
220
			$output[ pods_unsanitize( $key ) ] = pods_unsanitize( $val, $n_params );
221
		}
222
223
		$output = (object) $output;
224
	}
225 View Code Duplication
	elseif ( is_array( $input ) ) {
226
		$n_params             = (array) $params;
227
		$n_params[ 'nested' ] = true;
228
229
		foreach ( $input as $key => $val ) {
230
			$output[ pods_unsanitize( $key ) ] = pods_unsanitize( $val, $n_params );
231
		}
232
	}
233
	else {
234
		$output = wp_unslash( $input );
235
	}
236
237
	return $output;
238
239
}
240
241
/**
242
 * Filter input and return unslashed output
243
 *
244
 * @param mixed $input The string, array, or object to unsanitize
245
 *
246
 * @return array|mixed|object|string|void
247
 *
248
 * @since 2.3.9
249
 *
250
 * @see wp_unslash
251
 */
252 View Code Duplication
function pods_unslash( $input ) {
0 ignored issues
show
Duplication introduced by
This function 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...
253
254
	if ( '' === $input || is_int( $input ) || is_float( $input ) || empty( $input ) ) {
255
		return $input;
256
	}
257
258
	$output = array();
259
260
	if ( empty( $input ) ) {
261
		$output = $input;
262
	}
263
	elseif ( is_object( $input ) ) {
264
		$input = get_object_vars( $input );
265
266
		foreach ( $input as $key => $val ) {
267
			$output[ $key ] = pods_unslash( $val );
268
		}
269
270
		$output = (object) $output;
271
	}
272
	elseif ( is_array( $input ) ) {
273
		foreach ( $input as $key => $val ) {
274
			$output[ $key ] = pods_unslash( $val );
275
		}
276
	}
277
	else {
278
		$output = wp_unslash( $input );
279
	}
280
281
	return $output;
282
283
}
284
285
/**
286
 * Filter input and return sanitized output
287
 *
288
 * @param mixed $input The string, array, or object to sanitize
289
 * @param string $charlist (optional) List of characters to be stripped from the input.
290
 * @param string $lr Direction of the trim, can either be 'l' or 'r'.
291
 *
292
 * @return array|object|string
293
 * @since 1.2.0
294
 */
295
function pods_trim ( $input, $charlist = null, $lr = null ) {
296
297
	$output = array();
298
299
	if ( is_object( $input ) ) {
300
		$input = get_object_vars( $input );
301
302
		foreach ( $input as $key => $val ) {
303
			$output[ pods_sanitize( $key ) ] = pods_trim( $val, $charlist, $lr );
304
		}
305
306
		$output = (object) $output;
307
	}
308
	elseif ( is_array( $input ) ) {
309
		foreach ( $input as $key => $val ) {
310
			$output[ pods_sanitize( $key ) ] = pods_trim( $val, $charlist, $lr );
311
		}
312
	}
313
	else {
314
		if ( 'l' == $lr ) {
315
			$output = ltrim( $input, $charlist );
316
		}
317
		elseif ( 'r' == $lr ) {
318
			$output = rtrim( $input, $charlist );
319
		}
320
		else {
321
			$output = trim( $input, $charlist );
322
		}
323
	}
324
325
	return $output;
326
327
}
328
329
/**
330
 * Return a variable (if exists)
331
 *
332
 * @param mixed $var The variable name, can also be a modifier for specific types
333
 * @param string|array|object $type (optional) Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates
334
 * @param mixed $default (optional) The default value to set if variable doesn't exist
335
 * @param bool $strict (optional) Only allow values (must not be empty)
336
 * @param array $params (optional) Set 'casting'=>true to cast value from $default, 'allowed'=>$allowed to restrict a value to what's allowed
337
 *
338
 * @return mixed The variable (if exists), or default value
339
 * @since 2.3.10
340
 */
341
function pods_v( $var = null, $type = 'get', $default = null, $strict = false, $params = array() ) {
342
343
	$defaults = array(
344
		'casting' => false,
345
		'allowed' => null
346
	);
347
348
	$params = (object) array_merge( $defaults, (array) $params );
349
350
	$output = null;
351
352
	if ( null === $type || '' === $type ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
353
		// Invalid $type
354
	} elseif ( is_array( $type ) ) {
355
		if ( isset( $type[ $var ] ) ) {
356
			$output = $type[ $var ];
357
		}
358
	} elseif ( is_object( $type ) ) {
359
		if ( isset( $type->{$var} ) ) {
360
			$output = $type->{$var};
361
		}
362
	} else {
363
		$type = strtolower( (string) $type );
364
		switch ( $type ) {
365
			case 'get':
366
				if ( isset( $_GET[ $var ] ) ) {
367
					$output = pods_unslash( $_GET[ $var ] );
368
				}
369
				break;
370
			case 'post':
371
				if ( isset( $_POST[ $var ] ) ) {
372
					$output = pods_unslash( $_POST[ $var ] );
373
				}
374
				break;
375
			case 'request':
376
				if ( isset( $_REQUEST[ $var ] ) ) {
377
					$output = pods_unslash( $_REQUEST[ $var ] );
378
				}
379
				break;
380
			case 'url':
381
			case 'uri':
382
				$url = parse_url( pods_current_url() );
383
				$uri = trim( $url[ 'path' ], '/' );
384
				$uri = array_filter( explode( '/', $uri ) );
385
386
				if ( 'first' == $var ) {
387
					$var = 0;
388
				} elseif ( 'last' == $var ) {
389
					$var = - 1;
390
				}
391
392 View Code Duplication
				if ( is_numeric( $var ) ) {
393
					$output = ( $var < 0 ) ? pods_v( count( $uri ) + $var, $uri ) : pods_v( $var, $uri );
394
				}
395
				break;
396
			case 'url-relative':
397
				$url_raw = pods_current_url();
398
				$prefix  = get_site_url();
399
400 View Code Duplication
				if ( substr( $url_raw, 0, strlen( $prefix ) ) == $prefix ) {
401
					$url_raw = substr( $url_raw, strlen( $prefix ) + 1, strlen( $url_raw ) );
402
				}
403
404
				$url = parse_url( $url_raw );
405
				$uri = trim( $url[ 'path' ], '/' );
406
				$uri = array_filter( explode( '/', $uri ) );
407
408
				if ( 'first' == $var ) {
409
					$var = 0;
410
				} elseif ( 'last' == $var ) {
411
					$var = - 1;
412
				}
413
414 View Code Duplication
				if ( is_numeric( $var ) ) {
415
					$output = ( $var < 0 ) ? pods_v( count( $uri ) + $var, $uri ) : pods_v( $var, $uri );
416
				}
417
				break;
418
			case 'template-url':
419
				$output = get_template_directory_uri();
420
				break;
421
			case 'stylesheet-url':
422
				$output = get_stylesheet_directory_uri();
423
				break;
424 View Code Duplication
			case 'site-url':
425
				$blog_id = $scheme = null;
426
				$path    = '';
427
428
				if ( is_array( $var ) ) {
429
					if ( isset( $var[ 0 ] ) ) {
430
						$blog_id = $var[ 0 ];
431
					} elseif ( isset( $var[ 1 ] ) ) {
432
						$path = $var[ 1 ];
433
					} elseif ( isset( $var[ 2 ] ) ) {
434
						$scheme = $var[ 2 ];
435
					}
436
				} else {
437
					$blog_id = $var;
438
				}
439
440
				$output = get_site_url( $blog_id, $path, $scheme );
441
				break;
442 View Code Duplication
			case 'home-url':
443
				$blog_id = $scheme = null;
444
				$path    = '';
445
446
				if ( is_array( $var ) ) {
447
					if ( isset( $var[ 0 ] ) ) {
448
						$blog_id = $var[ 0 ];
449
					} elseif ( isset( $var[ 1 ] ) ) {
450
						$path = $var[ 1 ];
451
					} elseif ( isset( $var[ 2 ] ) ) {
452
						$scheme = $var[ 2 ];
453
					}
454
				} else {
455
					$blog_id = $var;
456
				}
457
458
				$output = get_home_url( $blog_id, $path, $scheme );
459
				break;
460 View Code Duplication
			case 'admin-url':
461
				$blog_id = $scheme = null;
462
				$path    = '';
463
464
				if ( is_array( $var ) ) {
465
					if ( isset( $var[ 0 ] ) ) {
466
						$blog_id = $var[ 0 ];
467
					} elseif ( isset( $var[ 1 ] ) ) {
468
						$path = $var[ 1 ];
469
					} elseif ( isset( $var[ 2 ] ) ) {
470
						$scheme = $var[ 2 ];
471
					}
472
				} else {
473
					$blog_id = $var;
474
				}
475
476
				$output = get_admin_url( $blog_id, $path, $scheme );
477
				break;
478
			case 'includes-url':
479
				$output = includes_url( $var );
480
				break;
481
			case 'content-url':
482
				$output = content_url( $var );
483
				break;
484
			case 'plugins-url':
485
				$path = $plugin = '';
486
487
				if ( is_array( $var ) ) {
488
					if ( isset( $var[ 0 ] ) ) {
489
						$path = $var[ 0 ];
490
					} elseif ( isset( $var[ 1 ] ) ) {
491
						$plugin = $var[ 1 ];
492
					}
493
				} else {
494
					$path = $var;
495
				}
496
497
				$output = plugins_url( $path, $plugin );
498
				break;
499 View Code Duplication
			case 'network-site-url':
500
				$path   = '';
501
				$scheme = null;
502
503
				if ( is_array( $var ) ) {
504
					if ( isset( $var[ 0 ] ) ) {
505
						$path = $var[ 0 ];
506
					} elseif ( isset( $var[ 1 ] ) ) {
507
						$scheme = $var[ 1 ];
508
					}
509
				} else {
510
					$path = $var;
511
				}
512
513
				$output = network_site_url( $path, $scheme );
514
				break;
515 View Code Duplication
			case 'network-home-url':
516
				$path   = '';
517
				$scheme = null;
518
519
				if ( is_array( $var ) ) {
520
					if ( isset( $var[ 0 ] ) ) {
521
						$path = $var[ 0 ];
522
					} elseif ( isset( $var[ 1 ] ) ) {
523
						$scheme = $var[ 1 ];
524
					}
525
				} else {
526
					$path = $var;
527
				}
528
529
				$output = network_home_url( $path, $scheme );
530
				break;
531 View Code Duplication
			case 'network-admin-url':
532
				$path   = '';
533
				$scheme = null;
534
535
				if ( is_array( $var ) ) {
536
					if ( isset( $var[ 0 ] ) ) {
537
						$path = $var[ 0 ];
538
					} elseif ( isset( $var[ 1 ] ) ) {
539
						$scheme = $var[ 1 ];
540
					}
541
				} else {
542
					$path = $var;
543
				}
544
545
				$output = network_admin_url( $path, $scheme );
546
				break;
547 View Code Duplication
			case 'user-admin-url':
548
				$path   = '';
549
				$scheme = null;
550
551
				if ( is_array( $var ) ) {
552
					if ( isset( $var[ 0 ] ) ) {
553
						$path = $var[ 0 ];
554
					} elseif ( isset( $var[ 1 ] ) ) {
555
						$scheme = $var[ 1 ];
556
					}
557
				} else {
558
					$path = $var;
559
				}
560
561
				$output = user_admin_url( $path, $scheme );
562
				break;
563
			case 'prefix':
564
				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...
565
566
				$output = $wpdb->prefix;
567
				break;
568
			case 'server':
569
				if ( ! pods_strict() ) {
570
					if ( isset( $_SERVER[ $var ] ) ) {
571
						$output = pods_unslash( $_SERVER[ $var ] );
572
					} elseif ( isset( $_SERVER[ strtoupper( $var ) ] ) ) {
573
						$output = pods_unslash( $_SERVER[ strtoupper( $var ) ] );
574
					}
575
				}
576
				break;
577
			case 'session':
578
				if ( isset( $_SESSION[ $var ] ) )
579
					$output = $_SESSION[ $var ];
580
				break;
581
			case 'global':
582
			case 'globals':
583
				if ( isset( $GLOBALS[ $var ] ) )
584
					$output = $GLOBALS[ $var ];
585
				break;
586
			case 'cookie':
587
				if ( isset( $_COOKIE[ $var ] ) )
588
					$output = pods_unslash( $_COOKIE[ $var ] );
589
				break;
590
			case 'constant':
591
				if ( defined( $var ) )
592
					$output = constant( $var );
593
				break;
594
			case 'user':
595
				if ( is_user_logged_in() ) {
596
					$user = get_userdata( get_current_user_id() );
597
598
					if ( isset( $user->{$var} ) ) {
599
						$value = $user->{$var};
600
					} elseif ( 'role' == $var ) {
601
						$value = '';
602
603
						if ( ! empty( $user->roles ) ) {
604
							$value = array_shift( $user->roles );
605
						}
606
					} else {
607
						$value = get_user_meta( $user->ID, $var );
608
					}
609
610
					if ( is_array( $value ) && ! empty( $value ) ) {
611
						$output = $value;
612
					} elseif ( ! is_array( $value ) && 0 < strlen( $value ) ) {
613
						$output = $value;
614
					}
615
				}
616
				break;
617
			case 'option':
618
				$output = get_option( $var, $default );
619
				break;
620
			case 'site-option':
621
				$output = get_site_option( $var, $default );
622
				break;
623
			case 'transient':
624
				$output = get_transient( $var );
625
				break;
626
			case 'site-transient':
627
				$output = get_site_transient( $var );
628
				break;
629 View Code Duplication
			case 'cache':
630
				if ( isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) ) {
631
					$group = 'default';
632
					$force = false;
633
634
					if ( ! is_array( $var ) ) {
635
						$var = explode( '|', $var );
636
					}
637
638
					if ( isset( $var[ 0 ] ) ) {
639
						if ( isset( $var[ 1 ] ) ) {
640
							$group = $var[ 1 ];
641
						}
642
643
						if ( isset( $var[ 2 ] ) ) {
644
							$force = $var[ 2 ];
645
						}
646
647
						$var = $var[ 0 ];
648
649
						$output = wp_cache_get( $var, $group, $force );
650
					}
651
				}
652
				break;
653 View Code Duplication
			case 'pods-transient':
654
				$callback = null;
655
656
				if ( ! is_array( $var ) ) {
657
					$var = explode( '|', $var );
658
				}
659
660
				if ( isset( $var[ 0 ] ) ) {
661
					if ( isset( $var[ 1 ] ) ) {
662
						$callback = $var[ 1 ];
663
					}
664
665
					$var = $var[ 0 ];
666
667
					$output = pods_transient_get( $var, $callback );
668
				}
669
				break;
670 View Code Duplication
			case 'pods-site-transient':
671
				$callback = null;
672
673
				if ( ! is_array( $var ) ) {
674
					$var = explode( '|', $var );
675
				}
676
677
				if ( isset( $var[ 0 ] ) ) {
678
					if ( isset( $var[ 1 ] ) ) {
679
						$callback = $var[ 1 ];
680
					}
681
682
					$var = $var[ 0 ];
683
684
					$output = pods_site_transient_get( $var, $callback );
685
				}
686
				break;
687 View Code Duplication
			case 'pods-cache':
688
				if ( isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) ) {
689
					$group    = 'default';
690
					$callback = null;
691
692
					if ( ! is_array( $var ) ) {
693
						$var = explode( '|', $var );
694
					}
695
696
					if ( isset( $var[ 0 ] ) ) {
697
						if ( isset( $var[ 1 ] ) ) {
698
							$group = $var[ 1 ];
699
						}
700
701
						if ( isset( $var[ 2 ] ) ) {
702
							$callback = $var[ 2 ];
703
						}
704
705
						$var = $var[ 0 ];
706
707
						$output = pods_cache_get( $var, $group, $callback );
708
					}
709
				}
710
				break;
711
			case 'pods-option-cache':
712
				$group    = 'default';
713
				$callback = null;
714
715
				if ( ! is_array( $var ) ) {
716
					$var = explode( '|', $var );
717
				}
718
719
				if ( isset( $var[ 0 ] ) ) {
720
					if ( isset( $var[ 1 ] ) ) {
721
						$group = $var[ 1 ];
722
					}
723
724
					if ( isset( $var[ 2 ] ) ) {
725
						$callback = $var[ 2 ];
726
					}
727
728
					$var = $var[ 0 ];
729
730
					$output = pods_option_cache_get( $var, $group, $callback );
731
				}
732
				break;
733
			case 'date':
734
				$var = explode( '|', $var );
735
736
				if ( ! empty( $var ) ) {
737
					$output = date_i18n( $var[ 0 ], ( isset( $var[ 1 ] ) ? strtotime( $var[ 1 ] ) : false ) );
738
				}
739
				break;
740
			case 'pods':
741
			case 'pods_display':
742
				/**
743
				 * @var $pods Pods
744
				 */
745
				global $pods;
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...
746
747
				if ( is_object( $pods ) && 'Pods' == get_class( $pods ) ) {
748
					if ( 'pods' === $type ) {
749
						$output = $pods->field( $var );
750
751
						if ( is_array( $output ) ) {
752
							$options = array(
753
								'field'  => $var,
754
								'fields' => $pods->fields
755
							);
756
757
							$output = pods_serial_comma( $output, $options );
758
						}
759
					} elseif ( 'pods_display' === $type ) {
760
						$output = $pods->display( $var );
761
					}
762
				}
763
				break;
764
			case 'post_id':
765
				if ( empty( $var ) ) {
766
					if ( ! empty( $default ) ) {
767
						$post_id = $default;
768
					} else {
769
						// If no $var and no $default then use current post ID
770
						$post_id = get_the_ID();
771
					}
772
				} else {
773
					$post_id = $var;
774
				}
775
				if ( did_action( 'wpml_loaded' ) ) {
776
					/* Only call filter if WPML is installed */
777
					$post_type = get_post_type( $post_id );
778
					$post_id = apply_filters( 'wpml_object_id', $post_id, $post_type, true );
779
				} elseif ( function_exists( 'pll_get_post' ) ) {
780
					$polylang_id = pll_get_post( $post_id );
781
					if ( ! empty( $polylang_id ) ) {
782
						$post_id = $polylang_id;
783
					}
784
				}
785
				// Add other translation plugin specific code here
786
787
				/**
788
				 * Filter to override post_id
789
				 *
790
				 * Generally used with language translation plugins in order to return the post id of a
791
				 * translated post
792
				 *
793
				 * @param  int $post_id The post ID of current post
794
				 * @param  mixed $default The default value to set if variable doesn't exist
795
				 * @param  mixed $var The variable name, can also be a modifier for specific types
796
				 * @param  bool $strict Only allow values (must not be empty)
797
				 * @param  array $params Set 'casting'=>true to cast value from $default, 'allowed'=>$allowed to restrict a value to what's allowed
798
				 *
799
				 * @since 2.6.6
800
				 */
801
				$output = apply_filters( 'pods_var_post_id', $post_id, $default, $var, $strict, $params );
802
				break;
803
			default:
804
				$output = apply_filters( 'pods_var_' . $type, $default, $var, $strict, $params );
805
		}
806
	}
807
808
	if ( null !== $default ) {
809
		// Set default
810
		if ( null === $output ) {
811
			$output = $default;
812
		}
813
814
		// Casting
815
		if ( true === $params->casting ) {
816
			$output = pods_cast( $output, $default );
817
		}
818
	}
819
820
	// Strict defaults for empty values
821
	if ( true === $strict ) {
822
		if ( empty( $output ) ) {
823
			$output = $default;
824
		}
825
	}
826
827
	// Allowed values
828
	if ( null !== $params->allowed ) {
829
		if ( is_array( $params->allowed ) ) {
830
			// Not in array and is not the same array
831
			if ( ! in_array( $output, $params->allowed ) && ( ! is_array( $output ) || $output !== $params->allowed ) ) {
832
				$output = $default;
833
			}
834
		} elseif ( $output !== $params->allowed ) {
835
			// Value doesn't match
836
			$output = $default;
837
		}
838
	}
839
840
	return $output;
841
}
842
843
/**
844
 * Return a sanitized variable (if exists)
845
 *
846
 * @param mixed $var The variable name, can also be a modifier for specific types
847
 * @param string|array|object $type (optional) Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates
848
 * @param mixed $default (optional) The default value to set if variable doesn't exist
849
 * @param bool $strict (optional) Only allow values (must not be empty)
850
 * @param array $params (optional) Set 'casting'=>true to cast value from $default, 'allowed'=>$allowed to restrict a value to what's allowed
851
 *
852
 * @return mixed The variable (if exists), or default value
853
 * @since 2.3.10
854
 *
855
 * @see pods_v
856
 */
857
function pods_v_sanitized( $var = null, $type = 'get', $default = null, $strict = false, $params = array() ) {
858
859
	$output = pods_v( $var, $type, $default, $strict, $params );
860
861
	$output = pods_sanitize( $output, $params );
862
863
	return $output;
864
865
}
866
867
/**
868
 * Set a variable
869
 *
870
 * @param mixed $value The value to be set
871
 * @param mixed $var The variable name, or URI segment position / query var name (if $type is 'url')
872
 * @param string|array|object $type (optional) Super globals, url/url-relative, constants, globals, user data, Pod field values
873
 *
874
 * @return mixed Updated URL (if $type is 'url'), $value (if $type is 'constant'), Item ID (if $type is 'pods'), $type, or false if not set
875
 * @since 2.3.10
876
 */
877
function pods_v_set( $value, $var, $type = 'get' ) {
878
879
	$ret = false;
880
881
	if ( null === $var || '' === $var ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
882
		// Invalid $var
883
	}
884
	elseif ( null === $type || '' === $type ) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
885
		// Invalid $type
886
	}
887
	elseif ( is_array( $type ) ) {
888
		$type[ $var ] = $value;
889
890
		$ret = $type;
891
	}
892
	elseif ( is_object( $type ) ) {
893
		$type->{$var} = $value;
894
895
		$ret = $type;
896
	}
897
	else {
898
		$type = strtolower( $type );
899
900
		if ( 'get' == $type ) {
901
			$_GET[ $var ] = $value;
902
903
			$ret = $_GET;
904
		}
905
		elseif ( 'post' == $type ) {
906
			$_POST[ $var ] = $value;
907
908
			$ret = $_POST;
909
		}
910
		elseif ( 'request' == $type ) {
911
			$_REQUEST[ $var ] = $value;
912
913
			$ret = $_REQUEST;
914
		}
915
		elseif ( 'url' == $type ) {
916
			if ( is_numeric( $var ) && function_exists( 'http_build_url' ) ) {
917
				$url = parse_url( pods_current_url() );
918
				$uri = trim( $url[ 'path' ], '/' );
919
				$uri = array_filter( explode( '/', $uri ) );
920
921
				if ( 'first' == $var ) {
922
					$var = 0;
923
				}
924
				elseif ( 'last' == $var ) {
925
					$var = -1;
926
				}
927
928
				if ( $var < 0 ) {
929
					$uri[ count( $uri ) + $var ] = $value;
930
				}
931
				else {
932
					$uri[ $var ] = $value;
933
				}
934
935
				$url[ 'path' ] = '/' . implode( '/', $uri ) . '/';
936
				$url[ 'path' ] = trim( $url[ 'path' ], '/' );
937
938
				$ret = http_build_url( $url );
939
			}
940
			else {
941
				$ret = add_query_arg( array( $var => $value ) );
942
			}
943
		}
944
		elseif ( 'server' == $type ) {
945
			$_SERVER[ $var ] = $value;
946
947
			$ret = $_SERVER;
948
		}
949
		elseif ( in_array( $type, array( 'global', 'globals' ) ) ) {
950
			$GLOBALS[ $var ] = $value;
951
952
			$ret = $GLOBALS;
953
		}
954
		elseif ( 'session' == $type ) {
955
			// Session start
956
			pods_session_start();
957
958
			$_SESSION[ $var ] = $value;
959
960
			$ret = $_SESSION;
961
		}
962
		elseif ( 'cookie' == $type && !headers_sent() ) {
963
			setcookie( $var, $value, time() + 10 * DAY_IN_SECONDS, COOKIEPATH );
964
965
			$ret = $_COOKIE;
966
		}
967
		elseif ( 'constant' == $type && !defined( $var ) && ( is_scalar( $value ) || null === $value ) ) {
968
			define( $var, $value );
969
970
			$ret = constant( $var );
971
		}
972
		elseif ( 'user' == $type && is_user_logged_in() ) {
973
			$user = get_userdata( get_current_user_id() );
974
975
			$user_data = $user->to_array();
976
977
			// Role
978
			if ( 'role' == $var ) {
979
				$user->set_role( $value );
980
			}
981
			// Core field
982
			elseif ( isset( $user_data[ $var ] ) ) {
983
				wp_update_user( array( 'ID' => $user->ID, $var => $value ) );
984
			}
985
			// Meta field
986
			else {
987
				update_user_meta( $user->ID, $var, $value );
988
			}
989
990
			$ret = get_userdata( $user->ID );
991
		}
992
		elseif ( 'pods' == $type ) {
993
			/**
994
			 * @var $pods Pods
995
			 */
996
			global $pods;
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...
997
998
			if ( is_object( $pods ) && 'Pods' == get_class( $pods ) && $pods->exists() ) {
999
				$ret = $pods->save( $var, $value );
1000
			}
1001
		}
1002
		else {
1003
			$ret = apply_filters( 'pods_var_set_' . $type, $value, $var );
1004
		}
1005
	}
1006
1007
	return $ret;
1008
1009
}
1010
1011
/**
1012
 * Return a variable (if exists)
1013
 *
1014
 * @param mixed $var The variable name or URI segment position
1015
 * @param string $type (optional) Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates
1016
 * @param mixed $default (optional) The default value to set if variable doesn't exist
1017
 * @param mixed $allowed (optional) The value(s) allowed
1018
 * @param bool $strict (optional) Only allow values (must not be empty)
1019
 * @param bool $casting (optional) Whether to cast the value returned like provided in $default
1020
 * @param string $context (optional) All returned values are sanitized unless this is set to 'raw'
1021
 *
1022
 * @return mixed The variable (if exists), or default value
1023
 * @since 1.10.6
1024
 *
1025
 * @deprecated 2.4 Use pods_v() or pods_v_sanitized() instead.
1026
 * @see pods_v_sanitized
1027
 */
1028
function pods_var( $var = 'last', $type = 'get', $default = null, $allowed = null, $strict = false, $casting = false, $context = 'display' ) {
1029
1030
	if ( 'raw' == $context ) {
1031
		$output = pods_v( $var, $type, $default, $strict, array( 'allowed' => $allowed, 'casting' => $casting ) );
1032
	}
1033
	else {
1034
		$output = pods_v_sanitized( $var, $type, $default, $strict, array( 'allowed' => $allowed, 'casting' => $casting ) );
1035
	}
1036
1037
    return $output;
1038
1039
}
1040
1041
/**
1042
 * Return a variable's raw value (if exists)
1043
 *
1044
 * @param mixed $var The variable name or URI segment position
1045
 * @param string $type (optional) Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates
1046
 * @param mixed $default (optional) The default value to set if variable doesn't exist
1047
 * @param mixed $allowed (optional) The value(s) allowed
1048
 * @param bool $strict (optional) Only allow values (must not be empty)
1049
 * @param bool $casting (optional) Whether to cast the value returned like provided in $default
1050
 *
1051
 * @return mixed The variable (if exists), or default value
1052
 * @since 2.0
1053
 *
1054
 * @deprecated 2.4 Use pods_v() instead.
1055
 * @see pods_v
1056
 */
1057
function pods_var_raw( $var = 'last', $type = 'get', $default = null, $allowed = null, $strict = false, $casting = false ) {
1058
1059
    return pods_v( $var, $type, $default, $strict, array( 'allowed' => $allowed, 'casting' => $casting ) );
1060
1061
}
1062
1063
/**
1064
 * Set a variable
1065
 *
1066
 * @param mixed $value The value to be set
1067
 * @param mixed $var The variable name or URI segment position
1068
 * @param string $type (optional) "url", "get", "post", "request", "server", "session", "cookie", "constant", or "user"
1069
 *
1070
 * @return mixed $value (if set), $type (if $type is array or object), or $url (if $type is 'url')
1071
 * @since 1.10.6
1072
 *
1073
 * @deprecated 2.4 Use pods_v_set() instead.
1074
 * @see pods_v_set
1075
 */
1076
function pods_var_set( $value, $var = 'last', $type = 'url' ) {
1077
1078
	return pods_v_set( $value, $var, $type );
1079
1080
}
1081
1082
/**
1083
 * Create a new URL off of the current one, with updated parameters
1084
 *
1085
 * @param array $array Parameters to be set (empty will remove it)
1086
 * @param array $allowed Parameters to keep (if empty, all are kept)
1087
 * @param array $excluded Parameters to always remove
1088
 * @param string $url URL to base update off of
1089
 *
1090
 * @return mixed
1091
 *
1092
 * @since 2.3.10
1093
 *
1094
 * @see add_query_arg
1095
 */
1096
function pods_query_arg( $array = null, $allowed = null, $excluded = null, $url = null ) {
1097
1098
	$array = (array) $array;
1099
	$allowed = (array) $allowed;
1100
	$excluded = (array) $excluded;
1101
1102
	if ( !isset( $_GET ) ) {
1103
		$query_args = array();
1104
	}
1105
	else {
1106
		$query_args = pods_unsanitize( $_GET );
1107
	}
1108
1109
	foreach ( $query_args as $key => $val ) {
1110
		if ( is_array( $val ) && empty( $val ) ) {
1111
			$query_args[ $key ] = false;
1112
		}
1113 View Code Duplication
		elseif ( !is_array( $val ) && strlen( $val ) < 1 ) {
1114
			$query_args[ $key ] = false;
1115
		}
1116
		elseif ( !empty( $allowed ) ) {
1117
			$allow_it = false;
1118
1119
			foreach ( $allowed as $allow ) {
1120
				if ( $allow == $key ) {
1121
					$allow_it = true;
1122
				}
1123
				elseif ( false !== strpos( $allow, '*' ) && 0 === strpos( $key, trim( $allow, '*' ) ) ) {
1124
					$allow_it = true;
1125
				}
1126
			}
1127
1128
			if ( !$allow_it ) {
1129
				$query_args[ $key ] = false;
1130
			}
1131
		}
1132
	}
1133
1134
	if ( !empty( $excluded ) ) {
1135
		foreach ( $excluded as $exclusion ) {
1136
			if ( isset( $query_args[ $exclusion ] ) && !in_array( $exclusion, $allowed ) ) {
1137
				$query_args[ $exclusion ] = false;
1138
			}
1139
		}
1140
	}
1141
1142
	if ( !empty( $array ) ) {
1143
		foreach ( $array as $key => $val ) {
1144
			if ( null !== $val || false === strpos( $key, '*' ) ) {
1145
				if ( is_array( $val ) && !empty( $val ) ) {
1146
					$query_args[ $key ] = $val;
1147
				}
1148 View Code Duplication
				elseif ( !is_array( $val ) && 0 < strlen( $val ) ) {
1149
					$query_args[ $key ] = $val;
1150
				}
1151
				elseif ( isset( $query_args[ $key ] ) ) {
1152
					$query_args[ $key ] = false;
1153
				}
1154
			}
1155
			else {
1156
				$key = str_replace( '*', '', $key );
1157
1158
				foreach ( $query_args as $k => $v ) {
1159
					if ( false !== strpos( $k, $key ) ) {
1160
						$query_args[ $k ] = false;
1161
					}
1162
				}
1163
			}
1164
		}
1165
	}
1166
1167
	if ( null === $url ) {
1168
		$url = add_query_arg( $query_args );
1169
	}
1170
	else {
1171
		$url = add_query_arg( $query_args, $url );
1172
	}
1173
1174
	return $url;
1175
1176
}
1177
1178
/**
1179
 * Create a new URL off of the current one, with updated parameters
1180
 *
1181
 * @param array $array Parameters to be set (empty will remove it)
1182
 * @param array $allowed Parameters to keep (if empty, all are kept)
1183
 * @param array $excluded Parameters to always remove
1184
 * @param string $url URL to base update off of
1185
 *
1186
 * @return mixed
1187
 *
1188
 * @since 2.0
1189
 *
1190
 * @deprecated 2.4 Use pods_query_arg() instead.
1191
 * @see pods_query_arg
1192
 */
1193
function pods_var_update( $array = null, $allowed = null, $excluded = null, $url = null ) {
1194
1195
	return pods_query_arg( $array, $allowed, $excluded, $url );
1196
1197
}
1198
1199
/**
1200
 * Cast a value as a specific type
1201
 *
1202
 * @param mixed $value
1203
 * @param mixed $cast_from
1204
 *
1205
 * @return bool
1206
 *
1207
 * @since 2.0
1208
 */
1209
function pods_cast( $value, $cast_from = null ) {
1210
1211
	if ( null !== $cast_from ) {
1212
		if ( is_object( $value ) && is_array( $cast_from ) ) {
1213
			$value = get_object_vars( $value );
1214
		}
1215
		elseif ( is_array( $value ) && is_object( $cast_from ) ) {
1216
			$value = (object) $value;
1217
		}
1218
		else {
1219
			settype( $value, gettype( $cast_from ) );
1220
		}
1221
	}
1222
1223
	return $value;
1224
1225
}
1226
1227
/**
1228
 * Create a slug from an input string
1229
 *
1230
 * @param $orig
1231
 *
1232
 * @return string Sanitized slug
1233
 *
1234
 * @since 1.8.9
1235
 */
1236
function pods_create_slug ( $orig, $strict = true ) {
1237
    $str = preg_replace( "/([_ \\/])/", "-", trim( $orig ) );
1238
1239
    if ( $strict )
1240
        $str = preg_replace( "/([^0-9a-z\-])/", "", strtolower( $str ) );
1241
    else
1242
        $str = urldecode( sanitize_title( strtolower( $str ) ) );
1243
1244
    $str = preg_replace( "/(\-){2,}/", "-", $str );
1245
    $str = trim( $str, '-' );
1246
    $str = apply_filters( 'pods_create_slug', $str, $orig );
1247
1248
    return $str;
1249
}
1250
1251
/**
1252
 * Build a unique slug
1253
 *
1254
 * @param string $slug The slug value
1255
 * @param string $column_name The column name
1256
 * @param string|array $pod The Pod name or array of Pod data
1257
 * @param int $pod_id The Pod ID
1258
 * @param int $id The item ID
1259
 * @param object $obj (optional)
1260
 *
1261
 * @return string The unique slug name
1262
 * @since 1.7.2
1263
 */
1264
function pods_unique_slug ( $slug, $column_name, $pod, $pod_id = 0, $id = 0, $obj = null, $strict = true ) {
1265
    $slug = pods_create_slug( $slug, $strict );
1266
1267
    $pod_data = array();
1268
1269
    if ( is_array( $pod ) ) {
1270
        $pod_data = $pod;
1271
        $pod_id = pods_v_sanitized( 'id', $pod_data, 0 );
1272
        $pod = pods_v_sanitized( 'name', $pod_data );
1273
    }
1274
1275
    $pod_id = absint( $pod_id );
1276
    $id = absint( $id );
1277
1278
    if ( empty( $pod_data ) )
1279
        $pod_data = pods_api()->load_pod( array( 'id' => $pod_id, 'name' => $pod ), false );
1280
1281
    if ( empty( $pod_data ) || empty( $pod_id ) || empty( $pod ) )
1282
        return $slug;
1283
1284
    if ( 'table' != $pod_data[ 'storage' ] || !in_array( $pod_data[ 'type' ], array( 'pod', 'table' ) ) )
1285
        return $slug;
1286
1287
    $check_sql = "
1288
        SELECT DISTINCT `t`.`{$column_name}` AS `slug`
1289
        FROM `@wp_pods_{$pod}` AS `t`
1290
        WHERE `t`.`{$column_name}` = %s AND `t`.`id` != %d
1291
        LIMIT 1
1292
    ";
1293
1294
    $slug_check = pods_query( array( $check_sql, $slug, $id ), $obj );
1295
1296
    if ( !empty( $slug_check ) || apply_filters( 'pods_unique_slug_is_bad_flat_slug', false, $slug, $column_name, $pod, $pod_id, $id, $pod_data, $obj ) ) {
1297
        $suffix = 2;
1298
1299
        do {
1300
            $alt_slug = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-{$suffix}";
1301
1302
            $slug_check = pods_query( array( $check_sql, $alt_slug, $id ), $obj );
1303
1304
            $suffix++;
1305
        }
1306
        while ( !empty( $slug_check ) || apply_filters( 'pods_unique_slug_is_bad_flat_slug', false, $alt_slug, $column_name, $pod, $pod_id, $id, $pod_data, $obj ) );
1307
1308
        $slug = $alt_slug;
1309
    }
1310
1311
    $slug = apply_filters( 'pods_unique_slug', $slug, $id, $column_name, $pod, $pod_id, $obj );
1312
1313
    return $slug;
1314
}
1315
1316
/**
1317
 * Return a lowercase alphanumeric name (use pods_js_name if you want "_" instead of "-" )
1318
 *
1319
 * @param string $orig Input string to clean
1320
 * @param boolean $lower Force lowercase
1321
 * @param boolean $trim_underscores Whether to trim off underscores
1322
 *
1323
 * @return string Sanitized name
1324
 *
1325
 * @since 1.2.0
1326
 */
1327
function pods_clean_name ( $orig, $lower = true, $trim_underscores = false ) {
1328
1329
	$str = trim( $orig );
1330
	$str = preg_replace( '/(\s)/', '_', $str );
1331
	$str = preg_replace( '/([^0-9a-zA-Z\-_])/', '', $str );
1332
	$str = preg_replace( '/(_){2,}/', '_', $str );
1333
	$str = preg_replace( '/(-){2,}/', '-', $str );
1334
1335
	if ( $lower ) {
1336
		$str = strtolower( $str );
1337
	}
1338
1339
	if ( $trim_underscores ) {
1340
		$str = trim( $str, '_' );
1341
	}
1342
1343
	return $str;
1344
}
1345
1346
/**
1347
 * Return a lowercase alphanumeric name (with underscores) for safe Javascript variable names
1348
 *
1349
 * @param string $orig Input string to clean
1350
 * @param boolean $lower Force lowercase
1351
 *
1352
 * @return string Sanitized name
1353
 *
1354
 * @since 2.5.3
1355
 */
1356
function pods_js_name( $orig, $lower = true ) {
1357
1358
	$str = pods_clean_name( $orig, $lower );
1359
	$str = str_replace( '-', '_', $str );
1360
1361
	return $str;
1362
}
1363
1364
/**
1365
 * Get the Absolute Integer of a value
1366
 *
1367
 * @param string $maybeint
1368
 * @param bool $strict (optional) Check if $maybeint is a integer.
1369
 * @param bool $allow_negative (optional)
1370
 *
1371
 * @return integer
1372
 * @since 2.0
1373
 */
1374
function pods_absint ( $maybeint, $strict = true, $allow_negative = false ) {
1375
    if ( true === $strict && !is_numeric( trim( $maybeint ) ) )
1376
        return 0;
1377
1378
    if ( false !== $allow_negative )
1379
        return intval( $maybeint );
1380
1381
    return absint( $maybeint );
1382
}
1383
1384
/**
1385
 * Functions like str_replace except it will restrict $occurrences
1386
 *
1387
 * @param mixed $find
1388
 * @param mixed $replace
1389
 * @param string $string
1390
 * @param int $occurrences (optional)
1391
 *
1392
 * @return mixed
1393
 * @version 2.0
1394
 */
1395
function pods_str_replace ( $find, $replace, $string, $occurrences = -1 ) {
1396 View Code Duplication
    if ( is_array( $string ) ) {
1397
        foreach ( $string as $k => $v ) {
1398
            $string[ $k ] = pods_str_replace( $find, $replace, $v, $occurrences );
1399
        }
1400
1401
        return $string;
1402
    }
1403
    elseif ( is_object( $string ) ) {
1404
        $string = get_object_vars( $string );
1405
1406
        foreach ( $string as $k => $v ) {
1407
            $string[ $k ] = pods_str_replace( $find, $replace, $v, $occurrences );
1408
        }
1409
1410
        return (object) $string;
1411
    }
1412
1413
    if ( is_array( $find ) ) {
1414
        foreach ( $find as &$f ) {
1415
            $f = '/' . preg_quote( $f, '/' ) . '/';
1416
        }
1417
    }
1418
    else
1419
        $find = '/' . preg_quote( $find, '/' ) . '/';
1420
1421
    return preg_replace( $find, $replace, $string, $occurrences );
1422
}
1423
1424
/**
1425
 * Use mb_strlen if available, otherwise fallback to strlen
1426
 *
1427
 * @param string $string
1428
 *
1429
 * @return int
1430
 */
1431
function pods_mb_strlen( $string ) {
1432
1433
	if ( function_exists( 'mb_strlen' ) ) {
1434
		return mb_strlen( $string );
1435
	}
1436
1437
	return strlen( $string );
1438
1439
}
1440
1441
/**
1442
 * Use mb_substr if available, otherwise fallback to substr
1443
 *
1444
 * @param string $string
1445
 * @param int $start
1446
 * @param null|int $length
1447
 * @param null|string $encoding
1448
 *
1449
 * @return string
1450
 */
1451
function pods_mb_substr( $string, $start, $length = null, $encoding = null ) {
1452
1453
	if ( function_exists( 'mb_substr' ) ) {
1454
		if ( null === $encoding ) {
1455
			$encoding = mb_internal_encoding();
1456
		}
1457
1458
		return mb_substr( $string, $start, $length, $encoding );
1459
	}
1460
1461
	return substr( $string, $start, $length );
1462
1463
}
1464
1465
/**
1466
 * Evaluate tags like magic tags but through pods_var
1467
 *
1468
 * @param string|array|object $tags String to be evaluated
1469
 * @param bool $sanitize Whether to sanitize tags
1470
 *
1471
 * @return string
1472
 *
1473
 * @version 2.1
1474
 *
1475
 * @see pods_evaluate_tag
1476
 */
1477
function pods_evaluate_tags ( $tags, $sanitize = false ) {
1478
1479 View Code Duplication
	if ( is_array( $tags ) ) {
1480
		foreach ( $tags as $k => $tag ) {
1481
			$tags[ $k ] = pods_evaluate_tags( $tag, $sanitize );
1482
		}
1483
1484
		return $tags;
1485
	}
1486
	elseif ( is_object( $tags ) ) {
1487
		$tags = get_object_vars( $tags );
1488
1489
		foreach ( $tags as $k => $tag ) {
1490
			$tags[ $k ] = pods_evaluate_tags( $tag, $sanitize );
1491
		}
1492
1493
		$tags = (object) $tags;
1494
1495
		return $tags;
1496
	}
1497
1498
	$callback = 'pods_evaluate_tag';
1499
1500
	if ( true === $sanitize ) {
1501
		$callback = 'pods_evaluate_tag_sanitized';
1502
	}
1503
1504
	return preg_replace_callback( '/({@(.*?)})/m', $callback, (string) $tags );
1505
1506
}
1507
1508
/**
1509
 * Evaluate tag like magic tag but mapped through pods_v_sanitized
1510
 *
1511
 * @param string|array $tag
1512
 *
1513
 * @return string
1514
 *
1515
 * @version 2.1
1516
 *
1517
 * @see pods_evaluate_tag
1518
 */
1519
function pods_evaluate_tag_sanitized( $tag ) {
1520
1521
	return pods_evaluate_tag( $tag, true );
1522
1523
}
1524
1525
/**
1526
 * Evaluate tag like magic tag but mapped through pods_v
1527
 *
1528
 * @param string|array $tag
1529
 * @param bool $sanitize Whether to sanitize tags
1530
 *
1531
 * @return string
1532
 *
1533
 * @version 2.1
1534
 */
1535
function pods_evaluate_tag( $tag, $sanitize = false ) {
1536
1537
	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...
1538
1539
	// Handle pods_evaluate_tags
1540 View Code Duplication
	if ( is_array( $tag ) ) {
1541
		if ( !isset( $tag[ 2 ] ) && strlen( trim( $tag[ 2 ] ) ) < 1 ) {
1542
			return '';
1543
		}
1544
1545
		$tag = $tag[ 2 ];
1546
	}
1547
1548
	$tag = trim( $tag, ' {@}' );
1549
	$tag = explode( '.', $tag );
1550
1551 View Code Duplication
	if ( empty( $tag ) || !isset( $tag[ 0 ] ) || strlen( trim( $tag[ 0 ] ) ) < 1 ) {
1552
		return '';
1553
	}
1554
1555
	// Fix formatting that may be after the first .
1556
	if ( 2 < count( $tag ) ) {
1557
		$first_tag = $tag[ 0 ];
1558
		unset( $tag[ 0 ] );
1559
1560
		$tag = array(
1561
			$first_tag,
1562
			implode( '.', $tag )
1563
		);
1564
	}
1565
1566
	foreach ( $tag as $k => $v ) {
1567
		$tag[ $k ] = trim( $v );
1568
	}
1569
1570
	$value = '';
1571
1572
	$single_supported = array(
1573
		'template-url',
1574
		'stylesheet-url',
1575
		'site-url',
1576
		'home-url',
1577
		'admin-url',
1578
		'includes-url',
1579
		'content-url',
1580
		'plugins-url',
1581
		'network-site-url',
1582
		'network-home-url',
1583
		'network-admin-url',
1584
		'user-admin-url',
1585
		'prefix'
1586
	);
1587
1588
	if ( in_array( $tag[ 0 ], $single_supported ) ) {
1589
		$value = pods_v( '', $tag[ 0 ], '', true );
1590
	}
1591 View Code Duplication
	elseif ( 1 == count( $tag ) ) {
1592
		$value = pods_v( $tag[ 0 ], 'get', '', true );
1593
	}
1594 View Code Duplication
	elseif ( 2 == count( $tag ) ) {
1595
		$value = pods_v( $tag[ 1 ], $tag[ 0 ], '', true );
1596
	}
1597
1598
	$value = apply_filters( 'pods_evaluate_tag', $value, $tag );
1599
1600
	if ( is_array( $value ) && 1 == count( $value ) ) {
1601
		$value = current( $value );
1602
	}
1603
1604
	if ( is_array( $value ) ) {
1605
		$value = pods_serial_comma( $value );
1606
	}
1607
1608
	if ( $sanitize ) {
1609
		$value = pods_sanitize( $value );
1610
	}
1611
1612
	return $value;
1613
1614
}
1615
1616
/**
1617
 * Split an array into human readable text (Item, Item, and Item)
1618
 *
1619
 * @param array $value
1620
 * @param string $field
1621
 * @param array $fields
1622
 * @param string $and
1623
 * @param string $field_index
1624
 *
1625
 * @return string
1626
 *
1627
 * @since 2.0
1628
 */
1629
function pods_serial_comma ( $value, $field = null, $fields = null, $and = null, $field_index = null ) {
1630
    if ( is_object( $value ) )
1631
        $value = get_object_vars( $value );
1632
1633
    $defaults = array(
1634
        'field' => $field,
1635
        'fields' => $fields,
1636
        'and' => $and,
1637
        'field_index' => $field_index,
1638
        'separator' => ',',
1639
        'serial' => true
1640
    );
1641
1642 View Code Duplication
    if ( is_array( $field ) ) {
1643
        $defaults[ 'field' ] = null;
1644
1645
        $params = array_merge( $defaults, $field );
1646
    }
1647
    else
1648
        $params = $defaults;
1649
1650
    $params = (object) $params;
1651
1652
    $simple = false;
1653
1654
    if ( !empty( $params->fields ) && is_array( $params->fields ) && isset( $params->fields[ $params->field ] ) ) {
1655
        $params->field = $params->fields[ $params->field ];
1656
1657
	    $simple_tableless_objects = PodsForm::simple_tableless_objects();
1658
1659
        if ( !empty( $params->field ) && is_array( $params->field ) && in_array( $params->field[ 'type' ], PodsForm::tableless_field_types() ) ) {
1660
            if ( in_array( $params->field[ 'type' ], PodsForm::file_field_types() ) ) {
1661
                if ( null === $params->field_index )
1662
                    $params->field_index = 'guid';
1663
            }
1664
            elseif ( in_array( $params->field[ 'pick_object' ], $simple_tableless_objects ) )
1665
                $simple = true;
1666
            else {
1667
                $table = pods_api()->get_table_info( $params->field[ 'pick_object' ], $params->field[ 'pick_val' ], null, null, $params->field );
1668
1669
                if ( !empty( $table ) ) {
1670
                    if ( null === $params->field_index )
1671
                        $params->field_index = $table[ 'field_index' ];
1672
                }
1673
            }
1674
        }
1675
    }
1676
    else
1677
        $params->field = null;
1678
1679 View Code Duplication
    if ( $simple && is_array( $params->field ) && !is_array( $value ) && '' !== $value && null !== $value )
1680
        $value = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $value, $params->field );
1681
1682
    if ( !is_array( $value ) )
1683
        return $value;
1684
1685
    if ( null === $params->and )
1686
        $params->and = ' ' . __( 'and', 'pods' ) . ' ';
1687
1688
    $last = '';
1689
1690
    $original_value = $value;
1691
1692
    if ( !empty( $value ) )
1693
        $last = array_pop( $value );
1694
1695 View Code Duplication
    if ( $simple && is_array( $params->field ) && !is_array( $last ) && '' !== $last && null !== $last )
1696
        $last = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $last, $params->field );
1697
1698
    if ( is_array( $last ) ) {
1699
        if ( null !== $params->field_index && isset( $last[ $params->field_index ] ) )
1700
            $last = $last[ $params->field_index ];
1701
        elseif ( isset( $last[ 0 ] ) )
1702
            $last = $last[ 0 ];
1703
        elseif ( $simple )
1704
            $last = current( $last );
1705
        else
1706
            $last = '';
1707
    }
1708
1709
    if ( !empty( $value ) ) {
1710
        if ( null !== $params->field_index && isset( $original_value[ $params->field_index ] ) )
1711
            return $original_value[ $params->field_index ];
1712 View Code Duplication
        elseif ( null !== $params->field_index && isset( $value[ $params->field_index ] ) )
1713
            return $value[ $params->field_index ];
1714
        elseif ( !isset( $value[ 0 ] ) )
1715
            $value = array( $value );
1716
1717
        foreach ( $value as $k => $v ) {
1718 View Code Duplication
            if ( $simple && is_array( $params->field ) && !is_array( $v ) && '' !== $v && null !== $v )
1719
                $v = PodsForm::field_method( 'pick', 'simple_value', $params->field[ 'name' ], $v, $params->field );
1720
1721
            if ( is_array( $v ) ) {
1722
                if ( null !== $params->field_index && isset( $v[ $params->field_index ] ) )
1723
                    $v = $v[ $params->field_index ];
1724
                elseif ( $simple )
1725
                    $v = trim( implode( $params->separator . ' ', $v ), $params->separator . ' ' );
1726
                else {
1727
                    unset( $value[ $k ] );
1728
1729
                    continue;
1730
                }
1731
            }
1732
1733
            $value[ $k ] = $v;
1734
        }
1735
1736
        if ( 1 == count( $value ) || !$params->serial )
1737
            $value = trim( implode( $params->separator . ' ', $value ), $params->separator . ' ' );
1738
        else
1739
            $value = trim( implode( $params->separator . ' ', $value ), $params->separator . ' ' ) . apply_filters( 'pods_serial_comma', $params->separator . ' ', $value, $original_value, $params );
1740
1741
        $value = trim( $value );
1742
        $last = trim( $last );
1743
1744
        if ( 0 < strlen( $value ) && 0 < strlen( $last ) )
1745
            $value = $value . $params->and . $last;
1746
        elseif ( 0 < strlen( $last ) )
1747
            $value = $last;
1748
        else
1749
            $value = '';
1750
    }
1751
    else
1752
        $value = $last;
1753
1754
    $value = trim( $value, $params->separator . ' ' );
1755
1756
    $value = apply_filters( 'pods_serial_comma_value', $value, $original_value, $params );
1757
1758
    return (string) $value;
1759
}
1760
1761
/**
1762
 * Return a variable if a user is logged in or anonymous, or a specific capability
1763
 *
1764
 * @param mixed $anon Variable to return if user is anonymous (not logged in)
1765
 * @param mixed $user Variable to return if user is logged in
1766
 * @param string|array $capability Capability or array of Capabilities to check to return $user on
1767
 *
1768
 * @return mixed $user Variable to return if user is logged in (if logged in), otherwise $anon
1769
 *
1770
 * @since 2.0.5
1771
 */
1772
function pods_var_user ( $anon = false, $user = true, $capability = null ) {
1773
    $value = $anon;
1774
1775
    if ( is_user_logged_in() ) {
1776
        if ( empty( $capability ) )
1777
            $value = $user;
1778
        else {
1779
            $capabilities = (array) $capability;
1780
1781
            foreach ( $capabilities as $capability ) {
1782
                if ( current_user_can( $capability ) ) {
1783
                    $value = $user;
1784
1785
                    break;
1786
                }
1787
            }
1788
        }
1789
    }
1790
1791
    return $value;
1792
}
1793
1794
/**
1795
 * Take a one-level list of items and make it hierarchical
1796
 *
1797
 * @param array|object $list List of items
1798
 * @param array $args Array of parent, children, and id keys to use
1799
 *
1800
 * @return array|object
1801
 * @since 2.3
1802
 */
1803
function pods_hierarchical_list ( $list, $args = array() ) {
1804
    if ( empty( $args ) || ( !is_object( $list ) && !is_array( $list ) ) )
1805
        return $list;
1806
1807
    $defaults = array(
1808
        'id' => 'id',
1809
        'parent' => 'parent',
1810
        'children' => 'children',
1811
        'orphans' => true,
1812
        'found' => array(),
1813
        'list' => array(),
1814
        'current_depth' => -1
1815
    );
1816
1817
    $args = array_merge( $defaults, (array) $args );
1818
1819
    $list = pods_hierarchical_list_recurse( 0, $list, $args );
1820
1821
    return $list;
1822
}
1823
1824
/**
1825
 * Recurse list of items and make it hierarchical
1826
 *
1827
 * @param int $parent Parent ID
1828
 * @param array|object $list List of items
1829
 * @param array $args Array of parent, children, and id keys to use
1830
 *
1831
 * @return array|object
1832
 * @since 2.3
1833
 */
1834
function pods_hierarchical_list_recurse ( $parent, $list, &$args ) {
1835
    $new = array();
1836
1837
    $object = false;
1838
1839
    if ( is_object( $list ) ) {
1840
        $object = true;
1841
        $list = get_object_vars( $list );
1842
    }
1843
1844
    $args[ 'current_depth' ]++;
1845
1846
    $depth = $args[ 'current_depth' ];
1847
1848
    if ( 0 == $depth )
1849
        $args[ 'list' ] = $list;
1850
1851
    foreach ( $list as $k => $list_item ) {
1852
        if ( is_object( $list_item ) && isset( $list_item->{$args[ 'id' ]} ) ) {
1853
            $list_item->{$args[ 'parent' ]} = (int) pods_v( $args[ 'parent' ], $list_item );
1854
1855
            if ( is_array( $list_item->{$args[ 'parent' ]} ) && isset( $list_item->{$args[ 'parent' ]}[ $args[ 'id' ] ] ) && $parent == $list_item->{$args[ 'parent' ]}[ $args[ 'id' ] ] )
1856
                $list_item->{$args[ 'children' ]} = pods_hierarchical_list_recurse( $list_item->{$args[ 'id' ]}, $list, $args );
1857
            elseif ( $parent == $list_item->{$args[ 'parent' ]} || ( 0 == $depth && $parent == $list_item->{$args[ 'id' ]} ) )
1858
                $list_item->{$args[ 'children' ]} = pods_hierarchical_list_recurse( $list_item->{$args[ 'id' ]}, $list, $args );
1859
            else
1860
                continue;
1861
1862
            $args[ 'found' ][ $k ] = $list_item;
1863
        }
1864
        elseif ( is_array( $list_item ) && isset( $list_item[ $args[ 'id' ] ] ) ) {
1865
            $list_item[ $args[ 'parent' ] ] = (int) pods_v( $args[ 'parent' ], $list_item );
1866
1867
            if ( is_array( $list_item[ $args[ 'parent' ] ] ) && isset( $list_item[ $args[ 'parent' ] ][ $args[ 'id' ] ] ) && $parent == $list_item[ $args[ 'parent' ] ][ $args[ 'id' ] ] )
1868
                $list_item[ $args[ 'children' ] ] = pods_hierarchical_list_recurse( $list_item[ $args[ 'id' ] ], $list, $args );
1869
            elseif ( $parent == $list_item[ $args[ 'parent' ] ] || ( 0 == $depth && $parent == $list_item[ $args[ 'id' ] ] ) )
1870
                $list_item[ $args[ 'children' ] ] = pods_hierarchical_list_recurse( $list_item[ $args[ 'id' ] ], $list, $args );
1871
            else
1872
                continue;
1873
1874
            $args[ 'found' ][ $k ] = $list_item;
1875
        }
1876
        else
1877
            continue;
1878
1879
        $new[ $k ] = $list_item;
1880
1881
        $args[ 'current_depth' ] = $depth;
1882
    }
1883
1884
    if ( 0 == $depth && empty( $new ) && !empty( $list ) ) {
1885
        $first = current( array_slice( $list, 0, 1 ) );
1886
1887
        $new_parent = 0;
1888
1889
        $args[ 'current_depth' ] = -1;
1890
1891
        if ( is_object( $first ) && isset( $first->{$args[ 'parent' ]} ) )
1892
            $new_parent = (int) $first->{$args[ 'parent' ]};
1893
        elseif ( is_array( $first ) && isset( $first[ $args[ 'parent' ] ] ) )
1894
            $new_parent = (int) $first[ $args[ 'parent' ] ];
1895
1896
        if ( !empty( $new_parent ) )
1897
            $new = pods_hierarchical_list_recurse( $new_parent, $list, $args );
1898
    }
1899
1900
    if ( 0 == $depth ) {
1901
        $orphans = array();
1902
1903
        foreach ( $args[ 'list' ] as $k => $list_item ) {
1904
            if ( !isset( $args[ 'found' ][ $k ] ) )
1905
                $orphans[ $k ] = $list_item;
1906
        }
1907
1908
        if ( !empty( $orphans ) ) {
1909
            foreach ( $orphans as $orphan ) {
1910
                $new[] = $orphan;
1911
            }
1912
        }
1913
    }
1914
1915
    if ( $object )
1916
        $new = (object) $new;
1917
1918
    return $new;
1919
}
1920
1921
/**
1922
 * Take a one-level list of items and make it hierarchical for <select>
1923
 *
1924
 * @param array|object $list List of items
1925
 * @param array $args Array of index, parent, children, id, and prefix keys to use
1926
 * @param string $children_key Key to recurse children into
0 ignored issues
show
Bug introduced by
There is no parameter named $children_key. 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...
1927
 *
1928
 * @return array|object
1929
 * @since 2.3
1930
 */
1931
function pods_hierarchical_select ( $list, $args = array() ) {
1932
    $object = false;
1933
1934
    if ( is_object( $list ) ) {
1935
        $object = true;
1936
        $list = get_object_vars( $list );
1937
    }
1938
1939
    $list = pods_hierarchical_list( $list, $args );
1940
1941
    $defaults = array(
1942
        'index' => 'name',
1943
        'children' => 'children',
1944
        'prefix' => '&nbsp;&nbsp;&nbsp;'
1945
    );
1946
1947
    $args = array_merge( $defaults, (array) $args );
1948
1949
    $list = pods_hierarchical_select_recurse( $list, $args, 0 );
1950
1951
    if ( $object )
1952
        $list = (object) $list;
1953
1954
    return $list;
1955
}
1956
1957
/**
1958
 * Recurse list of hierarchical data
1959
 *
1960
 * @param array|object $list List of items
0 ignored issues
show
Bug introduced by
There is no parameter named $list. 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...
1961
 * @param array $args Array of children and prefix keys to use
1962
 * @param string $children_key Key to recurse children into
0 ignored issues
show
Bug introduced by
There is no parameter named $children_key. 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...
1963
 *
1964
 * @see pods_hierarchical_select
1965
 * @return array
1966
 * @since 2.3
1967
 */
1968
function pods_hierarchical_select_recurse ( $items, $args, $depth = 0 ) {
1969
    $data = array();
1970
1971
    foreach ( $items as $k => $v ) {
1972
        $object = false;
1973
1974
        if ( is_object( $v ) ) {
1975
            $object = true;
1976
            $v = get_object_vars( $v );
1977
        }
1978
1979
        if ( isset( $v[ $args[ 'index' ] ] ) )
1980
            $v[ $args[ 'index' ] ] = ( 0 < $depth ? str_repeat( $args[ 'prefix' ], $depth ) : '' ) . $v[ $args[ 'index' ] ];
1981
1982
        $children = array();
1983
1984
        if ( isset( $v[ $args[ 'children' ] ] ) ) {
1985
            if ( !empty( $v[ $args[ 'children' ] ] ) )
1986
                $children = pods_hierarchical_select_recurse( $v[ $args[ 'children' ] ], $args, ( $depth + 1 ) );
1987
1988
            unset( $v[ $args[ 'children' ] ] );
1989
        }
1990
1991
        if ( $object )
1992
            $v = (object) $v;
1993
1994
        $data[ $k ] = $v;
1995
1996
        if ( !empty( $children ) ) {
1997
            foreach ( $children as $ck => $cv ) {
1998
                $data[ $ck ] = $cv;
1999
            }
2000
        }
2001
    }
2002
2003
    return $data;
2004
}
2005
2006
/**
2007
 * Filters a list of objects or arrays, based on a set of key => value arguments.
2008
 *
2009
 * @param array|object $list An array or object, with objects/arrays to filter
2010
 * @param array $args An array of key => value arguments to match against each object
2011
 * @param string $operator The logical operation to perform:
2012
 *    'AND' means all elements from the array must match;
2013
 *    'OR' means only one element needs to match;
2014
 *    'NOT' means no elements may match.
2015
 *   The default is 'AND'.
2016
 *
2017
 * @see wp_list_filter
2018
 * @return array
2019
 * @since 2.3
2020
 */
2021
function pods_list_filter ( $list, $args = array(), $operator = 'AND' ) {
2022
    if ( empty( $args ) )
2023
        return $list;
2024
2025
    $data = $list;
2026
2027
    $object = false;
2028
2029
    if ( is_object( $data ) ) {
2030
        $object = true;
2031
        $data = get_object_vars( $data );
2032
    }
2033
2034
    $operator = strtoupper( $operator );
2035
    $count = count( $args );
2036
    $filtered = array();
2037
2038
    foreach ( $data as $key => $obj ) {
2039
        $to_match = $obj;
2040
2041
        if ( is_object( $to_match ) )
2042
            $to_match = get_object_vars( $to_match );
2043
        elseif ( !is_array( $to_match ) )
2044
            continue;
2045
2046
        $matched = 0;
2047
2048
        foreach ( $args as $m_key => $m_value ) {
2049
            if ( array_key_exists( $m_key, $to_match ) && $m_value == $to_match[ $m_key ] )
2050
                $matched++;
2051
        }
2052
2053
        if ( 'AND' == $operator && $matched == $count )
2054
            $filtered[ $key ] = $obj;
2055
        elseif ( 'OR' == $operator && $matched > 0 )
2056
            $filtered[ $key ] = $obj;
2057
        elseif ( 'NOT' == $operator && 0 == $matched )
2058
            $filtered[ $key ] = $obj;
2059
        else
2060
            continue;
2061
    }
2062
2063
    if ( $object )
2064
        $filtered = (object) $filtered;
2065
2066
    return $filtered;
2067
}
2068