PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Utils / Urls.php
wp-staging / Framework / Utils Last commit date
Cache 1 day ago DBPermissions.php 1 day ago DataEncoder.php 1 day ago DatabaseOptions.php 1 day ago Env.php 1 day ago Escape.php 1 day ago Glob.php 1 day ago Hooks.php 1 day ago Math.php 1 day ago PluginInfo.php 1 day ago Sanitize.php 1 day ago ServerVars.php 1 day ago SlashMode.php 1 day ago Strings.php 1 day ago Times.php 1 day ago Urls.php 1 day ago Version.php 1 day ago WpDefaultDirectories.php 1 day ago
Urls.php
198 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 use WPStaging\Backup\Service\BackupsDirectoryResolver;
6 use WPStaging\Core\WPStaging;
7
8
9
10
11 class Urls
12 {
13
14
15
16
17
18
19
20
21
22
23 public function getHomeUrl($blogId = null, $scheme = null): string
24 {
25 if (empty($blogId) || !is_multisite()) {
26 $url = get_option('home');
27 } else {
28 switch_to_blog($blogId);
29 $url = get_option('home');
30 restore_current_blog();
31 }
32
33 if (!in_array($scheme, ['http', 'https', 'relative'])) {
34 if (is_ssl()) {
35 $scheme = 'https';
36 } else {
37 $scheme = parse_url($url, PHP_URL_SCHEME);
38 }
39 }
40
41 return set_url_scheme($url, $scheme);
42 }
43
44
45
46
47
48 public function getHomeUrlWithoutScheme(): string
49 {
50 return preg_replace('#^https?://#', '', rtrim($this->getHomeUrl(), '/'));
51 }
52
53
54
55
56
57
58
59
60
61
62
63 public function getSiteUrl($blogId = null, $scheme = null): string
64 {
65 if (empty($blogId) || !is_multisite()) {
66 $url = get_option('siteurl');
67 } else {
68 switch_to_blog($blogId);
69 $url = get_option('siteurl');
70 restore_current_blog();
71 }
72
73 if (!in_array($scheme, ['http', 'https', 'relative'])) {
74 if ($this->sslAvailable()) {
75 $scheme = 'https';
76 } else {
77 $scheme = parse_url($url, PHP_URL_SCHEME);
78 }
79 }
80
81 return set_url_scheme($url, $scheme);
82 }
83
84
85
86
87
88 public function getBaseUrl(): string
89 {
90 $result = parse_url($this->getHomeUrl());
91 return $result['scheme'] . "://" . $result['host'];
92 }
93
94
95
96
97
98 public function getBaseUrlWithoutScheme(): string
99 {
100 return preg_replace('#^https?://#', '', rtrim($this->getBaseUrl(), '/'));
101 }
102
103
104
105
106
107 public function getProductionHostname(): string
108 {
109 $connection = get_option('wpstg_connection');
110
111 if (!empty($connection['prodHostname'])) {
112 return $connection['prodHostname'];
113 }
114
115
116 $siteurl = get_site_url();
117 $result = parse_url($siteurl);
118 return $result['scheme'] . "://" . $result['host'];
119 }
120
121
122
123
124
125 public function getUploadsUrl(): string
126 {
127 $upload_dir = wp_upload_dir(null, false, false);
128 return trailingslashit($upload_dir['baseurl']);
129 }
130
131
132
133
134
135
136 public function getBackupUrl(): string
137 {
138 $uploads = wp_upload_dir(null, false);
139 $backupsDirResolver = WPStaging::make(BackupsDirectoryResolver::class);
140 $normalizedBackupPath = $backupsDirResolver->resolveFromUploadsDirectory($uploads['basedir']);
141 $normalizedUploadsDir = trailingslashit(wp_normalize_path($uploads['basedir']));
142
143 if (strpos($normalizedBackupPath, $normalizedUploadsDir) === 0) {
144 $relativePath = substr($normalizedBackupPath, strlen($normalizedUploadsDir));
145 return trailingslashit($this->maybeUseProtocolRelative($uploads['baseurl'])) . ltrim($relativePath, '/');
146 }
147
148 $normalizedWpContentDir = trailingslashit(wp_normalize_path(WP_CONTENT_DIR));
149 if (strpos($normalizedBackupPath, $normalizedWpContentDir) === 0) {
150 $relativePath = substr($normalizedBackupPath, strlen($normalizedWpContentDir));
151 return trailingslashit($this->maybeUseProtocolRelative(content_url())) . ltrim($relativePath, '/');
152 }
153
154 $normalizedAbspath = trailingslashit(wp_normalize_path(ABSPATH));
155 $relativePath = $normalizedBackupPath;
156 if (strpos($normalizedBackupPath, $normalizedAbspath) === 0) {
157 $relativePath = substr($normalizedBackupPath, strlen($normalizedAbspath));
158 }
159
160 $siteurl = $this->maybeUseProtocolRelative(get_option('siteurl'));
161
162 return trailingslashit($siteurl) . ltrim($relativePath, '/');
163 }
164
165
166 public function sslAvailable(): bool
167 {
168
169 if (!empty($_SERVER['HTTP_CF_VISITOR'])) {
170 // phpcs:ignore WPStagingCS.Security.SanitizeInput.InputNotSanitized
171 $cfo = json_decode($_SERVER['HTTP_CF_VISITOR']);
172 if (isset($cfo->scheme) && $cfo->scheme === 'https') {
173 return true;
174 }
175 }
176
177
178 if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
179 return true;
180 }
181
182 return is_ssl();
183 }
184
185
186
187
188
189 public function maybeUseProtocolRelative(string $url): string
190 {
191 if ($this->sslAvailable() && substr($url, 0, 7) === 'http://') {
192 $url = preg_replace('@^http://@', '//', $url);
193 }
194
195 return $url;
196 }
197 }
198