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