| 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 |
|