PluginProbe
404 Solution / 4.3.3
404 Solution v4.3.3
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 / engine / SlugMatchingEngine.php

SlugMatchingEngine.php in 404 Solution 4.3.3, at includes/engine/SlugMatchingEngine.php

53 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 * Matching engine that delegates to SpellChecker::getPermalinkUsingSlug().
9 *
10 * Performs an exact slug lookup against published posts/pages.
11 * This is a fast DB query, so shouldRun() only skips when the slug is empty.
12 */
13 class ABJ_404_Solution_SlugMatchingEngine implements ABJ_404_Solution_MatchingEngine {
14
15 /** @var ABJ_404_Solution_SpellChecker */
16 private $spellChecker;
17
18 /**
19 * @param ABJ_404_Solution_SpellChecker $spellChecker
20 */
21 public function __construct(ABJ_404_Solution_SpellChecker $spellChecker) {
22 $this->spellChecker = $spellChecker;
23 }
24
25 /** @return string */
26 public function getName(): string {
27 return __('exact slug', '404-solution');
28 }
29
30 /** @param ABJ_404_Solution_MatchRequest $request */
31 public function shouldRun(ABJ_404_Solution_MatchRequest $request): bool {
32 $slug = $request->getUrlSlugOnly();
33 return $slug !== '';
34 }
35
36 /** @param ABJ_404_Solution_MatchRequest $request */
37 public function match(ABJ_404_Solution_MatchRequest $request): ?ABJ_404_Solution_MatchResult {
38 $permalink = $this->spellChecker->getPermalinkUsingSlug($request->getUrlSlugOnly());
39
40 if (empty($permalink)) {
41 return null;
42 }
43
44 $id = isset($permalink['id']) && is_scalar($permalink['id']) ? (string)$permalink['id'] : '';
45 $type = isset($permalink['type']) && is_scalar($permalink['type']) ? (string)$permalink['type'] : '';
46 $link = isset($permalink['link']) && is_string($permalink['link']) ? $permalink['link'] : '';
47 $title = isset($permalink['title']) && is_string($permalink['title']) ? $permalink['title'] : '';
48 $score = isset($permalink['score']) && is_scalar($permalink['score']) ? (float)$permalink['score'] : 0.0;
49
50 return new ABJ_404_Solution_MatchResult($id, $type, $link, $title, $score, $this->getName());
51 }
52 }
53