Skip to content

Commit ff0d80c

Browse files
fix: never drop a settings error when its field can't be found
Credential errors had no fallback: if Gravity Forms couldn't find the named field, the error was lost and a refused key would save. While validate_credentials() runs, errors now fall back to the Mode field, and an error with nowhere to go is logged. Scope labels in key errors are escaped, since Gravity Forms prints field errors as HTML. Tests: the stub set_field_error() now throws on anything Gravity Forms would ignore, the fixture renderer only finds the add-on's defined fields, and the fallback paths are covered. Note on f37a492: the array argument crashed on every supported PHP version (7.2+), not only PHP 8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f37a492 commit ff0d80c

4 files changed

Lines changed: 86 additions & 10 deletions

File tree

‎class-gf-paystack.php‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,16 @@ public function update_plugin_settings($settings)
21042104
* still saves their posted values, so both modes' credentials are checked on every save.
21052105
*/
21062106
public function validate_credentials($field = null)
2107+
{
2108+
$this->validating_field = $field;
2109+
try {
2110+
$this->validate_both_modes($field);
2111+
} finally {
2112+
$this->validating_field = null;
2113+
}
2114+
}
2115+
2116+
private function validate_both_modes($field)
21072117
{
21082118
$selected = rgar($this->get_posted_settings(), 'api_mode');
21092119
if ($selected !== 'test' && $selected !== 'live') {
@@ -2115,7 +2125,7 @@ public function validate_credentials($field = null)
21152125
$failed = $this->validate_webhook_secret_for_mode($mode) || $failed;
21162126

21172127
if ($failed && $mode !== $selected && ($selected === 'test' || $selected === 'live')) {
2118-
$this->set_setting_error('api_mode', sprintf(esc_html__('Your %1$s settings have a problem. Select %2$s to see it. No settings were saved.', 'gravityformspaystack'), $mode, $mode === 'test' ? esc_html__('Test', 'gravityformspaystack') : esc_html__('Live', 'gravityformspaystack')));
2128+
$this->set_setting_error('api_mode', sprintf(esc_html__('Your %1$s settings have a problem. Select %2$s to see it. No settings were saved.', 'gravityformspaystack'), $mode, $mode === 'test' ? esc_html__('Test', 'gravityformspaystack') : esc_html__('Live', 'gravityformspaystack')), $field);
21192129
}
21202130
}
21212131
}
@@ -2287,25 +2297,30 @@ protected function plans_error_message($result, $mode, $context = 'feed')
22872297
return esc_html__('We couldn\'t reach Paystack to check this key. Please try again.', 'gravityformspaystack');
22882298
}
22892299

2300+
/** The Mode field while validate_credentials() runs: where an error goes if its own field can't be found. */
2301+
private $validating_field = null;
2302+
22902303
protected function set_save_error($field_name, $message)
22912304
{
22922305
$this->set_setting_error($field_name, $message . ' ' . esc_html__('No settings were saved.', 'gravityformspaystack'));
22932306
}
22942307

22952308
/**
22962309
* Gravity Forms 2.5+ only records an error on the settings field object, passed by reference, so the field is
2297-
* looked up by name. $fallback (the field being validated) keeps the save blocked if the lookup ever fails.
2310+
* looked up by name. If the lookup fails, the error goes on the field being validated, so the save stays blocked.
22982311
*/
22992312
protected function set_setting_error($name, $message, $fallback = null)
23002313
{
23012314
$renderer = $this->get_settings_renderer();
23022315
$field = $renderer ? $renderer->get_field($name) : false;
23032316
if (!is_object($field)) {
2304-
$field = $fallback;
2317+
$field = is_object($fallback) ? $fallback : $this->validating_field;
23052318
}
23062319
if (is_object($field)) {
23072320
$this->set_field_error($field, $message);
2321+
return;
23082322
}
2323+
$this->log_error(__METHOD__ . "(): no settings field to attach an error to ({$name}).");
23092324
}
23102325

23112326
private function other_mode($mode)
@@ -2361,7 +2376,7 @@ protected function scope_labels($scopes)
23612376
{
23622377
$out = array();
23632378
foreach ($scopes as $scope) {
2364-
$out[] = GF_Paystack_Restricted_Key::scope_label($scope);
2379+
$out[] = esc_html(GF_Paystack_Restricted_Key::scope_label($scope));
23652380
}
23662381

23672382
return implode(', ', $out);

‎tests/fixtures.php‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,41 @@ public function __construct($fixture)
212212
$this->fixture = $fixture;
213213
}
214214

215+
// Like Gravity Forms, only fields the add-on defines are found; renderer_fields overrides the list.
215216
public function get_field($name)
216217
{
217218
$known = $this->fixture->renderer_fields;
218-
if (is_array($known) && !in_array($name, $known, true)) {
219+
if (!is_array($known)) {
220+
$known = self::defined_field_names($this->fixture);
221+
}
222+
if (!in_array($name, $known, true)) {
219223
return false;
220224
}
221225

222226
return new GFPaystackTestSettingField($name, $this->fixture);
223227
}
228+
229+
public static function defined_field_names($addon)
230+
{
231+
$names = array();
232+
$collect = function ($items) use (&$collect, &$names) {
233+
foreach ((array) $items as $item) {
234+
if (!is_array($item)) {
235+
continue;
236+
}
237+
if (isset($item['name'])) {
238+
$names[] = $item['name'];
239+
}
240+
if (isset($item['fields'])) {
241+
$collect($item['fields']);
242+
}
243+
}
244+
};
245+
$collect($addon->plugin_settings_fields());
246+
$collect($addon->feed_settings_fields());
247+
248+
return $names;
249+
}
224250
}
225251

226252
class GFPaystackSettingsFixture extends GFPaystack

‎tests/stubs.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@ public static function include_payment_addon_framework()
156156
if (!class_exists('GFAddOn')) {
157157
class GFAddOn
158158
{
159-
// Same contract as Gravity Forms 2.5+: the field is a settings field object, passed by reference.
159+
// Gravity Forms 2.5+ takes the settings field object by reference and silently ignores anything else;
160+
// the stub throws instead, so a dropped error fails the test.
160161
public function set_field_error(&$field, $error_message = '')
161162
{
162-
if (is_object($field) && method_exists($field, 'set_error')) {
163-
$field->set_error($error_message);
163+
if (!$field instanceof GFPaystackTestSettingField) {
164+
throw new LogicException('set_field_error() needs a settings field object');
164165
}
166+
$field->set_error($error_message ? $error_message : 'This field is required.');
165167
}
166168

167169
public function get_settings_renderer()

‎tests/test-settings.php‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,42 @@ public function parent_callback($field, $value = null)
432432
$a = $feed_run(array('transactionType' => 'subscription'), array('api_mode' => 'test', 'test_secret_key' => $st_rk_test, 'live_secret_key' => $st_rk_live));
433433
gf_paystack_test_check($a->plans_calls === array($st_rk_test), 'feed without a mode follows the global api mode');
434434

435-
// Gravity Forms 2.5+ only records errors on field objects, so errors must never be raised with an array.
435+
// Gravity Forms 2.5+ only records errors on field objects, so every error goes through set_setting_error().
436436
$plugin_src = file_get_contents(dirname(__DIR__) . '/class-gf-paystack.php');
437-
gf_paystack_test_check(!preg_match('/set_field_error\(\s*array\(/', $plugin_src), 'no field error is raised with an array (Gravity Forms 2.5+ needs the field object)');
437+
gf_paystack_test_check(substr_count($plugin_src, '->set_field_error(') === 1, 'set_field_error is only called from set_setting_error');
438+
$names = GFPaystackTestSettingsRenderer::defined_field_names(new GFPaystackSettingsFixture());
439+
$wanted = array('api_mode', 'test_secret_key', 'test_webhook_secret', 'live_secret_key', 'live_webhook_secret', 'transactionType');
440+
gf_paystack_test_check(!array_diff($wanted, $names), 'every field an error is raised on is a defined settings field: missing ' . implode(',', array_diff($wanted, $names)));
441+
// metaData is added after Gravity Forms' own billingInformation field, which the stubs don't build.
442+
gf_paystack_test_check(preg_match("/'name'\\s*=>\\s*'metaData'/", $plugin_src) === 1, 'metaData is a defined feed field');
443+
444+
// If a field can't be found, the credential error lands on the Mode field instead of being dropped.
445+
$cred_lookup_fails = function ($posted, $stored = array()) {
446+
$addon = new GFPaystackSettingsFixture();
447+
$addon->posted = $posted;
448+
$addon->stored = $stored;
449+
$addon->renderer_fields = array();
450+
$addon->validate_credentials(new GFPaystackTestSettingField('api_mode', $addon));
451+
452+
return $addon;
453+
};
454+
$a = $cred_lookup_fails(array('api_mode' => 'test', 'test_secret_key' => $st_sk_test . 'x'));
455+
gf_paystack_test_check(strpos($a->field_errors['api_mode'] ?? '', 'Use a test restricted key instead.') === 0, 'credentials: selected-mode key error falls back to Mode when its field is missing');
456+
$a = $cred_lookup_fails(array('api_mode' => 'test', 'live_secret_key' => $st_sk_live . 'x'));
457+
gf_paystack_test_check(isset($a->field_errors['api_mode']), 'credentials: other-mode error falls back to Mode when fields are missing');
458+
$a = $cred_lookup_fails(array('api_mode' => 'test', 'test_secret_key' => $st_rk_test));
459+
gf_paystack_test_check(isset($a->field_errors['api_mode']), 'credentials: missing signing secret falls back to Mode');
460+
461+
$GLOBALS['gf_paystack_test_gf_logs'] = array();
462+
$a = new GFPaystackSettingsFixture();
463+
$a->renderer_fields = array();
464+
(function () { $this->set_setting_error('nope', 'x'); })->call($a);
465+
gf_paystack_test_check($a->field_errors === array() && strpos(json_encode($GLOBALS['gf_paystack_test_gf_logs']), 'no settings field to attach an error to (nope)') !== false, 'an error with nowhere to go is logged, not silently dropped');
466+
467+
$a = $st_run('live', array('api_mode' => 'live', 'live_secret_key' => $st_rk_live, 'live_webhook_secret' => $st_ws_live), array(), function ($addon) {
468+
$addon->verdict = array('code' => 'sensitive_scope', 'scopes' => array(), 'sensitive_scopes' => array('<img src=x>'), 'reason' => null, 'probes' => '');
469+
});
470+
gf_paystack_test_check(strpos($a->field_errors['live_secret_key'] ?? '', '&lt;img') !== false && strpos($a->field_errors['live_secret_key'], '<img') === false, 'scope labels are escaped in key errors');
438471

439472
$meta_run = function ($meta, $renderer_fields = null) {
440473
$addon = new GFPaystackSettingsFixture();

0 commit comments

Comments
 (0)