| | 538 | |
| | 539 | function make_absolute_url( $relative_path, $url ) { |
| | 540 | |
| | 541 | if ( empty( $url ) ) |
| | 542 | return $relative_path; |
| | 543 | |
| | 544 | // Check for a scheme |
| | 545 | if ( false !== strpos( $relative_path, '://' ) ) |
| | 546 | return $relative_path; |
| | 547 | |
| | 548 | $absolute_path = ''; |
| | 549 | $url_parts = @parse_url( $url ); |
| | 550 | |
| | 551 | if ( ! $url_parts ) |
| | 552 | return $relative_path; |
| | 553 | |
| | 554 | $absolute_path = $url_parts['scheme'] . '://' . $url_parts['host']; |
| | 555 | if ( isset( $url_parts['port'] ) ) |
| | 556 | $absolute_path .= ':' . $url_parts['port']; |
| | 557 | |
| | 558 | if ( '/' == $relative_path[0] || ! isset( $url_parts['path'] ) ) { // Absolute path provided, easy. |
| | 559 | $new_path = $relative_path; |
| | 560 | |
| | 561 | } elseif ( '../' == substr( $relative_path, 0, 3 ) ) { // Relative path to a parent directory, this will miss cases such as '/../something' or '/something/../something-else/' |
| | 562 | $url_parts['path'] = preg_replace( '![^/]+$!', '', $url_parts['path'] ); // strip files off |
| | 563 | |
| | 564 | while( '../' == substr( $relative_path, 0, 3 ) ) { |
| | 565 | $relative_path = substr( $relative_path, 3 ); |
| | 566 | $url_parts['path'] = preg_replace( '![^/]+$!', '', untrailingslashit( $url_parts['path'] ) ); |
| | 567 | } |
| | 568 | $new_path = ( '.' == $url_parts['path'] ? '' : trailingslashit( $url_parts['path'] ) ) . ltrim( $relative_path, '/' ); |
| | 569 | |
| | 570 | } else { // Relative path to a sibling, /here/me => /here/subling |
| | 571 | if ( '/' != $relative_path[0] ) |
| | 572 | $url_parts['path'] = preg_replace( '![^/]+$!', '', $url_parts['path'] ); // delete the last segment of the path, dirname('/here/') will return / |
| | 573 | $new_path = ( '.' == $url_parts['path'] ? '' : trailingslashit( $url_parts['path'] ) ) . ltrim( $relative_path, '/' ); |
| | 574 | |
| | 575 | } |
| | 576 | |
| | 577 | $absolute_path .= '/' . ltrim( $new_path, '/' ); |
| | 578 | |
| | 579 | return $absolute_path; |
| | 580 | |
| | 581 | } |