zaus / forms-3rdparty-integration

Wordpress Plugin - Forms 3rdparty Integration - a plugin to help integrate 3rdparty services with common WP Forms plugins like Contact Form 7 and Gravity Forms
http://wordpress.org/plugins/forms-3rdparty-integration/
47 stars 14 forks source link

Specify numeric values instead of string? #64

Closed ismdiego closed 8 years ago

ismdiego commented 8 years ago

Really nice plugin!

However, I am stuck with a problem while using this along with the xpost addon. I need to pass a numeric value (fixed, required) to a JSON API. It is failing because it receives the numeric value as a string.

Example mapping: Isvalue=YES label=FD-Priority formSubmissionField=2 (fixed numeric value) 3rd-party field=priority

And the submission array is: [timeout] => 10 [body] => { "priority":"1"} [headers] => Array ( [Content-Type] => application/json [Authorization] => Basic <-- omitted, but working --> )

When data arrives at xpost, the "1" is already passed as a string, and so encoded like that (with ""). So I think this must be changed somewhat in the submission array before passing it to xpost.

BTW, the 3rd-party service I am using is FreshDesk JSON API (other required fields omitted for clarity, but they are also included in the post).

Many thanks for the help

zaus commented 8 years ago

You'll want hook number 4 (see source here) to scan the $post values and convert eligible values to numbers.

It might be better to hook even earlier to fix the submission itself, although it's technically unnecessary processing if the 'use_form' check results in false. Either way the callback will be the same.

something like:

// super low priority in case other hooks doing something
// could also hook to `get_submission`
add_filter('Forms3rdPartyIntegration_service_filter_post'), 'f3p_fix_submission_numbers, 50, 1);

function f3p_fix_submission_numbers($post) {
    foreach($post as &$v) {
        if (is_numeric($v)) $v = $v + 0;
        // recurse for lists
        elseif (is_array($v)) $v = f3p_fix_submission_numbers($v);
    }
    return $post;
}
zaus commented 8 years ago

I think this only applies to JSON posts (via addon https://github.com/zaus/forms-3rdparty-xpost/), as regular FORM/url (or even XML) submissions don't notice variable type, so marking as 'wontfix' since it's not necessary for general functionality, and the above hook should "fix" it.