class-evf-stats.php
326 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 | /** |
| 23 | * Remote URl Constant. |
| 24 | */ |
| 25 | const REMOTE_URL = 'https://stats.wpeverest.com/wp-json/tgreporting/v1/process-free/'; |
| 26 | |
| 27 | const LAST_RUN_STAMP = 'everest_forms_send_usage_last_run'; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Constructor of the class. |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 35 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 36 | } |
| 37 | add_action( 'init', array( $this, 'init_usage' ), 4 ); |
| 38 | add_action( 'update_option_everest_forms_allow_usage_tracking', array( $this, 'run_on_save' ), 10, 3 ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get product license key. |
| 43 | */ |
| 44 | public function get_base_product_license() { |
| 45 | return get_option( 'everest-forms-pro_license_key' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get Pro addon file. |
| 50 | */ |
| 51 | public function get_base_product() { |
| 52 | if ( $this->is_premium() ) { |
| 53 | return 'everest-forms-pro/everest-forms-pro.php'; |
| 54 | } else { |
| 55 | return 'everest-forms/everest-forms.php'; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Check the is premium or not. |
| 61 | * |
| 62 | * @return boolean |
| 63 | */ |
| 64 | public function is_premium() { |
| 65 | return ( false === evf_get_license_plan() ) ? false : true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the number of entries |
| 70 | * |
| 71 | * @return int The number of entries |
| 72 | */ |
| 73 | public function get_entry_count() { |
| 74 | global $wpdb; |
| 75 | |
| 76 | return $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}evf_entries" ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the number of published forms |
| 81 | * |
| 82 | * @return int The number of published forms |
| 83 | */ |
| 84 | public function get_form_count() { |
| 85 | global $wpdb; |
| 86 | |
| 87 | return $wpdb->get_var( |
| 88 | $wpdb->prepare( |
| 89 | "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type=%s AND post_status=%s", |
| 90 | 'everest_form', |
| 91 | 'publish' |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the plugin information of active plugins and the base plugin |
| 98 | * |
| 99 | * @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. |
| 100 | */ |
| 101 | public function get_plugin_lists() { |
| 102 | |
| 103 | $is_premium = $this->is_premium(); |
| 104 | |
| 105 | $base_product = $this->get_base_product(); |
| 106 | |
| 107 | $active_plugins = get_option( 'active_plugins', array() ); |
| 108 | |
| 109 | $base_product_name = $is_premium ? 'Everest Forms Pro' : 'Everest Forms'; |
| 110 | |
| 111 | $product_meta = array(); |
| 112 | |
| 113 | $product_meta['form_count'] = $this->get_form_count(); |
| 114 | |
| 115 | $product_meta['entry_count'] = $this->get_entry_count(); |
| 116 | |
| 117 | $license_key = $this->get_base_product_license(); |
| 118 | |
| 119 | if ( $is_premium ) { |
| 120 | $product_meta['license_key'] = $license_key; |
| 121 | } |
| 122 | |
| 123 | $addons_data = array( |
| 124 | $base_product => array( |
| 125 | 'product_name' => $base_product_name, |
| 126 | 'product_version' => $is_premium ? EFP_VERSION : evf()->version, |
| 127 | 'product_meta' => $product_meta, |
| 128 | 'product_type' => 'plugin', |
| 129 | 'product_slug' => $base_product, |
| 130 | 'is_premium' => $is_premium, |
| 131 | ), |
| 132 | ); |
| 133 | |
| 134 | foreach ( $active_plugins as $plugin ) { |
| 135 | |
| 136 | $addon_file = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin; |
| 137 | $addon_file_data = get_plugin_data( $addon_file ); |
| 138 | if ( $base_product !== $plugin ) { |
| 139 | $addons_data[ $plugin ] = array( |
| 140 | 'product_name' => isset( $addon_file_data['Name'] ) ? trim( $addon_file_data['Name'] ) : '', |
| 141 | 'product_version' => isset( $addon_file_data['Version'] ) ? trim( $addon_file_data['Version'] ) : '', |
| 142 | 'product_type' => 'plugin', |
| 143 | 'product_slug' => $plugin, |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $addons_data; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check if usage tracking is allowed |
| 153 | * |
| 154 | * @return bool True if usage tracking is allowed, False otherwise |
| 155 | */ |
| 156 | public function is_usage_allowed() { |
| 157 | |
| 158 | return 'yes' === get_option( 'everest_forms_allow_usage_tracking', 'no' ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Initialize usage tracking |
| 163 | * This function adds an action hook that runs the 'process' method on a bi-weekly basis, |
| 164 | * only when the WordPress cron system is running. |
| 165 | */ |
| 166 | public function init_usage() { |
| 167 | |
| 168 | if ( wp_doing_cron() ) { |
| 169 | add_action( 'everest_forms_biweekly_scheduled_events', array( $this, 'process' ) ); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Run the process once when user gives consent. |
| 175 | * |
| 176 | * @param mixed $old_value The old value of the option. |
| 177 | * @param mixed $value The new value of the option. |
| 178 | * @param string $option The name of the option. |
| 179 | * @return mixed The new value of the option. |
| 180 | */ |
| 181 | public function run_on_save( $old_value, $value, $option ) { |
| 182 | |
| 183 | if ( $value !== $old_value && 'yes' === $value && ( false === get_option( self::LAST_RUN_STAMP ) ) ) { |
| 184 | $this->process(); |
| 185 | } |
| 186 | |
| 187 | return $value; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Process the API call |
| 192 | * This function will check if the usage is allowed and if it has been more than 15 days since the last API call. |
| 193 | * If usage is allowed and it has been more than 15 days, it will call the API and update the last run timestamp. |
| 194 | * |
| 195 | * @return void |
| 196 | */ |
| 197 | public function process() { |
| 198 | |
| 199 | if ( ! $this->is_usage_allowed() ) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | $last_send = get_option( self::LAST_RUN_STAMP ); |
| 204 | |
| 205 | // Make sure we do not run it more than once on each 15 days. |
| 206 | if ( false !== $last_send && ( time() - $last_send ) < ( DAY_IN_SECONDS * 15 ) ) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | $this->call_api(); |
| 211 | |
| 212 | // Update the last run option to the current timestamp. |
| 213 | update_option( self::LAST_RUN_STAMP, time() ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Call API and gather system information |
| 218 | * This function gathers information about the plugin, theme, and site, |
| 219 | * such as the WordPress version, PHP version, MySQL version, server software, |
| 220 | * SSL status, multi-site status, and theme name and version. |
| 221 | * It also retrieves the number of sites in the network and the time zone offset. |
| 222 | * |
| 223 | * @return void |
| 224 | */ |
| 225 | public function call_api() { |
| 226 | global $wpdb; |
| 227 | $theme = wp_get_theme(); |
| 228 | $data = array(); |
| 229 | $data['product_data'] = $this->get_plugin_lists(); |
| 230 | $data['admin_email'] = get_bloginfo( 'admin_email' ); |
| 231 | $data['website_url'] = get_bloginfo( 'url' ); |
| 232 | $data['wp_version'] = get_bloginfo( 'version' ); |
| 233 | $data['php_version'] = phpversion(); |
| 234 | $data['mysql_version'] = $wpdb->db_version(); |
| 235 | $data['server_software'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : ''; |
| 236 | $data['is_ssl'] = is_ssl(); |
| 237 | $data['is_multisite'] = is_multisite(); |
| 238 | $data['is_wp_com'] = defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 239 | $data['is_wp_com_vip'] = ( defined( 'WPCOM_IS_VIP_ENV' ) && WPCOM_IS_VIP_ENV ) || ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ); |
| 240 | $data['is_wp_cache'] = defined( 'WP_CACHE' ) && WP_CACHE; |
| 241 | $data['multi_site_count'] = $this->get_sites_total(); |
| 242 | $data['active_theme'] = $theme->name; |
| 243 | $data['active_theme_version'] = $theme->version; |
| 244 | $data['locale'] = get_locale(); |
| 245 | $data['timezone'] = $this->get_timezone_offset(); |
| 246 | $data['base_product'] = $this->get_base_product(); |
| 247 | |
| 248 | $this->send_request( self::REMOTE_URL, $data ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get the total number of sites in a multi-site installation |
| 253 | * |
| 254 | * @return int The total number of sites in the multi-site installation. If the current installation is not multi-site, returns 1. |
| 255 | */ |
| 256 | private function get_sites_total() { |
| 257 | |
| 258 | return function_exists( 'get_blog_count' ) ? (int) get_blog_count() : 1; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get the timezone offset |
| 263 | * Returns the timezone string in the format +00:00 or -00:00 if WordPress version is greater than 5.3. |
| 264 | * Otherwise, get the timezone offset from the 'timezone_string' option or 'gmt_offset' option. |
| 265 | * |
| 266 | * @return string The timezone offset |
| 267 | */ |
| 268 | private function get_timezone_offset() { |
| 269 | |
| 270 | // It was added in WordPress 5.3. |
| 271 | if ( function_exists( 'wp_timezone_string' ) ) { |
| 272 | return wp_timezone_string(); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * The code below is basically a copy-paste from that function. |
| 277 | */ |
| 278 | |
| 279 | $timezone_string = get_option( 'timezone_string' ); |
| 280 | |
| 281 | if ( $timezone_string ) { |
| 282 | return $timezone_string; |
| 283 | } |
| 284 | |
| 285 | $offset = (float) get_option( 'gmt_offset' ); |
| 286 | $hours = (int) $offset; |
| 287 | $minutes = ( $offset - $hours ); |
| 288 | |
| 289 | $sign = ( $offset < 0 ) ? '-' : '+'; |
| 290 | $abs_hour = abs( $hours ); |
| 291 | $abs_mins = abs( $minutes * 60 ); |
| 292 | $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 293 | |
| 294 | return $tz_offset; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Send Request to API. |
| 299 | * |
| 300 | * @param string $url URL. |
| 301 | * @param array $data Data. |
| 302 | */ |
| 303 | public function send_request( $url, $data ) { |
| 304 | $headers = array( |
| 305 | 'user-agent' => 'EverestForms/' . ( $this->is_premium() ? EFP_VERSION : evf()->version ) . '; ' . get_bloginfo( 'url' ), |
| 306 | ); |
| 307 | |
| 308 | $response = wp_remote_post( |
| 309 | $url, |
| 310 | array( |
| 311 | 'method' => 'POST', |
| 312 | 'timeout' => 45, |
| 313 | 'redirection' => 5, |
| 314 | 'httpversion' => '1.0', |
| 315 | 'blocking' => true, |
| 316 | 'headers' => $headers, |
| 317 | 'body' => array( 'free_data' => $data ), |
| 318 | ) |
| 319 | ); |
| 320 | return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | new EVF_Stats(); |
| 326 |