[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
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() { 35 $components = func_get_args(); 36 $components_in_flat_array = array_filter( gp_array_flatten( $components ) ); 37 $components_with_slashes = implode( '/', $components_in_flat_array ); 38 39 // Make sure all instances of the final URL are returned with a proper permalink ending. 40 $components_with_slashes = user_trailingslashit( $components_with_slashes ); 41 42 $components_without_consecutive_slashes = preg_replace( '|/{2,}|', '/', $components_with_slashes ); 43 $components_without_consecutive_slashes = str_replace( array( 'http:/', 'https:/' ), array( 'http://', 'https://' ), $components_without_consecutive_slashes ); 44 return $components_without_consecutive_slashes; 45 } 46 47 /** 48 * Builds a URL relative to the GlotPress' domain root. 49 * 50 * @param mixed $path string path or array of path components 51 * @param array $query associative array of query arguments (optional) 52 */ 53 function gp_url( $path = '/', $query = null ) { 54 $base = gp_url_path( gp_url_public_root() ); 55 $base = '/' . ltrim( $base, '/' ); // Make sure `$base` has always a leading slash. 56 57 /** 58 * Filter a URL relative to GlotPress' domain root. 59 * 60 * @since 1.0.0 61 * 62 * @param string $base The base path. 63 * @param string|array $path The GlotPress path or the components as an array. 64 * @param string $query The query part of the URL. 65 */ 66 return apply_filters( 'gp_url', gp_url_add_path_and_query( $base, $path, $query ), $path, $query ); 67 } 68 69 function gp_url_add_path_and_query( $base, $path, $query ) { 70 // todo: same domain with current url? 71 $url = gp_url_join( $base, $path ); 72 73 if ( $query && is_array( $query ) ) { 74 $url = add_query_arg( urlencode_deep( $query ), $url ); 75 } elseif ( $query ) { 76 $url .= '?' . ltrim( $query, '?' ); 77 } 78 79 /** 80 * Filter a GlotPress URL with path and query. 81 * 82 * @since 1.0.0 83 * 84 * @param string $url Generated URL. 85 * @param string $base The base path. 86 * @param array $path The GlotPress path or the components as an array. 87 * @param string $query The query part of the URL. 88 */ 89 return apply_filters( 'gp_url_add_path_and_query', $url, $base, $path, $query ); 90 } 91 92 /** 93 * Converts an absolute URL to the corresponding SSL URL if the GlotPress 94 * settings allow SSL 95 */ 96 function gp_url_ssl( $url ) { 97 if ( defined( 'GP_SSL' ) && GP_SSL ) { 98 $url = preg_replace( '/^http:/', 'https:', $url ); 99 } 100 return $url; 101 } 102 103 function gp_url_public_root() { 104 return home_url( gp_url_base_path() ); 105 } 106 107 /** 108 * Constructs URL for a project and locale. 109 * /<project-path>/<locale>/<path>/<page> 110 */ 111 function gp_url_project_locale( $project_or_path, $locale, $path = '', $query = null ) { 112 return gp_url_project( $project_or_path, array( $locale, $path ), $query ); 113 } 114 115 /** 116 * Get the URL for an image file 117 * 118 * @param string $file Image filename 119 * 120 * @return string 121 */ 122 function gp_url_img( $file ) { 123 return gp_plugin_url( "assets/img/$file" ); 124 } 125 126 /** 127 * The URL of the current page 128 */ 129 function gp_url_current() { 130 $protocol = is_ssl()? 'https://' : 'http://'; 131 $host = wp_unslash( gp_array_get( $_SERVER, 'HTTP_HOST' ) ); 132 $path_and_args = wp_unslash( gp_array_get( $_SERVER, 'REQUEST_URI' ) ); 133 134 return $protocol . $host . $path_and_args; 135 } 136 137 /** 138 * Get the URL for a project 139 * 140 * A leading double-slash will avoid prepending /projects/ to the path. 141 * 142 * @param bool|string|object $project_or_path Project path or object 143 * @param string|array $path Addition path to append to the base path 144 * @param array $query associative array of query arguments (optional) 145 * 146 * @return string 147 */ 148 function gp_url_project( $project_or_path = '', $path = '', $query = null ) { 149 $project_path = is_object( $project_or_path ) ? $project_or_path->path : $project_or_path; 150 151 // A leading double-slash will avoid prepending /projects/ to the path. 152 // This was introduced to enable linking to the locale glossary. 153 if ( '//' === substr( $project_path, 0, 2 ) ) { 154 $project_path = urlencode( ltrim( $project_path, '/' ) ); 155 } else { 156 $project_path = array( 'projects', urlencode( $project_path ) ); 157 } 158 159 return gp_url( array( $project_path, $path ), $query ); 160 } 161 162 function gp_url_profile( $user_nicename = '' ) { 163 $url = gp_url( array( '/profile', $user_nicename ) ); 164 /** 165 * Filter the URL of a user profile. 166 * 167 * @since 1.0.0 168 * 169 * @param string $url The URL of the profile. 170 * @param string $user_nicename User's nicename; the slug of the user. 171 */ 172 return apply_filters( 'gp_url_profile', $url, $user_nicename ); 173 } 174 175 function gp_url_base_path() { 176 /** 177 * Filter the base path of a GlotPress URL. 178 * 179 * @since 1.0.0 180 * 181 * @param string $url The url. 182 */ 183 return apply_filters( 'gp_url_base_path', user_trailingslashit( '/' . gp_const_get( 'GP_URL_BASE', 'glotpress' ) ) ); 184 } 185 186 function gp_plugin_url( $path = '' ) { 187 return plugins_url( $path, GP_PLUGIN_FILE ); 188 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Dec 13 01:01:55 2019 | Cross-referenced by PHPXref 0.7.1 |