| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for gathering analytics data for the scan feature. |
| 4 |
* |
| 5 |
* @package WP_Defender\Helper\Analytics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Helper\Analytics; |
| 9 |
|
| 10 |
use WP_Defender\Event; |
| 11 |
use WP_Defender\Model\Scan_Item; |
| 12 |
use WP_Defender\Model\Scan as Scan_Model; |
| 13 |
|
| 14 |
/** |
| 15 |
* Gather analytics data required for scan feature. |
| 16 |
*/ |
| 17 |
class Scan extends Event { |
| 18 |
|
| 19 |
public const EVENT_SCAN_FAILED = 'def_scan_failed_new'; |
| 20 |
public const EVENT_SCAN_FAILED_PROP = 'Failure reason'; |
| 21 |
public const EVENT_SCAN_FAILED_CANCEL = 'User Cancellation'; |
| 22 |
public const EVENT_SCAN_FAILED_ERROR = 'Error'; |
| 23 |
|
| 24 |
// Mandatory empty methods. |
| 25 |
// Start. |
| 26 |
/** |
| 27 |
* Provides data for the frontend. |
| 28 |
* |
| 29 |
* @return array An array of data for the frontend. |
| 30 |
*/ |
| 31 |
public function data_frontend(): array { |
| 32 |
return array(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Converts the current state of the object to an array. |
| 37 |
* |
| 38 |
* @return array Returns an associative array of object properties. |
| 39 |
*/ |
| 40 |
public function to_array(): array { |
| 41 |
return array(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Imports data into the model. |
| 46 |
* |
| 47 |
* @param array $data Data to be imported into the model. |
| 48 |
*/ |
| 49 |
public function import_data( array $data ) { |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Removes settings for all submodules. |
| 54 |
* |
| 55 |
* @return mixed |
| 56 |
*/ |
| 57 |
public function remove_settings() { |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Delete all the data & the cache. |
| 62 |
* |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
public function remove_data() { |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Exports strings. |
| 70 |
* |
| 71 |
* @return array An array of strings. |
| 72 |
*/ |
| 73 |
public function export_strings(): array { |
| 74 |
return array(); |
| 75 |
} |
| 76 |
// End. |
| 77 |
|
| 78 |
/** |
| 79 |
* Analytics data for scan completed event. |
| 80 |
* |
| 81 |
* @param Scan_Model $scan_model Scan model object. |
| 82 |
* |
| 83 |
* @return array{ |
| 84 |
* 'event' => string, |
| 85 |
* 'data' => array |
| 86 |
* } |
| 87 |
*/ |
| 88 |
public function scan_completed( Scan_Model $scan_model ): array { |
| 89 |
$last_scan = $scan_model::get_last(); |
| 90 |
if ( ! $last_scan instanceof Scan_Model ) { |
| 91 |
return array(); |
| 92 |
} |
| 93 |
$scan_item_group_total = wd_di()->get( Scan_Item::class ) |
| 94 |
->get_types_total( $last_scan->id, Scan_Item::STATUS_ACTIVE ); |
| 95 |
|
| 96 |
$data = array(); |
| 97 |
|
| 98 |
if ( isset( $scan_item_group_total['all'] ) ) { |
| 99 |
$data['Threats Count'] = $scan_item_group_total['all']; |
| 100 |
} |
| 101 |
|
| 102 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_INTEGRITY ] ) ) { |
| 103 |
$data['WP core issue count'] = $scan_item_group_total[ Scan_Item::TYPE_INTEGRITY ]; |
| 104 |
} |
| 105 |
|
| 106 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_SUSPICIOUS ] ) ) { |
| 107 |
$data['Suspicious Code'] = $scan_item_group_total[ Scan_Item::TYPE_SUSPICIOUS ]; |
| 108 |
} |
| 109 |
|
| 110 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_CHECK ] ) ) { |
| 111 |
$data['Plugin file modified'] = $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_CHECK ]; |
| 112 |
} |
| 113 |
|
| 114 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_VULNERABILITY ] ) ) { |
| 115 |
$data['Vulnerability'] = $scan_item_group_total[ Scan_Item::TYPE_VULNERABILITY ]; |
| 116 |
} |
| 117 |
|
| 118 |
$is_closed = false; |
| 119 |
$is_outdated = false; |
| 120 |
$count = 0; |
| 121 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_CLOSED ] ) ) { |
| 122 |
$is_closed = true; |
| 123 |
$count += $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_CLOSED ]; |
| 124 |
} |
| 125 |
if ( isset( $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_OUTDATED ] ) ) { |
| 126 |
$is_outdated = true; |
| 127 |
$count += $scan_item_group_total[ Scan_Item::TYPE_PLUGIN_OUTDATED ]; |
| 128 |
} |
| 129 |
if ( $is_closed || $is_outdated ) { |
| 130 |
$data['Outdated & Removed Plugins'] = $count; |
| 131 |
} |
| 132 |
|
| 133 |
return array( |
| 134 |
'event' => 'def_scan_completed_new', |
| 135 |
'data' => $data, |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|