|
| 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'] ) ? filter_var( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ), FILTER_SANITIZE_STRING ) : '', |
| 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 | + 'site_language' => get_locale(), |
| 89 | + 'timezone' => wp_timezone_string(), |
| 90 | + 'is_ssl' => is_ssl(), |
| 91 | + 'is_multisite' => is_multisite(), |
| 92 | + 'network_url' => network_site_url(), |
| 93 | + 'external_object_cache' => (bool) wp_using_ext_object_cache(), |
| 94 | + 'wp_debug' => WP_DEBUG, |
| 95 | + 'wp_debug_display' => WP_DEBUG_DISPLAY, |
| 96 | + 'script_debug' => SCRIPT_DEBUG, |
| 97 | + |
| 98 | + 'active_plugins' => $this->get_active_plugins(), |
| 99 | + |
| 100 | + 'active_theme' => get_template(), |
| 101 | + 'active_stylesheet' => get_stylesheet(), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get installed PHP version. |
| 107 | + * |
| 108 | + * @return float PHP version. |
| 109 | + * @since 1.0.0 |
| 110 | + */ |
| 111 | + private function get_php_version() { |
| 112 | + if ( defined( 'PHP_MAJOR_VERSION' ) && defined( 'PHP_MINOR_VERSION' ) && defined( 'PHP_RELEASE_VERSION' ) ) { // phpcs:ignore |
| 113 | + return PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION; |
| 114 | + } |
| 115 | + |
| 116 | + return phpversion(); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * User count on site. |
| 121 | + * |
| 122 | + * @return int User count. |
| 123 | + * @since 1.0.0 |
| 124 | + */ |
| 125 | + private function get_user_count() { |
| 126 | + if ( is_multisite() ) { |
| 127 | + $user_count = get_user_count(); |
| 128 | + } else { |
| 129 | + $count = count_users(); |
| 130 | + $user_count = $count['total_users']; |
| 131 | + } |
| 132 | + |
| 133 | + return $user_count; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get active plugin's data. |
| 138 | + * |
| 139 | + * @return array active plugin's list. |
| 140 | + * @since 1.0.0 |
| 141 | + */ |
| 142 | + private function get_active_plugins() { |
| 143 | + if ( ! $this->plugins ) { |
| 144 | + // Ensure get_plugin_data function is loaded. |
| 145 | + if ( ! function_exists( 'get_plugin_data' ) ) { |
| 146 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 147 | + } |
| 148 | + |
| 149 | + $plugins = wp_get_active_and_valid_plugins(); |
| 150 | + $plugins = array_map( 'get_plugin_data', $plugins ); |
| 151 | + $this->plugins = array_map( array( $this, 'format_plugin' ), $plugins ); |
| 152 | + } |
| 153 | + |
| 154 | + return $this->plugins; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Format plugin data. |
| 159 | + * |
| 160 | + * @param string $plugin plugin. |
| 161 | + * @return array formatted plugin data. |
| 162 | + * @since 1.0.0 |
| 163 | + */ |
| 164 | + public function format_plugin( $plugin ) { |
| 165 | + return array( |
| 166 | + 'name' => html_entity_decode( $plugin['Name'], ENT_COMPAT, 'UTF-8' ), |
| 167 | + 'url' => $plugin['PluginURI'], |
| 168 | + 'version' => $plugin['Version'], |
| 169 | + 'slug' => $plugin['TextDomain'], |
| 170 | + 'author_name' => html_entity_decode( wp_strip_all_tags( $plugin['Author'] ), ENT_COMPAT, 'UTF-8' ), |
| 171 | + 'author_url' => $plugin['AuthorURI'], |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Curl SSL version. |
| 177 | + * |
| 178 | + * @return float SSL version. |
| 179 | + * @since 1.0.0 |
| 180 | + */ |
| 181 | + private function get_curl_ssl_version() { |
| 182 | + $curl = array(); |
| 183 | + if ( function_exists( 'curl_version' ) ) { |
| 184 | + $curl = curl_version(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_version |
| 185 | + } |
| 186 | + |
| 187 | + return isset( $curl['ssl_version'] ) ? $curl['ssl_version'] : false; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Get cURL version. |
| 192 | + * |
| 193 | + * @return float cURL version. |
| 194 | + * @since 1.0.0 |
| 195 | + */ |
| 196 | + private function get_curl_version() { |
| 197 | + $curl = array(); |
| 198 | + if ( function_exists( 'curl_version' ) ) { |
| 199 | + $curl = curl_version(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_version |
| 200 | + } |
| 201 | + |
| 202 | + return isset( $curl['version'] ) ? $curl['version'] : false; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Get MySQL version. |
| 207 | + * |
| 208 | + * @return float MySQL version. |
| 209 | + * @since 1.0.0 |
| 210 | + */ |
| 211 | + private function get_mysql_version() { |
| 212 | + global $wpdb; |
| 213 | + return $wpdb->db_version(); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Check if content directory is writable. |
| 218 | + * |
| 219 | + * @return bool |
| 220 | + * @since 1.0.0 |
| 221 | + */ |
| 222 | + private function is_content_writable() { |
| 223 | + $upload_dir = wp_upload_dir(); |
| 224 | + return wp_is_writable( $upload_dir['basedir'] ); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +/** |
| 230 | + * Polyfill for sites using WP version less than 5.3 |
| 231 | + */ |
| 232 | +if ( ! function_exists( 'wp_timezone_string' ) ) { |
| 233 | + /** |
| 234 | + * Get timezone string. |
| 235 | + * |
| 236 | + * @return string timezone string. |
| 237 | + * @since 1.0.0 |
| 238 | + */ |
| 239 | + function wp_timezone_string() { |
| 240 | + $timezone_string = get_option( 'timezone_string' ); |
| 241 | + |
| 242 | + if ( $timezone_string ) { |
| 243 | + return $timezone_string; |
| 244 | + } |
| 245 | + |
| 246 | + $offset = (float) get_option( 'gmt_offset' ); |
| 247 | + $hours = (int) $offset; |
| 248 | + $minutes = ( $offset - $hours ); |
| 249 | + |
| 250 | + $sign = ( $offset < 0 ) ? '-' : '+'; |
| 251 | + $abs_hour = abs( $hours ); |
| 252 | + $abs_mins = abs( $minutes * 60 ); |
| 253 | + $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 254 | + |
| 255 | + return $tz_offset; |
| 256 | + } |
| 257 | +} |
0 commit comments