| @@ -1,0 +1,276 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\ARForms\Functions | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Get plugin version | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @return string | |
| 18 | + */ | |
| 19 | +function automatorwp_arforms_get_plugin_version() { | |
| 20 | + | |
| 21 | + global $wpdb; | |
| 22 | + | |
| 23 | + // Check installed versions | |
| 24 | + if( is_plugin_active( 'arforms-form-builder/arforms-form-builder.php' ) && is_plugin_active( 'arforms/arforms.php' ) ) { | |
| 25 | + $version = "pro"; | |
| 26 | + } elseif( !is_plugin_active( 'arforms-form-builder/arforms-form-builder.php' ) && is_plugin_active( 'arforms/arforms.php' ) ) { | |
| 27 | + $version = "only_pro"; | |
| 28 | + } | |
| 29 | + else { | |
| 30 | + $version = "lite"; | |
| 31 | + } | |
| 32 | + | |
| 33 | + return $version; | |
| 34 | + | |
| 35 | +} | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Get forms from ARForm | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + * | |
| 42 | + * @param stdClass $field | |
| 43 | + * | |
| 44 | + * @return array | |
| 45 | + */ | |
| 46 | +function automatorwp_arforms_options_cb_form( $field ) { | |
| 47 | + | |
| 48 | + // Setup vars | |
| 49 | + $value = $field->escaped_value; | |
| 50 | + $none_value = 'any'; | |
| 51 | + $none_label = __( 'any form', 'automatorwp-ameliabooking' ); | |
| 52 | + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label ); | |
| 53 | + | |
| 54 | + if( ! empty( $value ) ) { | |
| 55 | + if( ! is_array( $value ) ) { | |
| 56 | + $value = array( $value ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + foreach( $value as $form_id ) { | |
| 60 | + | |
| 61 | + // Skip option none | |
| 62 | + if( $form_id === $none_value ) { | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + | |
| 66 | + $options[$form_id] = automatorwp_arforms_get_form_name( $form_id ); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 70 | + return $options; | |
| 71 | + | |
| 72 | +} | |
| 73 | + | |
| 74 | +/** | |
| 75 | + * Get forms | |
| 76 | + * | |
| 77 | + * @since 1.0.0 | |
| 78 | + * | |
| 79 | + * @return array | |
| 80 | + */ | |
| 81 | +function automatorwp_arforms_get_forms( ) { | |
| 82 | + | |
| 83 | + global $wpdb; | |
| 84 | + | |
| 85 | + $all_forms = array(); | |
| 86 | + | |
| 87 | + $version = automatorwp_arforms_get_plugin_version(); | |
| 88 | + | |
| 89 | + // Check installed versions to get table | |
| 90 | + if( $version === "pro" ) { | |
| 91 | + $table_name = "{$wpdb->prefix}arf_forms"; | |
| 92 | + $sql_query = $wpdb->prepare( "SELECT id, name FROM {$table_name} WHERE is_template = 0" ); | |
| 93 | + } elseif ( $version === "only_pro" ) { | |
| 94 | + $table_name = "{$wpdb->prefix}arf_forms"; | |
| 95 | + $sql_query = $wpdb->prepare( "SELECT id, name FROM {$table_name} WHERE is_template = 0 AND arf_is_lite_form = 0" ); | |
| 96 | + } | |
| 97 | + else { | |
| 98 | + $table_name = "{$wpdb->prefix}arflite_forms"; | |
| 99 | + $sql_query = "SELECT id, name FROM " . $table_name; | |
| 100 | + } | |
| 101 | + | |
| 102 | + //$sql_query = $wpdb->prepare( "SELECT id, name FROM {$table_name}" ); | |
| 103 | + $results = $wpdb->get_results( $sql_query ); | |
| 104 | + | |
| 105 | + foreach ( $results as $form ) { | |
| 106 | + $all_forms[] = array( | |
| 107 | + 'id' => $form->id, | |
| 108 | + 'name' => $form->title, | |
| 109 | + ); | |
| 110 | + } | |
| 111 | + | |
| 112 | + return $all_forms; | |
| 113 | + | |
| 114 | +} | |
| 115 | + | |
| 116 | +/** | |
| 117 | + * Get form name | |
| 118 | + * | |
| 119 | + * @since 1.0.0 | |
| 120 | + * | |
| 121 | + * @return array | |
| 122 | + */ | |
| 123 | +function automatorwp_arforms_get_form_name( $form_id ) { | |
| 124 | + | |
| 125 | + global $wpdb; | |
| 126 | + | |
| 127 | + $version = automatorwp_arforms_get_plugin_version(); | |
| 128 | + | |
| 129 | + // Check installed versions to get table | |
| 130 | + if( $version === "pro" || $version === "only_pro" ) { | |
| 131 | + $table_name = "{$wpdb->prefix}arf_forms"; | |
| 132 | + } else { | |
| 133 | + $table_name = "{$wpdb->prefix}arflite_forms"; | |
| 134 | + } | |
| 135 | + | |
| 136 | + $sql_query = $wpdb->prepare( "SELECT name FROM $table_name WHERE id = %d", $form_id ); | |
| 137 | + $form_name = $wpdb->get_var( $sql_query ); | |
| 138 | + | |
| 139 | + return $form_name; | |
| 140 | + | |
| 141 | +} | |
| 142 | + | |
| 143 | +/** | |
| 144 | + * Get form fields values | |
| 145 | + * | |
| 146 | + * @since 1.0.0 | |
| 147 | + * | |
| 148 | + * @param array $field_data | |
| 149 | + * @param string $field_name | |
| 150 | + * @param string $field_value | |
| 151 | + * | |
| 152 | + * @return array | |
| 153 | + */ | |
| 154 | +function automatorwp_arforms_get_form_fields_values( $form_id, $item_meta_values ) { | |
| 155 | + | |
| 156 | + global $wpdb; | |
| 157 | + | |
| 158 | + $form_fields = array(); | |
| 159 | + | |
| 160 | + $version = automatorwp_arforms_get_plugin_version(); | |
| 161 | + | |
| 162 | + // Check installed versions to get table | |
| 163 | + if( $version === "pro" || $version === "only_pro" ) { | |
| 164 | + $table_name = "{$wpdb->prefix}arf_fields"; | |
| 165 | + } else { | |
| 166 | + $table_name = "{$wpdb->prefix}arflite_fields"; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $sql_query = $wpdb->prepare( "SELECT id, name FROM $table_name WHERE form_id = %d", $form_id ); | |
| 170 | + $field_data = $wpdb->get_results( $sql_query ); | |
| 171 | + | |
| 172 | + foreach( $field_data as $field ) { | |
| 173 | + | |
| 174 | + $name = $field->name; | |
| 175 | + $value = $item_meta_values[$field->id]; | |
| 176 | + | |
| 177 | + $form_fields[$name] = $value; | |
| 178 | + } | |
| 179 | + | |
| 180 | + // Check for AutomatorWP 1.4.4 | |
| 181 | + if( function_exists( 'automatorwp_utilities_pull_array_values' ) ) { | |
| 182 | + $form_fields = automatorwp_utilities_pull_array_values( $form_fields ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + return $form_fields; | |
| 186 | + | |
| 187 | +} | |
| 188 | + | |
| 189 | +/** | |
| 190 | + * Custom tags replacements | |
| 191 | + * | |
| 192 | + * @since 1.0.0 | |
| 193 | + * | |
| 194 | + * @param string $parsed_content Content parsed | |
| 195 | + * @param array $replacements Automation replacements | |
| 196 | + * @param int $automation_id The automation ID | |
| 197 | + * @param int $user_id The user ID | |
| 198 | + * @param string $content The content to parse | |
| 199 | + * | |
| 200 | + * @return string | |
| 201 | + */ | |
| 202 | +function automatorwp_arforms_parse_automation_tags( $parsed_content, $replacements, $automation_id, $user_id, $content ) { | |
| 203 | + | |
| 204 | + $new_replacements = array(); | |
| 205 | + | |
| 206 | + // Get automation triggers to pass their tags | |
| 207 | + $triggers = automatorwp_get_automation_triggers( $automation_id ); | |
| 208 | + | |
| 209 | + foreach( $triggers as $trigger ) { | |
| 210 | + | |
| 211 | + $trigger_args = automatorwp_get_trigger( $trigger->type ); | |
| 212 | + | |
| 213 | + // Skip if trigger is not from this integration | |
| 214 | + if( $trigger_args['integration'] !== 'arforms' ) { | |
| 215 | + continue; | |
| 216 | + } | |
| 217 | + | |
| 218 | + // Get the last trigger log (where data for tags replacement will be get | |
| 219 | + $log = automatorwp_get_user_last_completion( $trigger->id, $user_id, 'trigger' ); | |
| 220 | + | |
| 221 | + if( ! $log ) { | |
| 222 | + continue; | |
| 223 | + } | |
| 224 | + | |
| 225 | + ct_setup_table( 'automatorwp_logs' ); | |
| 226 | + $form_fields = ct_get_object_meta( $log->id, 'form_fields', true ); | |
| 227 | + ct_reset_setup_table(); | |
| 228 | + | |
| 229 | + // Skip if not form fields | |
| 230 | + if( ! is_array( $form_fields ) ) { | |
| 231 | + continue; | |
| 232 | + } | |
| 233 | + | |
| 234 | + // Look for form field tags | |
| 235 | + preg_match_all( "/\{t:" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); | |
| 236 | + | |
| 237 | + if( is_array( $matches ) && isset( $matches[1] ) ) { | |
| 238 | + | |
| 239 | + foreach( $matches[1] as $field_name ) { | |
| 240 | + // Replace {t:ID:form_field:NAME} by the field value | |
| 241 | + if( isset( $form_fields[$field_name] ) ) { | |
| 242 | + $new_replacements['{t:' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; | |
| 243 | + } | |
| 244 | + } | |
| 245 | + | |
| 246 | + } | |
| 247 | + | |
| 248 | + // Look for form field tags | |
| 249 | + preg_match_all( "/\{" . $trigger->id . ":form_field:\s*(.*?)\s*\}/", $parsed_content, $matches ); | |
| 250 | + | |
| 251 | + if( is_array( $matches ) && isset( $matches[1] ) ) { | |
| 252 | + | |
| 253 | + foreach( $matches[1] as $field_name ) { | |
| 254 | + // Replace {ID:form_field:NAME} by the field value | |
| 255 | + if( isset( $form_fields[$field_name] ) ) { | |
| 256 | + $new_replacements['{' . $trigger->id . ':form_field:' . $field_name . '}'] = $form_fields[$field_name]; | |
| 257 | + } | |
| 258 | + } | |
| 259 | + | |
| 260 | + } | |
| 261 | + | |
| 262 | + } | |
| 263 | + | |
| 264 | + if( count( $new_replacements ) ) { | |
| 265 | + | |
| 266 | + $tags = array_keys( $new_replacements ); | |
| 267 | + | |
| 268 | + // Replace all tags by their replacements | |
| 269 | + $parsed_content = str_replace( $tags, $new_replacements, $parsed_content ); | |
| 270 | + | |
| 271 | + } | |
| 272 | + | |
| 273 | + return $parsed_content; | |
| 274 | + | |
| 275 | +} | |
| 276 | +add_filter( 'automatorwp_parse_automation_tags', 'automatorwp_arforms_parse_automation_tags', 10, 5 ); | |