class-wpml-slash-management.php
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
class WPML_Slash_Management {
public function match_trailing_slash_to_reference( $url, $reference_url ) {
if ( trailingslashit( $reference_url ) === $reference_url && ! $this->has_lang_param( $url ) ) {
return trailingslashit( $url );
} else {
return untrailingslashit( $url );
}
}
/**
* @param string $url
*
* @return bool
*/
private function has_lang_param( $url ) {
return strpos( $url, '?lang=' ) !== false || strpos( $url, '&lang=' ) !== false;
}
/**
* @param string $url
* @param string $method Deprecated.
*
* @return mixed|string
*/
public function maybe_user_trailingslashit( $url, $method = '' ) {
$url_parts = wpml_parse_url( $url );
if ( ! $url_parts || ! is_array( $url_parts ) ) {
return $url;
}
$url_parts = $this->parse_missing_host_from_path( $url_parts );
if ( $this->is_root_url_with_trailingslash( $url_parts )
|| $this->is_root_url_without_trailingslash_and_without_query_args( $url_parts )
) {
return $url;
}
$path = isset( $url_parts['path'] ) ? $url_parts['path'] : '';
if ( ! $path && isset( $url_parts['query'] ) ) {
$url_parts['path'] = '/';
} elseif ( $this->is_file_path( $path ) ) {
$url_parts['path'] = untrailingslashit( $path );
} elseif ( $method ) {
$url_parts['path'] = 'untrailingslashit' === $method ? untrailingslashit( $path ) : trailingslashit( $path );
} else {
$url_parts['path'] = $this->user_trailingslashit( $path );
}
return http_build_url( $url_parts );
}
/**
* Follows the logic of WordPress core user_trailingslashit().
* Can be called on plugins_loaded event, when $wp_rewrite is not set yet.
*
* @param string $path
*
* @return string
*/
private function user_trailingslashit( $path ) {
global $wp_rewrite;
if ( $wp_rewrite ) {
return user_trailingslashit( $path );
}
$permalink_structure = get_option( 'permalink_structure' );
$use_trailing_slashes = ( '/' === substr( $permalink_structure, - 1, 1 ) );
if ( $use_trailing_slashes ) {
$path = trailingslashit( $path );
} else {
$path = untrailingslashit( $path );
}
return apply_filters( 'user_trailingslashit', $path, '' );
}
/**
* @param array $url_parts
*
* @return bool
*/
private function is_root_url_without_trailingslash_and_without_query_args( array $url_parts ) {
return ! isset( $url_parts['path'] ) && ! isset( $url_parts['query'] );
}
/**
* @param array $url_parts
*
* @return bool
*/
private function is_root_url_with_trailingslash( array $url_parts ) {
return isset( $url_parts['path'] ) && '/' === $url_parts['path'];
}
/**
* @see Test_WPML_Lang_Domains_Converter::check_domains_and_subdir
*
* @param array $url_parts
*
* @return array
*/
public function parse_missing_host_from_path( array $url_parts ) {
if ( ! isset( $url_parts['host'] ) && isset( $url_parts['path'] ) ) {
$domain_and_subdir = explode( '/', $url_parts['path'] );
$domain = $domain_and_subdir[0];
$url_parts['host'] = $domain;
array_shift( $domain_and_subdir );
if ( $domain_and_subdir ) {
$url_parts['path'] = preg_replace( '/^(' . $url_parts['host'] . ')/', '', $url_parts['path'] );
} else {
unset( $url_parts['path'] );
}
}
return $url_parts;
}
/**
* @param string $path
*
* @return bool
*/
private function is_file_path( $path ) {
$pathinfo = pathinfo( $path );
return isset( $pathinfo['extension'] ) && $pathinfo['extension'];
}
}