[ 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   * Returns the path of an URL.
   8   *
   9   * @since 1.0.0
  10   *
  11   * @param string $url Optional. The URL to parse. Defaults to GlotPress URL.
  12   * @return string Path of the URL. Empty string if no path was parsed.
  13   */
  14  function gp_url_path( $url = null ) {
  15      if ( null === $url ) {
  16          $url = gp_url();
  17      }
  18  
  19      $parsed = parse_url( $url );
  20      return isset( $parsed['path'] ) ? $parsed['path'] : '';
  21  }
  22  
  23  /**
  24   * Joins paths, and takes care of slashes between them.
  25   *
  26   * Example: gp_url_join( '/project', array( 'wp', 'dev) ) -> '/project/wp/dev'
  27   *
  28   * The function will keep leading and trailing slashes of the whole URL, but won't
  29   * allow more than consecutive slash inside.
  30   *
  31   * @param mixed ...$components Arbitrary number of string or path components.
  32   * @return string URL, built of all the components, separated with /.
  33   */
  34  function gp_url_join( ...$components ) {
  35      $components_in_flat_array = array_filter( gp_array_flatten( $components ) );
  36      $components_with_slashes  = implode( '/', $components_in_flat_array );
  37  
  38      // Make sure all instances of the final URL are returned with a proper permalink ending.
  39      $components_with_slashes = user_trailingslashit( $components_with_slashes );
  40  
  41      $components_without_consecutive_slashes = preg_replace( '|/{2,}|', '/', $components_with_slashes );
  42      $components_without_consecutive_slashes = str_replace( array( 'http:/', 'https:/' ), array( 'http://', 'https://' ), $components_without_consecutive_slashes );
  43      return $components_without_consecutive_slashes;
  44  }
  45  
  46  /**
  47   * Builds a URL relative to the GlotPress' domain root.
  48   *
  49   * @param mixed $path string path or array of path components
  50   * @param array $query associative array of query arguments (optional)
  51   */
  52  function gp_url( $path = '/', $query = null ) {
  53      $base = gp_url_path( gp_url_public_root() );
  54      $base = '/' . ltrim( $base, '/' ); // Make sure `$base` has always a leading slash.
  55  
  56      /**
  57       * Filter a URL relative to GlotPress' domain root.
  58       *
  59       * @since 1.0.0
  60       *
  61       * @param string        $base The base path.
  62       * @param string|array  $path The GlotPress path or the components as an array.
  63       * @param string $query The query part of the URL.
  64       */
  65      return apply_filters( 'gp_url', gp_url_add_path_and_query( $base, $path, $query ), $path, $query );
  66  }
  67  
  68  function gp_url_add_path_and_query( $base, $path, $query ) {
  69      // todo: same domain with current url?
  70      $url = gp_url_join( $base, $path );
  71  
  72      if ( $query && is_array( $query ) ) {
  73          $url = add_query_arg( urlencode_deep( $query ), $url );
  74      } elseif ( $query ) {
  75          $url .= '?' . ltrim( $query, '?' );
  76      }
  77  
  78      /**
  79       * Filter a GlotPress URL with path and query.
  80       *
  81       * @since 1.0.0
  82       *
  83       * @param string $url Generated URL.
  84       * @param string $base The base path.
  85       * @param array  $path The GlotPress path or the components as an array.
  86       * @param string $query The query part of the URL.
  87       */
  88      return apply_filters( 'gp_url_add_path_and_query', $url, $base, $path, $query );
  89  }
  90  
  91  /**
  92   * Retrieves the URL for the GlotPress root page.
  93   *
  94   * @since 1.0.0
  95   *
  96   * @return string GlotPress root page URL.
  97   */
  98  function gp_url_public_root() {
  99      return home_url( gp_url_base_path() );
 100  }
 101  
 102  /**
 103   * Constructs URL for a project and locale.
 104   * /<project-path>/<locale>/<path>/<page>
 105   */
 106  function gp_url_project_locale( $project_or_path, $locale, $path = '', $query = null ) {
 107      return gp_url_project( $project_or_path, array( $locale, $path ), $query );
 108  }
 109  
 110  /**
 111   * Get the URL for an image file
 112   *
 113   * @param string $file Image filename
 114   *
 115   * @return string
 116   */
 117  function gp_url_img( $file ) {
 118      return gp_plugin_url( "assets/img/$file" );
 119  }
 120  
 121  /**
 122   * The URL of the current page
 123   */
 124  function gp_url_current() {
 125      $protocol      = is_ssl() ? 'https://' : 'http://';
 126      $host          = wp_unslash( gp_array_get( $_SERVER, 'HTTP_HOST' ) );
 127      $path_and_args = wp_unslash( gp_array_get( $_SERVER, 'REQUEST_URI' ) );
 128  
 129      return $protocol . $host . $path_and_args;
 130  }
 131  
 132  /**
 133   * Get the URL for a project
 134   *
 135   * A leading double-slash will avoid prepending /projects/ to the path.
 136   *
 137   * @param GP_Project|string $project_or_path Project path or object.
 138   * @param string|array      $path            Addition path to append to the base path.
 139   * @param array             $query           Optional. Associative array of query arguments.
 140   *
 141   * @return string
 142   */
 143  function gp_url_project( $project_or_path = '', $path = '', $query = null ) {
 144      $project_path = is_object( $project_or_path ) ? $project_or_path->path : $project_or_path;
 145  
 146      // A leading double-slash will avoid prepending /projects/ to the path.
 147      // This was introduced to enable linking to the locale glossary.
 148      if ( '//' === substr( $project_path, 0, 2 ) ) {
 149          $project_path = ltrim( $project_path, '/' );
 150      } else {
 151          $project_path = array( 'projects', $project_path );
 152      }
 153  
 154      return gp_url( array( $project_path, $path ), $query );
 155  }
 156  
 157  function gp_url_profile( $user_nicename = '' ) {
 158      $url = gp_url( array( '/profile', $user_nicename ) );
 159      /**
 160       * Filter the URL of a user profile.
 161       *
 162       * @since 1.0.0
 163       *
 164       * @param string $url           The URL of the profile.
 165       * @param string $user_nicename User's nicename; the slug of the user.
 166       */
 167      return apply_filters( 'gp_url_profile', $url, $user_nicename );
 168  }
 169  
 170  function gp_url_base_path() {
 171      /**
 172       * Filter the base path of a GlotPress URL.
 173       *
 174       * @since 1.0.0
 175       *
 176       * @param string $url The url.
 177       */
 178      return apply_filters( 'gp_url_base_path', user_trailingslashit( '/' . gp_const_get( 'GP_URL_BASE', 'glotpress' ) ) );
 179  }
 180  
 181  function gp_plugin_url( $path = '' ) {
 182      return plugins_url( $path, GP_PLUGIN_FILE );
 183  }


Generated: Thu Dec 26 01:01:15 2024 Cross-referenced by PHPXref 0.7.1