| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves the missing URL that powers the frontend suggestions shortcode. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_ShortcodeRequestedUrlResolver { |
| 11 |
|
| 12 |
/** |
| 13 |
* @param ABJ_404_Solution_Functions $functions |
| 14 |
* @return array{url: string, cookieScripts: string} |
| 15 |
*/ |
| 16 |
public function resolve($functions): array { |
| 17 |
$urlRequest = ''; |
| 18 |
$cookieScripts = ''; |
| 19 |
$urlEncoder = abj_service('url_encoder'); |
| 20 |
|
| 21 |
$cookieName = ABJ404_PP . '_REQUEST_URI'; |
| 22 |
$cookieVal = isset($_COOKIE[$cookieName]) && is_string($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : ''; |
| 23 |
if ($cookieVal !== '') { |
| 24 |
$urlRequest = $urlEncoder->normalizeURLForCacheKey(abj_service('sanitizer')->normalizeUrlString($cookieVal)); |
| 25 |
$cookieScripts .= $this->renderCookieDeleteScript($cookieName, false); |
| 26 |
} |
| 27 |
|
| 28 |
$updateURLCookieName = ABJ404_PP . '_REQUEST_URI_UPDATE_URL'; |
| 29 |
$updateCookieVal = isset($_COOKIE[$updateURLCookieName]) && is_string($_COOKIE[$updateURLCookieName]) ? $_COOKIE[$updateURLCookieName] : ''; |
| 30 |
if ($updateCookieVal !== '') { |
| 31 |
if ($urlRequest == '') { |
| 32 |
$urlRequest = $urlEncoder->normalizeURLForCacheKey(abj_service('sanitizer')->normalizeUrlString($updateCookieVal)); |
| 33 |
} |
| 34 |
$cookieScripts .= $this->renderCookieDeleteScript($updateURLCookieName, true); |
| 35 |
} |
| 36 |
|
| 37 |
$requestContext = abj_service('request_context'); |
| 38 |
$ctxUrlRaw = is_object($requestContext) && property_exists($requestContext, 'requested_url') |
| 39 |
? $requestContext->requested_url : ''; |
| 40 |
$ctxUrl = is_string($ctxUrlRaw) ? $ctxUrlRaw : ''; |
| 41 |
if ($ctxUrl !== '') { |
| 42 |
$urlRequest = $urlEncoder->normalizeURLForCacheKey(abj_service('sanitizer')->normalizeUrlString($ctxUrl)); |
| 43 |
} |
| 44 |
|
| 45 |
$queryParamName = ABJ404_PP . '_ref'; |
| 46 |
$getParamVal = isset($_GET[$queryParamName]) && is_string($_GET[$queryParamName]) ? $_GET[$queryParamName] : ''; |
| 47 |
if ($urlRequest == '' && $getParamVal !== '') { |
| 48 |
$urlRequest = $urlEncoder->normalizeURLForCacheKey(abj_service('sanitizer')->normalizeUrlString($getParamVal)); |
| 49 |
} |
| 50 |
|
| 51 |
return array('url' => $urlRequest, 'cookieScripts' => $cookieScripts); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param string $cookieName |
| 56 |
* @param bool $includeDeleteComment |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
private function renderCookieDeleteScript(string $cookieName, bool $includeDeleteComment): string { |
| 60 |
$template = ABJ_404_Solution_FileSystemService::readFileContents( |
| 61 |
dirname(__DIR__) . '/html/shortcodeCookieDeleteScript.html', |
| 62 |
false |
| 63 |
); |
| 64 |
return str_replace( |
| 65 |
array('{delete_comment}', '{cookie_name}'), |
| 66 |
array($includeDeleteComment ? ' /* delete the cookie */' : '', $cookieName), |
| 67 |
$template |
| 68 |
) . "\n"; |
| 69 |
} |
| 70 |
} |
| 71 |
|