| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
// allow-no-test-found: exercised by PatternPriorityResolutionTest via does404PageHaveSuggestionsShortcode |
| 8 |
|
| 9 |
/** |
| 10 |
* Decides whether the site's configured custom 404 page embeds the suggestions |
| 11 |
* shortcode. The async-suggestion trigger consults this before kicking off a |
| 12 |
* background spell-check compute: there is no point pre-computing suggestions |
| 13 |
* for a 404 page that will never render them. |
| 14 |
* |
| 15 |
* Resolves the configured `dest404page` option, confirms it points at a real |
| 16 |
* user-specified page (via the NotFoundResponse service), loads that post, and |
| 17 |
* reports whether its content contains the suggestions shortcode. Pure |
| 18 |
* read/inspection policy: no SQL, no presentation, no spelling-match work. |
| 19 |
*/ |
| 20 |
class ABJ_404_Solution_SpellSuggestionShortcodeDetector { |
| 21 |
|
| 22 |
/** @var ABJ_404_Solution_NotFoundResponseService|null */ |
| 23 |
private $notFoundResponse; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param ABJ_404_Solution_NotFoundResponseService|null $notFoundResponse |
| 27 |
*/ |
| 28 |
public function __construct($notFoundResponse) { |
| 29 |
$this->notFoundResponse = $notFoundResponse; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @return bool true only when the configured custom 404 page exists and its |
| 34 |
* content contains the suggestions shortcode. |
| 35 |
*/ |
| 36 |
public function does404PageHaveSuggestionsShortcode(): bool { |
| 37 |
$options = abj_service('options_repository')->getOptions(); |
| 38 |
$dest404pageRaw = isset($options['dest404page']) ? $options['dest404page'] : null; |
| 39 |
$dest404page = is_string($dest404pageRaw) ? $dest404pageRaw : null; |
| 40 |
|
| 41 |
if (!$this->notFoundResponse instanceof ABJ_404_Solution_NotFoundResponseService |
| 42 |
|| !$this->notFoundResponse->thereIsAUserSpecified404Page($dest404page)) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
$parts = explode('|', $dest404page ?? ''); |
| 47 |
$page404Id = isset($parts[0]) ? intval($parts[0]) : 0; |
| 48 |
|
| 49 |
if ($page404Id <= 0) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$page = get_post($page404Id); |
| 54 |
if (!$page) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
return has_shortcode($page->post_content, ABJ404_SHORTCODE_NAME); |
| 59 |
} |
| 60 |
} |
| 61 |
|