Skip to content

Commit

Permalink
Adds the ability to limit the amount of data logged by WPP (resolves #69
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cabrerahector committed Aug 24, 2015
1 parent 6950358 commit 90ae438
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
12 changes: 12 additions & 0 deletions js/admin.js
Expand Up @@ -74,6 +74,18 @@

tb_remove();
};
// log limit
$("#log_limit").change(function(){
var me = $(this);

if (me.val() == 1) {
me.parent().children("label, .description").show();
me.parent().children("br").hide();
} else {
me.parent().children("label, .description").hide();
me.parent().children("br").show();
}
});
// cache interval
$("#cache").change(function() {
if ($(this).val() == 1) {
Expand Down
19 changes: 19 additions & 0 deletions views/admin.php
Expand Up @@ -53,6 +53,10 @@
$current = 'tools';

$this->user_settings['tools']['log']['level'] = $_POST['log_option'];
$this->user_settings['tools']['log']['limit'] = $_POST['log_limit'];
$this->user_settings['tools']['log']['expires_after'] = ( $this->__is_numeric($_POST['log_expire_time']) && $_POST['log_expire_time'] > 0 )
? $_POST['log_expire_time']
: $this->default_user_settings['tools']['log']['expires_after'];
$this->user_settings['tools']['ajax'] = $_POST['ajax'];

// if any of the caching settings was updated, destroy all transients created by the plugin
Expand Down Expand Up @@ -300,6 +304,21 @@ function confirm_clear_image_cache() {
<br />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="log_limit"><?php _e("Log limit", $this->plugin_slug); ?>:</label></th>
<td>
<select name="log_limit" id="log_limit">
<option <?php if ($this->user_settings['tools']['log']['limit'] == 0) {?>selected="selected"<?php } ?> value="0"><?php _e("Disabled", $this->plugin_slug); ?></option>
<option <?php if ($this->user_settings['tools']['log']['limit'] == 1) {?>selected="selected"<?php } ?> value="1"><?php _e("Keep data for", $this->plugin_slug); ?></option>
</select>

<label for="log_expire_time"<?php echo ($this->user_settings['tools']['log']['limit'] == 0) ? ' style="display:none;"' : ''; ?>><input type="text" id="log_expire_time" name="log_expire_time" value="<?php echo $this->user_settings['tools']['log']['expires_after']; ?>" size="3" /> <?php _e("day(s)", $this->plugin_slug); ?></label>

<p class="description"<?php echo ($this->user_settings['tools']['log']['limit'] == 0) ? ' style="display:none;"' : ''; ?>><?php _e("Data from entries that haven't been viewed within the specified time frame will be automatically discarded", $this->plugin_slug); ?>.</p>

<br<?php echo ($this->user_settings['tools']['log']['limit'] == 1) ? ' style="display:none;"' : ''; ?> />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="ajax"><?php _e("Ajaxify widget", $this->plugin_slug); ?>:</label></th>
<td>
Expand Down
44 changes: 23 additions & 21 deletions wordpress-popular-posts.php
Expand Up @@ -235,7 +235,9 @@ class WordpressPopularPosts extends WP_Widget {
'responsive' => false
),
'log' => array(
'level' => 1
'level' => 1,
'expires' => 0,
'expires_after' => 180
),
'cache' => array(
'active' => false,
Expand Down Expand Up @@ -403,14 +405,24 @@ public function __construct() {
add_action( 'admin_init', array($this, 'purge_post_init') );

// Enable data purging at midnight
add_action( 'wpp_cache_event', array($this, 'purge_data') );
if ( !wp_next_scheduled('wpp_cache_event') ) {
$tomorrow = time() + 86400;
$midnight = mktime(0, 0, 0,
date("m", $tomorrow),
date("d", $tomorrow),
date("Y", $tomorrow));
wp_schedule_event( $midnight, 'daily', 'wpp_cache_event' );
if ( 1 == $this->user_settings['tools']['log']['limit'] ) {

add_action( 'wpp_cache_event', array($this, 'purge_data') );
if ( !wp_next_scheduled('wpp_cache_event') ) {
$tomorrow = time() + 86400;
$midnight = mktime(0, 0, 0,
date("m", $tomorrow),
date("d", $tomorrow),
date("Y", $tomorrow));
wp_schedule_event( $midnight, 'daily', 'wpp_cache_event' );
}

} else {
// Remove the scheduled event if exists
if ( $timestamp = wp_next_scheduled('wpp_cache_event') ) {
wp_unschedule_event( $timestamp, 'wpp_cache_event' );
}

}

} // end constructor
Expand Down Expand Up @@ -1161,7 +1173,7 @@ public function purge_post( $pID ) {
} // end purge_post

/**
* Purges deleted posts from data/summary tables.
* Purges old post data from summary table.
*
* @since 2.0.0
* @global object $wpdb
Expand All @@ -1170,17 +1182,7 @@ public function purge_data() {

global $wpdb;

if ( $missing = $wpdb->get_results( "SELECT v.postid AS id FROM {$wpdb->prefix}popularpostsdata v WHERE NOT EXISTS (SELECT p.ID FROM {$wpdb->posts} p WHERE v.postid = p.ID);" ) ) {
$to_be_deleted = '';

foreach ( $missing as $deleted )
$to_be_deleted .= $deleted->id . ",";

$to_be_deleted = rtrim( $to_be_deleted, "," );

$wpdb->query( "DELETE FROM {$wpdb->prefix}popularpostsdata WHERE postid IN({$to_be_deleted});" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}popularpostssummary WHERE postid IN({$to_be_deleted});" );
}
$wpdb->query( "DELETE FROM {$wpdb->prefix}popularpostssummary WHERE view_date < DATE_SUB('{$this->__curdate()}', INTERVAL {$this->user_settings['tools']['log']['expires_after']} DAY);" );

} // end purge_data

Expand Down

0 comments on commit 90ae438

Please sign in to comment.