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 / policies / SettingsRegexPatternPolicy.php

SettingsRegexPatternPolicy.php in 404 Solution trunk, at includes/policies/SettingsRegexPatternPolicy.php

75 lines 2.7 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 /**
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