| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves request URLs to active or existing redirect rows. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RedirectLookupService { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Functions */ |
| 13 |
private $f; |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_RedirectLookupRepository */ |
| 16 |
private $lookupRepository; |
| 17 |
|
| 18 |
/** @var ABJ_404_Solution_PluginLogicUrlNormalization|null */ |
| 19 |
private $urlNormalization; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param ABJ_404_Solution_Functions $functions |
| 23 |
* @param ABJ_404_Solution_RedirectLookupRepository $lookupRepository |
| 24 |
* @param ABJ_404_Solution_PluginLogicUrlNormalization|null $urlNormalization |
| 25 |
*/ |
| 26 |
public function __construct( |
| 27 |
$functions, |
| 28 |
ABJ_404_Solution_RedirectLookupRepository $lookupRepository, |
| 29 |
?ABJ_404_Solution_PluginLogicUrlNormalization $urlNormalization = null |
| 30 |
) { |
| 31 |
$this->f = $functions; |
| 32 |
$this->lookupRepository = $lookupRepository; |
| 33 |
$this->urlNormalization = $urlNormalization; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param string $url |
| 38 |
* @param bool $degradedMode |
| 39 |
* @return array<string, mixed> |
| 40 |
*/ |
| 41 |
public function getActiveRedirectForURL($url, $degradedMode = false): array { |
| 42 |
return $this->findFirstCandidate($url, function($candidate) use ($degradedMode) { |
| 43 |
return $this->lookupRepository->getActiveRedirectForNormalizedUrl($candidate, $degradedMode); |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string $url |
| 49 |
* @return array<string, mixed> |
| 50 |
*/ |
| 51 |
public function getExistingRedirectForURL($url): array { |
| 52 |
return $this->findFirstCandidate($url, function($candidate) { |
| 53 |
return $this->lookupRepository->getExistingRedirectForNormalizedUrl($candidate); |
| 54 |
}); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param string $url |
| 59 |
* @param callable $lookup |
| 60 |
* @return array<string, mixed> |
| 61 |
*/ |
| 62 |
private function findFirstCandidate($url, callable $lookup): array { |
| 63 |
$url = $this->f->sanitizeInvalidUTF8($url); |
| 64 |
|
| 65 |
if (function_exists('mb_check_encoding') && !mb_check_encoding($url, 'UTF-8')) { |
| 66 |
return array('id' => 0); |
| 67 |
} |
| 68 |
|
| 69 |
$candidates = $this->urlNormalization()->getNormalizedUrlCandidates($url); |
| 70 |
foreach ($candidates as $candidate) { |
| 71 |
$redirect = $lookup($candidate); |
| 72 |
if (!is_array($redirect)) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
$assocRedirect = $this->assocRedirectRow($redirect); |
| 76 |
if (isset($assocRedirect['id']) && $assocRedirect['id'] !== 0) { |
| 77 |
return $assocRedirect; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return array('id' => 0); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param array<mixed, mixed> $redirect |
| 86 |
* @return array<string, mixed> |
| 87 |
*/ |
| 88 |
private function assocRedirectRow(array $redirect): array { |
| 89 |
$assoc = array(); |
| 90 |
foreach ($redirect as $key => $value) { |
| 91 |
if (is_string($key)) { |
| 92 |
$assoc[$key] = $value; |
| 93 |
} |
| 94 |
} |
| 95 |
return $assoc; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return ABJ_404_Solution_PluginLogicUrlNormalization |
| 100 |
*/ |
| 101 |
private function urlNormalization() { |
| 102 |
if ($this->urlNormalization !== null) { |
| 103 |
return $this->urlNormalization; |
| 104 |
} |
| 105 |
return abj_service('plugin_logic')->urlNormalization(); |
| 106 |
} |
| 107 |
} |
| 108 |
|