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