| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\weForms\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 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_weforms_get_form_fields_values( $form, $entry_id ) { |
| 22 |
|
| 23 |
$form_fields = array(); |
| 24 |
|
| 25 |
$obj_form = new weForms_Form($form); |
| 26 |
$obj_entry = new weForms_Form_Entry($entry_id, $obj_form); |
| 27 |
$name_fields = $obj_entry->get_form( $entry_id )->get_field_values(); |
| 28 |
|
| 29 |
|
| 30 |
// Loop all fields |
| 31 |
foreach ( $name_fields as $field_name => $value ) { |
| 32 |
|
| 33 |
if( ( isset( $value['template'] ) && $value['template'] === 'name_field' ) |
| 34 |
|| ( isset( $value['template'] ) && $value['template'] === 'checkbox_field' ) ) { |
| 35 |
|
| 36 |
$field_value = weforms_get_entry_meta( $entry_id, $field_name, true ); |
| 37 |
$field_value = explode( '| ', $field_value ); |
| 38 |
$form_fields[$field_name] = $field_value; |
| 39 |
|
| 40 |
( $value['template'] === 'name_field' ) && $form_fields['full_name'] = implode(' ', $field_value ); |
| 41 |
|
| 42 |
}else { |
| 43 |
$field_value = weforms_get_entry_meta( $entry_id, $field_name, true ); |
| 44 |
$form_fields[$field_name] = $field_value; |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { |
| 51 |
$form_fields = automatorwp_utilities_pull_array_values( $form_fields ); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
return $form_fields; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Custom tags replacements |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @param string $parsed_content Content parsed |
| 65 |
* @param array $replacements Automation replacements |
| 66 |
* @param int $automation_id The automation ID |
| 67 |
* @param int $user_id The user ID |
| 68 |
* @param string $content The content to parse |
| 69 |
* |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
function automatorwp_weforms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { |
| 73 |
|
| 74 |
$new_replacements = array(); |
| 75 |
|
| 76 |
// Get automation triggers to pass their tags |
| 77 |
$triggers = automatorwp_get_automation_triggers( $automation_id ); |
| 78 |
|
| 79 |
foreach( $triggers as $trigger ) { |
| 80 |
|
| 81 |
$trigger_args = automatorwp_get_trigger( $trigger->type ); |
| 82 |
|
| 83 |
// Skip if trigger is not from this integration |
| 84 |
if( $trigger_args['integration'] !== 'weforms' ) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
// Get the last trigger log (where data for tags replacement will be get |
| 89 |
$log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); |
| 90 |
|
| 91 |
if( ! $log ) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
ct_setup_table( 'automatorwp_logs' ); |
| 96 |
$form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); |
| 97 |
ct_reset_setup_table(); |
| 98 |
|
| 99 |
// Skip if not form fields |
| 100 |
if( ! is_array( $form_fields ) ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
// Look for form field tags |
| 105 |
preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 106 |
|
| 107 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 108 |
|
| 109 |
foreach( $matches[1] as $field_name ) { |
| 110 |
// Replace {t:ID:form_field:NAME} by the field value |
| 111 |
if( isset( $form_fields[$field_name] ) ) { |
| 112 |
$new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
// Look for form field tags |
| 119 |
preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 120 |
|
| 121 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 122 |
|
| 123 |
foreach( $matches[1] as $field_name ) { |
| 124 |
// Replace {ID:form_field:NAME} by the field value |
| 125 |
if( isset( $form_fields[$field_name] ) ) { |
| 126 |
$new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
if( count( $new_replacements ) ) { |
| 135 |
|
| 136 |
$tags = array_keys( $new_replacements ); |
| 137 |
|
| 138 |
// Replace all tags by their replacements |
| 139 |
$parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
return $parsed_content; |
| 144 |
|
| 145 |
} |
| 146 |
add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_weforms_parse_automation_tags', 10, 5 ); |