| 1 |
<?php |
| 2 |
/** |
| 3 |
* Analytics |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'Copy_The_Code_Analytics' ) ) : |
| 9 |
|
| 10 |
/** |
| 11 |
* Analytics |
| 12 |
*/ |
| 13 |
class Copy_The_Code_Analytics { |
| 14 |
|
| 15 |
/** |
| 16 |
* URL to the WooThemes Tracker API endpoint. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private static $api_url = 'https://surror.com/wp-json/surror/v1/analytics'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance |
| 24 |
* |
| 25 |
* @access private |
| 26 |
* @var object Class Instance. |
| 27 |
*/ |
| 28 |
private static $instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initiator |
| 32 |
* |
| 33 |
* @return object initialized object of class. |
| 34 |
*/ |
| 35 |
public static function get_instance() { |
| 36 |
if ( ! isset( self::$instance ) ) { |
| 37 |
self::$instance = new self(); |
| 38 |
} |
| 39 |
return self::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
add_action( 'copy_the_code_tracker_send_event', [ __CLASS__, 'send_tracking_data' ] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Send tracking data. |
| 51 |
*/ |
| 52 |
public static function send_tracking_data() { |
| 53 |
if ( wp_doing_ajax() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Send a maximum of once per week by default. |
| 58 |
$last_send = self::get_last_send_time(); |
| 59 |
if ( $last_send && $last_send > apply_filters( 'copy_the_code_tracker_last_send_interval', strtotime( '-1 week' ) ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Update time first before sending to ensure it is set. |
| 64 |
update_option( 'copy_the_code_tracker_last_send', time() ); |
| 65 |
|
| 66 |
$params = self::get_tracking_data(); |
| 67 |
wp_safe_remote_post( |
| 68 |
self::$api_url, |
| 69 |
[ |
| 70 |
'method' => 'POST', |
| 71 |
'timeout' => 45, |
| 72 |
'body' => $params, |
| 73 |
'cookies' => [], |
| 74 |
] |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the last time tracking data was sent. |
| 80 |
* |
| 81 |
* @return int|bool |
| 82 |
*/ |
| 83 |
private static function get_last_send_time() { |
| 84 |
return apply_filters( 'copy_the_code_tracker_last_send_time', get_option( 'copy_the_code_tracker_last_send', false ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get all the tracking data. |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public static function get_tracking_data() { |
| 93 |
$data = []; |
| 94 |
|
| 95 |
// General site info. |
| 96 |
$data['url'] = home_url(); |
| 97 |
$data['email'] = apply_filters( 'copy_the_code_tracker_admin_email', get_option( 'admin_email' ) ); |
| 98 |
$data['opt_in'] = get_option( 'copy_the_code_opt_in_welcome', 'yes' ); |
| 99 |
$data['active_themes'] = self::get_active_themes(); |
| 100 |
$data['inactive_themes'] = self::get_inactive_themes(); |
| 101 |
|
| 102 |
// WordPress Info. |
| 103 |
$data['wp'] = self::get_wordpress_info(); |
| 104 |
|
| 105 |
// Server Info. |
| 106 |
$data['server'] = self::get_server_info(); |
| 107 |
|
| 108 |
// Plugin info. |
| 109 |
$all_plugins = self::get_all_plugins(); |
| 110 |
$data['active_plugins'] = $all_plugins['active_plugins']; |
| 111 |
$data['inactive_plugins'] = $all_plugins['inactive_plugins']; |
| 112 |
|
| 113 |
// Count info. |
| 114 |
$data['users'] = self::get_user_counts(); |
| 115 |
|
| 116 |
$products = []; |
| 117 |
$orders = []; |
| 118 |
if ( class_exists( 'WC_Tracker' ) ) { |
| 119 |
$woo_data = WC_Tracker::get_tracking_data(); |
| 120 |
$products = $woo_data['products']; |
| 121 |
$orders = $woo_data['orders']; |
| 122 |
} |
| 123 |
$data['wc-products'] = $products; |
| 124 |
$data['wc-orders'] = $orders; |
| 125 |
|
| 126 |
return apply_filters( 'copy_the_code_tracker_data', $data ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get user totals based on user role. |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
private static function get_user_counts() { |
| 135 |
$user_count = []; |
| 136 |
$user_count_data = count_users(); |
| 137 |
$user_count['total'] = $user_count_data['total_users']; |
| 138 |
|
| 139 |
// Get user count based on user role. |
| 140 |
foreach ( $user_count_data['avail_roles'] as $role => $count ) { |
| 141 |
$user_count[ $role ] = $count; |
| 142 |
} |
| 143 |
|
| 144 |
return $user_count; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get all plugins grouped into activated or not. |
| 149 |
* |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
private static function get_all_plugins() { |
| 153 |
// Ensure get_plugins function is loaded. |
| 154 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 155 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 156 |
} |
| 157 |
|
| 158 |
$plugins = get_plugins(); |
| 159 |
$active_plugins_keys = get_option( 'active_plugins', [] ); |
| 160 |
$active_plugins = []; |
| 161 |
|
| 162 |
foreach ( $plugins as $k => $v ) { |
| 163 |
// Take care of formatting the data how we want it. |
| 164 |
$formatted = []; |
| 165 |
$formatted['name'] = strip_tags( $v['Name'] ); |
| 166 |
if ( isset( $v['Version'] ) ) { |
| 167 |
$formatted['version'] = strip_tags( $v['Version'] ); |
| 168 |
} |
| 169 |
if ( isset( $v['Author'] ) ) { |
| 170 |
$formatted['author'] = strip_tags( $v['Author'] ); |
| 171 |
} |
| 172 |
if ( isset( $v['Network'] ) ) { |
| 173 |
$formatted['network'] = strip_tags( $v['Network'] ); |
| 174 |
} |
| 175 |
if ( isset( $v['PluginURI'] ) ) { |
| 176 |
$formatted['plugin_uri'] = strip_tags( $v['PluginURI'] ); |
| 177 |
} |
| 178 |
if ( in_array( $k, $active_plugins_keys ) ) { // phpcs:ignore |
| 179 |
// Remove active plugins from list so we can show active and inactive separately. |
| 180 |
unset( $plugins[ $k ] ); |
| 181 |
$active_plugins[ $k ] = $formatted; |
| 182 |
} else { |
| 183 |
$plugins[ $k ] = $formatted; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return [ |
| 188 |
'active_plugins' => $active_plugins, |
| 189 |
'inactive_plugins' => $plugins, |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get server related info. |
| 195 |
* |
| 196 |
* @return array |
| 197 |
*/ |
| 198 |
private static function get_server_info() { |
| 199 |
$server_data = []; |
| 200 |
|
| 201 |
if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) { |
| 202 |
$server_data['software'] = $_SERVER['SERVER_SOFTWARE']; // @phpcs:ignore |
| 203 |
} |
| 204 |
|
| 205 |
if ( function_exists( 'phpversion' ) ) { |
| 206 |
$server_data['php_version'] = phpversion(); |
| 207 |
} |
| 208 |
|
| 209 |
if ( function_exists( 'ini_get' ) ) { |
| 210 |
$server_data['php_post_max_size'] = size_format( self::let_to_num( ini_get( 'post_max_size' ) ) ); |
| 211 |
$server_data['php_time_limt'] = ini_get( 'max_execution_time' ); |
| 212 |
$server_data['php_max_input_vars'] = ini_get( 'max_input_vars' ); |
| 213 |
$server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No'; |
| 214 |
} |
| 215 |
|
| 216 |
$database_version = self::get_server_database_version(); |
| 217 |
$server_data['mysql_version'] = $database_version['number']; |
| 218 |
|
| 219 |
$server_data['php_max_upload_size'] = size_format( wp_max_upload_size() ); |
| 220 |
$server_data['php_default_timezone'] = date_default_timezone_get(); |
| 221 |
$server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No'; |
| 222 |
$server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No'; |
| 223 |
$server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No'; |
| 224 |
|
| 225 |
return $server_data; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Retrieves the MySQL server version. Based on $wpdb. |
| 230 |
* |
| 231 |
* @since 3.4.1 |
| 232 |
* @return array Vesion information. |
| 233 |
*/ |
| 234 |
public static function get_server_database_version() { |
| 235 |
global $wpdb; |
| 236 |
|
| 237 |
if ( empty( $wpdb->is_mysql ) ) { |
| 238 |
return [ |
| 239 |
'string' => '', |
| 240 |
'number' => '', |
| 241 |
]; |
| 242 |
} |
| 243 |
|
| 244 |
// phpcs:disable WordPress.DB.RestrictedFunctions, PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved |
| 245 |
if ( $wpdb->use_mysqli ) { |
| 246 |
$server_info = mysqli_get_server_info( $wpdb->dbh ); |
| 247 |
} else { |
| 248 |
$server_info = mysql_get_server_info( $wpdb->dbh ); // phpcs:ignore |
| 249 |
} |
| 250 |
// phpcs:enable WordPress.DB.RestrictedFunctions, PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved |
| 251 |
|
| 252 |
return [ |
| 253 |
'string' => $server_info, |
| 254 |
'number' => preg_replace( '/([^\d.]+).*/', '', $server_info ), |
| 255 |
]; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Get WordPress related data. |
| 260 |
* |
| 261 |
* @return array |
| 262 |
*/ |
| 263 |
private static function get_wordpress_info() { |
| 264 |
$wp_data = []; |
| 265 |
|
| 266 |
$memory = self::let_to_num( WP_MEMORY_LIMIT ); |
| 267 |
|
| 268 |
if ( function_exists( 'memory_get_usage' ) ) { |
| 269 |
$system_memory = self::let_to_num( @ini_get( 'memory_limit' ) ); // phpcs:ignore |
| 270 |
$memory = max( $memory, $system_memory ); |
| 271 |
} |
| 272 |
|
| 273 |
// WordPress 5.5+ environment type specification. |
| 274 |
// 'production' is the default in WP, thus using it as a default here, too. |
| 275 |
$environment_type = 'production'; |
| 276 |
if ( function_exists( 'wp_get_environment_type' ) ) { |
| 277 |
$environment_type = wp_get_environment_type(); |
| 278 |
} |
| 279 |
|
| 280 |
$wp_data['memory_limit'] = size_format( $memory ); |
| 281 |
$wp_data['debug_mode'] = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No'; |
| 282 |
$wp_data['locale'] = get_locale(); |
| 283 |
$wp_data['version'] = get_bloginfo( 'version' ); |
| 284 |
$wp_data['multisite'] = is_multisite() ? 'Yes' : 'No'; |
| 285 |
$wp_data['env_type'] = $environment_type; |
| 286 |
|
| 287 |
return $wp_data; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Notation to numbers. |
| 292 |
* |
| 293 |
* This function transforms the php.ini notation for numbers (like '2M') to an integer. |
| 294 |
* |
| 295 |
* @param string $size Size value. |
| 296 |
* @return int |
| 297 |
*/ |
| 298 |
public static function let_to_num( $size ) { |
| 299 |
$l = substr( $size, -1 ); |
| 300 |
$ret = (int) substr( $size, 0, -1 ); |
| 301 |
switch ( strtoupper( $l ) ) { |
| 302 |
case 'P': |
| 303 |
$ret *= 1024; |
| 304 |
// No break. |
| 305 |
case 'T': |
| 306 |
$ret *= 1024; |
| 307 |
// No break. |
| 308 |
case 'G': |
| 309 |
$ret *= 1024; |
| 310 |
// No break. |
| 311 |
case 'M': |
| 312 |
$ret *= 1024; |
| 313 |
// No break. |
| 314 |
case 'K': |
| 315 |
$ret *= 1024; |
| 316 |
// No break. |
| 317 |
} |
| 318 |
return $ret; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Get the current theme info, theme name and version. |
| 323 |
* |
| 324 |
* @return array |
| 325 |
*/ |
| 326 |
public static function get_active_themes() { |
| 327 |
$theme_data = wp_get_theme(); |
| 328 |
$theme_child_theme = is_child_theme(); |
| 329 |
$theme_wc_support = current_theme_supports( 'copy_the_code' ); |
| 330 |
$theme_is_block_theme = self::current_theme_is_fse(); |
| 331 |
|
| 332 |
$theme_slug = sanitize_title( $theme_data->Name ); // phpcs:ignore |
| 333 |
|
| 334 |
$data = []; |
| 335 |
|
| 336 |
$data[ $theme_slug ] = [ |
| 337 |
'name' => $theme_data->Name, // @phpcs:ignore |
| 338 |
'version' => $theme_data->Version, // @phpcs:ignore |
| 339 |
'child_theme' => $theme_child_theme, |
| 340 |
'wc_support' => $theme_wc_support, |
| 341 |
'block_theme' => $theme_is_block_theme, |
| 342 |
]; |
| 343 |
|
| 344 |
return $data; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Get the current theme info, theme name and version. |
| 349 |
* |
| 350 |
* @return array |
| 351 |
*/ |
| 352 |
public static function get_inactive_themes() { |
| 353 |
$themes = wp_get_themes(); |
| 354 |
if ( empty( $themes ) ) { |
| 355 |
return []; |
| 356 |
} |
| 357 |
|
| 358 |
$inactive_themes = []; |
| 359 |
|
| 360 |
foreach ( $themes as $theme_slug => $theme_data ) { |
| 361 |
$inactive_themes[ $theme_slug ] = [ |
| 362 |
'name' => $theme_data->Name, // @phpcs:ignore |
| 363 |
'version' => $theme_data->Version, // @phpcs:ignore |
| 364 |
'child_theme' => '', |
| 365 |
'wc_support' => '', |
| 366 |
'block_theme' => '', |
| 367 |
]; |
| 368 |
} |
| 369 |
|
| 370 |
return $inactive_themes; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Get the current theme info, theme name and version. |
| 375 |
*/ |
| 376 |
public static function current_theme_is_fse() { |
| 377 |
if ( function_exists( 'wp_is_block_theme' ) ) { |
| 378 |
return (bool) wp_is_block_theme(); |
| 379 |
} |
| 380 |
if ( function_exists( 'gutenberg_is_fse_theme' ) ) { |
| 381 |
return (bool) gutenberg_is_fse_theme(); |
| 382 |
} |
| 383 |
|
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Kicking this off by calling 'get_instance()' method |
| 391 |
*/ |
| 392 |
Copy_The_Code_Analytics::get_instance(); |
| 393 |
|
| 394 |
endif; |
| 395 |
|