Skip to content

Commit

Permalink
WIP - DO NOT MERGE
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Mar 31, 2023
1 parent 2bc6a50 commit 2a57740
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
71 changes: 68 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 @@ -53,6 +54,16 @@ public static function init() {
)
);

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 @@ -101,7 +112,7 @@ public static function track_visit( $request ) {
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 @@ -122,7 +133,7 @@ public static function track_visit( $request ) {
public static function get_stats( $request ) {
$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 @@ -149,4 +160,58 @@ public static function get_stats( $request ) {

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( $request ) {
$scope = $request->get_param( 'scope' );
$post = $request->get_param( 'post' );

$status = 200;
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;
}
$stats['i18n'] = array( 'months' => array() );
foreach ( range( 1, 12 ) as $m ) {
$stats['i18n']['months'][ $m ] = date_i18n( 'M', strtotime( '0000-' . $m . '-01' ) );
}
} elseif ( 'day' === $scope ) {
$stats = Statify_Evaluation::get_views_for_all_days( $post );
} else {
$stats = array( 'error' => 'invalid scope (allowed: year, month, day)' );
$status = 400;
}

return new WP_REST_Response( $stats, $status );
}
}
41 changes: 41 additions & 0 deletions views/view-dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* The dashboard page.
*
* @package Statify
* @since 2.0.0
*/

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

?>
<div class="wrap eefstatify">
<h1><?php esc_html_e( 'Statify – Extended Evaluation', 'extended-evaluation-for-statify' ); ?></h1>

<section>
<h3><?php esc_html_e( 'Monthly / Yearly Views', 'extended-evaluation-for-statify' ); ?></h3>
<table id="table-data" class="wp-list-table widefat striped">
<thead>
<tr>
<th scope="col"><?php esc_html_e( 'Year', 'extended-evaluation-for-statify' ); ?></th>
<?php foreach ( $months as $month ) { ?>
<th scope="col"><?php echo esc_html( eefstatify_get_month_name( $month ) ); ?></th>
<?php } ?>
<th scope="col" class="right sum"><?php esc_html_e( 'Sum', 'extended-evaluation-for-statify' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $years as $year ) { ?>
<tr>
<th scope="row"><a href="<?php echo esc_url( admin_url( 'admin.php?page=extended_evaluation_for_statify_dashboard&year=' . $year ) ); ?>"><?php echo esc_html( $year ); ?></a></th>
<?php foreach ( $months as $month ) { ?>
<td class="right"><?php eefstatify_echo_number( eefstatify_get_monthly_views( $views_for_all_months, $year, $month ) ); ?></td>
<?php } ?>
<td class="right sum"><?php eefstatify_echo_number( eefstatify_get_yearly_views( $views_for_all_years, $year ) ); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</section>
</div>

0 comments on commit 2a57740

Please sign in to comment.