| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package AutomatorWP\Gravity_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_gravity_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_gravity_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_gravity_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 = RGFormsModel::get_form( $form_id ); |
| 65 |
|
| 66 |
return $form->title; |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get form fields values |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param array $lead |
| 76 |
* @param array $form |
| 77 |
* |
| 78 |
* @return array |
| 79 |
*/ |
| 80 |
function automatorwp_gravity_forms_get_form_fields_values( $lead, $form ) { |
| 81 |
|
| 82 |
$form_fields = array(); |
| 83 |
|
| 84 |
$fields = $form['fields']; |
| 85 |
|
| 86 |
// Loop all fields to trigger events per field value |
| 87 |
foreach ( $fields as $field ) { |
| 88 |
|
| 89 |
// Excluded fields |
| 90 |
if( in_array( $field->type, array( 'captcha', 'section' ) ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
$field_name = $field->id; |
| 95 |
$field_value = GFFormsModel::get_lead_field_value( $lead, $field ); |
| 96 |
|
| 97 |
// Turn serialized values into array |
| 98 |
if ( $field->type === 'list' ) { |
| 99 |
|
| 100 |
// On list fields is required to unserialize |
| 101 |
$field_value = maybe_unserialize( $field_value ); |
| 102 |
|
| 103 |
} else if ( $field->type === 'name' ) { |
| 104 |
|
| 105 |
$new_value = array(); |
| 106 |
|
| 107 |
foreach( $field_value as $value ) { |
| 108 |
if( ! empty( $value ) ) { |
| 109 |
$new_value[] = $value; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$field_value = $new_value; |
| 114 |
|
| 115 |
} else if ( $field->type === 'multiselect' ) { |
| 116 |
|
| 117 |
// Multiselect value is a json |
| 118 |
$field_value = json_decode( $field_value, true ); |
| 119 |
|
| 120 |
} else if ( $field->type === 'checkbox' ) { |
| 121 |
|
| 122 |
// On checkboxes, values are stored on {field_id}.{choice_number} |
| 123 |
$field_value = array(); |
| 124 |
$choice_number = 1; |
| 125 |
|
| 126 |
foreach ( $field->choices as $choice ) { |
| 127 |
$value = ( isset( $lead[$field->id . '.' . $choice_number] ) ? $lead[$field->id . '.' . $choice_number] : '' ); |
| 128 |
|
| 129 |
// Not checked options are empty |
| 130 |
if( ! empty( $value ) ) |
| 131 |
$field_value[] = $value; |
| 132 |
|
| 133 |
$choice_number++; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
// Turn indexes like 4.1, into 1 |
| 139 |
if( is_array( $field_value ) ) { |
| 140 |
$new_value = array(); |
| 141 |
|
| 142 |
// Remove the main keys |
| 143 |
foreach( $field_value as $key => $value ) { |
| 144 |
if( strpos( $key, '.' ) !== false ) { |
| 145 |
$keys = explode( '.', $key ); |
| 146 |
unset( $keys[0] ); |
| 147 |
|
| 148 |
$new_value[implode('/', $keys)] = $value; |
| 149 |
} else { |
| 150 |
$new_value[$key] = $value; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$field_value = $new_value; |
| 155 |
} |
| 156 |
|
| 157 |
$form_fields[$field_name] = $field_value; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
// Check for AutomatorWP 1.4.4 |
| 162 |
if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { |
| 163 |
$form_fields = automatorwp_utilities_pull_array_values( $form_fields ); |
| 164 |
} |
| 165 |
|
| 166 |
return $form_fields; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Custom tags replacements |
| 172 |
* |
| 173 |
* @since 1.0.0 |
| 174 |
* |
| 175 |
* @param string $parsed_content Content parsed |
| 176 |
* @param array $replacements Automation replacements |
| 177 |
* @param int $automation_id The automation ID |
| 178 |
* @param int $user_id The user ID |
| 179 |
* @param string $content The content to parse |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
function automatorwp_gravity_forms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { |
| 184 |
|
| 185 |
$new_replacements = array(); |
| 186 |
|
| 187 |
// Get automation triggers to pass their tags |
| 188 |
$triggers = automatorwp_get_automation_triggers( $automation_id ); |
| 189 |
|
| 190 |
foreach( $triggers as $trigger ) { |
| 191 |
|
| 192 |
$trigger_args = automatorwp_get_trigger( $trigger->type ); |
| 193 |
|
| 194 |
// Skip if trigger is not from this integration |
| 195 |
if( $trigger_args['integration'] !== 'gravity_forms' ) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
// Get the last trigger log (where data for tags replacement will be get |
| 200 |
$log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); |
| 201 |
|
| 202 |
if( ! $log ) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
ct_setup_table( 'automatorwp_logs' ); |
| 207 |
$form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); |
| 208 |
ct_reset_setup_table(); |
| 209 |
|
| 210 |
// Skip if not form fields |
| 211 |
if( ! is_array( $form_fields ) ) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
// Look for form field tags |
| 216 |
preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 217 |
|
| 218 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 219 |
|
| 220 |
foreach( $matches[1] as $field_name ) { |
| 221 |
// Replace {t:ID:form_field:NAME} by the field value |
| 222 |
if( isset( $form_fields[$field_name] ) ) { |
| 223 |
$new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
// Look for form field tags |
| 230 |
preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); |
| 231 |
|
| 232 |
if( is_array( $matches ) && isset( $matches[1] ) ) { |
| 233 |
|
| 234 |
foreach( $matches[1] as $field_name ) { |
| 235 |
// Replace {ID:form_field:NAME} by the field value |
| 236 |
if( isset( $form_fields[$field_name] ) ) { |
| 237 |
$new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
if( count( $new_replacements ) ) { |
| 246 |
|
| 247 |
$tags = array_keys( $new_replacements ); |
| 248 |
|
| 249 |
// Replace all tags by their replacements |
| 250 |
$parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
return $parsed_content; |
| 255 |
|
| 256 |
} |
| 257 |
add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_gravity_forms_parse_automation_tags', 10, 5 ); |