PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | integrations/caldera-forms/includes/functions.php +193 -0 5.7.5 → 6.0.3 View file →
@@ -1,0 +1,193 @@
1 +<?php
2 +/**
3 + * Functions
4 + *
5 + * @package AutomatorWP\Caldera_Forms\Functions
6 + * @since 1.0.0
7 + */
8 +// Exit if accessed directly
9 +if( !defined( 'ABSPATH' ) ) exit;
10 +
11 +/**
12 + * Options callback for select2 fields assigned to forms
13 + *
14 + * @since 1.0.0
15 + *
16 + * @param stdClass $field
17 + *
18 + * @return array
19 + */
20 +function automatorwp_caldera_forms_options_cb_form( $field ) {
21 +
22 + // Setup vars
23 + $value = $field->escaped_value;
24 + $none_value = 'any';
25 + $none_label = __( 'any form', 'automatorwp' );
26 + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
27 +
28 + if( ! empty( $value ) ) {
29 + if( ! is_array( $value ) ) {
30 + $value = array( $value );
31 + }
32 +
33 + foreach( $value as $form_id ) {
34 +
35 + // Skip option none
36 + if( $form_id === $none_value ) {
37 + continue;
38 + }
39 +
40 + $options[$form_id] = automatorwp_caldera_forms_get_form_title( $form_id );
41 + }
42 + }
43 +
44 + return $options;
45 +
46 +}
47 +
48 +/**
49 + * Get the form title
50 + *
51 + * @since 1.0.0
52 + *
53 + * @param int $form_id
54 + *
55 + * @return string|null
56 + */
57 +function automatorwp_caldera_forms_get_form_title( $form_id ) {
58 +
59 + // Empty title if no ID provided
60 + if( empty( $form_id ) ) {
61 + return '';
62 + }
63 +
64 + $form = Caldera_Forms::get_form( $form_id );
65 +
66 + return $form['name'];
67 +
68 +}
69 +
70 +/**
71 + * Get form fields values
72 + *
73 + * @since 1.0.0
74 + *
75 + * @param array $form
76 + * @param int $entry_id
77 + *
78 + * @return array
79 + */
80 +function automatorwp_caldera_forms_get_form_fields_values( $form, $entry_id ) {
81 +
82 + $form_fields = array();
83 +
84 + $form_id = $form['ID']; // Caldera Forms ID can be strings
85 + $form = Caldera_Forms_Forms::get_form( $form_id );
86 + $fields = $form['fields'];
87 +
88 + // Loop all fields to trigger events per field value
89 + foreach ( $fields as $field_id => $field ) {
90 +
91 + $field_name = $field_id;
92 + $field_value = Caldera_Forms::get_field_data( $field_id, $form, $entry_id );
93 +
94 + $form_fields[$field_name] = $field_value;
95 + }
96 +
97 + // Check for AutomatorWP 1.4.4
98 + if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) {
99 + $form_fields = automatorwp_utilities_pull_array_values( $form_fields );
100 + }
101 +
102 + return $form_fields;
103 +
104 +}
105 +
106 +/**
107 + * Custom tags replacements
108 + *
109 + * @since 1.0.0
110 + *
111 + * @param string $parsed_content Content parsed
112 + * @param array $replacements Automation replacements
113 + * @param int $automation_id The automation ID
114 + * @param int $user_id The user ID
115 + * @param string $content The content to parse
116 + *
117 + * @return string
118 + */
119 +function automatorwp_caldera_forms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) {
120 +
121 + $new_replacements = array();
122 +
123 + // Get automation triggers to pass their tags
124 + $triggers = automatorwp_get_automation_triggers( $automation_id );
125 +
126 + foreach( $triggers as $trigger ) {
127 +
128 + $trigger_args = automatorwp_get_trigger( $trigger->type );
129 +
130 + // Skip if trigger is not from this integration
131 + if( $trigger_args['integration'] !== 'caldera_forms' ) {
132 + continue;
133 + }
134 +
135 + // Get the last trigger log (where data for tags replacement will be get
136 + $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' );
137 +
138 + if( ! $log ) {
139 + continue;
140 + }
141 +
142 + ct_setup_table( 'automatorwp_logs' );
143 + $form_fields = ct_get_object_meta( $log->id, 'form_fields', true );
144 + ct_reset_setup_table();
145 +
146 + // Skip if not form fields
147 + if( ! is_array( $form_fields ) ) {
148 + continue;
149 + }
150 +
151 + // Look for form field tags
152 + preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
153 +
154 + if( is_array( $matches ) && isset( $matches[1] ) ) {
155 +
156 + foreach( $matches[1] as $field_name ) {
157 + // Replace {t:ID:form_field:NAME} by the field value
158 + if( isset( $form_fields[$field_name] ) ) {
159 + $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
160 + }
161 + }
162 +
163 + }
164 +
165 + // Look for form field tags
166 + preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
167 +
168 + if( is_array( $matches ) && isset( $matches[1] ) ) {
169 +
170 + foreach( $matches[1] as $field_name ) {
171 + // Replace {ID:form_field:NAME} by the field value
172 + if( isset( $form_fields[$field_name] ) ) {
173 + $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
174 + }
175 + }
176 +
177 + }
178 +
179 + }
180 +
181 + if( count( $new_replacements ) ) {
182 +
183 + $tags = array_keys( $new_replacements );
184 +
185 + // Replace all tags by their replacements
186 + $parsed_content = str_replace( $tags, $new_replacements, $parsed_content );
187 +
188 + }
189 +
190 + return $parsed_content;
191 +
192 +}
193 +add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_caldera_forms_parse_automation_tags', 10, 5 );