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