cf_http_build_url

Wrapper for "http_build_url" -- will use http_build_url() if it exists, if not will do same thing.

Description

(string) cf_http_build_url( $url, (array) $parts = array(), (constant) $flags = HTTP_URL_REPLACE, (bool) &$new_url = false ); 

See: http://php.net/manual/en/function.http-build-query.php

Returns (string)

Parameters (4)

0. $url
The url.
1. $parts — Optional. (array) => array()
The parts.
2. $flags — Optional. (constant) => HTTP_URL_REPLACE
The flags.
3. $new_url — Optional. (bool) => &$new_url = false
The new url.

Usage

  1. if ( !function_exists( 'cf_http_build_url' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-forms/includes/compat.php'; 
  3.  
  4. // The url. 
  5. $url = null; 
  6.  
  7. // The parts. 
  8. $parts = array(); 
  9.  
  10. // The flags. 
  11. $flags = HTTP_URL_REPLACE; 
  12.  
  13. // The new url. 
  14. $new_url = &$new_url = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = cf_http_build_url($url, $parts, $flags, $new_url); 
  18.  

Defined (1)

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

/includes/compat.php  
  1. function cf_http_build_url( $url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false ) { 
  2. if ( function_exists( 'http_build_url' ) ) { 
  3. return http_build_url( $url, $parts, $flags, $new_url ); 
  4.  
  5. $keys = array( 'user', 'pass', 'port', 'path', 'query', 'fragment' ); 
  6.  
  7. if ( $flags & HTTP_URL_STRIP_ALL ) { 
  8. $flags |= HTTP_URL_STRIP_USER
  9. $flags |= HTTP_URL_STRIP_PASS
  10. $flags |= HTTP_URL_STRIP_PORT
  11. $flags |= HTTP_URL_STRIP_PATH
  12. $flags |= HTTP_URL_STRIP_QUERY
  13. } elseif ( $flags & HTTP_URL_STRIP_AUTH ) { 
  14. $flags |= HTTP_URL_STRIP_USER
  15. $flags |= HTTP_URL_STRIP_PASS
  16.  
  17. // Parse the original URL 
  18. $parse_url = parse_url( $url ); 
  19.  
  20. // Scheme and Host are always replaced 
  21. if ( isset( $parts[ 'scheme' ] ) ) { 
  22. $parse_url[ 'scheme' ] = $parts[ 'scheme' ]; 
  23. if ( isset( $parts[ 'host' ] ) ) { 
  24. $parse_url[ 'host' ] = $parts[ 'host' ]; 
  25.  
  26.  
  27. if ( $flags & HTTP_URL_REPLACE ) { 
  28. foreach ( $keys as $key ) { 
  29. if ( isset( $parts[ $key ] ) ) { 
  30. $parse_url[ $key ] = $parts[ $key ]; 
  31. } else { 
  32.  
  33. if ( isset( $parts[ 'path' ] ) && ( $flags & HTTP_URL_JOIN_PATH ) ) { 
  34. if ( isset( $parse_url[ 'path' ] ) ) { 
  35. $parse_url[ 'path' ] = rtrim( str_replace( basename( $parse_url[ 'path' ] ), '', $parse_url[ 'path' ] ), '/' ) . '/' . ltrim( $parts[ 'path' ], '/' ); 
  36. } else { 
  37. $parse_url[ 'path' ] = $parts[ 'path' ]; 
  38.  
  39.  
  40. if ( isset( $parts[ 'query' ] ) && ( $flags & HTTP_URL_JOIN_QUERY ) ) { 
  41. if ( isset( $parse_url[ 'query' ] ) ) { 
  42. $parse_url[ 'query' ] .= '&' . $parts[ 'query' ]; 
  43. } else { 
  44. $parse_url[ 'query' ] = $parts[ 'query' ]; 
  45.  
  46.  
  47. foreach ( $keys as $key ) { 
  48. if ( $flags & (int) constant( 'HTTP_URL_STRIP_' . strtoupper( $key ) ) ) { 
  49. unset( $parse_url[ $key ] ); 
  50.  
  51. if( isset( $parse_url[ $key ] ) && is_array( $parse_url[ $key ] ) ) { 
  52. $parse_url[ $key ] = http_build_query( $parse_url[ $key ] ); 
  53.  
  54.  
  55.  
  56. $new_url = $parse_url; 
  57.  
  58. return ( ( isset( $parse_url[ 'scheme' ] ) ) ? $parse_url[ 'scheme' ] . '://' : '' ) 
  59. . ( ( isset( $parse_url[ 'user' ] ) ) ? $parse_url[ 'user' ] . ( ( isset( $parse_url[ 'pass' ] ) ) ? ':' . $parse_url[ 'pass' ] : '' ) . '@' : '' ) 
  60. . ( ( isset( $parse_url[ 'host' ] ) ) ? $parse_url[ 'host' ] : '' ) 
  61. . ( ( isset( $parse_url[ 'port' ] ) ) ? ':' . $parse_url[ 'port' ] : '' ) 
  62. . ( ( isset( $parse_url[ 'path' ] ) ) ? $parse_url[ 'path' ] : '' ) 
  63. . ( ( isset( $parse_url[ 'query' ] ) ) ? '?' . $parse_url[ 'query' ] : '' ) 
  64. . ( ( isset( $parse_url[ 'fragment' ] ) ) ? '#' . $parse_url[ 'fragment' ] : '' );