| @@ -1,45 +1,62 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace BitCode\BitForm\Core\Capability; |
| 3 | 4 | |
| 5 | +use BitCode\BitForm\Core\Util\FileDownloadProvider; | |
| 4 | 6 | |
| 5 | -use BitCode\BitForm\Core\Util\FileDownloadProvider; | |
| 6 | 7 | final class Request |
| 7 | 8 | { |
| 8 | - public static function Check($type) | |
| 9 | - { | |
| 10 | - switch ($type) { | |
| 11 | - case 'admin': | |
| 12 | - return is_admin(); | |
| 13 | - | |
| 14 | - case 'ajax': | |
| 15 | - return defined('DOING_AJAX'); | |
| 16 | - | |
| 17 | - case 'cron': | |
| 18 | - return defined('DOING_CRON'); | |
| 19 | - | |
| 20 | - case 'api': | |
| 21 | - return defined('REST_REQUEST'); | |
| 22 | - | |
| 23 | - case 'frontend': | |
| 24 | - return (! is_admin() || defined('DOING_AJAX')) && ! defined('DOING_CRON'); | |
| 25 | - } | |
| 9 | + public static function Check($type) | |
| 10 | + { | |
| 11 | + switch ($type) { | |
| 12 | + case 'admin': | |
| 13 | + return is_admin(); | |
| 14 | + | |
| 15 | + case 'ajax': | |
| 16 | + return defined('DOING_AJAX'); | |
| 17 | + | |
| 18 | + case 'cron': | |
| 19 | + return defined('DOING_CRON'); | |
| 20 | + | |
| 21 | + case 'api': | |
| 22 | + return defined('REST_REQUEST'); | |
| 23 | + | |
| 24 | + case 'frontend': | |
| 25 | + return (!is_admin() || defined('DOING_AJAX')) && !defined('DOING_CRON'); | |
| 26 | 26 | } |
| 27 | + } | |
| 27 | 28 | |
| 28 | - public static function isPluginPage() | |
| 29 | - { | |
| 30 | - global $wp; | |
| 31 | - $queryToRemove = array('formID','entryID','fileID','download'); | |
| 32 | - | |
| 33 | - $reqUrl = home_url(remove_query_arg($queryToRemove, $_SERVER['REQUEST_URI'])); | |
| 34 | - $downloadUrl = FileDownloadProvider::getBaseDownloadURL(); | |
| 35 | - $reqUrl = \substr($reqUrl, -1) !== '/' ? $reqUrl.'/' : $reqUrl; | |
| 36 | - $downloadUrl = \substr($downloadUrl, -1) !== '/' ? $downloadUrl.'/' : $downloadUrl; | |
| 37 | - switch ($reqUrl) { | |
| 38 | - case $downloadUrl:{ | |
| 39 | - return true; | |
| 40 | - } | |
| 41 | - default: | |
| 42 | - return false; | |
| 43 | - } | |
| 29 | + public static function isPluginPage() | |
| 30 | + { | |
| 31 | + // Plain permalinks put the file route in the query string (?post_type=bitforms&p=ID), | |
| 32 | + // pretty permalinks put it in the path (/bitforms/bitforms-file/). Match both, otherwise | |
| 33 | + // the download handler/shortcode never registers on plain-permalink sites. | |
| 34 | + $routes = get_option('bitforms_routes'); | |
| 35 | + $fileRouteId = isset($routes['file']) ? (int) $routes['file'] : 0; | |
| 36 | + | |
| 37 | + // Plain permalink: ?p=ID (post) or ?page_id=ID, with post_type=bitforms. | |
| 38 | + // Read-only: query vars inspected to detect plugin-managed URL. No state change. | |
| 39 | + if (!empty($fileRouteId)) { | |
| 40 | + $reqPostId = 0; | |
| 41 | + if (isset($_GET['p']) && !is_array($_GET['p'])) { | |
| 42 | + $reqPostId = (int) $_GET['p']; | |
| 43 | + } elseif (isset($_GET['page_id']) && !is_array($_GET['page_id'])) { | |
| 44 | + $reqPostId = (int) $_GET['page_id']; | |
| 45 | + } | |
| 46 | + if ($reqPostId === $fileRouteId) { | |
| 47 | + return true; | |
| 48 | + } | |
| 44 | 49 | } |
| 50 | + | |
| 51 | + // Pretty permalink: compare request path against the download URL path. | |
| 52 | + $downloadUrl = FileDownloadProvider::getBaseDownloadURL(); | |
| 53 | + $downloadPath = wp_parse_url($downloadUrl, PHP_URL_PATH); | |
| 54 | + // Read-only: REQUEST_URI parsed to check if current request targets a plugin-managed URL. No state change. | |
| 55 | + $reqPath = isset($_SERVER['REQUEST_URI']) ? wp_parse_url(wp_unslash($_SERVER['REQUEST_URI']), PHP_URL_PATH) : ''; | |
| 56 | + if (empty($downloadPath) || empty($reqPath)) { | |
| 57 | + return false; | |
| 58 | + } | |
| 59 | + | |
| 60 | + return untrailingslashit($downloadPath) === untrailingslashit($reqPath); | |
| 61 | + } | |
| 45 | 62 | } |