suggestCats = $suggestCats; $this->suggestTags = $suggestTags; $this->suggestMax = $suggestMax; $this->suggestMinscore = $suggestMinscore; $this->suggestMinscoreEnabled = $suggestMinscoreEnabled; $this->suggestTitle = $suggestTitle; $this->suggestBefore = $suggestBefore; $this->suggestAfter = $suggestAfter; $this->suggestEntryBefore = $suggestEntryBefore; $this->suggestEntryAfter = $suggestEntryAfter; $this->suggestNoResults = $suggestNoResults; } /** * Normalize the raw `get_option('abj404_settings')` array (or any * compatible payload) into a typed VO. A non-array input falls back * to the documented defaults, matching the behaviour of * `PluginLogic::getOptions()` when the option row is missing. * * @param mixed $raw */ public static function fromOptionsArray($raw): self { $options = is_array($raw) ? $raw : array(); return new self( self::coerceBoolString($options, 'suggest_cats', '1'), self::coerceBoolString($options, 'suggest_tags', '1'), self::coercePositiveInt($options, 'suggest_max', 5), self::coerceNonNegativeInt($options, 'suggest_minscore', 25), self::coerceBoolString($options, 'suggest_minscore_enabled', '0'), self::coerceString($options, 'suggest_title', '

{suggest_title_text}

'), self::coerceString($options, 'suggest_before', '
    '), self::coerceString($options, 'suggest_after', '
'), self::coerceString($options, 'suggest_entrybefore', '
  • '), self::coerceString($options, 'suggest_entryafter', '
  • '), self::coerceString($options, 'suggest_noresults', '

    {suggest_noresults_text}

    ') ); } /** * Legacy '0'/'1' string. Consumers that echo into form HTML or pass * to SpellChecker (which expects the legacy shape) want this. */ public function getSuggestCatsString(): string { return $this->suggestCats; } public function getSuggestTagsString(): string { return $this->suggestTags; } public function shouldSuggestCategories(): bool { return $this->suggestCats === '1'; } public function shouldSuggestTags(): bool { return $this->suggestTags === '1'; } public function getSuggestMax(): int { return $this->suggestMax; } public function getSuggestMinscore(): int { return $this->suggestMinscore; } public function isMinscoreEnabled(): bool { return $this->suggestMinscoreEnabled === '1'; } public function getSuggestTitleHtml(): string { return $this->suggestTitle; } public function getSuggestBeforeHtml(): string { return $this->suggestBefore; } public function getSuggestAfterHtml(): string { return $this->suggestAfter; } public function getSuggestEntryBeforeHtml(): string { return $this->suggestEntryBefore; } public function getSuggestEntryAfterHtml(): string { return $this->suggestEntryAfter; } public function getSuggestNoResultsHtml(): string { return $this->suggestNoResults; } /** * @param array $options */ private static function coerceString(array $options, string $key, string $default): string { if (!isset($options[$key])) { return $default; } $v = $options[$key]; if (is_string($v)) { return $v; } if (is_scalar($v)) { return (string)$v; } return $default; } /** * Legacy boolean stored as '0' / '1'. Coerces other truthy/falsy * scalar shapes (numeric int, bool) into the canonical string. * * @param array $options */ private static function coerceBoolString(array $options, string $key, string $default): string { if (!isset($options[$key])) { return $default; } $v = $options[$key]; if (is_string($v)) { return $v === '1' ? '1' : '0'; } if (is_bool($v)) { return $v ? '1' : '0'; } if (is_int($v) || is_float($v)) { return ((int)$v) === 1 ? '1' : '0'; } return $default; } /** * @param array $options */ private static function coerceNonNegativeInt(array $options, string $key, int $default): int { if (!isset($options[$key])) { return $default; } $v = $options[$key]; if (is_int($v)) { return $v < 0 ? $default : $v; } if (is_float($v)) { $i = (int)$v; return $i < 0 ? $default : $i; } if (is_string($v) && is_numeric($v)) { $i = (int)$v; return $i < 0 ? $default : $i; } return $default; } /** * suggest_max must be >= 1; a 0 or negative would render no * suggestions at all and is treated as a malformed override. * * @param array $options */ private static function coercePositiveInt(array $options, string $key, int $default): int { $i = self::coerceNonNegativeInt($options, $key, $default); return $i < 1 ? $default : $i; } }