diff --git a/includes/class-ssp-stats.php b/includes/class-ssp-stats.php index 59d8926..12ee445 100755 --- a/includes/class-ssp-stats.php +++ b/includes/class-ssp-stats.php @@ -209,6 +209,12 @@ public function __construct ( $file = '', $version = '1.0.0', $db_version = '1.0 // Get required episode IDs for stats add_action( 'init', array( $this, 'load_episode_ids' ), 10 ); + // Add admin notice to upgrade stats table + add_action( 'admin_notices', array( $this, 'maybe_notify_stats_update' ) ); + + // Anonymise the IP address details stored in the database + add_action( 'admin_init', array( $this, 'maybe_update_stats_data' ), 11 ); + // Track episode download add_action( 'ssp_file_download', array( $this, 'track_download' ), 10, 3 ); @@ -338,6 +344,9 @@ public function track_download ( $file = '', $episode = 0, $referrer = '' ) { return; } + // Anonymise the ip address + $ip_address = ss_stats_anonymise_ip($ip_address); + // Create transient name from episode ID, IP address and referrer $transient = 'sspdl_' . $episode_id . '_' . str_replace( '.', '', $ip_address ) . '_' . $referrer; @@ -753,6 +762,10 @@ public function stats_page () { $post = get_post( intval( $result->post_id ) ); + if ( ! $post ) { + continue; + } + $sql = "SELECT `date` FROM $this->_table WHERE `post_id` = '".$result->post_id."'"; $episode_results = $wpdb->get_results( $sql ); @@ -1448,4 +1461,65 @@ public function dashboard_widget_callback(){ echo $html; } + + /** + * Checks if the ssp_stats_ips_updated option is set, if not shows a message to the user. + */ + public function maybe_notify_stats_update(){ + $ssp_stats_ips_updated = get_option( 'ssp_stats_ips_updated', 'no' ); + if ( 'yes' === $ssp_stats_ips_updated ) { + return; + } + $data_upgrade_url = add_query_arg( array( 'upgrade_stats_table' => 'anonymise_ip' ) ); + ?> +
+

+

+
+ prefix}ssp_stats SET ip_address = CONCAT( SUBSTRING_INDEX( ip_address, '.', 3 ) , '.0' )"; + $affected_rows = $wpdb->query($query); + + if (false === $affected_rows){ + add_action( 'admin_notices', array( $this, 'update_stats_data_failed' ) ); + }else { + update_option( 'ssp_stats_ips_updated', 'yes' ); + add_action( 'admin_notices', array( $this, 'update_stats_data_succeeded' ) ); + } + + } + + public function update_stats_data_failed(){ + ?> +
+

+
+ +
+

+
+