| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Formidable |
| 4 |
{ |
| 5 |
protected $hashFieldTypes = [ |
| 6 |
'password', |
| 7 |
'credit_card', |
| 8 |
]; |
| 9 |
|
| 10 |
protected $ignoreFieldTypes = [ |
| 11 |
'divider', |
| 12 |
'end_divider', |
| 13 |
]; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_action('frm_after_create_entry', [$this, 'entryCreated'], 10, 3); |
| 18 |
} |
| 19 |
|
| 20 |
public function entryCreated($entry_id, $form_id, $is_child) |
| 21 |
{ |
| 22 |
if (apply_filters('logtivity_disable_formidable_logging', false)) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
try { |
| 27 |
$entry = FrmEntry::getOne( $entry_id, true ); |
| 28 |
|
| 29 |
$FrmEntryFormatter = new Logtivity_FrmEntryFormatter([ |
| 30 |
'entry' => $entry, |
| 31 |
'format' => 'array', |
| 32 |
]); |
| 33 |
|
| 34 |
$log = Logtivity_Logger::log() |
| 35 |
->setAction('Form Submitted') |
| 36 |
->setContext($entry->form_name) |
| 37 |
->addMeta('Form ID', $form_id) |
| 38 |
->addMeta('Entry ID', $entry_id); |
| 39 |
|
| 40 |
try { |
| 41 |
|
| 42 |
if (class_exists('FrmProEntriesController')) { |
| 43 |
if (isset($is_child['is_child']) && $is_child['is_child'] === true) { |
| 44 |
return; |
| 45 |
} |
| 46 |
$entry = FrmProEntriesController::show_entry_shortcode( array( 'id' => $entry_id, 'format' => 'array' ) ); |
| 47 |
|
| 48 |
$seenSubFields = []; |
| 49 |
|
| 50 |
foreach ($entry as $key => $value) { |
| 51 |
if (isset($value['form'])) { |
| 52 |
foreach ($value as $key => $sub_fields) { |
| 53 |
if ($key != 'form') { |
| 54 |
foreach ($sub_fields as $key => $value) { |
| 55 |
$seenSubFields[$key] = $key; |
| 56 |
$this->maybeStoreField($log, $key, $value); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} else { |
| 61 |
if (isset($seenSubFields[$key])) { |
| 62 |
unset($seenSubFields[$key]); |
| 63 |
continue; |
| 64 |
} |
| 65 |
$this->maybeStoreField($log, $key, $value); |
| 66 |
} |
| 67 |
} |
| 68 |
} else { |
| 69 |
foreach ($FrmEntryFormatter->logtivityGetEntryValues() as $key => $field_value) { |
| 70 |
$field_value->prepare_displayed_value(); |
| 71 |
|
| 72 |
if ( |
| 73 |
$this->shouldStoreField( |
| 74 |
$field_value->get_field_type(), |
| 75 |
$field_value->get_field_key(), |
| 76 |
$field_value->get_displayed_value() |
| 77 |
) |
| 78 |
) { |
| 79 |
$log->addMeta( |
| 80 |
$field_value->get_field_label(), |
| 81 |
$this->maybeLogFormValue( |
| 82 |
$field_value->get_field_type(), |
| 83 |
$field_value->get_displayed_value() |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
} catch (\Exception $e) { |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
$log->send(); |
| 95 |
} catch (\Exception $e) { |
| 96 |
error_log($e); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private function maybeStoreField($log, $key, $value) |
| 101 |
{ |
| 102 |
$field = FrmField::getOne( str_replace('-value', '', $key) ); |
| 103 |
|
| 104 |
if (strpos($key, '-value') !== false) { |
| 105 |
if ($field->type != 'address') { |
| 106 |
return; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if ($this->shouldStoreField($field->type, $field->field_key, $value)) { |
| 111 |
if ($value = $this->maybeLogFormValue($field->type, $value)) { |
| 112 |
$log->addMeta($field->name, $value); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private function shouldStoreField($field_type, $field_key, $value) |
| 118 |
{ |
| 119 |
if (in_array($field_type, $this->ignoreFieldTypes)) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
if (!$value) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
return apply_filters('logtivity_should_store_formidable_field_value', true, $field_key, $field_type); |
| 128 |
} |
| 129 |
|
| 130 |
private function maybeLogFormValue($field_type, $value) |
| 131 |
{ |
| 132 |
if (in_array($field_type, $this->hashFieldTypes)) { |
| 133 |
return '********'; |
| 134 |
} |
| 135 |
|
| 136 |
if ($field_type == 'address') { |
| 137 |
if (is_array($value)) { |
| 138 |
return implode(', ', $value); |
| 139 |
} |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
return $value; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$Logtivity_Formidable = new Logtivity_Formidable; |
| 148 |
|