PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.6
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.6
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
automatorwp / integrations / kadence-blocks / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.6, at integrations/kadence-blocks/includes/functions.php

242 lines 6.4 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\Kadence_Blocks\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_kadence_blocks_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_kadence_blocks_get_form_title( $form_id );
41 }
42 }
43
44 return $options;
45
46 }
47
48 /**
49 * Get all forms
50 *
51 * @since 1.0.0
52 *
53 * @return string|null
54 */
55 function automatorwp_kadence_blocks_get_forms( ) {
56
57 global $wpdb;
58
59 $all_forms = array();
60
61 $forms = $wpdb->get_results("select id,post_title,post_content from {$wpdb->posts} where post_content like '%<!-- wp:kadence/form%' and post_status = 'publish'");
62
63 foreach ($forms as $post) {
64 $form_id = $post->id;
65 $form_title = $post->post_title;
66 $form_content = $post->post_content;
67
68 $contentArray = explode('<!--', $form_content);
69 $content = [];
70
71 foreach ($contentArray as $key => $value) {
72 if (str_contains($value, ' wp:kadence/form')) {
73 $temp = str_replace(' wp:kadence/form', '', $value);
74 $temp1 = explode('-->', $temp, 2);
75 $content[] = json_decode($temp1[0]);
76 }
77 }
78
79 if (is_array($content)) {
80 foreach ($content as $form) {
81 $parent_id = $form->postID;
82 $unique_id = $form->uniqueID;
83 $all_forms[] = array(
84 'id' => $unique_id,
85 'name' => $form_title . '_' . $unique_id,
86 );
87 }
88 }
89 }
90
91 return $all_forms;
92
93 }
94
95 /**
96 * Get the form title
97 *
98 * @since 1.0.0
99 *
100 * @param int $form_id
101 *
102 * @return string|null
103 */
104 function automatorwp_kadence_blocks_get_form_title( $form_id ) {
105
106 // Empty title if no ID provided
107 if( empty( $form_id ) ) {
108 return '';
109 }
110
111 $complete_form_id = explode( '_', $form_id );
112 $post_title = get_the_title( $complete_form_id[0] );
113
114 return $post_title . '_' . $form_id;
115
116 }
117
118 /**
119 * Get form fields values
120 *
121 * @since 1.0.0
122 *
123 * @param array $fields
124 *
125 * @return array
126 */
127 function automatorwp_kadence_blocks_get_form_fields_values( $fields ) {
128
129 $form_fields = array();
130
131 // Loop all fields
132 foreach ( $fields as $field_name => $field_value ) {
133
134 if( is_array( $field_value ) ) {
135
136 $field_name = $field_value['label'];
137 $value = ( isset( $field_value['value'] ) ? $field_value['value'] : '' );
138
139 $form_fields[$field_name] = $value;
140
141 }
142 }
143
144 // Check for AutomatorWP 1.4.4
145 if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) {
146 $form_fields = automatorwp_utilities_pull_array_values( $form_fields );
147 }
148
149 return $form_fields;
150
151 }
152
153 /**
154 * Custom tags replacements
155 *
156 * @since 1.0.0
157 *
158 * @param string $parsed_content Content parsed
159 * @param array $replacements Automation replacements
160 * @param int $automation_id The automation ID
161 * @param int $user_id The user ID
162 * @param string $content The content to parse
163 *
164 * @return string
165 */
166 function automatorwp_kadence_blocks_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) {
167
168 $new_replacements = array();
169
170 // Get automation triggers to pass their tags
171 $triggers = automatorwp_get_automation_triggers( $automation_id );
172
173 foreach( $triggers as $trigger ) {
174
175 $trigger_args = automatorwp_get_trigger( $trigger->type );
176
177 // Skip if trigger is not from this integration
178 if( $trigger_args['integration'] !== 'kadence_blocks' ) {
179 continue;
180 }
181
182 // Get the last trigger log (where data for tags replacement will be get
183 $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' );
184
185 if( ! $log ) {
186 continue;
187 }
188
189 ct_setup_table( 'automatorwp_logs' );
190 $form_fields = ct_get_object_meta( $log->id, 'form_fields', true );
191 ct_reset_setup_table();
192
193 // Skip if not form fields
194 if( ! is_array( $form_fields ) ) {
195 continue;
196 }
197
198 // Look for form field tags
199 preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
200
201 if( is_array( $matches ) && isset( $matches[1] ) ) {
202
203 foreach( $matches[1] as $field_name ) {
204 // Replace {t:ID:form_field:NAME} by the field value
205 if( isset( $form_fields[$field_name] ) ) {
206 $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
207 }
208 }
209
210 }
211
212 // Look for form field tags
213 preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
214
215 if( is_array( $matches ) && isset( $matches[1] ) ) {
216
217 foreach( $matches[1] as $field_name ) {
218 // Replace {ID:form_field:NAME} by the field value
219 if( isset( $form_fields[$field_name] ) ) {
220 $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
221 }
222 }
223
224 }
225
226 }
227
228 if( count( $new_replacements ) ) {
229
230 $tags = array_keys( $new_replacements );
231
232 // Replace all tags by their replacements
233 $parsed_content = str_replace( $tags, $new_replacements, $parsed_content );
234
235 }
236
237 return $parsed_content;
238
239 }
240 add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_kadence_blocks_parse_automation_tags', 10, 5 );
241
242