Skip to content

Commit

Permalink
Merge pull request #30 from TheCraigHewitt/feature/update-stats-data
Browse files Browse the repository at this point in the history
Feature/update stats data
  • Loading branch information
jonathanbossenger authored May 25, 2018
2 parents edeb0c7 + c7d88d8 commit 0e9851e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 7 deletions.
74 changes: 74 additions & 0 deletions includes/class-ssp-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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' ) );
?>
<div class="notice notice-warning">
<p><?php _e( 'Seriously Simple Stats needs to perform an update to the stats database table. Click below to perform this one-time update.', 'seriously-simple-stats' ); ?></p>
<p><a href="<?php echo $data_upgrade_url ?>"><?php _e( 'Upgrade stats table.', 'seriously-simple-stats' ); ?></a></p>
</div>
<?php
}

/**
* Attempt to update the stats table
*/
public function maybe_update_stats_data(){

if ( ! isset( $_GET['upgrade_stats_table'] ) ) {
return;
}

$upgrade_stats_table = filter_var( $_GET['upgrade_stats_table'], FILTER_SANITIZE_STRING );
if ( ! 'anonymise_ip' === $upgrade_stats_table ) {
return;
}

global $wpdb;
$query = "UPDATE {$wpdb->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(){
?>
<div class="notice notice-warning is-dismissible">
<p><?php _e( 'An error occurred updating the stats database table, please try again or contact support.', 'seriously-simple-stats' ); ?></p>
</div>
<?php
}

public function update_stats_data_succeeded(){
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( 'Updating the stats database table completed successfully.', 'seriously-simple-stats' ); ?></p>
</div>
<?php
}

}
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: PodcastMotor, psykro, simondowdles, hlashbrooke, seriouspodcaster
Tags: seriously simple podcasting, stats, statistics, listeners, analytics, podcast, podcasting, ssp, free, add-ons, extensions, addons
Requires at least: 4.4
Tested up to: 4.8.2
Stable tag: 1.2.0
Stable tag: 1.2.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -78,6 +78,10 @@ Yes. This plugin uses the [Crawler Detect](https://github.com/JayBizzle/Crawler-

== Changelog ==

= 1.2.1 =
* 2018-05-25
* [NEW] Included a stats table upgrade to anonymise IP addresses stored

= 1.2.0 =
* 2017-10-11
* [NEW] Added stats widget to WordPress dashboard
Expand Down
10 changes: 5 additions & 5 deletions seriously-simple-stats.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/*
* Plugin Name: Seriously Simple Stats
* Version: 1.2.0
* Version: 1.2.1
* Plugin URI: https://wordpress.org/plugins/seriously-simple-stats
* Description: Integrated analytics and stats tracking for Seriously Simple Podcasting.
* Author: PodcastMotor
* Author URI: https://www.podcastmotor.com/
* Author: Castos
* Author URI: https://www.castos.com/
* Requires at least: 4.4
* Tested up to: 4.8.2
*
Expand All @@ -16,7 +16,7 @@
* @author Hugh Lashbrooke
* @since 1.0.0
*
* @author PodcastMotor
* @author Castos
* @since 1.2.0
*
*/
Expand All @@ -25,7 +25,7 @@
exit;
}

define( 'SSP_STATS_VERSION', '1.2.0' );
define( 'SSP_STATS_VERSION', '1.2.1' );

if ( ! function_exists( 'is_ssp_active' ) ) {
require_once( 'ssp-includes/ssp-functions.php' );
Expand Down
17 changes: 16 additions & 1 deletion ssp-includes/ssp-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@
function is_ssp_active( $minimum_version = '' ) {
return SSP_Dependencies::ssp_active_check( $minimum_version );
}
}
}

/**
* Anonymises IP address data by replacing the last octet with 0 (zero)
*/
if ( ! function_exists( 'ss_stats_anonymise_ip' ) ) {
function ss_stats_anonymise_ip( $ip = '' ) {
if ( empty( $ip ) ) {
return $ip;
}
$ip_octets = explode( '.', $ip );
$ip_octets[3] = '0';
$ip = implode( '.', $ip_octets );
return $ip;
}
}

0 comments on commit 0e9851e

Please sign in to comment.