| @@ -1,0 +1,206 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\WS_Form\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_ws_form_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_ws_form_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_ws_form_get_form_title( $form_id ) { | |
| 58 | + | |
| 59 | + // Empty title if no ID provided | |
| 60 | + if( absint( $form_id ) === 0 ) { | |
| 61 | + return ''; | |
| 62 | + } | |
| 63 | + | |
| 64 | + return wsf_form_get_form_label( $form_id ); | |
| 65 | + | |
| 66 | +} | |
| 67 | + | |
| 68 | +/** | |
| 69 | + * Get form fields values | |
| 70 | + * | |
| 71 | + * @since 1.0.0 | |
| 72 | + * | |
| 73 | + * @param array $lead | |
| 74 | + * @param array $form | |
| 75 | + * | |
| 76 | + * @return array | |
| 77 | + */ | |
| 78 | +function automatorwp_ws_form_get_form_fields_values( $ws_form_submit, $fields, $field_types ) { | |
| 79 | + | |
| 80 | + $form_fields = array(); | |
| 81 | + | |
| 82 | + // Loop all fields to trigger events per field value | |
| 83 | + foreach ( $fields as $field ) { | |
| 84 | + | |
| 85 | + // Get field type | |
| 86 | + if( ! isset( $field->type ) ) { | |
| 87 | + continue; | |
| 88 | + } | |
| 89 | + $field_type = $field->type; | |
| 90 | + | |
| 91 | + // Check field type for submit_save | |
| 92 | + if( ! isset( $field_types[$field_type] ) ) { | |
| 93 | + continue; | |
| 94 | + } | |
| 95 | + $field_type_config = $field_types[$field_type]; | |
| 96 | + $submit_save = isset( $field_type_config['submit_save'] ) ? $field_type_config['submit_save'] : false; | |
| 97 | + | |
| 98 | + // Excluded fields | |
| 99 | + if( ! $submit_save ) { | |
| 100 | + continue; | |
| 101 | + } | |
| 102 | + | |
| 103 | + $field_name = $field->id; | |
| 104 | + $field_value = wsf_submit_get_value( $ws_form_submit, WS_FORM_FIELD_PREFIX . $field_name ); | |
| 105 | + | |
| 106 | + $form_fields[$field_name] = $field_value; | |
| 107 | + | |
| 108 | + } | |
| 109 | + | |
| 110 | + // Check for AutomatorWP 1.4.4 | |
| 111 | + if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { | |
| 112 | + $form_fields = automatorwp_utilities_pull_array_values( $form_fields ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + return $form_fields; | |
| 116 | + | |
| 117 | +} | |
| 118 | + | |
| 119 | +/** | |
| 120 | + * Custom tags replacements | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + * | |
| 124 | + * @param string $parsed_content Content parsed | |
| 125 | + * @param array $replacements Automation replacements | |
| 126 | + * @param int $automation_id The automation ID | |
| 127 | + * @param int $user_id The user ID | |
| 128 | + * @param string $content The content to parse | |
| 129 | + * | |
| 130 | + * @return string | |
| 131 | + */ | |
| 132 | +function automatorwp_ws_form_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { | |
| 133 | + | |
| 134 | + $new_replacements = array(); | |
| 135 | + | |
| 136 | + // Get automation triggers to pass their tags | |
| 137 | + $triggers = automatorwp_get_automation_triggers( $automation_id ); | |
| 138 | + | |
| 139 | + foreach( $triggers as $trigger ) { | |
| 140 | + | |
| 141 | + $trigger_args = automatorwp_get_trigger( $trigger->type ); | |
| 142 | + | |
| 143 | + // Skip if trigger is not from this integration | |
| 144 | + if( $trigger_args['integration'] !== 'ws_form' ) { | |
| 145 | + continue; | |
| 146 | + } | |
| 147 | + | |
| 148 | + // Get the last trigger log (where data for tags replacement will be get | |
| 149 | + $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); | |
| 150 | + | |
| 151 | + if( ! $log ) { | |
| 152 | + continue; | |
| 153 | + } | |
| 154 | + | |
| 155 | + ct_setup_table( 'automatorwp_logs' ); | |
| 156 | + $form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); | |
| 157 | + ct_reset_setup_table(); | |
| 158 | + | |
| 159 | + // Skip if not form fields | |
| 160 | + if( ! is_array( $form_fields ) ) { | |
| 161 | + continue; | |
| 162 | + } | |
| 163 | + | |
| 164 | + // Look for form field tags | |
| 165 | + preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); | |
| 166 | + | |
| 167 | + if( is_array( $matches ) && isset( $matches[1] ) ) { | |
| 168 | + | |
| 169 | + foreach( $matches[1] as $field_name ) { | |
| 170 | + // Replace {t:ID:form_field:NAME} by the field value | |
| 171 | + if( isset( $form_fields[$field_name] ) ) { | |
| 172 | + $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; | |
| 173 | + } | |
| 174 | + } | |
| 175 | + | |
| 176 | + } | |
| 177 | + | |
| 178 | + // Look for form field tags | |
| 179 | + preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); | |
| 180 | + | |
| 181 | + if( is_array( $matches ) && isset( $matches[1] ) ) { | |
| 182 | + | |
| 183 | + foreach( $matches[1] as $field_name ) { | |
| 184 | + // Replace {ID:form_field:NAME} by the field value | |
| 185 | + if( isset( $form_fields[$field_name] ) ) { | |
| 186 | + $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + } | |
| 191 | + | |
| 192 | + } | |
| 193 | + | |
| 194 | + if( count( $new_replacements ) ) { | |
| 195 | + | |
| 196 | + $tags = array_keys( $new_replacements ); | |
| 197 | + | |
| 198 | + // Replace all tags by their replacements | |
| 199 | + $parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); | |
| 200 | + | |
| 201 | + } | |
| 202 | + | |
| 203 | + return $parsed_content; | |
| 204 | + | |
| 205 | +} | |
| 206 | +add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_ws_form_parse_automation_tags', 10, 5 ); | |