PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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 2 years ago DBPermissions.php 2 years ago DataEncoder.php 2 years ago Escape.php 2 years ago Glob.php 2 years ago Hooks.php 2 years ago Math.php 2 years ago PluginInfo.php 2 years ago Sanitize.php 2 years ago ServerVars.php 2 years ago SlashMode.php 5 years ago Strings.php 2 years ago Times.php 2 years ago Urls.php 2 years ago Version.php 2 years ago WpDefaultDirectories.php 2 years ago
Urls.php
180 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Backup\Service\Archiver;
7 use WPStaging\Backup\Service\BackupsFinder;
8 use WPStaging\Framework\Adapter\Directory;
9
10 class Urls
11 {
12
13 /**
14 * Retrieves the URL for a given site where the front end is accessible.
15 *
16 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
17 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
18 * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
19 */
20 public function getHomeUrl($blog_id = null, $scheme = null)
21 {
22
23 if (empty($blog_id) || !is_multisite()) {
24 $url = get_option('home');
25 } else {
26 switch_to_blog($blog_id);
27 $url = get_option('home');
28 restore_current_blog();
29 }
30
31 if (!in_array($scheme, ['http', 'https', 'relative'])) {
32 if (is_ssl()) {
33 $scheme = 'https';
34 } else {
35 $scheme = parse_url($url, PHP_URL_SCHEME);
36 }
37 }
38
39 $url = set_url_scheme($url, $scheme);
40
41 return $url;
42 }
43
44 /**
45 * Return WordPress home url without scheme e.h. host.com or www.host.com
46 * @return string
47 */
48 public function getHomeUrlWithoutScheme()
49 {
50 return preg_replace('#^https?://#', '', rtrim($this->getHomeUrl(), '/'));
51 }
52
53 /**
54 * Retrieves the URL for a given site where the front end is accessible.
55 *
56 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
57 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
58 * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
59 */
60 public function getSiteUrl($blog_id = null, $scheme = null)
61 {
62
63 if (empty($blog_id) || !is_multisite()) {
64 $url = get_option('siteurl');
65 } else {
66 switch_to_blog($blog_id);
67 $url = get_option('siteurl');
68 restore_current_blog();
69 }
70
71 if (!in_array($scheme, ['http', 'https', 'relative'])) {
72 if ($this->sslAvailable()) {
73 $scheme = 'https';
74 } else {
75 $scheme = parse_url($url, PHP_URL_SCHEME);
76 }
77 }
78
79 return set_url_scheme($url, $scheme);
80 }
81
82 /**
83 * Get raw base URL e.g. https://blog.domain.com or https://domain.com without any subfolder
84 * @return string
85 */
86 public function getBaseUrl()
87 {
88 $result = parse_url($this->getHomeUrl());
89 return $result['scheme'] . "://" . $result['host'];
90 }
91
92 /**
93 * Return base URL (domain) without scheme e.g. blog.domain.com or domain.com
94 * @return string
95 */
96 public function getBaseUrlWithoutScheme()
97 {
98 return preg_replace('#^https?://#', '', rtrim($this->getBaseUrl(), '/'));
99 }
100
101 /**
102 * Get hostname of production site including scheme
103 * @return string
104 */
105 public function getProductionHostname()
106 {
107
108 $connection = get_option('wpstg_connection');
109
110 // Get the stored hostname
111 if (!empty($connection['prodHostname'])) {
112 return $connection['prodHostname'];
113 }
114
115 // Default. Try to get the hostname from the main domain (Workaround for WP Staging Pro older < 2.9.1)
116 $siteurl = get_site_url();
117 $result = parse_url($siteurl);
118 return $result['scheme'] . "://" . $result['host'];
119 }
120
121 /**
122 * Get url of the uploads directory, e.g. http://example.com/wp-content/uploads
123 * @return string
124 */
125 public function getUploadsUrl()
126 {
127 $upload_dir = wp_upload_dir(null, false, false);
128 return trailingslashit($upload_dir['baseurl']);
129 }
130
131 /**
132 * Get url of the backup directory, e.g. http://example.com/*
133 * @return string
134 */
135 public function getBackupUrl()
136 {
137 if (!WPStaging::make(Directory::class)->isBackupPathOutsideAbspath()) {
138 return $this->getUploadsUrl() . WPSTG_PLUGIN_DOMAIN . '/' . Archiver::BACKUP_DIR_NAME . '/';
139 }
140
141 $backupDirAbsPath = WPStaging::make(BackupsFinder::class)->getBackupsDirectory();
142 $relativePathToBackupDir = str_replace(wp_normalize_path(ABSPATH), "", $backupDirAbsPath);
143 $siteurl = $this->maybeUseProtocolRelative(get_option('siteurl'));
144 return trailingslashit($siteurl) . $relativePathToBackupDir;
145 }
146
147 /** @return bool */
148 public function sslAvailable()
149 {
150 // Cloudflare
151 if (!empty($_SERVER['HTTP_CF_VISITOR'])) {
152 // phpcs:ignore WPStagingCS.Security.SanitizeInput.InputNotSanitized
153 $cfo = json_decode($_SERVER['HTTP_CF_VISITOR']);
154 if (isset($cfo->scheme) && $cfo->scheme === 'https') {
155 return true;
156 }
157 }
158
159 // Other proxy
160 if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
161 return true;
162 }
163
164 return is_ssl();
165 }
166
167 /**
168 * @param string $url
169 * @return string
170 */
171 public function maybeUseProtocolRelative($url)
172 {
173 if ($this->sslAvailable() && substr($url, 0, 7) === 'http://') {
174 $url = preg_replace('@^http://@', '//', $url);
175 }
176
177 return $url;
178 }
179 }
180