PluginProbe
404 Solution / trunk
404 Solution vtrunk
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 / redirects / AutoRedirectHandler.php

AutoRedirectHandler.php in 404 Solution trunk, at includes/redirects/AutoRedirectHandler.php

94 lines 3.4 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 * 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