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

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

139 lines 4.0 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\MetForm\Functions
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Get form fields values
13 *
14 * @since 1.0.0
15 *
16 * @param array $fields
17 *
18 * @return array
19 */
20 function automatorwp_metform_get_form_fields_values( $fields ) {
21
22 $form_fields = array();
23
24 // Loop all fields
25 foreach ( $fields as $field_name => $field_value ) {
26
27 if( is_array( $field_value ) ) {
28
29 // Loop all subfields
30 foreach ( $field_value as $subfield_name => $subfield_value ) {
31 if( is_string( $subfield_name ) ) {
32 $form_fields[$subfield_name] = $subfield_value;
33 }
34 }
35
36 } else {
37 $form_fields[$field_name] = $field_value;
38 }
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 function automatorwp_metform_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) {
66
67 $new_replacements = array();
68
69 // Get automation triggers to pass their tags
70 $triggers = automatorwp_get_automation_triggers( $automation_id );
71
72 foreach( $triggers as $trigger ) {
73
74 $trigger_args = automatorwp_get_trigger( $trigger->type );
75
76 // Skip if trigger is not from this integration
77 if( $trigger_args['integration'] !== 'metform' ) {
78 continue;
79 }
80
81 // Get the last trigger log (where data for tags replacement will be get
82 $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' );
83
84 if( ! $log ) {
85 continue;
86 }
87
88 ct_setup_table( 'automatorwp_logs' );
89 $form_fields = ct_get_object_meta( $log->id, 'form_fields', true );
90 ct_reset_setup_table();
91
92 // Skip if not form fields
93 if( ! is_array( $form_fields ) ) {
94 continue;
95 }
96
97 // Look for form field tags
98 preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
99
100 if( is_array( $matches ) && isset( $matches[1] ) ) {
101
102 foreach( $matches[1] as $field_name ) {
103 // Replace {t:ID:form_field:NAME} by the field value
104 if( isset( $form_fields[$field_name] ) ) {
105 $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
106 }
107 }
108
109 }
110
111 // Look for form field tags
112 preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches );
113
114 if( is_array( $matches ) && isset( $matches[1] ) ) {
115
116 foreach( $matches[1] as $field_name ) {
117 // Replace {ID:form_field:NAME} by the field value
118 if( isset( $form_fields[$field_name] ) ) {
119 $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name];
120 }
121 }
122
123 }
124
125 }
126
127 if( count( $new_replacements ) ) {
128
129 $tags = array_keys( $new_replacements );
130
131 // Replace all tags by their replacements
132 $parsed_content = str_replace( $tags, $new_replacements, $parsed_content );
133
134 }
135
136 return $parsed_content;
137
138 }
139 add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_metform_parse_automation_tags', 10, 5 );