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 / spelling / SpellCandidatePermalinkLookup.php

SpellCandidatePermalinkLookup.php in 404 Solution trunk, at includes/spelling/SpellCandidatePermalinkLookup.php

66 lines 1.9 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 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