Conditional Logic Feature?
Closed this issue · 5 comments
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.
(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).
@airtiming Did you end up trying that? Or releasing it as a plugin?
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
UPDATE fixed bypass response in previous solution(s), was missing response/code