| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Promotes matching-engine results into automatic redirects and emits them. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_AutoRedirectHandler { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_RedirectsRepository */ |
| 13 |
private $redirectsRepository; |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_MatchingEngineOrchestrator */ |
| 16 |
private $matchingEngineOrchestrator; |
| 17 |
|
| 18 |
/** @var ABJ_404_Solution_NotFoundResponseService */ |
| 19 |
private $notFoundResponse; |
| 20 |
|
| 21 |
/** @var ABJ_404_Solution_FrontendHitRecorder */ |
| 22 |
private $hitRecorder; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param ABJ_404_Solution_RedirectsRepository $redirectsRepository |
| 26 |
* @param ABJ_404_Solution_MatchingEngineOrchestrator $matchingEngineOrchestrator |
| 27 |
* @param ABJ_404_Solution_NotFoundResponseService $notFoundResponse |
| 28 |
* @param ABJ_404_Solution_FrontendHitRecorder $hitRecorder |
| 29 |
*/ |
| 30 |
function __construct($redirectsRepository, $matchingEngineOrchestrator, $notFoundResponse, $hitRecorder) { |
| 31 |
$this->redirectsRepository = $redirectsRepository; |
| 32 |
$this->matchingEngineOrchestrator = $matchingEngineOrchestrator; |
| 33 |
$this->notFoundResponse = $notFoundResponse; |
| 34 |
$this->hitRecorder = $hitRecorder; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param string $requestedURL |
| 39 |
* @param string $urlSlugOnly |
| 40 |
* @param array<string, mixed> $options |
| 41 |
* @param ABJ_404_Solution_FrontendPipelineTrace $trace |
| 42 |
* @return bool True when a matching engine produced and emitted a redirect. |
| 43 |
*/ |
| 44 |
function promoteAndRedirectIfMatched( |
| 45 |
string $requestedURL, |
| 46 |
string $urlSlugOnly, |
| 47 |
array $options, |
| 48 |
ABJ_404_Solution_FrontendPipelineTrace $trace |
| 49 |
): bool { |
| 50 |
$matchRequest = new ABJ_404_Solution_MatchRequest($requestedURL, $urlSlugOnly, $options); |
| 51 |
$matchResult = $this->matchingEngineOrchestrator->run($matchRequest, $trace); |
| 52 |
if ($matchResult === null) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
$defaultRedirect = isset($options['default_redirect']) && is_scalar($options['default_redirect']) ? (string)$options['default_redirect'] : ''; |
| 57 |
$this->redirectsRepository->setupRedirect(ABJ_404_Solution_RedirectSpec::create( |
| 58 |
$requestedURL, |
| 59 |
(string)ABJ404_STATUS_AUTO, |
| 60 |
$matchResult->getType(), |
| 61 |
$matchResult->getId(), |
| 62 |
$defaultRedirect, |
| 63 |
0, |
| 64 |
$matchResult->getEngineName(), |
| 65 |
$matchResult->getScore() |
| 66 |
)); |
| 67 |
|
| 68 |
$resolvedLink = $this->resolveMatchLink($matchResult); |
| 69 |
$this->hitRecorder->record($requestedURL, $resolvedLink, $matchResult->getEngineName(), null, $trace->getSteps()); |
| 70 |
$this->notFoundResponse->forceRedirect(esc_url($resolvedLink), (int)$defaultRedirect); |
| 71 |
exit; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param ABJ_404_Solution_MatchResult $matchResult |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
private function resolveMatchLink(ABJ_404_Solution_MatchResult $matchResult): string { |
| 79 |
$resolvedLink = $matchResult->getLink(); |
| 80 |
if ($matchResult->getId() === '' || $matchResult->getId() === '0') { |
| 81 |
return $resolvedLink; |
| 82 |
} |
| 83 |
|
| 84 |
$permalink = ABJ_404_Solution_PermalinkResolver::permalinkInfoToArray( |
| 85 |
$matchResult->getId() . '|' . $matchResult->getType(), |
| 86 |
0 |
| 87 |
); |
| 88 |
if (is_array($permalink) && !empty($permalink['link']) && is_string($permalink['link']) && $permalink['link'] !== 'dunno') { |
| 89 |
return $permalink['link']; |
| 90 |
} |
| 91 |
return $resolvedLink; |
| 92 |
} |
| 93 |
} |
| 94 |
|