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 / RedirectLookupService.php

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

108 lines 3.2 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 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