PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / URL.php

URL.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.0, at includes/Core/Importer/URL.php

67 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer;
4
5
6 /**
7 * Originally made by Elementor
8 */
9
10 class URL {
11
12 /**
13 * Migrate url to the current permalink structure.
14 * The function will also check and change absolute url to relative one by the base url.
15 * This is currently supports only "Post Name" permalink structure to any permalink structure.
16 *
17 * @param string $url The url that should be migrated.
18 * @param string|Null $base_url The base url that should be clean from the url.
19 *
20 * @return string The migrated url || the $url if it couldn't find a match in the current permalink structure.
21 */
22 public static function migrate( string $url, $base_url = '' ): string {
23 $full_url = $url;
24
25 if ( ! empty( $base_url ) ) {
26 $base_url = preg_quote( $base_url, '/' );
27 $url = preg_replace( "/^{$base_url}/", '', $url );
28 }
29
30 $parsed_url = wp_parse_url( $url );
31
32 if ( $url === $full_url && ! empty( $parsed_url['host'] ) ) {
33 return $full_url;
34 }
35
36 if ( ! empty( $parsed_url['path'] ) ) {
37 $page = get_page_by_path( $parsed_url['path'] );
38
39 if ( ! $page ) {
40 return $full_url;
41 }
42
43 $permalink = get_permalink( $page->ID );
44 }
45
46 if ( empty( $permalink ) ) {
47 return $full_url;
48 }
49
50 if ( ! empty( $parsed_url['query'] ) ) {
51 parse_str( $parsed_url['query'], $parsed_query );
52
53 // Clean WP permalinks query args to prevent collision with the new permalink.
54 unset( $parsed_query['p'] );
55 unset( $parsed_query['page_id'] );
56
57 $permalink = add_query_arg( $parsed_query, $permalink );
58 }
59
60 if ( ! empty( $parsed_url['fragment'] ) ) {
61 $permalink .= '#' . $parsed_url['fragment'];
62 }
63
64 return wp_make_link_relative( $permalink );
65 }
66 }
67