class-evf-stats.php
343 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class |
| 4 | * |
| 5 | * EVF_Stats |
| 6 | * |
| 7 | * @package EverestForms/Stats |
| 8 | * @since 1.9.8 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'EVF_Stats' ) ) { |
| 16 | |
| 17 | /** |
| 18 | * EVF_Stats class. |
| 19 | */ |
| 20 | class EVF_Stats { |
| 21 | /** |
| 22 | * Constructor of the class. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 26 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 27 | } |
| 28 | add_filter( 'everest_forms_logger_data', array( $this, 'provide_tracking_data' ) ); |
| 29 | |
| 30 | add_filter( |
| 31 | 'pre_option_everest_forms_sdk_enable_logger', |
| 32 | function ( $enabled ) { |
| 33 | return 'yes' === get_option( 'everest_forms_allow_usage_tracking' ) ? 'yes' : 'no'; |
| 34 | } |
| 35 | ); |
| 36 | |
| 37 | add_action( |
| 38 | 'update_option_everest_forms_sdk_enable_logger', |
| 39 | function ( $old_value, $value ) { |
| 40 | if ( 'yes' === $value ) { |
| 41 | update_option( 'everest_forms_allow_usage_tracking', 'yes' ); |
| 42 | } elseif ( 'no' === $value ) { |
| 43 | update_option( 'everest_forms_allow_usage_tracking', 'no' ); |
| 44 | } |
| 45 | }, |
| 46 | 10, |
| 47 | 2 |
| 48 | ); |
| 49 | add_action( 'update_option_everest_forms_allow_usage_tracking', array( $this, 'run_on_save' ), 10, 3 ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get product license key. |
| 54 | */ |
| 55 | public function get_base_product_license() { |
| 56 | return get_option( 'everest-forms-pro_license_key' ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get Pro addon file. |
| 61 | */ |
| 62 | public function get_base_product() { |
| 63 | if ( $this->is_premium() ) { |
| 64 | return 'everest-forms-pro/everest-forms-pro.php'; |
| 65 | } else { |
| 66 | return 'everest-forms/everest-forms.php'; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Check the is premium or not. |
| 72 | * |
| 73 | * @return boolean |
| 74 | */ |
| 75 | public function is_premium() { |
| 76 | return ( false === evf_get_license_plan() ) ? false : true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the number of entries |
| 81 | * |
| 82 | * @return int The number of entries |
| 83 | */ |
| 84 | public function get_entry_count() { |
| 85 | global $wpdb; |
| 86 | |
| 87 | return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}evf_entries" ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the number of published forms |
| 92 | * |
| 93 | * @return int The number of published forms |
| 94 | */ |
| 95 | public function get_form_count() { |
| 96 | global $wpdb; |
| 97 | |
| 98 | return $wpdb->get_var( |
| 99 | $wpdb->prepare( |
| 100 | "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type=%s AND post_status=%s", |
| 101 | 'everest_form', |
| 102 | 'publish' |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the plugin information of active plugins and the base plugin |
| 109 | * |
| 110 | * @return array The plugin information of active plugins and the base plugin including product name, version, type, slug, form count, entry count, and license key if it's a premium plugin. |
| 111 | */ |
| 112 | public function get_plugin_lists() { |
| 113 | $is_premium = $this->is_premium(); |
| 114 | $base_product = $this->get_base_product(); |
| 115 | $base_product_name = $is_premium ? 'Everest Forms Pro' : 'Everest Forms'; |
| 116 | |
| 117 | // Build base product metadata. |
| 118 | $product_meta = array( |
| 119 | 'active_features' => get_option( 'everest_forms_enabled_features', array() ), |
| 120 | 'form_count' => $this->get_form_count(), |
| 121 | 'entry_count' => $this->get_entry_count(), |
| 122 | ); |
| 123 | |
| 124 | if ( $is_premium ) { |
| 125 | $product_meta['license_key'] = $this->get_base_product_license(); |
| 126 | } |
| 127 | |
| 128 | // Initialize with base product. |
| 129 | $addons_data = array( |
| 130 | $base_product => array( |
| 131 | 'product_name' => $base_product_name, |
| 132 | 'product_version' => $is_premium ? EFP_VERSION : evf()->version, |
| 133 | 'product_meta' => $product_meta, |
| 134 | 'product_type' => 'plugin', |
| 135 | 'product_slug' => $base_product, |
| 136 | 'is_premium' => $is_premium, |
| 137 | ), |
| 138 | ); |
| 139 | |
| 140 | // Get installed and active plugins - cache this. |
| 141 | if ( ! function_exists( 'get_plugins' ) ) { |
| 142 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 143 | } |
| 144 | $all_plugins = get_plugins(); |
| 145 | $active_plugins = get_option( 'active_plugins', array() ); |
| 146 | $installed_plugin_slugs = array_keys( $all_plugins ); |
| 147 | |
| 148 | // Get addons list. |
| 149 | $extension_data = evf_get_json_file_contents( 'assets/extensions-json/sections/all_extensions.json' ); |
| 150 | if ( empty( $extension_data->products ) ) { |
| 151 | return $addons_data; |
| 152 | } |
| 153 | |
| 154 | // Process only active addons. |
| 155 | foreach ( $extension_data->products as $addon ) { |
| 156 | $addon_file = $addon->slug . '/' . $addon->slug . '.php'; |
| 157 | |
| 158 | // Skip if not installed or not active. |
| 159 | if ( ! in_array( $addon_file, $installed_plugin_slugs, true ) || ! is_plugin_active( $addon_file ) ) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | $addons_data['active_addons'][ $addon->slug ] = array( |
| 164 | 'product_name' => isset( $addon->name ) ? trim( $addon->name ) : '', |
| 165 | 'product_version' => $this->get_addon_version( $addon, $all_plugins, $active_plugins ), |
| 166 | 'product_type' => 'addon', |
| 167 | 'product_slug' => $addon->slug, |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | return $addons_data; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get the version of an addon |
| 176 | * |
| 177 | * @since 3.4.2 |
| 178 | * |
| 179 | * @param object $addon The addon object with slug property. |
| 180 | * @param array $all_plugins Array of all installed plugins. |
| 181 | * @param array $active_plugins Array of active plugin file paths. |
| 182 | * @return string The addon version or empty string if not found. |
| 183 | */ |
| 184 | private function get_addon_version( $addon, $all_plugins, $active_plugins ) { |
| 185 | $addon_file = $addon->slug . '/' . $addon->slug . '.php'; |
| 186 | |
| 187 | // First, check the standard addon file. |
| 188 | if ( isset( $all_plugins[ $addon_file ]['Version'] ) ) { |
| 189 | return $all_plugins[ $addon_file ]['Version']; |
| 190 | } |
| 191 | |
| 192 | // Fallback: check active_plugins for any file in the addon folder. |
| 193 | foreach ( $active_plugins as $active_file ) { |
| 194 | if ( 0 === strpos( $active_file, $addon->slug . '/' ) && isset( $all_plugins[ $active_file ]['Version'] ) ) { |
| 195 | return $all_plugins[ $active_file ]['Version']; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return ''; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Check if usage tracking is allowed |
| 204 | * |
| 205 | * @return bool True if usage tracking is allowed, False otherwise |
| 206 | */ |
| 207 | public function is_usage_allowed() { |
| 208 | |
| 209 | return 'yes' === get_option( 'everest_forms_allow_usage_tracking', 'no' ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Initialize usage tracking |
| 214 | * This function adds an action hook that runs the 'process' method on a bi-weekly basis, |
| 215 | * only when the WordPress cron system is running. |
| 216 | */ |
| 217 | public function init_usage() { |
| 218 | |
| 219 | if ( wp_doing_cron() ) { |
| 220 | add_action( 'everest_forms_biweekly_scheduled_events', array( $this, 'process' ) ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Run the process once when user gives consent. |
| 226 | * |
| 227 | * @since 3.4.2 Update logger flag option on settings save. |
| 228 | * |
| 229 | * @param mixed $old_value The old value of the option. |
| 230 | * @param mixed $value The new value of the option. |
| 231 | * @param string $option The name of the option. |
| 232 | * @return mixed The new value of the option. |
| 233 | */ |
| 234 | public function run_on_save( $old_value, $value, $option ) { |
| 235 | update_option( 'everest_forms_logger_flag', $value ); |
| 236 | return $value; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the total number of sites in a multi-site installation |
| 241 | * |
| 242 | * @return int The total number of sites in the multi-site installation. If the current installation is not multi-site, returns 1. |
| 243 | */ |
| 244 | private function get_sites_total() { |
| 245 | |
| 246 | return function_exists( 'get_blog_count' ) ? (int) get_blog_count() : 1; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get the timezone offset |
| 251 | * Returns the timezone string in the format +00:00 or -00:00 if WordPress version is greater than 5.3. |
| 252 | * Otherwise, get the timezone offset from the 'timezone_string' option or 'gmt_offset' option. |
| 253 | * |
| 254 | * @return string The timezone offset |
| 255 | */ |
| 256 | private function get_timezone_offset() { |
| 257 | |
| 258 | // It was added in WordPress 5.3. |
| 259 | if ( function_exists( 'wp_timezone_string' ) ) { |
| 260 | return wp_timezone_string(); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * The code below is basically a copy-paste from that function. |
| 265 | */ |
| 266 | |
| 267 | $timezone_string = get_option( 'timezone_string' ); |
| 268 | |
| 269 | if ( $timezone_string ) { |
| 270 | return $timezone_string; |
| 271 | } |
| 272 | |
| 273 | $offset = (float) get_option( 'gmt_offset' ); |
| 274 | $hours = (int) $offset; |
| 275 | $minutes = ( $offset - $hours ); |
| 276 | |
| 277 | $sign = ( $offset < 0 ) ? '-' : '+'; |
| 278 | $abs_hour = abs( $hours ); |
| 279 | $abs_mins = abs( $minutes * 60 ); |
| 280 | $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 281 | |
| 282 | return $tz_offset; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Calculates the number of days since the plugin was installed. |
| 287 | * |
| 288 | * Retrieves the installation date from the 'everest_forms_install' option. |
| 289 | * If the value is not numeric, it attempts to convert it to a timestamp. |
| 290 | * Returns the number of full days elapsed since installation. |
| 291 | * |
| 292 | * @since 3.4.2 |
| 293 | * @return int Number of days since the plugin was installed. |
| 294 | */ |
| 295 | public static function get_install_days() { |
| 296 | $install_time = get_option( 'everest_forms_install', time() ); |
| 297 | if ( ! is_numeric( $install_time ) ) { |
| 298 | $install_time = strtotime( $install_time ); |
| 299 | } |
| 300 | $current_time = time(); |
| 301 | $days_since_install = floor( ( $current_time - $install_time ) / DAY_IN_SECONDS ); |
| 302 | return $days_since_install; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Callback for SDK tracking filter. |
| 308 | * |
| 309 | * @return array Tracking data payload. |
| 310 | * @since 3.4.2 |
| 311 | */ |
| 312 | public function provide_tracking_data() { |
| 313 | if ( ! $this->is_usage_allowed() ) { |
| 314 | return array(); |
| 315 | } |
| 316 | |
| 317 | global $wpdb; |
| 318 | $data = array(); |
| 319 | $data['product_data'] = $this->get_plugin_lists(); |
| 320 | $data['admin_email'] = get_bloginfo( 'admin_email' ); |
| 321 | $data['website_url'] = get_bloginfo( 'url' ); |
| 322 | $data['install_days'] = $this->get_install_days() ?? null; |
| 323 | $data['wp_version'] = get_bloginfo( 'version' ); |
| 324 | $data['php_version'] = phpversion(); |
| 325 | $data['mysql_version'] = $wpdb->db_version(); |
| 326 | $data['server_software'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; |
| 327 | $data['is_ssl'] = is_ssl(); |
| 328 | $data['is_multisite'] = is_multisite(); |
| 329 | $data['is_wp_com'] = defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 330 | $data['is_wp_com_vip'] = ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) || ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ); |
| 331 | $data['is_wp_cache'] = defined( 'WP_CACHE' ) && WP_CACHE; |
| 332 | $data['multi_site_count'] = $this->get_sites_total(); |
| 333 | $data['locale'] = get_locale(); |
| 334 | $data['timezone'] = $this->get_timezone_offset(); |
| 335 | $data['base_product'] = $this->get_base_product(); |
| 336 | |
| 337 | return $data; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | new EVF_Stats(); |
| 343 |