94 |
94 |
95 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) { |
95 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) { |
96 $post = $id; |
96 $post = $id; |
97 $sample = true; |
97 $sample = true; |
98 } else { |
98 } else { |
99 $post = &get_post($id); |
99 $post = get_post($id); |
100 $sample = false; |
100 $sample = false; |
101 } |
101 } |
102 |
102 |
103 if ( empty($post->ID) ) |
103 if ( empty($post->ID) ) |
104 return false; |
104 return false; |
105 |
105 |
106 if ( $post->post_type == 'page' ) |
106 if ( $post->post_type == 'page' ) |
107 return get_page_link($post->ID, $leavename, $sample); |
107 return get_page_link($post->ID, $leavename, $sample); |
108 elseif ( $post->post_type == 'attachment' ) |
108 elseif ( $post->post_type == 'attachment' ) |
109 return get_attachment_link($post->ID); |
109 return get_attachment_link( $post->ID, $leavename ); |
110 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) |
110 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) |
111 return get_post_permalink($post->ID, $leavename, $sample); |
111 return get_post_permalink($post->ID, $leavename, $sample); |
112 |
112 |
113 $permalink = get_option('permalink_structure'); |
113 $permalink = get_option('permalink_structure'); |
114 |
114 |
120 $category = ''; |
120 $category = ''; |
121 if ( strpos($permalink, '%category%') !== false ) { |
121 if ( strpos($permalink, '%category%') !== false ) { |
122 $cats = get_the_category($post->ID); |
122 $cats = get_the_category($post->ID); |
123 if ( $cats ) { |
123 if ( $cats ) { |
124 usort($cats, '_usort_terms_by_ID'); // order by ID |
124 usort($cats, '_usort_terms_by_ID'); // order by ID |
125 $category = $cats[0]->slug; |
125 $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); |
126 if ( $parent = $cats[0]->parent ) |
126 $category_object = get_term( $category_object, 'category' ); |
|
127 $category = $category_object->slug; |
|
128 if ( $parent = $category_object->parent ) |
127 $category = get_category_parents($parent, false, '/', true) . $category; |
129 $category = get_category_parents($parent, false, '/', true) . $category; |
128 } |
130 } |
129 // show default category in permalinks, without |
131 // show default category in permalinks, without |
130 // having to assign it explicitly |
132 // having to assign it explicitly |
131 if ( empty($category) ) { |
133 if ( empty($category) ) { |
228 * |
230 * |
229 * Respects page_on_front. Use this one. |
231 * Respects page_on_front. Use this one. |
230 * |
232 * |
231 * @since 1.5.0 |
233 * @since 1.5.0 |
232 * |
234 * |
233 * @param int $id Optional. Post ID. |
235 * @param mixed $post Optional. Post ID or object. |
234 * @param bool $leavename Optional, defaults to false. Whether to keep page name. |
236 * @param bool $leavename Optional, defaults to false. Whether to keep page name. |
235 * @param bool $sample Optional, defaults to false. Is it a sample permalink. |
237 * @param bool $sample Optional, defaults to false. Is it a sample permalink. |
236 * @return string |
238 * @return string |
237 */ |
239 */ |
238 function get_page_link( $id = false, $leavename = false, $sample = false ) { |
240 function get_page_link( $post = false, $leavename = false, $sample = false ) { |
239 global $post; |
241 $post = get_post( $post ); |
240 |
242 |
241 $id = (int) $id; |
243 if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) |
242 if ( !$id ) |
|
243 $id = (int) $post->ID; |
|
244 |
|
245 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) |
|
246 $link = home_url('/'); |
244 $link = home_url('/'); |
247 else |
245 else |
248 $link = _get_page_link( $id , $leavename, $sample ); |
246 $link = _get_page_link( $post, $leavename, $sample ); |
249 |
247 |
250 return apply_filters('page_link', $link, $id, $sample); |
248 return apply_filters( 'page_link', $link, $post->ID, $sample ); |
251 } |
249 } |
252 |
250 |
253 /** |
251 /** |
254 * Retrieve the page permalink. |
252 * Retrieve the page permalink. |
255 * |
253 * |
256 * Ignores page_on_front. Internal use only. |
254 * Ignores page_on_front. Internal use only. |
257 * |
255 * |
258 * @since 2.1.0 |
256 * @since 2.1.0 |
259 * @access private |
257 * @access private |
260 * |
258 * |
261 * @param int $id Optional. Post ID. |
259 * @param mixed $post Optional. Post ID or object. |
262 * @param bool $leavename Optional. Leave name. |
260 * @param bool $leavename Optional. Leave name. |
263 * @param bool $sample Optional. Sample permalink. |
261 * @param bool $sample Optional. Sample permalink. |
264 * @return string |
262 * @return string |
265 */ |
263 */ |
266 function _get_page_link( $id = false, $leavename = false, $sample = false ) { |
264 function _get_page_link( $post = false, $leavename = false, $sample = false ) { |
267 global $post, $wp_rewrite; |
265 global $wp_rewrite; |
268 |
266 |
269 if ( !$id ) |
267 $post = get_post( $post ); |
270 $id = (int) $post->ID; |
|
271 else |
|
272 $post = &get_post($id); |
|
273 |
268 |
274 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
269 $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); |
275 |
270 |
276 $link = $wp_rewrite->get_page_permastruct(); |
271 $link = $wp_rewrite->get_page_permastruct(); |
277 |
272 |
278 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { |
273 if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { |
279 if ( ! $leavename ) { |
274 if ( ! $leavename ) { |
280 $link = str_replace('%pagename%', get_page_uri($id), $link); |
275 $link = str_replace('%pagename%', get_page_uri( $post ), $link); |
281 } |
276 } |
282 |
277 |
283 $link = home_url($link); |
278 $link = home_url($link); |
284 $link = user_trailingslashit($link, 'page'); |
279 $link = user_trailingslashit($link, 'page'); |
285 } else { |
280 } else { |
286 $link = home_url("?page_id=$id"); |
281 $link = home_url( '?page_id=' . $post->ID ); |
287 } |
282 } |
288 |
283 |
289 return apply_filters( '_get_page_link', $link, $id ); |
284 return apply_filters( '_get_page_link', $link, $post->ID ); |
290 } |
285 } |
291 |
286 |
292 /** |
287 /** |
293 * Retrieve permalink for attachment. |
288 * Retrieve permalink for attachment. |
294 * |
289 * |
295 * This can be used in the WordPress Loop or outside of it. |
290 * This can be used in the WordPress Loop or outside of it. |
296 * |
291 * |
297 * @since 2.0.0 |
292 * @since 2.0.0 |
298 * |
293 * |
299 * @param int $id Optional. Post ID. |
294 * @param mixed $post Optional. Post ID or object. |
300 * @return string |
295 * @param bool $leavename Optional. Leave name. |
301 */ |
296 * @return string |
302 function get_attachment_link($id = false) { |
297 */ |
303 global $post, $wp_rewrite; |
298 function get_attachment_link( $post = null, $leavename = false ) { |
|
299 global $wp_rewrite; |
304 |
300 |
305 $link = false; |
301 $link = false; |
306 |
302 |
307 if ( ! $id) |
303 $post = get_post( $post ); |
308 $id = (int) $post->ID; |
304 |
309 |
305 if ( $wp_rewrite->using_permalinks() && ( $post->post_parent > 0 ) && ( $post->post_parent != $post->ID ) ) { |
310 $object = get_post($id); |
306 $parent = get_post($post->post_parent); |
311 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) { |
|
312 $parent = get_post($object->post_parent); |
|
313 if ( 'page' == $parent->post_type ) |
307 if ( 'page' == $parent->post_type ) |
314 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front |
308 $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front |
315 else |
309 else |
316 $parentlink = get_permalink( $object->post_parent ); |
310 $parentlink = get_permalink( $post->post_parent ); |
317 |
311 |
318 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) |
312 if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) |
319 $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker |
313 $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker |
320 else |
314 else |
321 $name = $object->post_name; |
315 $name = $post->post_name; |
322 |
316 |
323 if ( strpos($parentlink, '?') === false ) |
317 if ( strpos($parentlink, '?') === false ) |
324 $link = user_trailingslashit( trailingslashit($parentlink) . $name ); |
318 $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' ); |
|
319 |
|
320 if ( ! $leavename ) |
|
321 $link = str_replace( '%postname%', $name, $link ); |
325 } |
322 } |
326 |
323 |
327 if ( ! $link ) |
324 if ( ! $link ) |
328 $link = home_url( "/?attachment_id=$id" ); |
325 $link = home_url( '/?attachment_id=' . $post->ID ); |
329 |
326 |
330 return apply_filters('attachment_link', $link, $id); |
327 return apply_filters( 'attachment_link', $link, $post->ID ); |
331 } |
328 } |
332 |
329 |
333 /** |
330 /** |
334 * Retrieve the permalink for the year archives. |
331 * Retrieve the permalink for the year archives. |
335 * |
332 * |
1093 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1118 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1094 * @param bool $previous Optional. Whether to retrieve previous post. |
1119 * @param bool $previous Optional. Whether to retrieve previous post. |
1095 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. |
1120 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. |
1096 */ |
1121 */ |
1097 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { |
1122 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { |
1098 global $post, $wpdb; |
1123 global $wpdb; |
1099 |
1124 |
1100 if ( empty( $post ) ) |
1125 if ( ! $post = get_post() ) |
1101 return null; |
1126 return null; |
1102 |
1127 |
1103 $current_post_date = $post->post_date; |
1128 $current_post_date = $post->post_date; |
1104 |
1129 |
1105 $join = ''; |
1130 $join = ''; |
1106 $posts_in_ex_cats_sql = ''; |
1131 $posts_in_ex_cats_sql = ''; |
1107 if ( $in_same_cat || ! empty( $excluded_categories ) ) { |
1132 if ( $in_same_cat || ! empty( $excluded_categories ) ) { |
1108 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; |
1133 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; |
1109 |
1134 |
1110 if ( $in_same_cat ) { |
1135 if ( $in_same_cat ) { |
|
1136 if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) ) |
|
1137 return ''; |
1111 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids')); |
1138 $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids')); |
|
1139 if ( ! $cat_array || is_wp_error( $cat_array ) ) |
|
1140 return ''; |
1112 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; |
1141 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"; |
1113 } |
1142 } |
1114 |
1143 |
1115 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; |
1144 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; |
1116 if ( ! empty( $excluded_categories ) ) { |
1145 if ( ! empty( $excluded_categories ) ) { |
1143 |
1172 |
1144 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); |
1173 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); |
1145 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories ); |
1174 $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories ); |
1146 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); |
1175 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); |
1147 |
1176 |
1148 $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort"; |
1177 $query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort"; |
1149 $query_key = 'adjacent_post_' . md5($query); |
1178 $query_key = 'adjacent_post_' . md5($query); |
1150 $result = wp_cache_get($query_key, 'counts'); |
1179 $result = wp_cache_get($query_key, 'counts'); |
1151 if ( false !== $result ) |
1180 if ( false !== $result ) { |
|
1181 if ( $result ) |
|
1182 $result = get_post( $result ); |
1152 return $result; |
1183 return $result; |
1153 |
1184 } |
1154 $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); |
1185 |
|
1186 $result = $wpdb->get_var( $query ); |
1155 if ( null === $result ) |
1187 if ( null === $result ) |
1156 $result = ''; |
1188 $result = ''; |
1157 |
1189 |
1158 wp_cache_set($query_key, $result, 'counts'); |
1190 wp_cache_set($query_key, $result, 'counts'); |
|
1191 |
|
1192 if ( $result ) |
|
1193 $result = get_post( $result ); |
|
1194 |
1159 return $result; |
1195 return $result; |
1160 } |
1196 } |
1161 |
1197 |
1162 /** |
1198 /** |
1163 * Get adjacent post relational link. |
1199 * Get adjacent post relational link. |
1171 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1207 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1172 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. |
1208 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. |
1173 * @return string |
1209 * @return string |
1174 */ |
1210 */ |
1175 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { |
1211 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { |
1176 if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) |
1212 if ( $previous && is_attachment() && $post = get_post() ) |
1177 $post = & get_post($GLOBALS['post']->post_parent); |
1213 $post = get_post( $post->post_parent ); |
1178 else |
1214 else |
1179 $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); |
1215 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous ); |
1180 |
1216 |
1181 if ( empty($post) ) |
1217 if ( empty($post) ) |
1182 return; |
1218 return; |
1183 |
1219 |
1184 if ( empty($post->post_title) ) |
1220 if ( empty($post->post_title) ) |
1185 $post->post_title = $previous ? __('Previous Post') : __('Next Post'); |
1221 $post_title = $previous ? __('Previous Post') : __('Next Post'); |
|
1222 else |
|
1223 $post_title = $post->post_title; |
1186 |
1224 |
1187 $date = mysql2date(get_option('date_format'), $post->post_date); |
1225 $date = mysql2date(get_option('date_format'), $post->post_date); |
1188 |
1226 |
1189 $title = str_replace('%title', $post->post_title, $title); |
1227 $title = str_replace('%title', $post_title, $title); |
1190 $title = str_replace('%date', $date, $title); |
1228 $title = str_replace('%date', $date, $title); |
1191 $title = apply_filters('the_title', $title, $post->ID); |
1229 $title = apply_filters('the_title', $title, $post->ID); |
1192 |
1230 |
1193 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; |
1231 $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; |
1194 $link .= esc_attr( $title ); |
1232 $link .= esc_attr( $title ); |
1335 * @param string $link Link permalink format. |
1372 * @param string $link Link permalink format. |
1336 * @param bool $in_same_cat Optional. Whether link should be in a same category. |
1373 * @param bool $in_same_cat Optional. Whether link should be in a same category. |
1337 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1374 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs. |
1338 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. |
1375 * @param bool $previous Optional, default is true. Whether to display link to previous or next post. |
1339 */ |
1376 */ |
1340 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { |
1377 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) { |
1341 if ( $previous && is_attachment() ) |
1378 if ( $previous && is_attachment() ) |
1342 $post = & get_post($GLOBALS['post']->post_parent); |
1379 $post = get_post( get_post()->post_parent ); |
1343 else |
1380 else |
1344 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); |
1381 $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous ); |
1345 |
1382 |
1346 if ( !$post ) |
1383 if ( ! $post ) { |
1347 return; |
1384 $output = ''; |
1348 |
1385 } else { |
1349 $title = $post->post_title; |
1386 $title = $post->post_title; |
1350 |
1387 |
1351 if ( empty($post->post_title) ) |
1388 if ( empty( $post->post_title ) ) |
1352 $title = $previous ? __('Previous Post') : __('Next Post'); |
1389 $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); |
1353 |
1390 |
1354 $title = apply_filters('the_title', $title, $post->ID); |
1391 $title = apply_filters( 'the_title', $title, $post->ID ); |
1355 $date = mysql2date(get_option('date_format'), $post->post_date); |
1392 $date = mysql2date( get_option( 'date_format' ), $post->post_date ); |
1356 $rel = $previous ? 'prev' : 'next'; |
1393 $rel = $previous ? 'prev' : 'next'; |
1357 |
1394 |
1358 $string = '<a href="'.get_permalink($post).'" rel="'.$rel.'">'; |
1395 $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">'; |
1359 $link = str_replace('%title', $title, $link); |
1396 $inlink = str_replace( '%title', $title, $link ); |
1360 $link = str_replace('%date', $date, $link); |
1397 $inlink = str_replace( '%date', $date, $inlink ); |
1361 $link = $string . $link . '</a>'; |
1398 $inlink = $string . $inlink . '</a>'; |
1362 |
1399 |
1363 $format = str_replace('%link', $link, $format); |
1400 $output = str_replace( '%link', $inlink, $format ); |
|
1401 } |
1364 |
1402 |
1365 $adjacent = $previous ? 'previous' : 'next'; |
1403 $adjacent = $previous ? 'previous' : 'next'; |
1366 echo apply_filters( "{$adjacent}_post_link", $format, $link ); |
1404 |
|
1405 echo apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post ); |
1367 } |
1406 } |
1368 |
1407 |
1369 /** |
1408 /** |
1370 * Retrieve links for page numbers. |
1409 * Retrieve links for page numbers. |
1371 * |
1410 * |
1861 * @return string Home url link with optional path appended. |
1900 * @return string Home url link with optional path appended. |
1862 */ |
1901 */ |
1863 function get_home_url( $blog_id = null, $path = '', $scheme = null ) { |
1902 function get_home_url( $blog_id = null, $path = '', $scheme = null ) { |
1864 $orig_scheme = $scheme; |
1903 $orig_scheme = $scheme; |
1865 |
1904 |
1866 if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) |
1905 if ( empty( $blog_id ) || !is_multisite() ) { |
1867 $scheme = is_ssl() && !is_admin() ? 'https' : 'http'; |
|
1868 |
|
1869 if ( empty( $blog_id ) || !is_multisite() ) |
|
1870 $url = get_option( 'home' ); |
1906 $url = get_option( 'home' ); |
1871 else |
1907 } else { |
1872 $url = get_blog_option( $blog_id, 'home' ); |
1908 switch_to_blog( $blog_id ); |
1873 |
1909 $url = get_option( 'home' ); |
1874 if ( 'relative' == $scheme ) |
1910 restore_current_blog(); |
1875 $url = preg_replace( '#^.+://[^/]*#', '', $url ); |
1911 } |
1876 elseif ( 'http' != $scheme ) |
1912 |
1877 $url = str_replace( 'http://', "$scheme://", $url ); |
1913 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { |
1878 |
1914 if ( is_ssl() && ! is_admin() ) |
1879 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
1915 $scheme = 'https'; |
|
1916 else |
|
1917 $scheme = parse_url( $url, PHP_URL_SCHEME ); |
|
1918 } |
|
1919 |
|
1920 $url = set_url_scheme( $url, $scheme ); |
|
1921 |
|
1922 if ( ! empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
1880 $url .= '/' . ltrim( $path, '/' ); |
1923 $url .= '/' . ltrim( $path, '/' ); |
1881 |
1924 |
1882 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); |
1925 return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); |
1883 } |
1926 } |
1884 |
1927 |
1916 * @param string $path Optional. Path relative to the site url. |
1959 * @param string $path Optional. Path relative to the site url. |
1917 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'. |
1960 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'. |
1918 * @return string Site url link with optional path appended. |
1961 * @return string Site url link with optional path appended. |
1919 */ |
1962 */ |
1920 function get_site_url( $blog_id = null, $path = '', $scheme = null ) { |
1963 function get_site_url( $blog_id = null, $path = '', $scheme = null ) { |
1921 // should the list of allowed schemes be maintained elsewhere? |
1964 if ( empty( $blog_id ) || !is_multisite() ) { |
1922 $orig_scheme = $scheme; |
|
1923 if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { |
|
1924 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) ) |
|
1925 $scheme = 'https'; |
|
1926 elseif ( ( 'login' == $scheme ) && force_ssl_admin() ) |
|
1927 $scheme = 'https'; |
|
1928 elseif ( ( 'admin' == $scheme ) && force_ssl_admin() ) |
|
1929 $scheme = 'https'; |
|
1930 else |
|
1931 $scheme = ( is_ssl() ? 'https' : 'http' ); |
|
1932 } |
|
1933 |
|
1934 if ( empty( $blog_id ) || !is_multisite() ) |
|
1935 $url = get_option( 'siteurl' ); |
1965 $url = get_option( 'siteurl' ); |
1936 else |
1966 } else { |
1937 $url = get_blog_option( $blog_id, 'siteurl' ); |
1967 switch_to_blog( $blog_id ); |
1938 |
1968 $url = get_option( 'siteurl' ); |
1939 if ( 'relative' == $scheme ) |
1969 restore_current_blog(); |
1940 $url = preg_replace( '#^.+://[^/]*#', '', $url ); |
1970 } |
1941 elseif ( 'http' != $scheme ) |
1971 |
1942 $url = str_replace( 'http://', "{$scheme}://", $url ); |
1972 $url = set_url_scheme( $url, $scheme ); |
1943 |
1973 |
1944 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
1974 if ( ! empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
1945 $url .= '/' . ltrim( $path, '/' ); |
1975 $url .= '/' . ltrim( $path, '/' ); |
1946 |
1976 |
1947 return apply_filters( 'site_url', $url, $path, $orig_scheme, $blog_id ); |
1977 return apply_filters( 'site_url', $url, $path, $scheme, $blog_id ); |
1948 } |
1978 } |
1949 |
1979 |
1950 /** |
1980 /** |
1951 * Retrieve the url to the admin area for the current site. |
1981 * Retrieve the url to the admin area for the current site. |
1952 * |
1982 * |
2067 * |
2095 * |
2068 * @package WordPress |
2096 * @package WordPress |
2069 * @since 3.0.0 |
2097 * @since 3.0.0 |
2070 * |
2098 * |
2071 * @param string $path Optional. Path relative to the site url. |
2099 * @param string $path Optional. Path relative to the site url. |
2072 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'. |
2100 * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme(). |
2073 * @return string Site url link with optional path appended. |
2101 * @return string Site url link with optional path appended. |
2074 */ |
2102 */ |
2075 function network_site_url( $path = '', $scheme = null ) { |
2103 function network_site_url( $path = '', $scheme = null ) { |
2076 global $current_site; |
2104 global $current_site; |
2077 |
2105 |
2078 if ( !is_multisite() ) |
2106 if ( ! is_multisite() ) |
2079 return site_url($path, $scheme); |
2107 return site_url($path, $scheme); |
2080 |
|
2081 $orig_scheme = $scheme; |
|
2082 if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { |
|
2083 if ( ( 'login_post' == $scheme || 'rpc' == $scheme ) && ( force_ssl_login() || force_ssl_admin() ) ) |
|
2084 $scheme = 'https'; |
|
2085 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) ) |
|
2086 $scheme = 'https'; |
|
2087 elseif ( ('admin' == $scheme) && force_ssl_admin() ) |
|
2088 $scheme = 'https'; |
|
2089 else |
|
2090 $scheme = ( is_ssl() ? 'https' : 'http' ); |
|
2091 } |
|
2092 |
2108 |
2093 if ( 'relative' == $scheme ) |
2109 if ( 'relative' == $scheme ) |
2094 $url = $current_site->path; |
2110 $url = $current_site->path; |
2095 else |
2111 else |
2096 $url = $scheme . '://' . $current_site->domain . $current_site->path; |
2112 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme ); |
2097 |
2113 |
2098 if ( !empty($path) && is_string($path) && strpos($path, '..') === false ) |
2114 if ( ! empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
2099 $url .= ltrim($path, '/'); |
2115 $url .= ltrim( $path, '/' ); |
2100 |
2116 |
2101 return apply_filters('network_site_url', $url, $path, $orig_scheme); |
2117 return apply_filters( 'network_site_url', $url, $path, $scheme ); |
2102 } |
2118 } |
2103 |
2119 |
2104 /** |
2120 /** |
2105 * Retrieve the home url for the current network. |
2121 * Retrieve the home url for the current network. |
2106 * |
2122 * |
2116 * @return string Home url link with optional path appended. |
2132 * @return string Home url link with optional path appended. |
2117 */ |
2133 */ |
2118 function network_home_url( $path = '', $scheme = null ) { |
2134 function network_home_url( $path = '', $scheme = null ) { |
2119 global $current_site; |
2135 global $current_site; |
2120 |
2136 |
2121 if ( !is_multisite() ) |
2137 if ( ! is_multisite() ) |
2122 return home_url($path, $scheme); |
2138 return home_url($path, $scheme); |
2123 |
2139 |
2124 $orig_scheme = $scheme; |
2140 $orig_scheme = $scheme; |
2125 |
2141 |
2126 if ( !in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) |
2142 if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) |
2127 $scheme = is_ssl() && !is_admin() ? 'https' : 'http'; |
2143 $scheme = is_ssl() && ! is_admin() ? 'https' : 'http'; |
2128 |
2144 |
2129 if ( 'relative' == $scheme ) |
2145 if ( 'relative' == $scheme ) |
2130 $url = $current_site->path; |
2146 $url = $current_site->path; |
2131 else |
2147 else |
2132 $url = $scheme . '://' . $current_site->domain . $current_site->path; |
2148 $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme ); |
2133 |
2149 |
2134 if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
2150 if ( ! empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) |
2135 $url .= ltrim( $path, '/' ); |
2151 $url .= ltrim( $path, '/' ); |
2136 |
2152 |
2137 return apply_filters( 'network_home_url', $url, $path, $orig_scheme); |
2153 return apply_filters( 'network_home_url', $url, $path, $orig_scheme); |
2138 } |
2154 } |
2139 |
2155 |