| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Applies regex-backed settings from the settings form payload. |
| 9 |
* |
| 10 |
* The stored contract has two parts for each textarea setting: the raw |
| 11 |
* sanitized textarea value and the derived list of anchored, wildcard-aware |
| 12 |
* regular expressions used at runtime. |
| 13 |
*/ |
| 14 |
class ABJ_404_Solution_SettingsRegexPatternPolicy { |
| 15 |
|
| 16 |
/** @var ABJ_404_Solution_Functions */ |
| 17 |
private $functions; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param ABJ_404_Solution_Functions $functions |
| 21 |
*/ |
| 22 |
public function __construct($functions) { |
| 23 |
$this->functions = $functions; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param array<string, mixed> $options |
| 28 |
* @param array<string, mixed> $postData |
| 29 |
* @return string Empty string; kept for the section-updater message contract. |
| 30 |
*/ |
| 31 |
public function apply(array &$options, array $postData): string { |
| 32 |
if (isset($postData['folders_files_ignore'])) { |
| 33 |
$foldersFilesVal = is_string($postData['folders_files_ignore']) ? $postData['folders_files_ignore'] : ''; |
| 34 |
$options['folders_files_ignore'] = wp_unslash(wp_kses_post($foldersFilesVal)); |
| 35 |
$options['folders_files_ignore_usable'] = $this->patternsFromLines( |
| 36 |
$this->functions->explodeNewline($options['folders_files_ignore']), |
| 37 |
true |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
if (isset($postData['suggest_regex_exclusions'])) { |
| 42 |
$suggestRegexRaw = is_string($postData['suggest_regex_exclusions']) ? $postData['suggest_regex_exclusions'] : ''; |
| 43 |
$sanitizedExclusions = sanitize_textarea_field(wp_unslash($suggestRegexRaw)); |
| 44 |
$options['suggest_regex_exclusions'] = $sanitizedExclusions; |
| 45 |
$options['suggest_regex_exclusions_usable'] = $this->patternsFromLines( |
| 46 |
$this->functions->explodeNewline($sanitizedExclusions), |
| 47 |
false |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
return ""; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @param array<int, string> $lines |
| 56 |
* @param bool $includeEmpty Existing folders_files_ignore behavior stores |
| 57 |
* the anchored empty pattern when the splitter |
| 58 |
* returns one; suggest exclusions skip empties. |
| 59 |
* @return array<int, string> |
| 60 |
*/ |
| 61 |
private function patternsFromLines(array $lines, bool $includeEmpty): array { |
| 62 |
$usablePatterns = array(); |
| 63 |
foreach ($lines as $line) { |
| 64 |
$trimmedPattern = trim($line); |
| 65 |
if (!$includeEmpty && empty($trimmedPattern)) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
$newPattern = '^' . preg_quote($trimmedPattern, '/') . '$'; |
| 69 |
$newPattern = str_replace('\*', '.*', $newPattern); |
| 70 |
$usablePatterns[] = $newPattern; |
| 71 |
} |
| 72 |
return $usablePatterns; |
| 73 |
} |
| 74 |
} |
| 75 |
|