| 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( $size_in_bytes ) { |
| 161 |
if ( empty( $size_in_bytes ) ) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
$unit_mb = pow( 1024, 2 ); |
| 165 |
|
| 166 |
return round( $size_in_bytes / $unit_mb, 2 ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Retrieves the maximum execution time for PHP scripts from the ini settings. |
| 171 |
* |
| 172 |
* @return int The maximum execution time in seconds. |
| 173 |
*/ |
| 174 |
private function get_max_execution_time() { |
| 175 |
return (int) ini_get( 'max_execution_time' ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Retrieves the MySQL version from the global $wpdb object. |
| 180 |
* |
| 181 |
* @return string The MySQL version. |
| 182 |
*/ |
| 183 |
private function get_mysql_version() { |
| 184 |
if ( ! $this->mysql_version ) { |
| 185 |
global $wpdb; |
| 186 |
$this->mysql_version = $wpdb->db_version(); |
| 187 |
} |
| 188 |
|
| 189 |
return $this->mysql_version; |
| 190 |
} |
| 191 |
} |
| 192 |
|