| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Fluent_Form Extension |
| 5 |
* |
| 6 |
* @package NotificationX\Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace NotificationX\Extensions\FluentForm; |
| 10 |
|
| 11 |
use NotificationX\Core\Helper; |
| 12 |
use NotificationX\Core\Rules; |
| 13 |
use NotificationX\GetInstance; |
| 14 |
use NotificationX\Extensions\Extension; |
| 15 |
use NotificationX\Extensions\GlobalFields; |
| 16 |
use NotificationX\Admin\Entries; |
| 17 |
|
| 18 |
/** |
| 19 |
* Fluent_Form Extension |
| 20 |
* @method static Fluent_Form get_instance($args = null) |
| 21 |
*/ |
| 22 |
class FluentForm extends Extension { |
| 23 |
/** |
| 24 |
* Instance of Fluent_Form |
| 25 |
* |
| 26 |
* @var FluentForm |
| 27 |
*/ |
| 28 |
use GetInstance; |
| 29 |
|
| 30 |
public $priority = 20; |
| 31 |
public $id = 'fluentform'; |
| 32 |
public $img = ''; |
| 33 |
public $doc_link = 'https://notificationx.com/docs/contact-form-submission-alert/'; |
| 34 |
public $types = 'form'; |
| 35 |
public $module = 'modules_fluentform'; |
| 36 |
public $module_priority = 10; |
| 37 |
public $constant = 'FLUENTFORM_VERSION'; |
| 38 |
public $post_type = 'fluentform'; |
| 39 |
/** |
| 40 |
* Initially Invoked when initialized. |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
parent::__construct(); |
| 44 |
} |
| 45 |
|
| 46 |
public function init_extension() |
| 47 |
{ |
| 48 |
$this->title = __('Fluent Forms', 'notificationx'); |
| 49 |
$this->module_title = __('Fluent Forms', 'notificationx'); |
| 50 |
} |
| 51 |
|
| 52 |
public function init() { |
| 53 |
parent::init(); |
| 54 |
add_action('fluentform_submission_inserted', array($this, 'save_new_records'),10,3); |
| 55 |
} |
| 56 |
|
| 57 |
public function init_fields(){ |
| 58 |
parent::init_fields(); |
| 59 |
add_filter('nx_form_list', [$this, 'nx_form_list'], 9); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* This functions is hooked |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function admin_actions() { |
| 68 |
parent::admin_actions(); |
| 69 |
|
| 70 |
add_filter("nx_can_entry_{$this->id}", array($this, 'can_entry'), 10, 3); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* This functions is hooked |
| 75 |
* |
| 76 |
* @hooked nx_public_action |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function public_actions() { |
| 80 |
parent::public_actions(); |
| 81 |
|
| 82 |
add_filter("nx_filtered_data_{$this->id}", array($this, 'filter_by_form'), 11, 3); |
| 83 |
} |
| 84 |
|
| 85 |
public function source_error_message($messages) { |
| 86 |
if (!$this->class_exists()) { |
| 87 |
$url = admin_url('plugin-install.php?s=fluent-forms&tab=search&type=term'); |
| 88 |
$messages[$this->id] = [ |
| 89 |
'message' => sprintf( '%s <a href="%s" target="_blank">%s</a> %s', |
| 90 |
__( 'You have to install', 'notificationx' ), |
| 91 |
$url, |
| 92 |
__( 'Fluent Forms', 'notificationx' ), |
| 93 |
__( 'plugin first.', 'notificationx' ) |
| 94 |
), |
| 95 |
'html' => true, |
| 96 |
'type' => 'error', |
| 97 |
'rules' => Rules::is('source', $this->id), |
| 98 |
]; |
| 99 |
} |
| 100 |
return $messages; |
| 101 |
} |
| 102 |
|
| 103 |
public function nx_form_list($forms) { |
| 104 |
$_forms = GlobalFields::get_instance()->normalize_fields($this->get_forms(), 'source', $this->id); |
| 105 |
return array_merge($forms, $_forms); |
| 106 |
} |
| 107 |
|
| 108 |
public function get_forms() { |
| 109 |
$forms = []; |
| 110 |
if (!$this->class_exists()) { |
| 111 |
return []; |
| 112 |
} |
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$table_name = $wpdb->prefix . 'fluentform_forms'; |
| 116 |
$limit = 10; |
| 117 |
// Prepare the query with a WHERE condition |
| 118 |
$query = $wpdb->prepare( |
| 119 |
"SELECT id, title FROM {$table_name} WHERE status = %s LIMIT %d", |
| 120 |
'published', $limit |
| 121 |
); |
| 122 |
// Execute the query and retrieve the results |
| 123 |
$form_result = $wpdb->get_results($query); |
| 124 |
|
| 125 |
if (!empty($form_result)) { |
| 126 |
foreach ($form_result as $form) { |
| 127 |
$key = $this->key($form->id); |
| 128 |
$forms[$key] = $form->title; |
| 129 |
} |
| 130 |
} |
| 131 |
return $forms; |
| 132 |
} |
| 133 |
|
| 134 |
public function restResponse($args) { |
| 135 |
$forms = []; |
| 136 |
if (!$this->class_exists()) { |
| 137 |
return []; |
| 138 |
} |
| 139 |
|
| 140 |
global $wpdb; |
| 141 |
$table_name = $wpdb->prefix . 'fluentform_forms'; |
| 142 |
if (!empty($args['inputValue'])) { |
| 143 |
$limit = 10; |
| 144 |
// Prepare the query with a LIKE condition |
| 145 |
$query = $wpdb->prepare( |
| 146 |
"SELECT id, title FROM {$table_name} WHERE title LIKE %s AND status = %s LIMIT %d", |
| 147 |
'%' . $wpdb->esc_like($args['inputValue']) . '%','published',$limit |
| 148 |
); |
| 149 |
// Execute the query and retrieve the results |
| 150 |
$form_result = $wpdb->get_results($query); |
| 151 |
if (!empty($form_result)) { |
| 152 |
foreach ($form_result as $form) { |
| 153 |
$key = $this->key($form->id); |
| 154 |
$forms[$key] = $form->title; |
| 155 |
} |
| 156 |
} |
| 157 |
$result = array_values(GlobalFields::get_instance()->normalize_fields($forms, 'source', $this->id)); |
| 158 |
return $result; |
| 159 |
} |
| 160 |
if (isset($args['form_id'])) { |
| 161 |
return $this->keys_generator($args['form_id']); |
| 162 |
} |
| 163 |
|
| 164 |
wp_send_json_error([]); |
| 165 |
} |
| 166 |
|
| 167 |
public function keys_generator($form_id) { |
| 168 |
$formData = []; |
| 169 |
$formApi = fluentFormApi('forms')->form($form_id); |
| 170 |
$formFields = $formApi->inputs(); |
| 171 |
if( !empty( $formFields ) ) { |
| 172 |
$formData = $this->extractVisibleFields($formFields); |
| 173 |
}; |
| 174 |
return $formData; |
| 175 |
} |
| 176 |
|
| 177 |
function extractVisibleFields($inputArray , $recursive = false) { |
| 178 |
$outputArray = []; |
| 179 |
foreach ($inputArray as $field) { |
| 180 |
if ( $recursive && isset($field['settings']['visible']) && $field['settings']['visible']) { |
| 181 |
$label = $field['settings']['label']; |
| 182 |
$value = $field['attributes']['name']; |
| 183 |
if( $value == 'first_name' || $value == 'last_name' ) { |
| 184 |
$outputArray[] = ['label' => $label, 'value' => 'tag__' . $value]; |
| 185 |
}else{ |
| 186 |
$outputArray[] = ['label' => $label, 'value' => 'tag_' . $value]; |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
if (isset($field['raw']['fields']) && is_array($field['raw']['fields'])) { |
| 191 |
$main_label = $field['admin_label']; |
| 192 |
$main_value = $field['raw']['attributes']['name']; |
| 193 |
$outputArray[] = [ 'label' => $main_label, 'value' => 'tag_'. $main_value ]; |
| 194 |
$outputArray = array_merge($outputArray, $this->extractVisibleFields($field['raw']['fields'], true ) ); |
| 195 |
}else if( !$recursive ) { |
| 196 |
$label = $field['raw']['settings']['label']; |
| 197 |
$value = $field['raw']['attributes']['name']; |
| 198 |
$outputArray[] = [ 'label' => $label, 'value' => 'tag_'. $value ]; |
| 199 |
} |
| 200 |
} |
| 201 |
return $outputArray; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
public function save_new_records($insertId,$formData,$form) { |
| 206 |
$submission = wpFluent()->table('fluentform_submissions') |
| 207 |
->where('form_id', $form->id) |
| 208 |
->where('id', $insertId) |
| 209 |
->first(); |
| 210 |
$data = []; |
| 211 |
if( !empty( $submission ) ) { |
| 212 |
$data['ip'] = $submission->ip; |
| 213 |
$data['timestamp'] = Helper::get_utc_time($submission->created_at); |
| 214 |
$inputs = \FluentForm\App\Modules\Form\FormFieldsParser::getEntryInputs($form); |
| 215 |
$submission = \FluentForm\App\Modules\Form\FormDataParser::parseFormEntry($submission, $form, $inputs, false); |
| 216 |
foreach ($submission->user_inputs as $key => $field) { |
| 217 |
$getFieldRow = wpFluent()->table('fluentform_entry_details') |
| 218 |
->where('submission_id', $submission->id) |
| 219 |
->where('field_name',$key) |
| 220 |
->get(); |
| 221 |
if( count( $getFieldRow ) > 1 ) { |
| 222 |
foreach ($getFieldRow as $_key => $_value) { |
| 223 |
if( !empty( $getFieldRow[$_key] ) ) { |
| 224 |
if( $_value->sub_field_name == 'first_name' || $_value->sub_field_name == 'last_name' ) { |
| 225 |
$data['_'.$_value->sub_field_name] = $_value->field_value; |
| 226 |
}else{ |
| 227 |
$data[$_value->sub_field_name] = $_value->field_value; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
$data[$key] = $field; |
| 232 |
}else{ |
| 233 |
if( $key == 'first_name' || $key == 'last_name' ) { |
| 234 |
$data['_'.$key] = $field; |
| 235 |
}else{ |
| 236 |
$data[$key] = $field; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
$data['title'] = $form->title ? $form->title : ''; |
| 242 |
$data['timestamp'] = isset($data['timestamp']) ? $data['timestamp'] : time(); |
| 243 |
$data['submission_id'] = $submission->id; |
| 244 |
|
| 245 |
if (!empty($data)) { |
| 246 |
$key = $this->key($form->id); |
| 247 |
$this->save([ |
| 248 |
'source' => $this->id, |
| 249 |
'entry_key' => $key, |
| 250 |
'data' => $data, |
| 251 |
]); |
| 252 |
return true; |
| 253 |
} |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
public function key($key = '') { |
| 258 |
$key = $this->id . '_' . $key; |
| 259 |
return $key; |
| 260 |
} |
| 261 |
|
| 262 |
public function saved_post($post, $data, $nx_id) { |
| 263 |
$this->get_notification_ready($data); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* This function responsible for making ready the notifications for the first time |
| 268 |
* we have made a notification. |
| 269 |
* |
| 270 |
* @param string $type |
| 271 |
* @param array $data |
| 272 |
* @return void |
| 273 |
*/ |
| 274 |
public function get_notification_ready($data = array()) { |
| 275 |
if( !empty( $data['__form_list']['value'] ) ) { |
| 276 |
$form_list = explode('_',$data['__form_list']['value']); |
| 277 |
if( !empty( $form_list[1] ) ) { |
| 278 |
$form = wpFluent()->table('fluentform_forms')->where('id', $form_list[1])->first(); |
| 279 |
// $valueFrom = date('Y-m-d',strtotime('-'.$data['display_from'].' days',time())); |
| 280 |
$valueFrom = date('Y-m-d H:i:s', Helper::generate_time_string($data)); |
| 281 |
$valueTo = date('Y-m-d',strtotime('1 days',time())); |
| 282 |
$query = wpFluent()->table('fluentform_submissions') |
| 283 |
->where('form_id', $form->id); |
| 284 |
// define('FLUENTFORM_VERSION', '5.0.6') |
| 285 |
if( version_compare(FLUENTFORM_VERSION, '5.0.0', '>=')){ |
| 286 |
$query = $query->whereBetween('created_at', [$valueFrom, $valueTo] ); |
| 287 |
} |
| 288 |
else{ |
| 289 |
$query = $query->whereBetween('created_at', $valueFrom, $valueTo ); |
| 290 |
} |
| 291 |
|
| 292 |
$query = $query->orderBy('id','DESC') |
| 293 |
->limit($data['display_last']); |
| 294 |
$submissionArr = $query->get(); |
| 295 |
$entries = []; |
| 296 |
foreach ($submissionArr as $sub) { |
| 297 |
if( !empty( $sub ) ) { |
| 298 |
$entry_data = []; |
| 299 |
$inputs = \FluentForm\App\Modules\Form\FormFieldsParser::getEntryInputs($form); |
| 300 |
$submission = \FluentForm\App\Modules\Form\FormDataParser::parseFormEntry($sub, $form, $inputs, false); |
| 301 |
foreach ($submission->user_inputs as $key => $field) { |
| 302 |
$getFieldRow = wpFluent()->table('fluentform_entry_details') |
| 303 |
->where('submission_id', $submission->id) |
| 304 |
->where('field_name',$key) |
| 305 |
->get(); |
| 306 |
if( count( $getFieldRow ) > 1 ) { |
| 307 |
foreach ($getFieldRow as $__key => $_value) { |
| 308 |
if( !empty( $getFieldRow[$__key] ) ) { |
| 309 |
if( $_value->sub_field_name == 'first_name' || $_value->sub_field_name == 'last_name' ) { |
| 310 |
$entry_data['_'.$_value->sub_field_name] = $_value->field_value; |
| 311 |
}else{ |
| 312 |
$entry_data[$_value->sub_field_name] = $_value->field_value; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
$entry_data[$key] = $field; |
| 317 |
}else{ |
| 318 |
if( $key == 'first_name' || $key == 'last_name' ) { |
| 319 |
$entry_data['_'.$key] = $field; |
| 320 |
}else{ |
| 321 |
$entry_data[$key] = $field; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
$entry_data['title'] = $form->title ? $form->title : ''; |
| 326 |
$entry_data['ip'] = $sub->ip; |
| 327 |
$entry_data['timestamp'] = Helper::get_utc_time($sub->created_at); |
| 328 |
$entry_data['submission_id'] = $submission->id; |
| 329 |
if( $this->is_submission_exists((int) $data['nx_id'], $submission->id) ) { |
| 330 |
continue; |
| 331 |
} |
| 332 |
$_key = $this->key($form->id); |
| 333 |
if (!empty($data)) { |
| 334 |
$entries[] = [ |
| 335 |
'nx_id' => $data['nx_id'], |
| 336 |
'source' => $this->id, |
| 337 |
'entry_key' => $_key, |
| 338 |
'data' => $entry_data, |
| 339 |
]; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
$this->update_notifications($entries); |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
public function is_submission_exists( $nx_id, $submission_id ) { |
| 349 |
$entries = Entries::get_instance()->get_entries($nx_id); |
| 350 |
$filteredData = array_filter($entries, function ($item) use ($submission_id) { |
| 351 |
return $item['submission_id'] == $submission_id; |
| 352 |
}); |
| 353 |
return $filteredData ? true : false; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Limit entry by selected form in 'Select a Form'; |
| 358 |
* |
| 359 |
* @param [type] $return |
| 360 |
* @param [type] $entry |
| 361 |
* @param [type] $settings |
| 362 |
* @return boolean |
| 363 |
*/ |
| 364 |
public function can_entry($return, $entry, $settings){ |
| 365 |
if(!empty($settings['form_list']) && !empty($entry['entry_key'])){ |
| 366 |
$selected_form = $settings['form_list']; |
| 367 |
$form_id = $entry['entry_key']; |
| 368 |
if($selected_form != $form_id){ |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
} |
| 373 |
return $return; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Filter entries based on selected form |
| 378 |
* |
| 379 |
* @param [type] $data |
| 380 |
* @param [type] $settings |
| 381 |
* @return boolean |
| 382 |
*/ |
| 383 |
public function filter_by_form($data, $settings){ |
| 384 |
if( empty( $settings['form_list'] )) { |
| 385 |
return $data; |
| 386 |
} |
| 387 |
|
| 388 |
$new_data = []; |
| 389 |
|
| 390 |
if( ! empty( $data ) ) { |
| 391 |
foreach( $data as $key => $entry ) { |
| 392 |
$selected_form = $settings['form_list']; |
| 393 |
$form_id = $entry['entry_key']; |
| 394 |
if($selected_form == $form_id){ |
| 395 |
$new_data[] = $entry; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
return $new_data; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
public function doc() { |
| 404 |
return sprintf(__(' |
| 405 |
<p>To use the campaign & form subscription data, make sure that you have <a target="_blank" href="%1$s">Fluent Forms installed and configured</a> on your website. For detailed guidelines, follow this <a target="_blank" href="%2$s">documentation</a>.</p> |
| 406 |
|
| 407 |
<p>🎥 Learn quickly from the <a target="_blank" href="%3$s">video tutorial</a>.</p> |
| 408 |
|
| 409 |
<p>⚙️ NotificationX integration with Fluent Forms</p> |
| 410 |
|
| 411 |
<p>📖 Recommended Reading: </p> |
| 412 |
<p>🔥How To <a target="_blank" href="%4$s">Display Fluent Forms Submission Alert</a> Using NotificationX?</p> |
| 413 |
', 'notificationx'), |
| 414 |
'https://wordpress.org/plugins/fluentform/', |
| 415 |
'https://notificationx.com/docs/fluent-forms-submission-alert-notificationx', |
| 416 |
'https://youtu.be/cl0WEazGflU', |
| 417 |
'https://notificationx.com/blog/display-fluent-forms-submission-alert/' |
| 418 |
); |
| 419 |
} |
| 420 |
} |
| 421 |
|