PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.0
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 / Rest / Rest.php
wp-staging / Framework / Rest Last commit date
Rest.php 2 years ago
Rest.php
89 lines
1 <?php
2
3 namespace WPStaging\Framework\Rest;
4
5 use WPStaging\Framework\Utils\Sanitize;
6
7 /**
8 * Class Rest
9 *
10 * @package WPStaging\Framework\Rest
11 *
12 * @todo merge into WPAdapter class?
13 */
14 class Rest
15 {
16 /** @var Sanitize */
17 private $sanitize;
18
19 public function __construct(Sanitize $sanitize)
20 {
21 $this->sanitize = $sanitize;
22 }
23
24 // Is Rest URL
25 public function isRestUrl()
26 {
27 // Early bail if uri is empty
28 if (empty($_SERVER['REQUEST_URI'])) {
29 return false;
30 }
31
32 $requestPath = trim($this->sanitize->sanitizeUrl($_SERVER['REQUEST_URI']), '/');
33
34 $originalUrl = trailingslashit(get_home_url(get_current_blog_id(), ''));
35
36 $url = add_query_arg('rest_route', '/', $originalUrl);
37 $restPath = $this->getApiRequestURI($url);
38 $requestPathApiURI = $this->getApiRequestURI($requestPath);
39 if (!empty($restPath) && strpos($requestPathApiURI, $restPath) === 0) {
40 return true;
41 }
42
43 // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
44 // To work around this, we manually add index.php to the URL, avoiding the redirect.
45 if ('index.php/' !== substr($originalUrl, -10)) {
46 $urlWithIndex = $originalUrl . 'index.php';
47 }
48
49 if (!empty($urlWithIndex)) {
50 $urlWithIndex = add_query_arg('rest_route', '/', $urlWithIndex);
51 $restPathWithIndex = $this->getApiRequestURI($urlWithIndex);
52 $requestPathApiURI = $this->getApiRequestURI($requestPath);
53 if (!empty($restPathWithIndex) && strpos($requestPathApiURI, $restPathWithIndex) === 0) {
54 return true;
55 }
56 }
57
58 // Early bail rest url function not exists
59 if (!function_exists('rest_url')) {
60 return false;
61 }
62
63 $baseRestURL = get_rest_url(get_current_blog_id(), '/');
64 $restPath = $this->getApiRequestURI($baseRestURL);
65
66 // Early bail if rest path is empty
67 if (empty($restPath)) {
68 return false;
69 }
70
71 return strpos($requestPath, $restPath) === 0;
72 }
73
74 private function getApiRequestURI($url)
75 {
76 if (empty($url)) {
77 return '';
78 }
79
80 $path = parse_url($url, PHP_URL_PATH);
81 $path = empty($path) ? '' : trim($path, '/');
82
83 $query = parse_url($url, PHP_URL_QUERY);
84 $query = empty($query) ? '' : trim($query, '/');
85
86 return $query === '' ? $path : $path . '?' . $query;
87 }
88 }
89