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