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 / ninja-forms / includes / functions.php

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

198 lines 5.2 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\Ninja_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_ninja_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_ninja_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_ninja_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 global $wpdb;
65
66 return $wpdb->get_var( $wpdb->prepare(
67 "SELECT title FROM {$wpdb->prefix}nf3_forms WHERE id = %s",
68 $form_id
69 ) );
70
71 }
72
73 /**
74 * Get form fields values
75 *
76 * @since 1.0.0
77 *
78 * @param array $data
79 *
80 * @return array
81 */
82 function automatorwp_ninja_forms_get_form_fields_values( $data ) {
83
84 $form_fields = array();
85
86 $fields = $data['fields'];
87
88 // Loop all fields
89 foreach ( $fields as $field ) {
90
91 // Excluded fields
92 if( in_array( $field['type'], array( 'captcha', 'section', 'submit' ) ) ) {
93 continue;
94 }
95
96 $field_name = $field['key'];
97 $field_value = $field['value'];
98
99 $form_fields[$field_name] = $field_value;
100 }
101
102 // Check for AutomatorWP 1.4.4
103 if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) {
104 $form_fields = automatorwp_utilities_pull_array_values( $form_fields );
105 }
106
107 return $form_fields;
108
109 }
110
111 /**
112 * Custom tags replacements
113 *
114 * @since 1.0.0
115 *
116 * @param string $parsed_content Content parsed
117 * @param array $replacements Automation replacements
118 * @param int $automation_id The automation ID
119 * @param int $user_id The user ID
120 * @param string $content The content to parse
121 *
122 * @return string
123 */
124 function automatorwp_ninja_forms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) {
125
126 $new_replacements = array();
127
128 // Get automation triggers to pass their tags
129 $triggers = automatorwp_get_automation_triggers( $automation_id );
130
131 foreach( $triggers as $trigger ) {
132
133 $trigger_args = automatorwp_get_trigger( $trigger->type );
134
135 // Skip if trigger is not from this integration
136 if( $trigger_args['integration'] !== 'ninja_forms' ) {
137 continue;
138 }
139
140 // Get the last trigger log (where data for tags replacement will be get
141 $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' );
142
143 if( ! $log ) {
144 continue;
145 }
146
147 ct_setup_table( 'automatorwp_logs' );
148 $form_fields = ct_get_object_meta( $log->id, 'form_fields', true );
149 ct_reset_setup_table();
150
151 // Skip if not form fields
152 if( ! is_array( $form_fields ) ) {
153 continue;
154 }
155
156 // Look for form field tags
157 preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
158
159 if( is_array( $matches ) && isset( $matches[1] ) ) {
160
161 foreach( $matches[1] as $field_name ) {
162 // Replace {t:ID:form_field:NAME} by the field value
163 if( isset( $form_fields[$field_name] ) ) {
164 $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
165 }
166 }
167
168 }
169
170 // Look for form field tags
171 preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
172
173 if( is_array( $matches ) && isset( $matches[1] ) ) {
174
175 foreach( $matches[1] as $field_name ) {
176 // Replace {ID:form_field:NAME} by the field value
177 if( isset( $form_fields[$field_name] ) ) {
178 $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
179 }
180 }
181
182 }
183
184 }
185
186 if( count( $new_replacements ) ) {
187
188 $tags = array_keys( $new_replacements );
189
190 // Replace all tags by their replacements
191 $parsed_content = str_replace( $tags, $new_replacements, $parsed_content );
192
193 }
194
195 return $parsed_content;
196
197 }
198 add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_ninja_forms_parse_automation_tags', 10, 5 );