PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
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 5.6.2 All 33 releases
automatorwp / integrations / formidable-forms / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.2, at integrations/formidable-forms/includes/functions.php

194 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\Formidable_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_formidable_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_formidable_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_formidable_forms_get_form_title( $form_id ) {
58
59 // Empty title if no ID provided
60 if( absint( $form_id ) === 0 ) {
61 return '';
62 }
63
64 $form = FrmForm::getOne( $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 FrmForm $form
76 * @param int $entry_id
77 *
78 * @return array
79 */
80 function automatorwp_formidable_forms_get_form_fields_values( $form, $entry_id ) {
81
82 $form_fields = array();
83
84 $fields = FrmFieldsHelper::get_form_fields( $form->id );
85 $entry_values = new FrmEntryValues( $entry_id );
86 $field_values = $entry_values->get_field_values();
87
88 // Loop all fields to trigger events per field value
89 foreach ( $fields as $field ) {
90
91 $field_name = $field->field_key;
92 $field_value = ( isset( $field_values[$field->id] ) ? $field_values[$field->id]->get_saved_value() : '' );
93
94 $form_fields[$field_name] = $field_value;
95
96 }
97
98 // Check for AutomatorWP 1.4.4
99 if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) {
100 $form_fields = automatorwp_utilities_pull_array_values( $form_fields );
101 }
102
103 return $form_fields;
104
105 }
106
107 /**
108 * Custom tags replacements
109 *
110 * @since 1.0.0
111 *
112 * @param string $parsed_content Content parsed
113 * @param array $replacements Automation replacements
114 * @param int $automation_id The automation ID
115 * @param int $user_id The user ID
116 * @param string $content The content to parse
117 *
118 * @return string
119 */
120 function automatorwp_formidable_forms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) {
121
122 $new_replacements = array();
123
124 // Get automation triggers to pass their tags
125 $triggers = automatorwp_get_automation_triggers( $automation_id );
126
127 foreach( $triggers as $trigger ) {
128
129 $trigger_args = automatorwp_get_trigger( $trigger->type );
130
131 // Skip if trigger is not from this integration
132 if( $trigger_args['integration'] !== 'formidable_forms' ) {
133 continue;
134 }
135
136 // Get the last trigger log (where data for tags replacement will be get
137 $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' );
138
139 if( ! $log ) {
140 continue;
141 }
142
143 ct_setup_table( 'automatorwp_logs' );
144 $form_fields = ct_get_object_meta( $log->id, 'form_fields', true );
145 ct_reset_setup_table();
146
147 // Skip if not form fields
148 if( ! is_array( $form_fields ) ) {
149 continue;
150 }
151
152 // Look for form field tags
153 preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
154
155 if( is_array( $matches ) && isset( $matches[1] ) ) {
156
157 foreach( $matches[1] as $field_name ) {
158 // Replace {t:ID:form_field:NAME} by the field value
159 if( isset( $form_fields[$field_name] ) ) {
160 $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
161 }
162 }
163
164 }
165
166 // Look for form field tags
167 preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
168
169 if( is_array( $matches ) && isset( $matches[1] ) ) {
170
171 foreach( $matches[1] as $field_name ) {
172 // Replace {ID:form_field:NAME} by the field value
173 if( isset( $form_fields[$field_name] ) ) {
174 $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
175 }
176 }
177
178 }
179
180 }
181
182 if( count( $new_replacements ) ) {
183
184 $tags = array_keys( $new_replacements );
185
186 // Replace all tags by their replacements
187 $parsed_content = str_replace( $tags, $new_replacements, $parsed_content );
188
189 }
190
191 return $parsed_content;
192
193 }
194 add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_formidable_forms_parse_automation_tags', 10, 5 );