Count views for single posts and pages

This commit is contained in:
Brian Miyaji
2014-01-25 16:09:55 +11:00
parent 8db2a05fa3
commit 84233c4d8b
5 changed files with 51 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ function sportspress_manage_posts_columns( $defaults ){
return $defaults; return $defaults;
} }
add_filter( 'manage_posts_columns', 'sportspress_manage_posts_columns' ); add_filter( 'manage_posts_columns', 'sportspress_manage_posts_columns' );
add_filter( 'manage_page_posts_columns', 'sportspress_manage_posts_columns' );
function sportspress_manage_posts_custom_column( $column, $post_id ) { function sportspress_manage_posts_custom_column( $column, $post_id ) {
global $post; global $post;

View File

@@ -1,6 +1,7 @@
<?php <?php
function sportspress_the_content( $content ) { function sportspress_the_content( $content ) {
sportspress_set_post_views( get_the_ID() ); if ( is_single() || is_page() )
sportspress_set_post_views( get_the_ID() );
return $content; return $content;
} }
add_filter( 'the_content', 'sportspress_the_content' ); add_filter( 'the_content', 'sportspress_the_content' );

View File

@@ -9,7 +9,8 @@ function viewport() {
(function($) { (function($) {
// Data tables /* Data Tables */
if (viewport().width > 640) { if (viewport().width > 640) {
$(".sp-league-table, .sp-event-statistics, .sp-player-list").dataTable({ $(".sp-league-table, .sp-event-statistics, .sp-player-list").dataTable({
"aaSorting": [], "aaSorting": [],
@@ -45,7 +46,16 @@ function viewport() {
}); });
} }
// Responsive tables
/*
* Responsive Tables
*
* Based on responsive-tables.js by ZURB
*
* Credit: ZURB
* Original: https://github.com/zurb/responsive-tables
*/
var switched = false; var switched = false;
var updateTables = function() { var updateTables = function() {
if ((viewport().width <= 640) && !switched ){ if ((viewport().width <= 640) && !switched ){
@@ -111,7 +121,9 @@ function viewport() {
}); });
} }
// Google Maps
/* Google Maps */
function initialize_google_maps() { function initialize_google_maps() {
$maps = $('.sp-google-map'); $maps = $('.sp-google-map');
$maps.each(function() { $maps.each(function() {

View File

@@ -107,28 +107,32 @@ if ( !function_exists( 'sportspress_get_the_term_id' ) ) {
} }
} }
function sportspress_get_post_views( $post_id ) { if ( !function_exists( 'sportspress_get_post_views' ) ) {
$count_key = 'sp_views'; function sportspress_get_post_views( $post_id ) {
$count = get_post_meta( $post_id, $count_key, true ); $count_key = 'sp_views';
if ( $count == '' ): $count = get_post_meta( $post_id, $count_key, true );
delete_post_meta( $post_id, $count_key ); if ( $count == '' ):
add_post_meta( $post_id, $count_key, '0' ); delete_post_meta( $post_id, $count_key );
return sprintf( _n( '1 View', '%1$s Views', '0', 'sportspress' ), '0' ); add_post_meta( $post_id, $count_key, '0' );
endif; return sprintf( _n( '1 View', '%1$s Views', '0', 'sportspress' ), '0' );
return sprintf( _n( '1 View', '%1$s Views', $count, 'sportspress' ), $count ); endif;
return sprintf( _n( '1 View', '%1$s Views', $count, 'sportspress' ), $count );
}
} }
function sportspress_set_post_views( $post_id ) { if ( !function_exists( 'sportspress_set_post_views' ) ) {
$count_key = 'sp_views'; function sportspress_set_post_views( $post_id ) {
$count = get_post_meta( $post_id, $count_key, true ); $count_key = 'sp_views';
if ( $count == '' ): $count = get_post_meta( $post_id, $count_key, true );
$count = 0; if ( $count == '' ):
delete_post_meta( $post_id, $count_key ); $count = 0;
add_post_meta( $post_id, $count_key, '0' ); delete_post_meta( $post_id, $count_key );
else: add_post_meta( $post_id, $count_key, '0' );
$count++; else:
update_post_meta( $post_id, $count_key, $count ); $count++;
endif; update_post_meta( $post_id, $count_key, $count );
endif;
}
} }
if ( !function_exists( 'sportspress_get_post_precision' ) ) { if ( !function_exists( 'sportspress_get_post_precision' ) ) {

View File

@@ -72,6 +72,15 @@ SportsPress is currently in beta and is undergoing testing. We are still activel
== Changelog == == Changelog ==
= 0.2 =
* Feature - Add option to select whether statistics are calculated as a sum or average.
* Feature - Enable pageview tracking for posts and custom post types.
* Feature - Responsive datatables.
* Fix - Add site admin capabilities for multisite.
* Fix - Force numerical sorting of number column.
* Tweak - Enable SportsPress content functions to be called without explicit ID.
* Tweak - Remove redundant admin menu links via filter.
= 0.1.10 = = 0.1.10 =
* Documentation - Add Installation, FAQ and Screenshots to assets. * Documentation - Add Installation, FAQ and Screenshots to assets.