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

integrate extended evaluation #257

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions css/dashboard.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* @group Front page */

#statify_chart {
.statify-chart {
color: #a7aaad;
flex: 0 0 100%;
height: 140px;
Expand All @@ -10,34 +10,40 @@
text-align: center;
}

#statify_chart * {
.statify-chart * {
direction: ltr;
}

body.rtl #statify_chart * {
body.rtl .statify-chart * {
direction: rtl;
}

#statify_chart .spinner {
.statify-chart .spinner {
float: none;
margin: 1em;
}

#statify_chart .ct-line {
.statify-chart .ct-line {
stroke: #3582c4;
stroke-width: 2px;
}

#statify_chart .ct-point {
.statify-chart .ct-point {
fill: #fff;
stroke: #3582c4;
stroke-width: 1.5px;
}

#statify_chart .ct-area {
.statify-chart .ct-area {
fill: #3582c4;
}

.statify-chart .ct-label.ct-horizontal.ct-end {
align-items: center;
justify-content: center;
margin-left: -50%;
}

.statify-chartist-tooltip {
border: 1px solid #000;
background-color: #fff;
Expand All @@ -55,6 +61,29 @@ body.rtl #statify_chart * {
color: #3582c4;
}


.statify-table th,
.statify-table tr.statify-table-sum td,
.statify-table td.statify-table-sum {
font-weight: 700;
}

.statify-table tr.placeholder {
background-color: #fff;
}

.statify-table tr.placeholder td span {
background-color: #f5f5f5;
border-radius: 1em;
display: inline-block;
width: 80%;
}

.statify-table + a.button {
margin: 1em 0 0 1em;
}


#statify_dashboard .postbox-title-action a,
#statify_dashboard .settings-link a {
display: block;
Expand Down Expand Up @@ -139,4 +168,21 @@ body.rtl #statify_chart * {
line-height: 28px;
}

.statify-chart-container {
background: #fff;
margin: 1em 0;
padding: 1em;
}

.statify-chart-container .statify-chart {
height: 280px;
margin: 10px 20px;
}

.statify-chart-title {
text-align: center;
font-size: 1.5em;
padding: 0.5em 0;
}

/* @end group */
124 changes: 121 additions & 3 deletions inc/class-statify-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class Statify_Api extends Statify {
*
* @var string
*/
const REST_NAMESPACE = 'statify/v1';
const REST_NAMESPACE = 'statify/v1';
const REST_ROUTE_TRACK = 'track';
const REST_ROUTE_STATS = 'stats';
const REST_ROUTE_STATS_EXTENDED = 'stats/extended';

/**
* Initialize REST API routes.
Expand Down Expand Up @@ -55,6 +56,16 @@ public static function init(): void {
)
);

register_rest_route(
self::REST_NAMESPACE,
self::REST_ROUTE_STATS_EXTENDED,
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( __CLASS__, 'get_extended' ),
'permission_callback' => array( __CLASS__, 'user_can_see_stats' ),
)
);

add_filter( 'rest_authentication_errors', array( __CLASS__, 'check_authentication' ), 5 );
}

Expand Down Expand Up @@ -103,7 +114,7 @@ public static function track_visit( WP_REST_Request $request ): WP_REST_Response
if ( null !== $referrer ) {
$referrer = filter_var( $referrer, FILTER_SANITIZE_URL );
}
$target = $request->get_param( 'target' );
$target = $request->get_param( 'target' );
if ( null !== $target ) {
$target = filter_var( $target, FILTER_SANITIZE_URL );
}
Expand All @@ -124,7 +135,7 @@ public static function track_visit( WP_REST_Request $request ): WP_REST_Response
public static function get_stats( WP_REST_Request $request ): WP_REST_Response {
$refresh = '1' === $request->get_param( 'refresh' );

$stats = Statify_Dashboard::get_stats( $refresh );
$stats = Statify_Dashboard::get_stats( $refresh );

$visits = $stats['visits'];
$stats['visits'] = array();
Expand All @@ -151,4 +162,111 @@ public static function get_stats( WP_REST_Request $request ): WP_REST_Response {

return new WP_REST_Response( $stats );
}


/**
* Get extended statistics.
*
* @param WP_REST_Request $request The request.
*
* @return WP_REST_Response The response.
*
* @since 2.0.0
*/
public static function get_extended( WP_REST_Request $request ): WP_REST_Response {
// Verify scope.
$scope = $request->get_param( 'scope' );
if ( ! in_array( $scope, array( 'year', 'month', 'day' ) ) ) {
return new WP_REST_Response(
array( 'error' => 'invalid scope (allowed: year, month, day)' ),
400
);
}

// Parse year, if provided.
$yr = $request->get_param( 'year' );
if ( ! empty( $yr ) ) {
$yr = intval( $yr );
if ( $yr <= 0 ) {
return new WP_REST_Response(
array( 'error' => 'invalid year' ),
400
);
}
} else {
$yr = 0;
}

// Retrieve from cache, if data is not post-specific.
$post = $request->get_param( 'post' );
$stats = false;
if ( ! $post ) {
$stats = self::from_cache( $scope, $yr );
}

if ( ! $stats ) {
if ( 'year' === $scope ) {
$stats = Statify_Evaluation::get_views_for_all_years( $post );
} elseif ( 'month' === $scope ) {
$visits = Statify_Evaluation::get_views_for_all_months( $post );
$stats = array( 'visits' => array() );
$last_ym = 0;
foreach ( $visits as $ym => $v ) {
$ym = explode( '-', $ym );
$year = intval( $ym[0] );
$month = intval( $ym[1] );
$year_month = $year * 12 + $month;
for ( $ym = $last_ym + 1; $last_ym > 0 && $ym < $year_month; $ym++ ) {
// Fill gaps.
$y = intval( $ym / 12 );
if ( ! isset( $stats['visits'][ $y ] ) ) {
$stats['visits'][ $y ] = array();
}
$stats['visits'][ $y ][ $ym % 12 ] = 0;
}
if ( ! isset( $stats['visits'][ $year ] ) ) {
$stats['visits'][ $year ] = array();
}
$stats['visits'][ $year ][ $month ] = $v;
$last_ym = $year_month;
}
} elseif ( 'day' === $scope ) {
$stats = Statify_Evaluation::get_views_for_all_days( $yr, $post );
}

// Update cache, if data is not post-specific.
if ( ! $post ) {
self::update_cache( $scope, $yr, $stats );
}
}

return new WP_REST_Response( $stats );
}

/**
* Retrieve data from cache.
*
* @param string $scope Scope (year, month, day).
* @param int $index Optional index (e.g. year).
*
* @return array|false Transient data or FALSE.
*/
private static function from_cache( string $scope, int $index = 0 ) {
return get_transient( 'statify_data_' . $scope . ( $index > 0 ? '_' . $index : '' ) );
}

/**
* Update data cache.
*
* @param string $scope Scope (year, month, day).
* @param int $index Optional index (e.g. year).
* @param array $data Data.
*/
private static function update_cache( string $scope, int $index, array $data ): void {
set_transient(
'statify_data_' . $scope . ( $index > 0 ? '_' . $index : '' ),
$data,
30 * MINUTE_IN_SECONDS
);
}
}
101 changes: 2 additions & 99 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
*/
class Statify_Dashboard extends Statify {

/**
* Plugin version.
*
* @since 1.4.0
* @var string
*/
protected static $_plugin_version;

/**
* Dashboard widget initialize
*
Expand All @@ -48,9 +40,6 @@ public static function init(): void {
wp_normalize_path( sprintf( '%s/lang', STATIFY_DIR ) )
);

// Plugin version.
self::_get_version();

// Add dashboard widget.
wp_add_dashboard_widget(
'statify_dashboard',
Expand All @@ -60,82 +49,12 @@ public static function init(): void {
);

// Init CSS.
add_action( 'admin_print_styles', array( __CLASS__, 'add_style' ) );
add_action( 'admin_print_styles', array( 'Statify', 'add_style' ) );

// Init JS.
add_action( 'admin_print_scripts', array( __CLASS__, 'add_js' ) );
}

/**
* Print CSS
*
* @since 0.1.0
* @version 1.4.0
*/
public static function add_style(): void {

// Register CSS.
wp_register_style(
'chartist_css',
plugins_url( '/css/chartist.min.css', STATIFY_FILE ),
array(),
self::$_plugin_version
);
wp_register_style(
'chartist_tooltip_css',
plugins_url( '/css/chartist-plugin-tooltip.min.css', STATIFY_FILE ),
array(),
self::$_plugin_version
);
wp_register_style(
'statify',
plugins_url( '/css/dashboard.min.css', STATIFY_FILE ),
array(),
self::$_plugin_version
);

// Load CSS.
wp_enqueue_style( 'chartist_css' );
wp_enqueue_style( 'chartist_tooltip_css' );
wp_enqueue_style( 'statify' );
}

/**
* Print JavaScript
*
* @since 0.1.0
* @version 1.4.0
*/
public static function add_js(): void {

// Register JS.
wp_register_script(
'chartist_js',
plugins_url( 'js/chartist.min.js', STATIFY_FILE ),
array(),
self::$_plugin_version,
true
);
wp_register_script(
'chartist_tooltip_js',
plugins_url( 'js/chartist-plugin-tooltip.min.js', STATIFY_FILE ),
array( 'chartist_js' ),
self::$_plugin_version,
true
);
wp_register_script(
'statify_chart_js',
plugins_url( 'js/dashboard.min.js', STATIFY_FILE ),
array( 'wp-api-fetch', 'chartist_tooltip_js', 'wp-i18n' ),
self::$_plugin_version,
true
);

// Sets translated strings for the script.
wp_set_script_translations( 'statify_chart_js', 'statify' );
add_action( 'admin_print_scripts', array( 'Statify', 'add_js' ) );
}


/**
* Print widget frontview.
*
Expand Down Expand Up @@ -228,22 +147,6 @@ private static function _save_widget_options(): void {
update_option( 'statify', $options );
}


/**
* Set plugin version from plugin meta data
*
* @since 1.4.0
* @version 1.4.0
*/
private static function _get_version(): void {

// Get plugin meta.
$meta = get_plugin_data( STATIFY_FILE );

self::$_plugin_version = $meta['Version'];
}


/**
* Get stats from cache
*
Expand Down
Loading