PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / services / ShortcodeRequestedUrlResolver.php

ShortcodeRequestedUrlResolver.php in 404 Solution 4.3.0, at includes/services/ShortcodeRequestedUrlResolver.php

71 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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