| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class ABJ_404_Solution_View_Suggestions { |
| 9 |
|
| 10 |
/** @var self|null */ |
| 11 |
private static $instance = null; |
| 12 |
/** |
| 13 |
* Test seam: install or clear the cached singleton instance without |
| 14 |
* private-field reflection. Pass null to reset between tests; pass a |
| 15 |
* configured instance (or double) to install it (M105 singleton-reset seam). |
| 16 |
* |
| 17 |
* @param self|null $instance |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public static function setInstance($instance) { |
| 21 |
self::$instance = $instance; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** @var ABJ_404_Solution_Functions */ |
| 26 |
private $f; |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor with dependency injection. |
| 30 |
* |
| 31 |
* @param ABJ_404_Solution_Functions|null $functions String utilities |
| 32 |
*/ |
| 33 |
public function __construct($functions = null) { |
| 34 |
$this->f = $functions !== null ? $functions : abj_service('functions'); |
| 35 |
} |
| 36 |
|
| 37 |
/** @return self */ |
| 38 |
public static function getInstance() { |
| 39 |
if (self::$instance == null) { |
| 40 |
self::$instance = new ABJ_404_Solution_View_Suggestions(); |
| 41 |
} |
| 42 |
|
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @param array<string, mixed> $options |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
function getAdminOptionsPage404Suggestions($options) { |
| 51 |
$options = array_merge(array( |
| 52 |
'suggest_cats' => '0', |
| 53 |
'suggest_tags' => '0', |
| 54 |
'update_suggest_url' => '0', |
| 55 |
'suggest_max' => '5', |
| 56 |
'suggest_title' => '', |
| 57 |
'suggest_before' => '', |
| 58 |
'suggest_after' => '', |
| 59 |
'suggest_entrybefore' => '', |
| 60 |
'suggest_entryafter' => '', |
| 61 |
'suggest_noresults' => '', |
| 62 |
), $options); |
| 63 |
|
| 64 |
// Suggested Alternatives Options |
| 65 |
$selectedSuggestCats = ""; |
| 66 |
if ($options['suggest_cats'] == '1') { |
| 67 |
$selectedSuggestCats = " checked"; |
| 68 |
} |
| 69 |
$selectedSuggestTags = ""; |
| 70 |
if ($options['suggest_tags'] == '1') { |
| 71 |
$selectedSuggestTags = " checked"; |
| 72 |
} |
| 73 |
$selectedSuggestURL = ""; |
| 74 |
if ($options['update_suggest_url'] == '1') { |
| 75 |
$selectedSuggestURL = " checked"; |
| 76 |
} |
| 77 |
$selectedSuggestMinscoreEnabled = ""; |
| 78 |
if (isset($options['suggest_minscore_enabled']) && $options['suggest_minscore_enabled'] == '1') { |
| 79 |
$selectedSuggestMinscoreEnabled = " checked"; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
// read the html content. |
| 84 |
$html = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . "/html/viewSuggestions.html"); |
| 85 |
// do special replacements |
| 86 |
$html = $this->f->str_replace('{SELECTED_SUGGEST_CATS}', $selectedSuggestCats, $html); |
| 87 |
$html = $this->f->str_replace('{SELECTED_SUGGEST_TAGS}', $selectedSuggestTags, $html); |
| 88 |
$html = $this->f->str_replace('{SELECTED_SUGGEST_MINSCORE_ENABLED}', $selectedSuggestMinscoreEnabled, $html); |
| 89 |
$html = $this->f->str_replace('{SELECTED_SUGGEST_URL}', $selectedSuggestURL, $html); |
| 90 |
$sugMax = $options['suggest_max']; |
| 91 |
$sugTitle = $options['suggest_title']; |
| 92 |
$sugBefore = $options['suggest_before']; |
| 93 |
$sugAfter = $options['suggest_after']; |
| 94 |
$sugEntryBefore = $options['suggest_entrybefore']; |
| 95 |
$sugEntryAfter = $options['suggest_entryafter']; |
| 96 |
$sugNoResults = $options['suggest_noresults']; |
| 97 |
$html = $this->f->str_replace('{SUGGEST_MAX_SUGGESTIONS}', esc_attr(is_scalar($sugMax) ? (string)$sugMax : ''), $html); |
| 98 |
$html = $this->f->str_replace('{SUGGEST_USER_TITLE}', esc_attr(is_scalar($sugTitle) ? (string)$sugTitle : ''), $html); |
| 99 |
$html = $this->f->str_replace('{SUGGEST_USER_BEFORE}', esc_attr(is_scalar($sugBefore) ? (string)$sugBefore : ''), $html); |
| 100 |
$html = $this->f->str_replace('{SUGGEST_USER_AFTER}', esc_attr(is_scalar($sugAfter) ? (string)$sugAfter : ''), $html); |
| 101 |
$html = $this->f->str_replace('{SUGGEST_USER_ENTRY_BEFORE}', esc_attr(is_scalar($sugEntryBefore) ? (string)$sugEntryBefore : ''), $html); |
| 102 |
$html = $this->f->str_replace('{SUGGEST_USER_ENTRY_AFTER}', esc_attr(is_scalar($sugEntryAfter) ? (string)$sugEntryAfter : ''), $html); |
| 103 |
$html = $this->f->str_replace('{SUGGEST_USER_NO_RESULTS}', esc_attr(is_scalar($sugNoResults) ? (string)$sugNoResults : ''), $html); |
| 104 |
// constants and translations. |
| 105 |
$html = $this->f->doNormalReplacements($html); |
| 106 |
|
| 107 |
return $html; |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|