[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> url.php (source)

   1  <?php
   2  /**
   3   * Functions, which deal with URLs: manipulation, generation
   4   */
   5  
   6  /**
   7   * Gives the path of an URL
   8   *
   9   * @param string $url Optional. The default is the GlotPress URL
  10   */
  11  function gp_url_path( $url = null ) {
  12      if ( is_null( $url ) ) $url = gp_url();
  13      $parsed = parse_url( $url );
  14      return $parsed['path'];
  15  }
  16  
  17  /**
  18   * Joins paths, and takes care of slashes between them
  19   *
  20   * Example: gp_url_join( '/project', array( 'wp', 'dev) ) -> '/project/wp/dev'
  21   *
  22   * The function will keep leading and trailing slashes of the whole URL, but won't
  23   * allow more than consecutive slash inside.
  24   *
  25   * @param mixed components... arbitrary number of string or path components
  26   * @return string URL, built of all the components, separated with /
  27   */
  28  function gp_url_join() {
  29      $components = func_get_args();
  30      $components_in_flat_array = array_filter( gp_array_flatten( $components ) );
  31      $components_with_slashes = implode( '/', $components_in_flat_array );
  32      $components_without_consecutive_slashes = preg_replace( '|/{2,}|', '/', $components_with_slashes );
  33      $components_without_consecutive_slashes = str_replace( array( 'http:/', 'https:/' ), array( 'http://', 'https://' ), $components_without_consecutive_slashes );
  34      return $components_without_consecutive_slashes;
  35  }
  36  
  37  /**
  38   * Builds a URL relative to the GlotPress' domain root
  39   *
  40   * @param mixed $path string path or array of path components
  41   * @param array $query associative array of query arguments (optional)
  42   */
  43  function gp_url( $path = '/', $query = null ) {
  44      return apply_filters( 'gp_url', gp_url_add_path_and_query( gp_url_path( gp_url_public_root() ), $path, $query ), $path, $query );
  45  }
  46  
  47  function gp_url_base( $path = '/', $query = null ) {
  48      return apply_filters( 'gp_url_base', gp_url_add_path_and_query( gp_url_path( gp_url_base_root() ), $path, $query ), $path, $query );
  49  }
  50  
  51  
  52  function gp_url_add_path_and_query( $base, $path, $query ) {
  53      // todo: same domain with current url?
  54      $url = gp_url_join( $base, $path );
  55      if ( $query && is_array( $query ) )
  56          $url = add_query_arg( urlencode_deep( $query ), $url );
  57      elseif ( $query )
  58          $url .= '?' . ltrim( $query, '?' );
  59      return apply_filters( 'gp_url_add_path_and_query', $url, $base, $path, $query );
  60  }
  61  
  62  /**
  63   * Converts an absolute URL to the corresponding SSL URL if the GlotPress
  64   * settings allow SSL
  65   */
  66  function gp_url_ssl( $url ) {
  67      if ( defined( 'GP_SSL' ) && GP_SSL ) {
  68          $url = preg_replace( '/^http:/', 'https:', $url );
  69      }
  70      return $url;
  71  }
  72  
  73  function gp_url_base_root() {
  74      $url_from_db = gp_get_option( 'url' );
  75      return gp_const_get( 'GP_BASE_URL', $url_from_db? $url_from_db : '' );
  76  }
  77  
  78  function gp_url_public_root() {
  79      return gp_const_get( 'GP_URL', gp_url_base_root() );
  80  }
  81  
  82  /**
  83   * Constructs URL for a project and locale.
  84   * /<project-path>/<locale>/<path>/<page>
  85   */
  86  function gp_url_project_locale( $project_or_path, $locale, $path = '', $query = null ) {
  87      return gp_url_project( $project_or_path, array( $locale, $path ), $query );
  88  }
  89  
  90  function gp_url_img( $file ) {
  91      return gp_url_base( array( 'img', $file ) );
  92  }
  93  
  94  /**
  95   * The URL of the current page
  96   */
  97  function gp_url_current() {
  98      $default_port = is_ssl()? 443 : 80;
  99      $host = gp_array_get( $_SERVER, 'HTTP_HOST' );
 100      if ( gp_array_get( $_SERVER, 'SERVER_PORT', $default_port ) != $default_port ) $host .= ':' . gp_array_get( $_SERVER, 'SERVER_PORT' );
 101      $path_and_args = gp_array_get( $_SERVER, 'REQUEST_URI' );
 102      $protocol = is_ssl()? 'https' : 'http';
 103      return "{$protocol}://{$host}{$path_and_args}";
 104  }
 105  
 106  function gp_url_project( $project_or_path = '', $path = '', $query = null ) {
 107      $project_path = is_object( $project_or_path )? $project_or_path->path : $project_or_path;
 108      return gp_url( array( 'projects', $project_path, $path ), $query );
 109  }
 110  
 111  function gp_url_login( $redirect_to = null ) {
 112      return gp_url( '/login', array( 'redirect_to' => $redirect_to? $redirect_to : gp_url_current() ) );
 113  }
 114  
 115  function gp_url_logout() {
 116      return gp_url( '/logout' );
 117  }


Generated: Fri May 24 04:00:00 2013 Hosted by follow the white rabbit.