/includes/data.php

https://github.com/ElmsPark/pods · PHP · 826 lines · 521 code · 128 blank · 177 comment · 185 complexity · c175fd6aed6cf8caf2ad9cf01ade71cf MD5 · raw file

  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 bool $nested
  10. *
  11. * @return array|mixed|object|string|void
  12. * @since 1.2.0
  13. */
  14. function pods_sanitize ( $input, $nested = false ) {
  15. $output = array();
  16. if ( empty( $input ) )
  17. $output = $input;
  18. elseif ( is_object( $input ) ) {
  19. $input = get_object_vars( $input );
  20. foreach ( $input as $key => $val ) {
  21. $output[ pods_sanitize( $key ) ] = pods_sanitize( $val, true );
  22. }
  23. $output = (object) $output;
  24. }
  25. elseif ( is_array( $input ) ) {
  26. foreach ( $input as $key => $val ) {
  27. $output[ pods_sanitize( $key ) ] = pods_sanitize( $val, true );
  28. }
  29. }
  30. else
  31. $output = esc_sql( $input );
  32. if ( false === $nested )
  33. $output = apply_filters( 'pods_sanitize', $output, $input );
  34. return $output;
  35. }
  36. /**
  37. * Filter input and return unsanitized output
  38. *
  39. * @param mixed $input The string, array, or object to unsanitize
  40. * @param bool $nested
  41. *
  42. * @return array|mixed|object|string|void
  43. * @since 1.2.0
  44. */
  45. function pods_unsanitize ( $input, $nested = false ) {
  46. $output = array();
  47. if ( empty( $input ) )
  48. $output = $input;
  49. elseif ( is_object( $input ) ) {
  50. $input = get_object_vars( $input );
  51. foreach ( $input as $key => $val ) {
  52. $output[ pods_unsanitize( $key ) ] = pods_unsanitize( $val, true );
  53. }
  54. $output = (object) $output;
  55. }
  56. elseif ( is_array( $input ) ) {
  57. foreach ( $input as $key => $val ) {
  58. $output[ pods_unsanitize( $key ) ] = pods_unsanitize( $val, true );
  59. }
  60. }
  61. else
  62. $output = stripslashes( $input );
  63. if ( false === $nested )
  64. $output = apply_filters( 'pods_unsanitize', $output, $input );
  65. return $output;
  66. }
  67. /**
  68. * Filter input and return sanitized output
  69. *
  70. * @param mixed $input The string, array, or object to sanitize
  71. * @param string $charlist (optional) List of characters to be stripped from the input.
  72. * @param string $lr Direction of the trim, can either be 'l' or 'r'.
  73. *
  74. * @return array|object|string
  75. * @since 1.2.0
  76. */
  77. function pods_trim ( $input, $charlist = null, $lr = null ) {
  78. $output = array();
  79. if ( is_object( $input ) ) {
  80. $input = get_object_vars( $input );
  81. foreach ( $input as $key => $val ) {
  82. $output[ pods_sanitize( $key ) ] = pods_trim( $val, $charlist, $lr );
  83. }
  84. $output = (object) $output;
  85. }
  86. elseif ( is_array( $input ) ) {
  87. foreach ( $input as $key => $val ) {
  88. $output[ pods_sanitize( $key ) ] = pods_trim( $val, $charlist, $lr );
  89. }
  90. }
  91. else {
  92. if ( 'l' == $lr )
  93. $output = ltrim( $input, $charlist );
  94. elseif ( 'r' == $lr )
  95. $output = rtrim( $input, $charlist );
  96. else
  97. $output = trim( $input, $charlist );
  98. }
  99. return $output;
  100. }
  101. /**
  102. * Return a variable (if exists)
  103. *
  104. * @param mixed $var The variable name or URI segment position
  105. * @param string $type (optional) get|url|post|request|server|session|cookie|constant|globals|user|option|site-option|transient|site-transient|cache|date|pods
  106. * @param mixed $default (optional) The default value to set if variable doesn't exist
  107. * @param mixed $allowed (optional) The value(s) allowed
  108. * @param bool $strict (optional) Only allow values (must not be empty)
  109. * @param bool $casting (optional) Whether to cast the value returned like provided in $default
  110. * @param string $context (optional) All returned values are sanitized unless this is set to 'raw'
  111. *
  112. * @return mixed The variable (if exists), or default value
  113. * @since 1.10.6
  114. */
  115. function pods_var ( $var = 'last', $type = 'get', $default = null, $allowed = null, $strict = false, $casting = false, $context = 'display' ) {
  116. if ( is_array( $type ) )
  117. $output = isset( $type[ $var ] ) ? $type[ $var ] : $default;
  118. elseif ( is_object( $type ) )
  119. $output = isset( $type->$var ) ? $type->$var : $default;
  120. else {
  121. $type = strtolower( (string) $type );
  122. if ( 'get' == $type && isset( $_GET[ $var ] ) )
  123. $output = stripslashes_deep( $_GET[ $var ] );
  124. elseif ( in_array( $type, array( 'url', 'uri' ) ) ) {
  125. $url = parse_url( get_current_url() );
  126. $uri = trim( $url[ 'path' ], '/' );
  127. $uri = array_filter( explode( '/', $uri ) );
  128. if ( 'first' == $var )
  129. $var = 0;
  130. elseif ( 'last' == $var )
  131. $var = -1;
  132. if ( is_numeric( $var ) )
  133. $output = ( $var < 0 ) ? pods_var_raw( count( $uri ) + $var, $uri ) : pods_var_raw( $var, $uri );
  134. }
  135. elseif ( 'url-relative' == $type ) {
  136. $url_raw = get_current_url();
  137. $prefix = get_bloginfo( 'wpurl' );
  138. if ( substr( $url_raw, 0, strlen( $prefix ) ) == $prefix )
  139. $url_raw = substr( $url_raw, strlen( $prefix ) + 1, strlen( $url_raw ) );
  140. $url = parse_url( $url_raw );
  141. $uri = trim( $url[ 'path' ], '/' );
  142. $uri = array_filter( explode( '/', $uri ) );
  143. if ( 'first' == $var )
  144. $var = 0;
  145. elseif ( 'last' == $var )
  146. $var = -1;
  147. if ( is_numeric( $var ) )
  148. $output = ( $var < 0 ) ? pods_var_raw( count( $uri ) + $var, $uri ) : pods_var_raw( $var, $uri );
  149. }
  150. elseif ( 'post' == $type && isset( $_POST[ $var ] ) )
  151. $output = stripslashes_deep( $_POST[ $var ] );
  152. elseif ( 'request' == $type && isset( $_REQUEST[ $var ] ) )
  153. $output = stripslashes_deep( $_REQUEST[ $var ] );
  154. elseif ( 'server' == $type ) {
  155. if ( isset( $_SERVER[ $var ] ) )
  156. $output = stripslashes_deep( $_SERVER[ $var ] );
  157. elseif ( isset( $_SERVER[ strtoupper( $var ) ] ) )
  158. $output = stripslashes_deep( $_SERVER[ strtoupper( $var ) ] );
  159. }
  160. elseif ( 'session' == $type && isset( $_SESSION[ $var ] ) )
  161. $output = $_SESSION[ $var ];
  162. elseif ( in_array( $type, array( 'global', 'globals' ) ) && isset( $GLOBALS[ $var ] ) )
  163. $output = $GLOBALS[ $var ];
  164. elseif ( 'cookie' == $type && isset( $_COOKIE[ $var ] ) )
  165. $output = stripslashes_deep( $_COOKIE[ $var ] );
  166. elseif ( 'constant' == $type && defined( $var ) )
  167. $output = constant( $var );
  168. elseif ( 'user' == $type && is_user_logged_in() ) {
  169. $user = get_userdata( get_current_user_id() );
  170. if ( isset( $user->{$var} ) )
  171. $value = $user->{$var};
  172. else
  173. $value = get_user_meta( $user->ID, $var );
  174. if ( is_array( $value ) && !empty( $value ) )
  175. $output = $value;
  176. elseif ( !is_array( $value ) && 0 < strlen( $value ) )
  177. $output = $value;
  178. }
  179. elseif ( 'option' == $type )
  180. $output = get_option( $var, $default );
  181. elseif ( 'site-option' == $type )
  182. $output = get_site_option( $var, $default );
  183. elseif ( 'transient' == $type )
  184. $output = get_transient( $var );
  185. elseif ( 'site-transient' == $type )
  186. $output = get_site_transient( $var );
  187. elseif ( 'cache' == $type && isset( $GLOBALS[ 'wp_object_cache' ] ) && is_object( $GLOBALS[ 'wp_object_cache' ] ) ) {
  188. $group = 'default';
  189. $force = false;
  190. if ( is_array( $var ) ) {
  191. if ( isset( $var[ 1 ] ) )
  192. $group = $var[ 1 ];
  193. if ( isset( $var[ 2 ] ) )
  194. $force = $var[ 2 ];
  195. if ( isset( $var[ 0 ] ) )
  196. $var = $var[ 0 ];
  197. }
  198. $output = wp_cache_get( $var, $group, $force );
  199. }
  200. elseif ( 'date' == $type ) {
  201. $var = explode( '|', $var );
  202. if ( !empty( $var ) )
  203. $output = date_i18n( $var[ 0 ], ( isset( $var[ 1 ] ) ? strtotime( $var[ 1 ] ) : false ) );
  204. }
  205. elseif ( 'pods' == $type ) {
  206. if ( isset( $GLOBALS[ 'pods' ] ) && 'Pods' == get_class( $GLOBALS[ 'pods' ] ) ) {
  207. $output = $GLOBALS[ 'pods' ]->field( $var );
  208. if ( is_array( $output ) )
  209. $output = pods_serial_comma( $output, $var, $GLOBALS[ 'pods' ]->fields );
  210. }
  211. }
  212. else
  213. $output = apply_filters( 'pods_var_' . $type, $default, $var, $allowed, $strict, $casting, $context );
  214. }
  215. if ( null !== $allowed ) {
  216. if ( is_array( $allowed ) ) {
  217. if ( !in_array( $output, $allowed ) )
  218. $output = $default;
  219. }
  220. elseif ( $allowed !== $output )
  221. $output = $default;
  222. }
  223. if ( true === $strict ) {
  224. if ( empty( $output ) )
  225. $output = $default;
  226. elseif ( true === $casting )
  227. $output = pods_cast( $output, $default );
  228. }
  229. if ( 'raw' != $context )
  230. $output = pods_sanitize( $output );
  231. return $output;
  232. }
  233. /**
  234. * Return a variable's raw value (if exists)
  235. *
  236. * @param mixed $var The variable name or URI segment position
  237. * @param string $type (optional) get|url|post|request|server|session|cookie|constant|user|option|site-option|transient|site-transient|cache
  238. * @param mixed $default (optional) The default value to set if variable doesn't exist
  239. * @param mixed $allowed (optional) The value(s) allowed
  240. * @param bool $strict (optional) Only allow values (must not be empty)
  241. * @param bool $casting (optional) Whether to cast the value returned like provided in $default
  242. *
  243. * @return mixed The variable (if exists), or default value
  244. * @since 2.0.0
  245. */
  246. function pods_var_raw ( $var = 'last', $type = 'get', $default = null, $allowed = null, $strict = false, $casting = false ) {
  247. return pods_var( $var, $type, $default, $allowed, $strict, $casting, 'raw' );
  248. }
  249. /**
  250. * Cast a variable as a specific type
  251. *
  252. * @param $var
  253. * @param null $default
  254. *
  255. * @return bool
  256. */
  257. function pods_cast ( $var, $default = null ) {
  258. if ( is_object( $var ) && is_array( $default ) )
  259. $var = get_object_vars( $var );
  260. else
  261. settype( $var, gettype( $default ) );
  262. return $var;
  263. }
  264. /**
  265. * Set a variable
  266. *
  267. * @param mixed $value The value to be set
  268. * @param mixed $key The variable name or URI segment position
  269. * @param string $type (optional) "url", "get", "post", "request", "server", "session", "cookie", "constant", or "user"
  270. *
  271. * @return mixed $value (if set), $type (if $type is array or object), or $url (if $type is 'url')
  272. * @since 1.10.6
  273. */
  274. function pods_var_set ( $value, $key = 'last', $type = 'url' ) {
  275. $type = strtolower( $type );
  276. $ret = false;
  277. if ( is_array( $type ) ) {
  278. $type[ $key ] = $value;
  279. $ret = $type;
  280. }
  281. elseif ( is_object( $type ) ) {
  282. $type->$key = $value;
  283. $ret = $type;
  284. }
  285. elseif ( 'url' == $type ) {
  286. $url = parse_url( get_current_url() );
  287. $uri = trim( $url[ 'path' ], '/' );
  288. $uri = array_filter( explode( '/', $uri ) );
  289. if ( 'first' == $key )
  290. $key = 0;
  291. elseif ( 'last' == $key )
  292. $key = -1;
  293. if ( is_numeric( $key ) ) {
  294. if ( $key < 0 )
  295. $uri[ count( $uri ) + $key ] = $value;
  296. else
  297. $uri[ $key ] = $value;
  298. }
  299. $url[ 'path' ] = '/' . implode( '/', $uri ) . '/';
  300. $url[ 'path' ] = trim( $url[ 'path' ], '/' );
  301. $ret = http_build_url( $url );
  302. }
  303. elseif ( 'get' == $type )
  304. $ret = $_GET[ $key ] = $value;
  305. elseif ( 'post' == $type )
  306. $ret = $_POST[ $key ] = $value;
  307. elseif ( 'request' == $type )
  308. $ret = $_REQUEST[ $key ] = $value;
  309. elseif ( 'server' == $type )
  310. $ret = $_SERVER[ $key ] = $value;
  311. elseif ( 'session' == $type )
  312. $ret = $_SESSION[ $key ] = $value;
  313. elseif ( 'cookie' == $type )
  314. $ret = $_COOKIE[ $key ] = $value;
  315. elseif ( 'constant' == $type && !defined( $key ) ) {
  316. define( $key, $value );
  317. $ret = constant( $key );
  318. }
  319. elseif ( 'user' == $type && is_user_logged_in() ) {
  320. global $user_ID;
  321. get_currentuserinfo();
  322. update_user_meta( $user_ID, $key, $value );
  323. $ret = $value;
  324. }
  325. return apply_filters( 'pods_var_set', $ret, $value, $key, $type );
  326. }
  327. /**
  328. * Create a new URL off of the current one, with updated parameters
  329. *
  330. * @param array $array Parameters to be set (empty will remove it)
  331. * @param array $allowed Parameters to keep (if empty, all are kept)
  332. * @param array $excluded Parameters to always remove
  333. * @param string $url URL to base update off of
  334. *
  335. * @return mixed
  336. *
  337. * @since 2.0.0
  338. */
  339. function pods_var_update ( $array = null, $allowed = null, $excluded = null, $url = null ) {
  340. $array = (array) $array;
  341. $allowed = (array) $allowed;
  342. $excluded = (array) $excluded;
  343. if ( empty( $url ) )
  344. $url = $_SERVER[ 'REQUEST_URI' ];
  345. if ( !isset( $_GET ) )
  346. $get = array();
  347. else
  348. $get = $_GET;
  349. $get = pods_unsanitize( $get );
  350. foreach ( $get as $key => $val ) {
  351. if ( is_array( $val ) && empty( $val ) )
  352. unset( $get[ $key ] );
  353. elseif ( !is_array( $val ) && strlen( $val ) < 1 )
  354. unset( $get[ $key ] );
  355. elseif ( !empty( $allowed ) ) {
  356. $allow_it = false;
  357. foreach ( $allowed as $allow ) {
  358. if ( $allow == $key )
  359. $allow_it = true;
  360. elseif ( false !== strpos( $allow, '*' ) && 0 === strpos( $key, trim( $allow, '*' ) ) )
  361. $allow_it = true;
  362. }
  363. if ( !$allow_it )
  364. unset( $get[ $key ] );
  365. }
  366. }
  367. if ( !empty( $excluded ) ) {
  368. foreach ( $excluded as $exclusion ) {
  369. if ( isset( $get[ $exclusion ] ) && !in_array( $exclusion, $allowed ) )
  370. unset( $get[ $exclusion ] );
  371. }
  372. }
  373. if ( !empty( $array ) ) {
  374. foreach ( $array as $key => $val ) {
  375. if ( null !== $val || false === strpos( $key, '*' ) ) {
  376. if ( is_array( $val ) && !empty( $val ) )
  377. $get[ $key ] = $val;
  378. elseif ( !is_array( $val ) && 0 < strlen( $val ) )
  379. $get[ $key ] = $val;
  380. elseif ( isset( $get[ $key ] ) )
  381. unset( $get[ $key ] );
  382. }
  383. else {
  384. $key = str_replace( '*', '', $key );
  385. foreach ( $get as $k => $v ) {
  386. if ( false !== strpos( $k, $key ) )
  387. unset( $get[ $k ] );
  388. }
  389. }
  390. }
  391. }
  392. $url = current( explode( '#', current( explode( '?', $url ) ) ) );
  393. if ( !empty( $get ) )
  394. $url = $url . '?' . http_build_query( $get );
  395. return $url;
  396. }
  397. /**
  398. * Create a slug from an input string
  399. *
  400. * @param $orig
  401. *
  402. * @return mixed|void
  403. *
  404. * @since 1.8.9
  405. */
  406. function pods_create_slug ( $orig, $strict = true ) {
  407. $str = preg_replace( "/([_ ])/", "-", trim( $orig ) );
  408. if ( $strict )
  409. $str = preg_replace( "/([^0-9a-z-])/", "", strtolower( $str ) );
  410. else
  411. $str = urldecode( sanitize_title( strtolower( $str ) ) );
  412. $str = preg_replace( "/(-){2,}/", "-", $str );
  413. $str = trim( $str, '-' );
  414. $str = apply_filters( 'pods_create_slug', $str, $orig );
  415. return $str;
  416. }
  417. /**
  418. * Return a lowercase alphanumeric name (with underscores)
  419. *
  420. * @param string $orig Input string to clean
  421. * @param boolean $lower Force lowercase
  422. * @param boolean $trim_underscores Whether to trim off underscores
  423. *
  424. * @return mixed|void
  425. * @since 1.2.0
  426. */
  427. function pods_clean_name ( $orig, $lower = true, $trim_underscores = true ) {
  428. $str = preg_replace( "/([- ])/", "_", trim( $orig ) );
  429. if ( $lower )
  430. $str = strtolower( $str );
  431. $str = preg_replace( "/([^0-9a-zA-Z_])/", "", $str );
  432. $str = preg_replace( "/(_){2,}/", "_", $str );
  433. $str = trim( $str );
  434. if ( $trim_underscores )
  435. $str = trim( $str, '_' );
  436. $str = apply_filters( 'pods_clean_name', $str, $orig, $lower );
  437. return $str;
  438. }
  439. /**
  440. * Get the Absolute Integer of a value
  441. *
  442. * @param string $maybeint
  443. * @param bool $strict (optional) Check if $maybeint is a integer.
  444. * @param bool $allow_negative (optional)
  445. *
  446. * @return integer
  447. * @since 2.0.0
  448. */
  449. function pods_absint ( $maybeint, $strict = true, $allow_negative = false ) {
  450. if ( true === $strict && !is_numeric( trim( $maybeint ) ) )
  451. return 0;
  452. if ( false !== $allow_negative )
  453. return intval( $maybeint );
  454. return absint( $maybeint );
  455. }
  456. /**
  457. * Functions like str_replace except it will restrict $occurrences
  458. *
  459. * @param mixed $find
  460. * @param mixed $replace
  461. * @param string $string
  462. * @param int $occurrences (optional)
  463. *
  464. * @return mixed
  465. * @version 2.0.0
  466. */
  467. function pods_str_replace ( $find, $replace, $string, $occurrences = -1 ) {
  468. if ( is_array( $string ) ) {
  469. foreach ( $string as $k => $v ) {
  470. $string[ $k ] = pods_str_replace( $find, $replace, $v, $occurrences );
  471. }
  472. return $string;
  473. }
  474. elseif ( is_object( $string ) ) {
  475. $string = get_object_vars( $string );
  476. foreach ( $string as $k => $v ) {
  477. $string[ $k ] = pods_str_replace( $find, $replace, $v, $occurrences );
  478. }
  479. return (object) $string;
  480. }
  481. if ( is_array( $find ) ) {
  482. foreach ( $find as &$f ) {
  483. $f = '/' . preg_quote( $f, '/' ) . '/';
  484. }
  485. }
  486. else
  487. $find = '/' . preg_quote( $find, '/' ) . '/';
  488. return preg_replace( $find, $replace, $string, $occurrences );
  489. }
  490. /**
  491. * Evaluate tags like magic tags but through pods_var
  492. *
  493. * @param string|array $tags String to be evaluated
  494. * @param bool $sanitize Whether to sanitize tags
  495. *
  496. * @return string
  497. * @version 2.1.0
  498. */
  499. function pods_evaluate_tags ( $tags, $sanitize = false ) {
  500. if ( is_array( $tags ) ) {
  501. foreach ( $tags as $k => $tag ) {
  502. $tags[ $k ] = pods_evaluate_tags( $tag, $sanitize );
  503. }
  504. return $tags;
  505. }
  506. if ( $sanitize )
  507. return preg_replace_callback( '/({@(.*?)})/m', 'pods_evaluate_tag_sanitized', (string) $tags );
  508. else
  509. return preg_replace_callback( '/({@(.*?)})/m', 'pods_evaluate_tag', (string) $tags );
  510. }
  511. /**
  512. * Evaluate tag like magic tag but through pods_var_raw and sanitized
  513. *
  514. * @param string|array $tag
  515. *
  516. * @return string
  517. * @version 2.1.0
  518. * @see pods_evaluate_tag
  519. */
  520. function pods_evaluate_tag_sanitized ( $tag ) {
  521. return pods_evaluate_tag( $tag, true );
  522. }
  523. /**
  524. * Evaluate tag like magic tag but through pods_var_raw
  525. *
  526. * @param string|array $tag
  527. * @param bool $sanitize Whether to sanitize tags
  528. *
  529. * @return string
  530. * @version 2.1.0
  531. */
  532. function pods_evaluate_tag ( $tag, $sanitize = false ) {
  533. // Handle pods_evaluate_tags
  534. if ( is_array( $tag ) ) {
  535. if ( !isset( $tag[ 2 ] ) && strlen( trim( $tag[ 2 ] ) ) < 1 )
  536. return;
  537. $tag = $tag[ 2 ];
  538. }
  539. $tag = trim( $tag, ' {@}' );
  540. $tag = explode( '.', $tag );
  541. if ( empty( $tag ) || !isset( $tag[ 0 ] ) || strlen( trim( $tag[ 0 ] ) ) < 1 )
  542. return;
  543. // Fix formatting that may be after the first .
  544. if ( 2 < count( $tag ) ) {
  545. $first_tag = $tag[ 0 ];
  546. unset( $tag[ 0 ] );
  547. $tag = array(
  548. $first_tag,
  549. implode( '.', $tag )
  550. );
  551. }
  552. foreach ( $tag as $k => $v ) {
  553. $tag[ $k ] = trim( $v );
  554. }
  555. $value = '';
  556. if ( 1 == count( $tag ) )
  557. $value = pods_var_raw( $tag[ 0 ], 'get', '', null, true );
  558. elseif ( 2 == count( $tag ) )
  559. $value = pods_var_raw( $tag[ 1 ], $tag[ 0 ], '', null, true );
  560. $value = apply_filters( 'pods_evaluate_tag', $value, $tag );
  561. if ( $sanitize )
  562. $value = pods_sanitize( $value );
  563. return $value;
  564. }
  565. /**
  566. * Split an array into human readable text (Item, Item, and Item)
  567. *
  568. * @param array $value
  569. * @param string $field
  570. * @param array $fields
  571. * @param string $and
  572. * @param string $field_index
  573. *
  574. * @return string
  575. */
  576. function pods_serial_comma ( $value, $field = null, $fields = null, $and = null, $field_index = null ) {
  577. if ( is_object( $value ) )
  578. $value = get_object_vars( $value );
  579. $simple = false;
  580. if ( !empty( $fields ) && is_array( $fields ) && isset( $fields[ $field ] ) ) {
  581. $field = $fields[ $field ];
  582. $tableless_field_types = apply_filters( 'pods_tableless_field_types', array( 'pick', 'file', 'avatar', 'taxonomy' ) );
  583. $simple_tableless_objects = PodsForm::field_method( 'pick', 'simple_objects' );
  584. if ( !empty( $field ) && is_array( $field ) && in_array( $field[ 'type' ], $tableless_field_types ) ) {
  585. if ( in_array( $field[ 'type' ], apply_filters( 'pods_file_field_types', array( 'file', 'avatar' ) ) ) ) {
  586. if ( null === $field_index )
  587. $field_index = 'guid';
  588. }
  589. elseif ( in_array( $field[ 'pick_object' ], $simple_tableless_objects ) )
  590. $simple = true;
  591. else {
  592. $table = pods_api()->get_table_info( $field[ 'pick_object' ], $field[ 'pick_val' ] );
  593. if ( !empty( $table ) ) {
  594. if ( null === $field_index )
  595. $field_index = $table[ 'field_index' ];
  596. }
  597. }
  598. }
  599. }
  600. if ( $simple && is_array( $field ) && !is_array( $value ) && !empty( $value ) )
  601. $value = PodsForm::field_method( 'pick', 'simple_value', $value, $field );
  602. if ( !is_array( $value ) )
  603. return $value;
  604. if ( null === $and )
  605. $and = ' ' . __( 'and', 'pods' ) . ' ';
  606. $last = '';
  607. $original_value = $value;
  608. if ( !empty( $value ) )
  609. $last = array_pop( $value );
  610. if ( $simple && is_array( $field ) && !is_array( $last ) && !empty( $last ) )
  611. $last = PodsForm::field_method( 'pick', 'simple_value', $last, $field );
  612. if ( is_array( $last ) ) {
  613. if ( null !== $field_index && isset( $last[ $field_index ] ) )
  614. $last = $last[ $field_index ];
  615. elseif ( isset( $last[ 0 ] ) )
  616. $last = $last[ 0 ];
  617. elseif ( $simple )
  618. $last = current( $last );
  619. else
  620. $last = '';
  621. }
  622. if ( !empty( $value ) ) {
  623. if ( null !== $field_index && isset( $original_value[ $field_index ] ) )
  624. return $original_value[ $field_index ];
  625. if ( 1 == count( $value ) ) {
  626. if ( isset( $value[ 0 ] ) )
  627. $value = $value[ 0 ];
  628. if ( $simple && is_array( $field ) && !is_array( $value ) && !empty( $value ) )
  629. $value = PodsForm::field_method( 'pick', 'simple_value', $value, $field );
  630. if ( is_array( $value ) ) {
  631. if ( null !== $field_index && isset( $value[ $field_index ] ) )
  632. $value = $value[ $field_index ];
  633. elseif ( $simple )
  634. $value = implode( ', ', $value );
  635. else
  636. $value = '';
  637. }
  638. $value = trim( $value, ', ' ) . ', ';
  639. }
  640. else {
  641. if ( null !== $field_index && isset( $value[ $field_index ] ) )
  642. return $value[ $field_index ];
  643. elseif ( !isset( $value[ 0 ] ) )
  644. $value = array( $value );
  645. foreach ( $value as $k => &$v ) {
  646. if ( $simple && is_array( $field ) && !is_array( $v ) && !empty( $v ) )
  647. $v = PodsForm::field_method( 'pick', 'simple_value', $v, $field );
  648. if ( is_array( $v ) ) {
  649. if ( null !== $field_index && isset( $v[ $field_index ] ) )
  650. $v = $v[ $field_index ];
  651. elseif ( $simple )
  652. $v = trim( implode( ', ', $v ), ', ' );
  653. else
  654. unset( $value[ $k ] );
  655. }
  656. }
  657. $value = trim( implode( ', ', $value ), ', ' ) . ', ';
  658. }
  659. $value = trim( $value );
  660. $last = trim( $last );
  661. if ( 0 < strlen( $value ) && 0 < strlen( $last ) )
  662. $value = $value . $and . $last;
  663. elseif ( 0 < strlen( $last ) )
  664. $value = $last;
  665. else
  666. $value = '';
  667. }
  668. else
  669. $value = $last;
  670. $value = trim( $value, ', ' );
  671. return (string) $value;
  672. }
  673. /**
  674. * Return a variable if a user is logged in or anonymous, or a specific capability
  675. *
  676. * @param mixed $anon Variable to return if user is anonymous (not logged in)
  677. * @param mixed $user Variable to return if user is logged in
  678. * @param string|array $capability Capability or array of Capabilities to check to return $user on
  679. *
  680. * @since 2.0.5
  681. */
  682. function pods_var_user ( $anon = false, $user = true, $capability = null ) {
  683. $value = $anon;
  684. if ( is_user_logged_in() ) {
  685. if ( empty( $capability ) )
  686. $value = $user;
  687. else {
  688. $capabilities = (array) $capability;
  689. foreach ( $capabilities as $capability ) {
  690. if ( current_user_can( $capability ) ) {
  691. $value = $user;
  692. break;
  693. }
  694. }
  695. }
  696. }
  697. return $value;
  698. }