Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce option to separately configure storage and display ranges #72

Merged
merged 10 commits into from
Apr 13, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec
* Add options to track logged in users, feeds and search requests
* Add option to show total visits
* Updated Chartist JS library for dashboard widget
* Introduced new option to separate display from storage range

## 1.6.3
* Fix compatibility issue with some PHP implementations not populating `INPUT_SERVER`
Expand Down
23 changes: 14 additions & 9 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ private static function _save_widget_options() {
// We only do a partial update, so initialize with current values.
$options = Statify::$_options;

// Parse numeric "limit" value.
if ( isset( $_POST['statify']['limit'] ) && (int) $_POST['statify']['limit'] > 0 ) {
$options['limit'] = (int) $_POST['statify']['limit'];
// Parse numeric values.
foreach ( array( 'days', 'days_show', 'limit' ) as $option_name ) {
$options[ $option_name ] = Statify::$_options[ $option_name ];
if ( isset( $_POST['statify'][ $option_name ] ) && (int) $_POST['statify'][ $option_name ] > 0 ) {
$options[ $option_name ] = (int) $_POST['statify'][ $option_name ];
}
}
if ( $options['limit'] > 100 ) {
$options['limit'] = 100;
Expand Down Expand Up @@ -314,30 +317,32 @@ private static function _select_data() {
global $wpdb;

// Init values.
$days = (int) self::$_options['days'];
$days_show = (int) self::$_options['days_show'];
$limit = (int) self::$_options['limit'];
$today = (int) self::$_options['today'];
$show_totals = (int) self::$_options['show_totals'];

$limit_args = ( $today ) ? $limit : array( $days_show, $limit );

$data = array(
'visits' => $wpdb->get_results(
$wpdb->prepare(
"SELECT `created` as `date`, COUNT(`created`) as `count` FROM `$wpdb->statify` GROUP BY `created` ORDER BY `created` DESC LIMIT %d",
$days
$days_show
),
ARRAY_A
),
'target' => $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` " . ( $today ? 'WHERE created = DATE(NOW())' : '' ) . ' GROUP BY `target` ORDER BY `count` DESC LIMIT %d',
$limit
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created " . ( $today ? '= DATE(NOW())' : '>= DATE_SUB(NOW(), INTERVAL %d DAY)' ) . ' GROUP BY `target` ORDER BY `count` DESC LIMIT %d',
$limit_args
),
ARRAY_A
),
'referrer' => $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' " . ( $today ? 'AND created = DATE(NOW())' : '' ) . ' GROUP BY `host` ORDER BY `count` DESC LIMIT %d',
$limit
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created " . ( $today ? '= DATE(NOW())' : '>= DATE_SUB(NOW(), INTERVAL %d DAY)' ) . ' GROUP BY `host` ORDER BY `count` DESC LIMIT %d',
$limit_args
),
ARRAY_A
),
Expand Down
23 changes: 22 additions & 1 deletion inc/class-statify-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public static function register_settings() {
array( __CLASS__, 'header_dashboard' ),
'statify'
);
add_settings_field(
'statify-days_show',
__( 'Period of data display in Dashboard', 'statify' ),
array( __CLASS__, 'options_days_show' ),
'statify',
'statify-dashboard',
array( 'label_for' => 'statify-days-show' )
);
add_settings_field(
'statify-limit',
__( 'Number of entries in top lists', 'statify' ),
Expand Down Expand Up @@ -163,6 +171,19 @@ public static function header_dashboard() {
<?php
}

/**
* Option for data display period.
*
* @return void
*/
public static function options_days_show() {
?>
<input id="statify-days-show" name="statify[days_show]" type="number" min="1" value="<?php echo esc_attr( Statify::$_options['days_show'] ); ?>">
<?php esc_html_e( 'days', 'statify' ); ?>
(<?php esc_html_e( 'Default', 'statify' ); ?>: 14)
<?php
}

/**
* Option for number of entries in top lists.
*
Expand Down Expand Up @@ -279,7 +300,7 @@ public static function sanitize_options( $options ) {

// Sanitize numeric values.
$res = array();
foreach ( array( 'days', 'limit' ) as $o ) {
foreach ( array( 'days', 'days_show', 'limit' ) as $o ) {
$res[ $o ] = Statify::$_options[ $o ];
if ( isset( $options[ $o ] ) && (int) $options[ $o ] > 0 ) {
$res[ $o ] = (int) $options[ $o ];
Expand Down
1 change: 1 addition & 0 deletions inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function __construct() {
get_option( 'statify' ),
array(
'days' => 14,
'days_show' => 14,
'limit' => 3,
'today' => 0,
'snippet' => 0,
Expand Down
6 changes: 6 additions & 0 deletions views/widget-back.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class_exists( 'Statify' ) || exit; ?>

<h3><?php esc_html_e( 'Widget Settings', 'statify' ); ?></h3>
<fieldset>
<label for="statify_days_show">
<input name="statify[days_show]" id="statify_days_show" type="number" min="1"
value="<?php echo esc_attr( Statify::$_options['days_show'] ); ?>">
<?php esc_html_e( 'days', 'statify' ); ?> -
<?php esc_html_e( 'Period of data display in Dashboard', 'statify' ); ?>
</label>
<label for="statify_limit">
<input name="statify[limit]" id="statify_limit" type="number" min="1" max="100"
value="<?php echo esc_attr( Statify::$_options['limit'] ); ?>">
Expand Down