PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
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 / settings / SettingsFieldValidator.php

SettingsFieldValidator.php in 404 Solution 4.3.0, at includes/settings/SettingsFieldValidator.php

66 lines 2.5 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 * Validates numeric settings fields and reports already-translated error
9 * messages.
10 *
11 * Replaces the previous PluginLogicSettingsUpdate::validateAndSetNumericField()
12 * helper, which called __($errorMessage, '404-solution') on a runtime
13 * variable. The WordPress POT extractor only sees __() calls with literal
14 * arguments, so every error message that flowed through the old helper
15 * was invisible to the translator and could never be translated (audit
16 * finding 440 in design-audit-2026-06-04.md).
17 *
18 * This validator requires the caller to pass an already-translated string
19 * (built with __('literal text', '404-solution') at the call site). The
20 * literal then appears in the POT file and is translatable. The validator
21 * itself never calls __() on a non-literal argument.
22 */
23 class ABJ_404_Solution_SettingsFieldValidator {
24
25 /**
26 * Validate a numeric field from a POST payload and, on success, write
27 * the absint() of the value into the options array.
28 *
29 * @param array<string, mixed> $options Mutated in place on success.
30 * @param array<string, mixed> $postData
31 * @param string $fieldName The field key in both $postData and $options.
32 * @param string $alreadyTranslatedErrorMessage Built with __('literal', '404-solution') at the call site.
33 * @param int $minValue Minimum permitted value (inclusive in the default mode, exclusive in absint-strict mode).
34 * @param bool $useAbsintForCheck When true, requires absint($value) > $minValue. When false, requires raw value >= $minValue.
35 * @return string Empty string on success, or the translated error suffixed with ".<BR/>".
36 */
37 public function validateAndSetNumericField(
38 array &$options,
39 array $postData,
40 string $fieldName,
41 string $alreadyTranslatedErrorMessage,
42 int $minValue = 0,
43 bool $useAbsintForCheck = false
44 ): string {
45 if (!isset($postData[$fieldName])) {
46 return '';
47 }
48
49 $value = $postData[$fieldName];
50 $scalarValue = is_scalar($value) ? $value : 0;
51
52 if ($useAbsintForCheck) {
53 $passes = is_numeric($value) && absint($scalarValue) > $minValue;
54 } else {
55 $passes = is_numeric($value) && $value >= $minValue;
56 }
57
58 if ($passes) {
59 $options[$fieldName] = absint($scalarValue);
60 return '';
61 }
62
63 return $alreadyTranslatedErrorMessage . '.<BR/>';
64 }
65 }
66