pods_v

Return a variable (if exists).

Description

pods_v( (constant) $var = null, (string) $type = 'get', (constant) $default = null, (constant) $strict = false, (array) $params = array() ); 

Parameters (5)

0. $var — Optional. (constant) => null
The variable name, can also be a modifier for specific types
1. $type — Optional. (string) => 'get'
Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates
2. $default — Optional. (constant) => null
The default value to set if variable doesn't exist
3. $strict — Optional. (constant) => false
Only allow values (must not be empty)
4. $params — Optional. (array) => array()
Set casting=>true to cast value from $default, allowed=>$allowed to restrict a value to what's allowed

Usage

  1. if ( !function_exists( 'pods_v' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'pods-custom-content-types-and-fields/includes/data.php'; 
  3.  
  4. // The variable name, can also be a modifier for specific types 
  5. $var = null; 
  6.  
  7. // (optional) Super globals, url/url-relative, constants, globals, options, transients, cache, user data, Pod field values, dates 
  8. $type = 'get'; 
  9.  
  10. // (optional) The default value to set if variable doesn't exist 
  11. $default = null; 
  12.  
  13. // (optional) Only allow values (must not be empty) 
  14. $strict = false; 
  15.  
  16. // (optional) Set 'casting'=>true to cast value from $default, 'allowed'=>$allowed to restrict a value to what's allowed 
  17. $params = array(); 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = pods_v($var, $type, $default, $strict, $params); 
  21.  

Defined (1)

The function is defined in the following location(s).

/includes/data.php  
  1. function pods_v( $var = null, $type = 'get', $default = null, $strict = false, $params = array() ) { 
  2.  
  3. $defaults = array( 
  4. 'casting' => false,  
  5. 'allowed' => null 
  6. ); 
  7.  
  8. $params = (object) array_merge( $defaults, (array) $params ); 
  9.  
  10. $output = null; 
  11.  
  12. if ( null === $type || '' === $type ) { 
  13. // Invalid $type 
  14. } elseif ( is_array( $type ) ) { 
  15. if ( isset( $type[ $var ] ) ) { 
  16. $output = $type[ $var ]; 
  17. } elseif ( is_object( $type ) ) { 
  18. if ( isset( $type->{$var} ) ) { 
  19. $output = $type->{$var}; 
  20. } else { 
  21. $type = strtolower( (string) $type ); 
  22. switch ( $type ) { 
  23. case 'get': 
  24. if ( isset( $_GET[ $var ] ) ) { 
  25. $output = pods_unslash( $_GET[ $var ] ); 
  26. break; 
  27. case 'post': 
  28. if ( isset( $_POST[ $var ] ) ) { 
  29. $output = pods_unslash( $_POST[ $var ] ); 
  30. break; 
  31. case 'request': 
  32. if ( isset( $_REQUEST[ $var ] ) ) { 
  33. $output = pods_unslash( $_REQUEST[ $var ] ); 
  34. break; 
  35. case 'url': 
  36. case 'uri': 
  37. $url = parse_url( pods_current_url() ); 
  38. $uri = trim( $url[ 'path' ], '/' ); 
  39. $uri = array_filter( explode( '/', $uri ) ); 
  40.  
  41. if ( 'first' == $var ) { 
  42. $var = 0; 
  43. } elseif ( 'last' == $var ) { 
  44. $var = - 1; 
  45.  
  46. if ( is_numeric( $var ) ) { 
  47. $output = ( $var < 0 ) ? pods_v( count( $uri ) + $var, $uri ) : pods_v( $var, $uri ); 
  48. break; 
  49. case 'url-relative': 
  50. $url_raw = pods_current_url(); 
  51. $prefix = get_site_url(); 
  52.  
  53. if ( substr( $url_raw, 0, strlen( $prefix ) ) == $prefix ) { 
  54. $url_raw = substr( $url_raw, strlen( $prefix ) + 1, strlen( $url_raw ) ); 
  55.  
  56. $url = parse_url( $url_raw ); 
  57. $uri = trim( $url[ 'path' ], '/' ); 
  58. $uri = array_filter( explode( '/', $uri ) ); 
  59.  
  60. if ( 'first' == $var ) { 
  61. $var = 0; 
  62. } elseif ( 'last' == $var ) { 
  63. $var = - 1; 
  64.  
  65. if ( is_numeric( $var ) ) { 
  66. $output = ( $var < 0 ) ? pods_v( count( $uri ) + $var, $uri ) : pods_v( $var, $uri ); 
  67. break; 
  68. case 'template-url': 
  69. $output = get_template_directory_uri(); 
  70. break; 
  71. case 'stylesheet-url': 
  72. $output = get_stylesheet_directory_uri(); 
  73. break; 
  74. case 'site-url': 
  75. $blog_id = $scheme = null; 
  76. $path = ''; 
  77.  
  78. if ( is_array( $var ) ) { 
  79. if ( isset( $var[ 0 ] ) ) { 
  80. $blog_id = $var[ 0 ]; 
  81. } elseif ( isset( $var[ 1 ] ) ) { 
  82. $path = $var[ 1 ]; 
  83. } elseif ( isset( $var[ 2 ] ) ) { 
  84. $scheme = $var[ 2 ]; 
  85. } else { 
  86. $blog_id = $var; 
  87.  
  88. $output = get_site_url( $blog_id, $path, $scheme ); 
  89. break; 
  90. case 'home-url': 
  91. $blog_id = $scheme = null; 
  92. $path = ''; 
  93.  
  94. if ( is_array( $var ) ) { 
  95. if ( isset( $var[ 0 ] ) ) { 
  96. $blog_id = $var[ 0 ]; 
  97. } elseif ( isset( $var[ 1 ] ) ) { 
  98. $path = $var[ 1 ]; 
  99. } elseif ( isset( $var[ 2 ] ) ) { 
  100. $scheme = $var[ 2 ]; 
  101. } else { 
  102. $blog_id = $var; 
  103.  
  104. $output = get_home_url( $blog_id, $path, $scheme ); 
  105. break; 
  106. case 'admin-url': 
  107. $blog_id = $scheme = null; 
  108. $path = ''; 
  109.  
  110. if ( is_array( $var ) ) { 
  111. if ( isset( $var[ 0 ] ) ) { 
  112. $blog_id = $var[ 0 ]; 
  113. } elseif ( isset( $var[ 1 ] ) ) { 
  114. $path = $var[ 1 ]; 
  115. } elseif ( isset( $var[ 2 ] ) ) { 
  116. $scheme = $var[ 2 ]; 
  117. } else { 
  118. $blog_id = $var; 
  119.  
  120. $output = get_admin_url( $blog_id, $path, $scheme ); 
  121. break; 
  122. case 'includes-url': 
  123. $output = includes_url( $var ); 
  124. break; 
  125. case 'content-url': 
  126. $output = content_url( $var ); 
  127. break; 
  128. case 'plugins-url': 
  129. $path = $plugin = ''; 
  130.  
  131. if ( is_array( $var ) ) { 
  132. if ( isset( $var[ 0 ] ) ) { 
  133. $path = $var[ 0 ]; 
  134. } elseif ( isset( $var[ 1 ] ) ) { 
  135. $plugin = $var[ 1 ]; 
  136. } else { 
  137. $path = $var; 
  138.  
  139. $output = plugins_url( $path, $plugin ); 
  140. break; 
  141. case 'network-site-url': 
  142. $path = ''; 
  143. $scheme = null; 
  144.  
  145. if ( is_array( $var ) ) { 
  146. if ( isset( $var[ 0 ] ) ) { 
  147. $path = $var[ 0 ]; 
  148. } elseif ( isset( $var[ 1 ] ) ) { 
  149. $scheme = $var[ 1 ]; 
  150. } else { 
  151. $path = $var; 
  152.  
  153. $output = network_site_url( $path, $scheme ); 
  154. break; 
  155. case 'network-home-url': 
  156. $path = ''; 
  157. $scheme = null; 
  158.  
  159. if ( is_array( $var ) ) { 
  160. if ( isset( $var[ 0 ] ) ) { 
  161. $path = $var[ 0 ]; 
  162. } elseif ( isset( $var[ 1 ] ) ) { 
  163. $scheme = $var[ 1 ]; 
  164. } else { 
  165. $path = $var; 
  166.  
  167. $output = network_home_url( $path, $scheme ); 
  168. break; 
  169. case 'network-admin-url': 
  170. $path = ''; 
  171. $scheme = null; 
  172.  
  173. if ( is_array( $var ) ) { 
  174. if ( isset( $var[ 0 ] ) ) { 
  175. $path = $var[ 0 ]; 
  176. } elseif ( isset( $var[ 1 ] ) ) { 
  177. $scheme = $var[ 1 ]; 
  178. } else { 
  179. $path = $var; 
  180.  
  181. $output = network_admin_url( $path, $scheme ); 
  182. break; 
  183. case 'user-admin-url': 
  184. $path = ''; 
  185. $scheme = null; 
  186.  
  187. if ( is_array( $var ) ) { 
  188. if ( isset( $var[ 0 ] ) ) { 
  189. $path = $var[ 0 ]; 
  190. } elseif ( isset( $var[ 1 ] ) ) { 
  191. $scheme = $var[ 1 ]; 
  192. } else { 
  193. $path = $var; 
  194.  
  195. $output = user_admin_url( $path, $scheme ); 
  196. break; 
  197. case 'prefix': 
  198. global $wpdb; 
  199.  
  200. $output = $wpdb->prefix; 
  201. break; 
  202. case 'server': 
  203. if ( ! pods_strict() ) { 
  204. if ( isset( $_SERVER[ $var ] ) ) { 
  205. $output = pods_unslash( $_SERVER[ $var ] ); 
  206. } elseif ( isset( $_SERVER[ strtoupper( $var ) ] ) ) { 
  207. $output = pods_unslash( $_SERVER[ strtoupper( $var ) ] ); 
  208. break; 
  209. case 'session': 
  210. if ( isset( $_SESSION[ $var ] ) ) 
  211. $output = $_SESSION[ $var ]; 
  212. break; 
  213. case 'global': 
  214. case 'globals': 
  215. if ( isset( $GLOBALS[ $var ] ) ) 
  216. $output = $GLOBALS[ $var ]; 
  217. break; 
  218. case 'cookie': 
  219. if ( isset( $_COOKIE[ $var ] ) ) 
  220. $output = pods_unslash( $_COOKIE[ $var ] ); 
  221. break; 
  222. case 'constant': 
  223. if ( defined( $var ) ) 
  224. $output = constant( $var ); 
  225. break; 
  226. case 'user': 
  227. if ( is_user_logged_in() ) { 
  228. $user = get_userdata( get_current_user_id() ); 
  229.  
  230. if ( isset( $user->{$var} ) ) { 
  231. $value = $user->{$var}; 
  232. } elseif ( 'role' == $var ) { 
  233. $value = ''; 
  234.  
  235. if ( ! empty( $user->roles ) ) { 
  236. $value = array_shift( $user->roles ); 
  237. } else { 
  238. $value = get_user_meta( $user->ID, $var ); 
  239.  
  240. if ( is_array( $value ) && ! empty( $value ) ) { 
  241. $output = $value; 
  242. } elseif ( ! is_array( $value ) && 0 < strlen( $value ) ) { 
  243. $output = $value; 
  244. break; 
  245. case 'option': 
  246. $output = get_option( $var, $default ); 
  247. break; 
  248. case 'site-option': 
  249. $output = get_site_option( $var, $default ); 
  250. break; 
  251. case 'transient': 
  252. $output = get_transient( $var ); 
  253. break; 
  254. case 'site-transient': 
  255. $output = get_site_transient( $var ); 
  256. break; 
  257. case 'cache': 
  258. if ( isset( $GLOBALS[ wp_object_cache ] ) && is_object( $GLOBALS[ wp_object_cache ] ) ) { 
  259. $group = 'default'; 
  260. $force = false; 
  261.  
  262. if ( ! is_array( $var ) ) { 
  263. $var = explode( '|', $var ); 
  264.  
  265. if ( isset( $var[ 0 ] ) ) { 
  266. if ( isset( $var[ 1 ] ) ) { 
  267. $group = $var[ 1 ]; 
  268.  
  269. if ( isset( $var[ 2 ] ) ) { 
  270. $force = $var[ 2 ]; 
  271.  
  272. $var = $var[ 0 ]; 
  273.  
  274. $output = wp_cache_get( $var, $group, $force ); 
  275. break; 
  276. case 'pods-transient': 
  277. $callback = null; 
  278.  
  279. if ( ! is_array( $var ) ) { 
  280. $var = explode( '|', $var ); 
  281.  
  282. if ( isset( $var[ 0 ] ) ) { 
  283. if ( isset( $var[ 1 ] ) ) { 
  284. $callback = $var[ 1 ]; 
  285.  
  286. $var = $var[ 0 ]; 
  287.  
  288. $output = pods_transient_get( $var, $callback ); 
  289. break; 
  290. case 'pods-site-transient': 
  291. $callback = null; 
  292.  
  293. if ( ! is_array( $var ) ) { 
  294. $var = explode( '|', $var ); 
  295.  
  296. if ( isset( $var[ 0 ] ) ) { 
  297. if ( isset( $var[ 1 ] ) ) { 
  298. $callback = $var[ 1 ]; 
  299.  
  300. $var = $var[ 0 ]; 
  301.  
  302. $output = pods_site_transient_get( $var, $callback ); 
  303. break; 
  304. case 'pods-cache': 
  305. if ( isset( $GLOBALS[ wp_object_cache ] ) && is_object( $GLOBALS[ wp_object_cache ] ) ) { 
  306. $group = 'default'; 
  307. $callback = null; 
  308.  
  309. if ( ! is_array( $var ) ) { 
  310. $var = explode( '|', $var ); 
  311.  
  312. if ( isset( $var[ 0 ] ) ) { 
  313. if ( isset( $var[ 1 ] ) ) { 
  314. $group = $var[ 1 ]; 
  315.  
  316. if ( isset( $var[ 2 ] ) ) { 
  317. $callback = $var[ 2 ]; 
  318.  
  319. $var = $var[ 0 ]; 
  320.  
  321. $output = pods_cache_get( $var, $group, $callback ); 
  322. break; 
  323. case 'pods-option-cache': 
  324. $group = 'default'; 
  325. $callback = null; 
  326.  
  327. if ( ! is_array( $var ) ) { 
  328. $var = explode( '|', $var ); 
  329.  
  330. if ( isset( $var[ 0 ] ) ) { 
  331. if ( isset( $var[ 1 ] ) ) { 
  332. $group = $var[ 1 ]; 
  333.  
  334. if ( isset( $var[ 2 ] ) ) { 
  335. $callback = $var[ 2 ]; 
  336.  
  337. $var = $var[ 0 ]; 
  338.  
  339. $output = pods_option_cache_get( $var, $group, $callback ); 
  340. break; 
  341. case 'date': 
  342. $var = explode( '|', $var ); 
  343.  
  344. if ( ! empty( $var ) ) { 
  345. $output = date_i18n( $var[ 0 ], ( isset( $var[ 1 ] ) ? strtotime( $var[ 1 ] ) : false ) ); 
  346. break; 
  347. case pods 
  348. case 'pods_display': 
  349. /** 
  350. * @var $pods Pods 
  351. */ 
  352. global $pods; 
  353.  
  354. if ( is_object( $pods ) && 'Pods' == get_class( $pods ) ) { 
  355. if ( pods === $type ) { 
  356. $output = $pods->field( $var ); 
  357.  
  358. if ( is_array( $output ) ) { 
  359. $options = array( 
  360. 'field' => $var,  
  361. 'fields' => $pods->fields 
  362. ); 
  363.  
  364. $output = pods_serial_comma( $output, $options ); 
  365. } elseif ( 'pods_display' === $type ) { 
  366. $output = $pods->display( $var ); 
  367. break; 
  368. default: 
  369. $output = apply_filters( 'pods_var_' . $type, $default, $var, $strict, $params ); 
  370.  
  371. if ( null !== $default ) { 
  372. // Set default 
  373. if ( null === $output ) { 
  374. $output = $default; 
  375.  
  376. // Casting 
  377. if ( true === $params->casting ) { 
  378. $output = pods_cast( $output, $default ); 
  379.  
  380. // Strict defaults for empty values 
  381. if ( true === $strict ) { 
  382. if ( empty( $output ) ) { 
  383. $output = $default; 
  384.  
  385. // Allowed values 
  386. if ( null !== $params->allowed ) { 
  387. if ( is_array( $params->allowed ) ) { 
  388. // Not in array and is not the same array 
  389. if ( ! in_array( $output, $params->allowed ) && ( ! is_array( $output ) || $output !== $params->allowed ) ) { 
  390. $output = $default; 
  391. } elseif ( $output !== $params->allowed ) { 
  392. // Value doesn't match 
  393. $output = $default; 
  394.  
  395. return $output;