| 1 |
<?php |
| 2 |
/** |
| 3 |
* BSF analytics stat class file. |
| 4 |
* |
| 5 |
* @package bsf-analytics |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! class_exists( 'BSF_Analytics_Stats' ) ) { |
| 13 |
/** |
| 14 |
* BSF analytics stat class. |
| 15 |
*/ |
| 16 |
class BSF_Analytics_Stats { |
| 17 |
|
| 18 |
/** |
| 19 |
* Active plugins. |
| 20 |
* |
| 21 |
* Holds the sites active plugins list. |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $plugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Instance of BSF_Analytics_Stats. |
| 29 |
* |
| 30 |
* Holds only the first object of class. |
| 31 |
* |
| 32 |
* @var object |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create only once instance of a class. |
| 38 |
* |
| 39 |
* @return object |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public static function instance() { |
| 43 |
if ( null === self::$instance ) { |
| 44 |
self::$instance = new self(); |
| 45 |
} |
| 46 |
|
| 47 |
return self::$instance; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get stats. |
| 52 |
* |
| 53 |
* @return array stats data. |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
public function get_stats() { |
| 57 |
return apply_filters( 'bsf_core_stats', $this->get_default_stats() ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Retrieve stats for site. |
| 62 |
* |
| 63 |
* @return array stats data. |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
private function get_default_stats() { |
| 67 |
return array( |
| 68 |
'graupi_version' => defined( 'BSF_UPDATER_VERSION' ) ? BSF_UPDATER_VERSION : false, |
| 69 |
'domain_name' => get_site_url(), |
| 70 |
'php_os' => PHP_OS, |
| 71 |
'server_software' => isset( $_SERVER['SERVER_SOFTWARE'] ) ? wp_kses( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ), [] ) : '', |
| 72 |
'mysql_version' => $this->get_mysql_version(), |
| 73 |
'php_version' => $this->get_php_version(), |
| 74 |
'php_max_input_vars' => ini_get( 'max_input_vars' ), // phpcs:ignore:PHPCompatibility.IniDirectives.NewIniDirectives.max_input_varsFound |
| 75 |
'php_post_max_size' => ini_get( 'post_max_size' ), |
| 76 |
'php_max_execution_time' => ini_get( 'max_execution_time' ), |
| 77 |
'php_memory_limit' => ini_get( 'memory_limit' ), |
| 78 |
'zip_installed' => extension_loaded( 'zip' ), |
| 79 |
'imagick_availabile' => extension_loaded( 'imagick' ), |
| 80 |
'xmlreader_exists' => class_exists( 'XMLReader' ), |
| 81 |
'gd_available' => extension_loaded( 'gd' ), |
| 82 |
'curl_version' => $this->get_curl_version(), |
| 83 |
'curl_ssl_version' => $this->get_curl_ssl_version(), |
| 84 |
'is_writable' => $this->is_content_writable(), |
| 85 |
|
| 86 |
'wp_version' => get_bloginfo( 'version' ), |
| 87 |
'user_count' => $this->get_user_count(), |
| 88 |
'posts_count' => wp_count_posts()->publish, |
| 89 |
'page_count' => wp_count_posts( 'page' )->publish, |
| 90 |
'site_language' => get_locale(), |
| 91 |
'timezone' => wp_timezone_string(), |
| 92 |
'is_ssl' => is_ssl(), |
| 93 |
'is_multisite' => is_multisite(), |
| 94 |
'network_url' => network_site_url(), |
| 95 |
'external_object_cache' => (bool) wp_using_ext_object_cache(), |
| 96 |
'wp_debug' => WP_DEBUG, |
| 97 |
'wp_debug_display' => WP_DEBUG_DISPLAY, |
| 98 |
'script_debug' => SCRIPT_DEBUG, |
| 99 |
|
| 100 |
'active_plugins' => $this->get_active_plugins(), |
| 101 |
|
| 102 |
'active_theme' => get_template(), |
| 103 |
'active_stylesheet' => get_stylesheet(), |
| 104 |
|
| 105 |
'admin_email' => get_option( 'admin_email' ), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get installed PHP version. |
| 111 |
* |
| 112 |
* @return float PHP version. |
| 113 |
* @since 1.0.0 |
| 114 |
*/ |
| 115 |
private function get_php_version() { |
| 116 |
if ( defined( 'PHP_MAJOR_VERSION' ) && defined( 'PHP_MINOR_VERSION' ) && defined( 'PHP_RELEASE_VERSION' ) ) { // phpcs:ignore |
| 117 |
return PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION; |
| 118 |
} |
| 119 |
|
| 120 |
return phpversion(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* User count on site. |
| 125 |
* |
| 126 |
* @return int User count. |
| 127 |
* @since 1.0.0 |
| 128 |
*/ |
| 129 |
private function get_user_count() { |
| 130 |
if ( is_multisite() ) { |
| 131 |
$user_count = get_user_count(); |
| 132 |
} else { |
| 133 |
$count = count_users(); |
| 134 |
$user_count = $count['total_users']; |
| 135 |
} |
| 136 |
|
| 137 |
return $user_count; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get active plugin's data. |
| 142 |
* |
| 143 |
* @return array active plugin's list. |
| 144 |
* @since 1.0.0 |
| 145 |
*/ |
| 146 |
private function get_active_plugins() { |
| 147 |
if ( ! $this->plugins ) { |
| 148 |
// Ensure get_plugin_data function is loaded. |
| 149 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 150 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 151 |
} |
| 152 |
|
| 153 |
$plugins = wp_get_active_and_valid_plugins(); |
| 154 |
$plugins = array_map( 'get_plugin_data', $plugins ); |
| 155 |
$this->plugins = array_map( array( $this, 'format_plugin' ), $plugins ); |
| 156 |
} |
| 157 |
|
| 158 |
return $this->plugins; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Format plugin data. |
| 163 |
* |
| 164 |
* @param string $plugin plugin. |
| 165 |
* @return array formatted plugin data. |
| 166 |
* @since 1.0.0 |
| 167 |
*/ |
| 168 |
public function format_plugin( $plugin ) { |
| 169 |
return array( |
| 170 |
'name' => html_entity_decode( $plugin['Name'], ENT_COMPAT, 'UTF-8' ), |
| 171 |
'url' => $plugin['PluginURI'], |
| 172 |
'version' => $plugin['Version'], |
| 173 |
'slug' => $plugin['TextDomain'], |
| 174 |
'author_name' => html_entity_decode( wp_strip_all_tags( $plugin['Author'] ), ENT_COMPAT, 'UTF-8' ), |
| 175 |
'author_url' => $plugin['AuthorURI'], |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Curl SSL version. |
| 181 |
* |
| 182 |
* @return float SSL version. |
| 183 |
* @since 1.0.0 |
| 184 |
*/ |
| 185 |
private function get_curl_ssl_version() { |
| 186 |
$curl = array(); |
| 187 |
if ( function_exists( 'curl_version' ) ) { |
| 188 |
$curl = curl_version(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_version |
| 189 |
} |
| 190 |
|
| 191 |
return isset( $curl['ssl_version'] ) ? $curl['ssl_version'] : false; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get cURL version. |
| 196 |
* |
| 197 |
* @return float cURL version. |
| 198 |
* @since 1.0.0 |
| 199 |
*/ |
| 200 |
private function get_curl_version() { |
| 201 |
if ( function_exists( 'curl_version' ) ) { |
| 202 |
$curl = curl_version(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_version |
| 203 |
} |
| 204 |
|
| 205 |
return isset( $curl['version'] ) ? $curl['version'] : false; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get MySQL version. |
| 210 |
* |
| 211 |
* @return float MySQL version. |
| 212 |
* @since 1.0.0 |
| 213 |
*/ |
| 214 |
private function get_mysql_version() { |
| 215 |
global $wpdb; |
| 216 |
return $wpdb->db_version(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Check if content directory is writable. |
| 221 |
* |
| 222 |
* @return bool |
| 223 |
* @since 1.0.0 |
| 224 |
*/ |
| 225 |
private function is_content_writable() { |
| 226 |
$upload_dir = wp_upload_dir(); |
| 227 |
return wp_is_writable( $upload_dir['basedir'] ); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Polyfill for sites using WP version less than 5.3 |
| 234 |
*/ |
| 235 |
if ( ! function_exists( 'wp_timezone_string' ) ) { |
| 236 |
/** |
| 237 |
* Get timezone string. |
| 238 |
* |
| 239 |
* @return string timezone string. |
| 240 |
* @since 1.0.0 |
| 241 |
*/ |
| 242 |
function wp_timezone_string() { |
| 243 |
$timezone_string = get_option( 'timezone_string' ); |
| 244 |
|
| 245 |
if ( $timezone_string ) { |
| 246 |
return $timezone_string; |
| 247 |
} |
| 248 |
|
| 249 |
$offset = (float) get_option( 'gmt_offset' ); |
| 250 |
$hours = (int) $offset; |
| 251 |
$minutes = ( $offset - $hours ); |
| 252 |
|
| 253 |
$sign = ( $offset < 0 ) ? '-' : '+'; |
| 254 |
$abs_hour = abs( $hours ); |
| 255 |
$abs_mins = abs( $minutes * 60 ); |
| 256 |
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 257 |
|
| 258 |
return $tz_offset; |
| 259 |
} |
| 260 |
} |
| 261 |
|