Addon's "update_plugin_settings" called twice for custom type

I’m trying to encrypt one addon configuration field whose purpose is as an API key. Based on this $field definition:

array(
                'name'                => 'apikeyStatus',
                'type'                => 'apikey',
                'save_callback'       => array($this, 'update_apikey'),
            ),

I have this function:

public function update_apikey($field, $field_value)
    {
<field value duplicate check logic snipped>
        if (\GFCommon::current_user_can_uninstall()) {
            $settings[self::SDK_API_KEY] = encrypt(rgar($settings, self::SDK_API_KEY));
            $this->update_plugin_settings($settings);
        }
}

Imagine my surprise when the database value reflects the unencrypted value.
It turns out that there’s a double write

[24-Jun-2024 16:51:51 UTC] UPDATE `wp_options` SET `option_value` = 'a:2:{s:25:\"sdk_app_name\";s:5:\"sd108\";s:24:\"sdk_api_key\";s:95:\"AK*EBn+RxTw1MYSV1GAtXfL7HKMDnJHURClmudsiQCuvNP1sxIumt0cuTfGFSc+V5pCH9LI72spTCqz3Of6H9iyzw7B\";}' WHERE `option_name` = 'gravityformsaddon_crm_settings' - (0.0029609203338623 s)
[24-Jun-2024 16:51:51 UTC] UPDATE `wp_options` SET `option_value` = 'a:4:{s:25:\"sdk_app_name\";s:5:\"sd108\";s:24:\"sdk_api_key\";s:57:\"AK-41e0556ea0ba1d03ce736e739c33cccb78683320eb011e563c\";s:12:\"apikeyStatus\";N;s:11:\"clear_cache\";s:0:\"\";}' WHERE `option_name` = 'gravityformsaddon_crm_settings' - (0.0027480125427246 s)

what’s causing the 2nd write?

tia,

The function hooked to save_callback should only return the value to be saved, so you should remove the update_plugin_settings() call. The add-on framework already handles calling update_plugin_settings() after it’s collected all the values to be saved.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.