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 / SpellSuggestionShortcodeDetector.php

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

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