diff --git a/inc/class-statify-api.php b/inc/class-statify-api.php index 8a12a53..82f2ee3 100644 --- a/inc/class-statify-api.php +++ b/inc/class-statify-api.php @@ -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. @@ -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 ); } @@ -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 ); } @@ -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(); @@ -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 ); + } } diff --git a/views/view-dashboard.php b/views/view-dashboard.php new file mode 100644 index 0000000..c4a3ebc --- /dev/null +++ b/views/view-dashboard.php @@ -0,0 +1,41 @@ + +