| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves selected spell-candidate IDs to permalinks after candidate pruning. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_SpellCandidatePermalinkLookup { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_ContentRepository */ |
| 13 |
private $contentRepository; |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_SpellURLMatcher */ |
| 16 |
private $urlMatcher; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param ABJ_404_Solution_ContentRepository $contentRepository |
| 20 |
* @param ABJ_404_Solution_SpellURLMatcher $urlMatcher |
| 21 |
*/ |
| 22 |
public function __construct($contentRepository, $urlMatcher) { |
| 23 |
$this->contentRepository = $contentRepository; |
| 24 |
$this->urlMatcher = $urlMatcher; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param array<int, mixed> $ids |
| 29 |
* @param string $rowType |
| 30 |
* @param array<int, string> $observedPermalinksById |
| 31 |
* @return array<int|string, string> |
| 32 |
*/ |
| 33 |
public function lookup(array $ids, string $rowType, array $observedPermalinksById = array()): array { |
| 34 |
if (empty($ids)) { |
| 35 |
return []; |
| 36 |
} |
| 37 |
$result = []; |
| 38 |
if ($rowType === 'pages' || $rowType === 'image') { |
| 39 |
$intIds = array_map(function($v) { return is_scalar($v) ? (int)$v : 0; }, $ids); |
| 40 |
$rows = $this->contentRepository->getPermalinksByIds($intIds); |
| 41 |
foreach ($rows as $row) { |
| 42 |
$row = (array)$row; |
| 43 |
if (isset($row['id'], $row['url']) && is_string($row['url'])) { |
| 44 |
$result[(int)$row['id']] = abj_service('sanitizer')->normalizeUrlString($row['url']); |
| 45 |
} |
| 46 |
} |
| 47 |
foreach ($intIds as $id) { |
| 48 |
if (!isset($result[$id]) && isset($observedPermalinksById[$id])) { |
| 49 |
$result[$id] = abj_service('sanitizer')->normalizeUrlString($observedPermalinksById[$id]); |
| 50 |
} |
| 51 |
} |
| 52 |
return $result; |
| 53 |
} |
| 54 |
|
| 55 |
foreach ($ids as $id) { |
| 56 |
$idInt = is_scalar($id) ? (int)$id : 0; |
| 57 |
$permalink = $this->urlMatcher->getPermalink($idInt, $rowType); |
| 58 |
if (is_string($permalink) && $permalink !== '') { |
| 59 |
$key = (is_int($id) || is_string($id)) ? $id : (string)$idInt; |
| 60 |
$result[$key] = $permalink; |
| 61 |
} |
| 62 |
} |
| 63 |
return $result; |
| 64 |
} |
| 65 |
} |
| 66 |
|