| 1 |
<?php |
| 2 |
|
| 3 |
if (!function_exists('wpstgIsAdminActionRequest')) { |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
function wpstgIsAdminActionRequest(string $requestUri): bool |
| 9 |
{ |
| 10 |
$requestPath = (string)parse_url($requestUri, PHP_URL_PATH); |
| 11 |
|
| 12 |
return strpos($requestPath, '/wp-admin/admin-ajax.php') !== false |
| 13 |
|| strpos($requestPath, '/wp-admin/admin-post.php') !== false; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
if (!function_exists('wpstgIsPluginAction')) { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
function wpstgIsPluginAction(string $action): bool |
| 23 |
{ |
| 24 |
return strpos($action, 'wpstg') === 0 || strpos($action, 'raw_wpstg') === 0; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
if (!function_exists('wpstgGetRequestAction')) { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
function wpstgGetRequestAction(): string |
| 33 |
{ |
| 34 |
if (isset($_GET['action'])) { |
| 35 |
$action = sanitize_key($_GET['action']); |
| 36 |
} elseif (isset($_POST['action'])) { |
| 37 |
$action = sanitize_key($_POST['action']); |
| 38 |
} elseif (isset($_REQUEST['action'])) { |
| 39 |
$action = sanitize_key($_REQUEST['action']); |
| 40 |
} else { |
| 41 |
$action = ''; |
| 42 |
} |
| 43 |
|
| 44 |
if (!is_scalar($action)) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
|
| 48 |
return sanitize_key(wp_unslash((string) $action)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if (!function_exists('wpstgIsPluginRestRoute')) { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
function wpstgIsPluginRestRoute(string $route): bool |
| 58 |
{ |
| 59 |
$route = ltrim($route, '/'); |
| 60 |
|
| 61 |
return $route === 'wpstg/v1' || strpos($route, 'wpstg/v1/') === 0; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (!function_exists('wpstgGetPrettyRestRoute')) { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
function wpstgGetPrettyRestRoute(string $requestUri): string |
| 71 |
{ |
| 72 |
$requestPath = (string)parse_url($requestUri, PHP_URL_PATH); |
| 73 |
if ($requestPath === '') { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$restPrefix = '/' . trim(apply_filters('rest_url_prefix', 'wp-json'), '/') . '/'; |
| 80 |
$prefixPosition = strpos($requestPath, $restPrefix); |
| 81 |
if ($prefixPosition === false) { |
| 82 |
return ''; |
| 83 |
} |
| 84 |
|
| 85 |
return ltrim(substr($requestPath, $prefixPosition + strlen($restPrefix)), '/'); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (!function_exists('wpstgIsRestRequest')) { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
function wpstgIsRestRequest(string $requestUri): bool |
| 95 |
{ |
| 96 |
if (wpstgGetPrettyRestRoute($requestUri) !== '') { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
return !empty($_GET['rest_route']); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (!function_exists('wpstgIsPluginRestRequest')) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
function wpstgIsPluginRestRequest(string $requestUri): bool |
| 110 |
{ |
| 111 |
$prettyRoute = wpstgGetPrettyRestRoute($requestUri); |
| 112 |
if ($prettyRoute !== '') { |
| 113 |
return wpstgIsPluginRestRoute($prettyRoute); |
| 114 |
} |
| 115 |
|
| 116 |
if (empty($_GET['rest_route'])) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$restRoute = sanitize_text_field(wp_unslash($_GET['rest_route'])); |
| 121 |
|
| 122 |
return wpstgIsPluginRestRoute($restRoute); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (!function_exists('wpstgIsStagingSite')) { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
function wpstgIsStagingSite(string $stagingMarkerFileName = '.wp-staging', string $stagingSiteOptionName = 'wpstg_is_staging_site'): bool |
| 133 |
{ |
| 134 |
if (defined('WPSTAGING_DEV_SITE') && WPSTAGING_DEV_SITE === true) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
if (get_option($stagingSiteOptionName) === 'true') { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
return file_exists(ABSPATH . $stagingMarkerFileName); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (!function_exists('wpstgShouldSkipBootstrap')) { |
| 147 |
function wpstgShouldSkipBootstrap(): bool |
| 148 |
{ |
| 149 |
if (defined('WP_INSTALLING') && WP_INSTALLING) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
if (defined('DOING_CRON') && DOING_CRON) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
if (defined('WP_CLI') && WP_CLI) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
$requestUri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : ''; |
| 162 |
|
| 163 |
|
| 164 |
if (strpos($requestUri, '/wp-login.php') !== false) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
$action = wpstgGetRequestAction(); |
| 170 |
if ( |
| 171 |
!empty($_GET['wpstg_login']) || |
| 172 |
!empty($_GET['wpstg_staging_login']) || |
| 173 |
wpstgIsPluginAction($action) |
| 174 |
) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
if (wpstgIsStagingSite()) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if (php_sapi_name() === 'cli') { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
if (wpstgIsAdminActionRequest($requestUri)) { |
| 189 |
return !wpstgIsPluginAction($action); |
| 190 |
} |
| 191 |
|
| 192 |
if (is_admin()) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
if (wpstgIsRestRequest($requestUri)) { |
| 197 |
return !wpstgIsPluginRestRequest($requestUri); |
| 198 |
} |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
} |
| 203 |
|