| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\WPForms\Functions |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get form fields values |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @param array $fields |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function automatorwp_happyforms_get_form_fields_values( $fields ) { |
| 22 |
|
| 23 |
$form_fields = array(); |
| 24 |
|
| 25 |
// Loop all fields |
| 26 |
foreach ( $fields as $field_name => $field_value ) { |
| 27 |
|
| 28 |
$form_fields[$field_name] = $field_value; |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
// Check for AutomatorWP 1.4.4 |
| 33 |
if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { |
| 34 |
$form_fields = automatorwp_utilities_pull_array_values( $form_fields ); |
| 35 |
} |
| 36 |
|
| 37 |
return $form_fields; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Custom tags replacements |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* |
| 46 |
* @param string $parsed_content Content parsed |
| 47 |
* @param array $replacements Automation replacements |
| 48 |
* @param int $automation_id The automation ID |
| 49 |
* @param int $user_id The user ID |
| 50 |
* @param string $content The content to parse |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
function automatorwp_happyforms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { |
| 55 |
|
| 56 |
$new_replacements = array(); |
| 57 |
|
| 58 |
// Get automation triggers to pass their tags |
| 59 |
$triggers = automatorwp_get_automation_triggers( $automation_id ); |
| 60 |
|
| 61 |
foreach( $triggers as $trigger ) { |
| 62 |
|
| 63 |
$trigger_args = automatorwp_get_trigger( $trigger->type ); |
| 64 |
|
| 65 |
// Skip if trigger is not from this integration |
| 66 |
if( $trigger_args['integration'] !== 'happyforms' ) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
// Get the last trigger log (where data for tags replacement will be get |
| 71 |
$log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); |
| 72 |
|
| 73 |
if( ! $log ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
ct_setup_table( 'automatorwp_logs' ); |
| 78 |
$form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); |
| 79 |
ct_reset_setup_table(); |
| 80 |
|
| 81 |
// Skip if not form fields |
| 82 |
if( ! is_array( $form_fields ) ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
// Look for form field tags |
| 87 |
preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 88 |
|
| 89 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 90 |
|
| 91 |
foreach( $matches[1] as $field_name ) { |
| 92 |
// Replace {t:ID:form_field:NAME} by the field value |
| 93 |
if( isset( $form_fields[$field_name] ) ) { |
| 94 |
$new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
// Look for form field tags |
| 101 |
preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 102 |
|
| 103 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 104 |
|
| 105 |
foreach( $matches[1] as $field_name ) { |
| 106 |
// Replace {ID:form_field:NAME} by the field value |
| 107 |
if( isset( $form_fields[$field_name] ) ) { |
| 108 |
$new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
if( count( $new_replacements ) ) { |
| 117 |
|
| 118 |
$tags = array_keys( $new_replacements ); |
| 119 |
|
| 120 |
// Replace all tags by their replacements |
| 121 |
$parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
return $parsed_content; |
| 126 |
|
| 127 |
} |
| 128 |
add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_happyforms_parse_automation_tags', 10, 5 ); |