| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm; |
| 4 |
|
| 5 |
use BitCode\BitForm\BitApps\WPTelemetry\Telemetry\Telemetry; |
| 6 |
|
| 7 |
if (!\defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
class BfAnalytics |
| 11 |
{ |
| 12 |
public function modifyTelemetryData() |
| 13 |
{ |
| 14 |
global $wpdb; |
| 15 |
$allForms = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}bitforms_form"); |
| 16 |
|
| 17 |
$bfInfo = []; |
| 18 |
$formsArr = []; |
| 19 |
foreach ($allForms as $form) { |
| 20 |
$formData = []; |
| 21 |
$formData['id'] = $form->id; |
| 22 |
$formContent = json_decode($form->form_content, true); |
| 23 |
$formData['fields'] = $this->fieldCount($formContent['fields']); |
| 24 |
$formData['workflows'] = $this->countRow($form->id, 'bitforms_workflows'); |
| 25 |
$formData['pdfTemplate'] = $this->countRow($form->id, 'bitforms_pdf_template'); |
| 26 |
$formData['emailTemplate'] = $this->countRow($form->id, 'bitforms_email_template'); |
| 27 |
$formData['integrations'] = $this->getIntegrations($form->id); |
| 28 |
$formData['formAbandonment'] = $this->getFormAbandonment($form->id); |
| 29 |
$formData['doubleOptin'] = $this->getDoubleOptin($form->id); |
| 30 |
$formData['tableView'] = $this->countRow($form->id, 'bitforms_frontend_views'); |
| 31 |
$formsArr[] = $formData; |
| 32 |
} |
| 33 |
$bfInfo['forms'] = $formsArr; |
| 34 |
$bfInfo['totalForms'] = count($allForms); |
| 35 |
$bfInfo['reCaptchaV3'] = $this->getReCaptchaV3(); |
| 36 |
$bfInfo['paymentGateway'] = $this->getPaymentGateway(); |
| 37 |
$bfInfo['smtp'] = $this->isSMTPExist(); |
| 38 |
|
| 39 |
return $bfInfo; |
| 40 |
} |
| 41 |
|
| 42 |
public function analyticsOptIn($permission) |
| 43 |
{ |
| 44 |
if (true === $permission) { |
| 45 |
Telemetry::report()->trackingOptIn(); |
| 46 |
return true; |
| 47 |
} |
| 48 |
Telemetry::report()->trackingOptOut(); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
public function isTrackingEnabled() |
| 53 |
{ |
| 54 |
return (bool) Telemetry::report()->isTrackingAllowed(); |
| 55 |
} |
| 56 |
|
| 57 |
private function fieldCount($fields) |
| 58 |
{ |
| 59 |
$count = []; |
| 60 |
foreach ($fields as $field) { |
| 61 |
if (isset($count[$field['typ']])) { |
| 62 |
$count[$field['typ']]++; |
| 63 |
} else { |
| 64 |
$count[$field['typ']] = 1; |
| 65 |
} |
| 66 |
} |
| 67 |
return $count; |
| 68 |
} |
| 69 |
|
| 70 |
private function countRow($formId, $tablename) |
| 71 |
{ |
| 72 |
global $wpdb; |
| 73 |
$totalRow = $wpdb->get_results( |
| 74 |
$wpdb->prepare( |
| 75 |
"SELECT COUNT(*) as count FROM `{$wpdb->prefix}{$tablename}` WHERE form_id = %d;", |
| 76 |
$formId |
| 77 |
) |
| 78 |
); |
| 79 |
return $totalRow[0]->count || 0; |
| 80 |
} |
| 81 |
|
| 82 |
private function getIntegrations($formId) |
| 83 |
{ |
| 84 |
global $wpdb; |
| 85 |
$integrations = $wpdb->get_results( |
| 86 |
$wpdb->prepare( |
| 87 |
"SELECT integration_type FROM `{$wpdb->prefix}bitforms_integration` WHERE form_id = %d AND category='form';", |
| 88 |
$formId |
| 89 |
) |
| 90 |
); |
| 91 |
|
| 92 |
$integrationNameArr = array_map(function ($integration) { |
| 93 |
return $integration->integration_type; |
| 94 |
}, $integrations); |
| 95 |
|
| 96 |
return $integrationNameArr; |
| 97 |
} |
| 98 |
|
| 99 |
private function getFormAbandonment($formId) |
| 100 |
{ |
| 101 |
global $wpdb; |
| 102 |
$formAbandonment = $wpdb->get_results( |
| 103 |
$wpdb->prepare( |
| 104 |
"SELECT integration_details FROM `{$wpdb->prefix}bitforms_integration` WHERE form_id = %d AND category='formAbandonment';", |
| 105 |
$formId |
| 106 |
) |
| 107 |
); |
| 108 |
if (count($formAbandonment) > 0) { |
| 109 |
$formAbandonment = json_decode($formAbandonment[0]->integration_details, true); |
| 110 |
} else { |
| 111 |
$formAbandonment = (object)[]; |
| 112 |
} |
| 113 |
return $formAbandonment; |
| 114 |
} |
| 115 |
|
| 116 |
private function getDoubleOptin($formId) |
| 117 |
{ |
| 118 |
global $wpdb; |
| 119 |
$doubleOptin = $wpdb->get_results( |
| 120 |
$wpdb->prepare( |
| 121 |
"SELECT integration_details FROM `{$wpdb->prefix}bitforms_integration` WHERE category='doubleOptin' AND form_id = %d;", |
| 122 |
$formId |
| 123 |
) |
| 124 |
); |
| 125 |
if (count($doubleOptin) > 0) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
private function getIntegType($integration_type) |
| 132 |
{ |
| 133 |
global $wpdb; |
| 134 |
return $wpdb->get_results( |
| 135 |
$wpdb->prepare( |
| 136 |
"SELECT integration_type, integration_details FROM `{$wpdb->prefix}bitforms_integration` WHERE integration_type=%s;", |
| 137 |
$integration_type |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
private function getReCaptchaV3() |
| 143 |
{ |
| 144 |
$name = $this->getIntegType('gReCaptchaV3'); |
| 145 |
if (count($name) > 0) { |
| 146 |
return true; |
| 147 |
} |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
private function getPaymentGateway() |
| 152 |
{ |
| 153 |
$types = $this->getIntegType('payments'); |
| 154 |
|
| 155 |
$paymentTypeArr = array_map(function ($type) { |
| 156 |
$integration_details = json_decode($type->integration_details, true); |
| 157 |
return $integration_details['type']; |
| 158 |
}, $types); |
| 159 |
|
| 160 |
return $paymentTypeArr; |
| 161 |
} |
| 162 |
|
| 163 |
private function isSMTPExist() |
| 164 |
{ |
| 165 |
$type = $this->getIntegType('smtp'); |
| 166 |
|
| 167 |
if (count($type) > 0) { |
| 168 |
return true; |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
} |
| 173 |
|