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

Conditional Logic Feature? #34

Closed airtiming closed 9 years ago

airtiming commented 9 years ago

It would be great if there was a way to make sending off the HTTP POST dependent on a field value within a given form, such as a radio button selection. For example, one could add a 'Get Email Updates' checkbox to any form, and have it used as a Condition for external posting to the email marketing service.

Zaus (thank you!) suggested the following approach. However I am am not familiar with how to actually code this:

You could write a hook on on Forms3rdPartyIntegration_use_form and inspect the $form argument, then return false on your condition.

Or you could hook to Forms3rdPartyIntegration_service_filter_args later down and set response_bypass so it skips over the post. You'd have access to already transformed submission by then.

zaus commented 9 years ago

(from original forum post: https://wordpress.org/support/topic/conditional-logic-feature?replies=5#post-6434793)

I think something like:

add_filter('Forms3rdPartyIntegration_service_filter_args', 'forms3rdparty_conditional_send', 10, 3);

function forms3rdparty_conditional_send($post_args, $service, $form) {
    // inspect transformed submission body for presence/lack/value of the desired value
    if( !isset($post_args['body']['my-conditional-field']) ) {
        // set plugin bypass -- will treat this as though it had sent the submission; we don't want the plugin to think it failed
        $post_args['response_bypass'] = array('body' => $service['success'], 'response' => array('code' => 200)); // essentially a success placeholder
    }

    return $post_args;
}

You could even have it scan the post body for "generic triggers" (i.e. anything that had been mapped with a prefix like ##.

If you're pluginizing this solution, add a setting to tell it what field to check for and possibly what value to expect (isset vs particular value).

zaus commented 9 years ago

@airtiming Did you end up trying that? Or releasing it as a plugin?

zaus commented 9 years ago

Slightly better:

add_filter('Forms3rdPartyIntegration_service_filter_args', 'forms3rdparty_conditional_send', 10, 3);

function forms3rdparty_conditional_send($post_args, $service, $form) {
    $bypass_style = 0; // presence=1, absence=0, value=-1;

    // inspect transformed submission body for presence/lack/value of the desired value
    $bypass = $bypass_style == 0; // starting condition must match expectation
    foreach($post_args['body'] as $field => $value) {
        // anything starting with "conditional flag" and provided
        if($bypass_style == 1 && strpos($field, '??', 0) === 0 && !empty($value)) {
            $bypass = true;
            break;
        }
        // anything starting with "conditional flag" and provided THAT WE DON'T WANT
        elseif($bypass_style == 0 && strpos($field, '??', 0) === 0 && !empty($value)) {
            $bypass = false;
            break;
        }
        // anything starting with "conditional flag" and value matched
        elseif($bypass_style == -1 && strpos($field, '??', 0) === 0) {
            // extract the match from the mapping name
            $match = substr($field, 2); // strip the flag indicator

            if($value == $match) {
                $bypass = true;
                break;
            }
        }
    }

    // did we trigger a bypass already, or did we expect the absence of a value?
    if( $bypass ) {
        // set plugin bypass -- will treat this as though it had sent the submission; we don't want the plugin to think it failed
        $post_args['response_bypass'] = array('body' => $service['success'], 'response' => array('code' => 200)); // reuse expected success placeholder, set required code
    }

    return $post_args;
}

(probably need to strip out anything starting with ??)

but if other plugins have already transformed it into something else (i.e. Xpost), you'd have to use a different hook ...service_filter_post, stash the condition result, then in the above hook apply the bypass.

UPDATE fixed bypass response, was missing response/code

zaus commented 9 years ago

UPDATE fixed bypass response in previous solution(s), was missing response/code

zaus commented 9 years ago

Addressed in v1.6.4 in commit 345f6ba7dafd69995c2b5dabf2aafbb60dc20117