PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.4
Defender Security – Malware Scanner, Login Security & Firewall v6.2.4
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / class-product-analytics.php

class-product-analytics.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.4, at src/component/class-product-analytics.php

189 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles product analytics for the WP Defender plugin, specifically using Mixpanel for tracking.
4 *
5 * @package WP_Defender\Component
6 */
7
8 namespace WP_Defender\Component;
9
10 use WPMUDEV_Analytics_V4;
11 use Calotes\Base\Component;
12 use WP_Defender\Traits\Device;
13 use WP_Defender\Behavior\WPMUDEV;
14 use WP_Defender\Component\Security_Tweaks\Servers\Server;
15
16 /**
17 * Handles product analytics for the WP Defender plugin, specifically using Mixpanel for tracking.
18 *
19 * @since 4.2.0
20 */
21 class Product_Analytics extends Component {
22
23 use Device;
24
25 private const PROJECT_TOKEN = '5d545622e3a040aca63f2089b0e6cae7';
26
27 /**
28 * Holds the Mixpanel analytics instance.
29 *
30 * @var null|WPMUDEV_Analytics_V4
31 */
32 private $mixpanel = null;
33 /**
34 * Holds the MySQL version.
35 *
36 * @var string|null
37 */
38 private $mysql_version;
39
40 /**
41 * Constructor that initializes the Mixpanel instance and sets up the default properties.
42 */
43 public function __construct() {
44 if ( is_null( $this->mixpanel ) ) {
45 // Create new mixpanel instance.
46 $this->mixpanel = $this->prepare_mixpanel_instance();
47 }
48 $this->mixpanel->identify( $this->get_unique_id() );
49 $this->mixpanel->registerAll( $this->get_super_properties() );
50 }
51
52 /**
53 * Get configured mixpanel instance.
54 *
55 * @return WPMUDEV_Analytics_v4
56 */
57 public function get_mixpanel() {
58 return $this->mixpanel;
59 }
60
61 /**
62 * Handle mixpanel error.
63 *
64 * @param string $code Error code.
65 * @param string $data Error data.
66 *
67 * @return void
68 */
69 private function handle_error( $code, $data ) {
70 $this->log( $code . ':' . $data, wd_internal_log() );
71 }
72
73 /**
74 * Prepare Mixpanel instance.
75 *
76 * @return WPMUDEV_Analytics_V4
77 */
78 private function prepare_mixpanel_instance() {
79 if ( ! class_exists( 'WPMUDEV_Analytics_V4' ) ) {
80 require_once defender_path( 'extra/wpmudev-analytics/autoload.php' );
81 }
82 $extra_options = array(
83 'consumers' => array(
84 'file' => 'WPMUDEV_Analytics_Vendor\ConsumerStrategies_FileConsumer',
85 'curl' => 'WPMUDEV_Analytics_Vendor\ConsumerStrategies_CurlConsumer',
86 'socket' => 'WPMUDEV_Analytics_Vendor\ConsumerStrategies_SocketConsumer',
87 ),
88 'consumer' => 'socket',
89 );
90 $this->mixpanel = new WPMUDEV_Analytics_V4( 'defender', 'Defender', 55, self::PROJECT_TOKEN, $extra_options );
91
92 return $this->mixpanel;
93 }
94
95 /**
96 * Get super properties for all events.
97 *
98 * @return array
99 */
100 private function get_super_properties(): array {
101 global $wp_version;
102
103 return array(
104 'active_theme' => get_stylesheet(),
105 'locale' => get_locale(),
106 'mysql_version' => $this->get_mysql_version(),
107 'php_version' => PHP_VERSION,
108 'plugin' => 'Defender',
109 'plugin_type' => ( new WPMUDEV() )->is_pro() ? 'pro' : 'free',
110 'plugin_version' => DEFENDER_VERSION,
111 'server_type' => Server::get_current_server(),
112 'wp_type' => is_multisite() ? 'multisite' : 'single',
113 'wp_version' => $wp_version,
114 'memory_limit' => $this->convert_to_megabytes( $this->get_memory_limit() ),
115 'max_execution_time' => $this->get_max_execution_time(),
116 'device' => $this->get_device(),
117 'user_agent' => defender_get_user_agent(),
118 );
119 }
120
121 /**
122 * Get unique identity for current site.
123 *
124 * @return string
125 */
126 private function get_unique_id(): string {
127 $url = str_replace( array( 'http://', 'https://', 'www.' ), '', home_url() );
128
129 return untrailingslashit( $url );
130 }
131
132 /**
133 * Retrieves the PHP memory limit in bytes and converts it to an integer.
134 *
135 * @return int The memory limit in bytes.
136 */
137 private function get_memory_limit(): int {
138 if ( function_exists( 'ini_get' ) ) {
139 $memory_limit = ini_get( 'memory_limit' );
140 } else {
141 // Sensible default.
142 $memory_limit = '128M';
143 }
144
145 if ( ! $memory_limit || - 1 === $memory_limit ) {
146 // Unlimited, set to 32GB.
147 $memory_limit = '32000M';
148 }
149
150 return (int) $memory_limit * 1024 * 1024;
151 }
152
153 /**
154 * Converts a size in bytes to megabytes.
155 *
156 * @param int $size_in_bytes The size in bytes.
157 *
158 * @return float The size in megabytes.
159 */
160 private function convert_to_megabytes( int $size_in_bytes ) {
161 $unit_mb = pow( 1024, 2 );
162
163 return round( $size_in_bytes / $unit_mb, 2 );
164 }
165
166 /**
167 * Retrieves the maximum execution time for PHP scripts from the ini settings.
168 *
169 * @return int The maximum execution time in seconds.
170 */
171 private function get_max_execution_time() {
172 return (int) ini_get( 'max_execution_time' );
173 }
174
175 /**
176 * Retrieves the MySQL version from the global $wpdb object.
177 *
178 * @return string The MySQL version.
179 */
180 private function get_mysql_version() {
181 if ( ! $this->mysql_version ) {
182 global $wpdb;
183 $this->mysql_version = $wpdb->db_version();
184 }
185
186 return $this->mysql_version;
187 }
188 }
189